summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEnderByEndera <[email protected]>2020-12-31 14:42:59 +0800
committerEnderByEndera <[email protected]>2020-12-31 14:42:59 +0800
commite1bca343806707ba8f7180bb1e8e7095f8b4c4f1 (patch)
tree0db1c44dabc164304cd7d3cc714118c42c90dbdf
parent48ee10695238c01aa8e7f5380d0c57f0c57721da (diff)
1. update EvaluatePathRule func, make it a little
faster 2. updated all the test to fit Jenkins test
-rw-r--r--rules/marshal_and_unmarshal_test.go6
-rw-r--r--rules/rulestype_test.go6
-rw-r--r--rules/ruletypes.go90
-rw-r--r--static/rules/pathrules.json5
4 files changed, 73 insertions, 34 deletions
diff --git a/rules/marshal_and_unmarshal_test.go b/rules/marshal_and_unmarshal_test.go
index 1c3249d..4be1163 100644
--- a/rules/marshal_and_unmarshal_test.go
+++ b/rules/marshal_and_unmarshal_test.go
@@ -1,7 +1,7 @@
/*
* @Author: EnderByEndera
* @Date: 2020-12-16 14:31:00
- * @LastEditTime: 2020-12-28 17:21:35
+ * @LastEditTime: 2020-12-31 13:59:05
* @LastEditors: Please set LastEditors
* @Description: Test marshalling.go
* @FilePath: /commdetection/rules/marshalling_test.go
@@ -59,7 +59,7 @@ func TestUnmarshalSensitiveCommSetting(t *testing.T) {
func TestMarshalSensitivePathSetting(t *testing.T) {
spaths := SPaths{
{
- Path: "/root/go/src/commdetection/rules",
+ Path: "/root",
Coefficient: 0.7,
},
}
@@ -80,7 +80,7 @@ func TestUnmarshalSensitivePathSetting(t *testing.T) {
}
predict := SPaths{
{
- Path: "/root/go/src/commdetection/rules",
+ Path: "/root",
Coefficient: 0.7,
},
}
diff --git a/rules/rulestype_test.go b/rules/rulestype_test.go
index 3245b18..df25ef7 100644
--- a/rules/rulestype_test.go
+++ b/rules/rulestype_test.go
@@ -1,7 +1,7 @@
/*
* @Author: EnderByEndera
* @Date: 2020-12-08 11:28:49
- * @LastEditTime: 2020-12-31 11:01:47
+ * @LastEditTime: 2020-12-31 14:05:30
* @LastEditors: Please set LastEditors
* @Description: Test UnmarshalSetting and MarshalSetting
* @FilePath: /commdetection/rules/rulestype_test.go
@@ -36,12 +36,12 @@ func TestEvaluatePathRule(t *testing.T) {
cs := CommScore{
Command: comm.Command{
CommName: "wget",
- Args: []string{"/root/go/src/commdetection/rules/rulesjson/rules.json"},
+ Args: []string{"rules.json"},
},
Score: 100,
}
cs = EvaluatePathRule(cs)
- if cs.Score >= 0 {
+ if cs.Score >= 0 && cs.Score < 100 {
log.Printf("result score is %f", cs.Score)
} else {
t.Errorf("score is not as predicted")
diff --git a/rules/ruletypes.go b/rules/ruletypes.go
index b4d9f81..e57da98 100644
--- a/rules/ruletypes.go
+++ b/rules/ruletypes.go
@@ -1,7 +1,7 @@
/*
* @Author: EnderByEndera
* @Date: 2020-12-08 10:59:19
- * @LastEditTime: 2020-12-30 11:15:59
+ * @LastEditTime: 2020-12-31 14:11:38
* @LastEditors: Please set LastEditors
* @Description: Unmarshal and marshal various types of settings and rules
* @FilePath: /commdetection/rules/ruletypes.go
@@ -11,7 +11,7 @@ package rules
import (
"commdetection/logger"
- "os"
+ "io/ioutil"
"path/filepath"
)
@@ -42,38 +42,74 @@ func EvaluatePathRule(cs CommScore) CommScore {
return cs
}
for _, spath := range spaths {
- err := filepath.Walk(spath.Path, func(path string, info os.FileInfo, err error) error {
- if info == nil {
- return err
- }
- for _, flag := range cs.Command.Args {
- similar := 0
- indexPath, indexFlag := len(path)-1, len(flag)-1
- for indexPath >= 0 && indexFlag >= 0 {
- if path[indexPath] == flag[indexFlag] {
- similar++
- indexPath--
- indexFlag--
- } else {
- break
- }
- }
- ratio := float64(similar) / float64(len(spath.Path))
- // when raitio is larger than threshold 0.1
- if ratio > 0.1 {
- // execute evaluation formula
- cs.Score *= 1.0 - ratio*(1.0-spath.Coefficient)
- }
- }
- return nil
- })
+ fileInfos, err := readAllFiles(spath.Path, []string{})
if err != nil {
logger.Warnf("Error occured during EvaluatePathRule, error is %s", err)
+ break
+ }
+ ratio := 0.0
+ in := make(chan float64)
+ out := make(chan float64)
+ // Creates the outside go func
+ go func(spath SPath) {
+ r1 := 0.0
+ for _, fileInfo := range fileInfos {
+ // Creates the inside go func
+ go func(fileInfo string) {
+ r2 := 0.0
+ for _, arg := range cs.Command.Args {
+ baseArg, baseSPath := filepath.Base(arg), filepath.Base(fileInfo)
+ if baseArg == baseSPath {
+ r2 += float64(len(arg)) / float64(len(fileInfo))
+ }
+ }
+ in <- r2
+ }(fileInfo)
+ }
+ for range fileInfos {
+ r1 += <-in
+ }
+ out <- r1
+ }(spath)
+
+ for range spaths {
+ ratio += <-out
+ }
+ // when raitio is larger than threshold 0.1
+ if ratio > 0.1 {
+ // execute evaluation formula
+ cs.Score *= 1.0 - ratio*(1.0-spath.Coefficient)
+ }
+ if cs.Score < 0 {
+ cs.Score = 0.0
}
}
return cs
}
+func readAllFiles(pathname string, s []string) ([]string, error) {
+ fromSlash := filepath.FromSlash(pathname)
+ rd, err := ioutil.ReadDir(fromSlash)
+ if err != nil {
+ logger.Warnln("read dir fail:", err)
+ return s, err
+ }
+ for _, fi := range rd {
+ if fi.IsDir() {
+ fullDir := filepath.Join(fromSlash, fi.Name())
+ s, err = readAllFiles(fullDir, s)
+ if err != nil {
+ logger.Warnln("read dir fail:", err)
+ return s, err
+ }
+ } else {
+ fullName := filepath.Join(fromSlash, fi.Name())
+ s = append(s, fullName)
+ }
+ }
+ return s, nil
+}
+
// EvaluateCommandRule defines a rule from json file to judge command score by
// whether sensitive command appears in the Command
func EvaluateCommandRule(cs CommScore) CommScore {
diff --git a/static/rules/pathrules.json b/static/rules/pathrules.json
index 6e2e93d..c48b038 100644
--- a/static/rules/pathrules.json
+++ b/static/rules/pathrules.json
@@ -1 +1,4 @@
-[{"Path":"/root/go/src/commdetection/rules","Coefficient":0.7}] \ No newline at end of file
+[{
+ "Path": "/root/go/src/commdetection",
+ "Coefficient": 0.7
+}] \ No newline at end of file