summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEnderByEndera <[email protected]>2021-01-05 18:12:47 +0800
committerEnderByEndera <[email protected]>2021-01-05 18:12:47 +0800
commitff5e63a5c277b9fe2284fd2d9ed988cec177431a (patch)
tree14dc81eecac2a88b2c8e75d1302f8cb771e6c89a
parentc8e63c7cf633d01688b7d65199703d03863910ee (diff)
1. in root.go, added a new goroutine to output0.0.5
output.json in 3 mins(will let user change it in the future) 2. in version.go changed version from 0.0.2 to 0.0.5 3. changed GetCommands, not GetCommands func will have no args and will call GetCommandsFromHist func 4. deleted verEndpoint and startEvaluateEndpoint func for it has no value 5. added history.sh to use history command-line tool 6. deleted router.StartRouter() in main.go
-rw-r--r--.gitignore4
-rw-r--r--cmd/root.go38
-rw-r--r--cmd/version.go4
-rw-r--r--comm/commflush.go2
-rw-r--r--comm/commflush_test.go4
-rw-r--r--comm/commget.go108
-rw-r--r--comm/commget_test.go50
-rw-r--r--conf.yaml2
-rw-r--r--go.mod8
-rw-r--r--go.sum8
-rwxr-xr-xhistory.sh15
-rw-r--r--main.go2
-rw-r--r--router/endpoint.go28
-rw-r--r--router/endpoint_test.go19
-rw-r--r--router/router.go23
-rw-r--r--router/router_test.go16
-rw-r--r--rules/rules.go2
-rw-r--r--rules/rules_test.go4
-rw-r--r--rules/rulestype_test.go8
-rw-r--r--static/rules/pathrules.json5
20 files changed, 159 insertions, 191 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..601e296
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+commdetection
+.vscode
+static/base
+.VSCodeCounter \ No newline at end of file
diff --git a/cmd/root.go b/cmd/root.go
index 87ec324..a81d8fe 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -1,7 +1,7 @@
/*
* @Author: EnderByEndera
* @Date: 2020-12-19 11:59:02
- * @LastEditTime: 2020-12-28 16:42:48
+ * @LastEditTime: 2021-01-05 18:04:44
* @LastEditors: Please set LastEditors
* @Description: root of the commdetection cmd
* @FilePath: /commdetection/cmd/root.go
@@ -13,7 +13,13 @@ import (
"commdetection/comm"
"commdetection/logger"
"commdetection/model"
+ "commdetection/router"
"commdetection/rules"
+ "encoding/json"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "time"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@@ -89,21 +95,19 @@ func root() {
case "man":
filters = append(filters, comm.ManCommandFilter)
}
-}
-// StartEvaluateCommands starts evaluating commands using variables rootCmd provided
-func StartEvaluateCommands(filename string) []model.CommScore {
- // 从文件中获取路径,默认获取路径为/root/.bash_history
- logger.Debugf("Start getting commmands from \"%s\"", filename)
- commands := comm.GetCommands(filename, "")
- // 清理无效命令,利用filter函数保留有效命令以便提供分析
- logger.Debugf("Start flushing commands using \"%s\" filter", filterStr)
- commands = comm.FlushCommands(commands, filters)
- // 初始化命令得分
- logger.Debugf("Initializing commands' scores")
- css := rules.InitCommScores(commands)
- // 评估命令,利用rs中保留的规则进行评估
- logger.Debugf("Evaluating commands' scores using %s evaluations", evaluations)
- css = rules.EvaluateCommScore(css, rs)
- return css
+ go func() {
+ for {
+ comms := comm.GetCommands()
+ comms = comm.FlushCommands(comms, filters)
+ css := rules.InitCommScores(comms)
+ css = rules.EvaluateCommScore(css, rs)
+ jsonBuf, _ := json.Marshal(css)
+ ioutil.WriteFile(filepath.Join(os.Getenv("COMMDEPATH"), "static", "base", "output.json"), jsonBuf, os.ModeAppend)
+ logger.Debugln("New output.json file is built")
+ time.Sleep(3 * time.Minute)
+ }
+ }()
+
+ router.StartRouter()
}
diff --git a/cmd/version.go b/cmd/version.go
index e239c74..f851996 100644
--- a/cmd/version.go
+++ b/cmd/version.go
@@ -1,7 +1,7 @@
/*
* @Author: your name
* @Date: 2020-12-19 11:49:14
- * @LastEditTime: 2020-12-29 15:48:06
+ * @LastEditTime: 2021-01-05 18:09:49
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /commdetection/cmd/version.go
@@ -16,7 +16,7 @@ import (
)
// Ver means version of the system
-const Ver = "command detection ver 0.0.2"
+const Ver = "command detection ver 0.0.5"
var verCmd = &cobra.Command{
Use: "version",
diff --git a/comm/commflush.go b/comm/commflush.go
index df5c7a8..d0b79e8 100644
--- a/comm/commflush.go
+++ b/comm/commflush.go
@@ -1,7 +1,7 @@
/*
* @Author: EnderByEndera
* @Date: 2020-12-07 09:22:53
- * @LastEditTime: 2020-12-31 11:04:07
+ * @LastEditTime: 2021-01-05 13:27:31
* @LastEditors: Please set LastEditors
* @Description: this file flushes invalid commands using various types of filters
* @FilePath: /commdetection/commflush.go
diff --git a/comm/commflush_test.go b/comm/commflush_test.go
index 2a3f408..c1d3344 100644
--- a/comm/commflush_test.go
+++ b/comm/commflush_test.go
@@ -1,7 +1,7 @@
/*
* @Author: your name
* @Date: 2020-12-03 12:51:28
- * @LastEditTime: 2020-12-31 11:04:35
+ * @LastEditTime: 2021-01-05 17:10:16
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /commdetection/comm/commflush_test.go
@@ -84,7 +84,7 @@ func TestFlushCommands(t *testing.T) {
func BenchmarkFlushCommands(b *testing.B) {
b.Run("FlushCommands BenchMark Test", func(b *testing.B) {
filters := []Filter{WhichCommandFilter}
- commands := GetCommands("/root/.bash_history", "")
+ commands := GetCommands()
b.ResetTimer()
commands = FlushCommands(commands, filters)
b.StopTimer()
diff --git a/comm/commget.go b/comm/commget.go
index d868252..22ee6f7 100644
--- a/comm/commget.go
+++ b/comm/commget.go
@@ -1,7 +1,7 @@
/*
* @Author: EnderByEndera
* @Date: 2020-12-02 17:08:59
- * @LastEditTime: 2021-01-04 15:59:10
+ * @LastEditTime: 2021-01-05 17:19:16
* @LastEditors: Please set LastEditors
* @Description: Get commands from file or network
* @FilePath: /commdetection/preprocessing/commget.go
@@ -10,37 +10,93 @@
package comm
import (
+ "bytes"
"commdetection/logger"
"io/ioutil"
- "reflect"
+ "os/exec"
+ "strconv"
"strings"
+ "time"
)
// Command contains command and its flags or symbols
type Command struct {
- CommName string `json:"commName"`
- Args []string `json:"args"`
- Flags []string `json:"flags,omitempty"`
+ CommName string `json:"commName"`
+ Args []string `json:"args"`
+ Flags []string `json:"flags"`
+ TimeStamp time.Time `json:"timestamp"`
+ User string `json:"user"`
+ Mac string `json:"mac"`
}
-// GetCommands returns a list of commands preprocessed which first get commands from file then net
-func GetCommands(file string, url string) []Command {
- if file != "" {
- commands, err := getCommandsFromFile(file)
- if err == nil {
- return commands
+// GetCommandsFromHist gets the commands by using `history` bash command
+func GetCommandsFromHist() ([]Command, error) {
+ var (
+ stderr bytes.Buffer
+ stdout bytes.Buffer
+ )
+ hist := exec.Command("/bin/bash", "-c", `$COMMDEPATH/history.sh`)
+ hist.Stderr = &stderr
+ hist.Stdout = &stdout
+ err := hist.Run()
+ commands := commandsFromString(stdout.String())
+ return commands, err
+}
+
+func commandsFromString(s string) []Command {
+ var commands []Command
+ commLines := strings.Split(s, "\n")
+ for _, comm := range commLines {
+ newComm := Command{}
+ var timestamp string
+ for num, commArg := range strings.Fields(comm) {
+ if _, err := strconv.Atoi(commArg); err != nil && num == 0 {
+ break
+ }
+ switch {
+ case num == 0:
+ continue
+ case num == 1:
+ timestamp = commArg
+ case num == 2:
+ timestamp += " " + commArg
+ var err error
+ newComm.TimeStamp, err = time.Parse("2006-01-02 15:04:05", timestamp)
+ if err != nil {
+ newComm.TimeStamp = time.Time{}
+ }
+ case num == 3:
+ newComm.Mac = commArg
+ case num == 4:
+ newComm.User = commArg
+ case num == 5:
+ newComm.CommName = commArg
+ default:
+ if commArg[0] != '-' { // comm is a flag
+ newComm.Args = append(newComm.Args, commArg)
+ } else { // comm is just a normal argument
+ newComm.Flags = append(newComm.Flags, commArg)
+ }
+ }
}
- logger.Warnln(err)
- }
- if url != "" {
- commands, err := getCommandsFromNet(url)
- if err == nil {
- return commands
+ if newComm.CommName != "" {
+ commands = append(commands, newComm)
}
+ }
+ return commands
+}
+
+// GetCommands returns a list of commands preprocessed which first get commands from file then net
+//
+// If file and url is set to null, GetCommands will call getCommandsFromHistory func automatically
+//TODO: Will temporarily use GetCommandsFromHist to replace GetCommands, will put commands in the mongo db later
+func GetCommands() []Command {
+ commands, err := GetCommandsFromHist()
+ if err != nil {
logger.Warnln(err)
+ return []Command{}
}
- logger.Warnln("cannot get commands from any file or net")
- return []Command{}
+ return commands
}
func getCommandsFromFile(f string) ([]Command, error) {
@@ -58,15 +114,11 @@ func getCommandsFromFile(f string) ([]Command, error) {
func splitCommandsInLine(tComm []string) []Command {
commands := []Command{}
- newComm := Command{
- CommName: "",
- Args: []string{},
- Flags: []string{},
- }
+ newComm := Command{}
for commNum, comm := range tComm {
if commNum == 0 {
newComm.CommName = comm
- } else if comm == "&&" { // comm is the separator of the whole commmand
+ } else if comm == "&&" { // comm is the separator of the whole command
commands = append(commands, splitCommandsInLine(tComm[commNum+1:])...)
break
} else if comm[0] != '-' { // comm is a flag
@@ -75,11 +127,7 @@ func splitCommandsInLine(tComm []string) []Command {
newComm.Flags = append(newComm.Flags, comm)
}
}
- if !reflect.DeepEqual(newComm, Command{
- CommName: "",
- Args: []string{},
- Flags: []string{},
- }) {
+ if newComm.CommName != "" {
commands = append([]Command{newComm}, commands...)
}
return commands
diff --git a/comm/commget_test.go b/comm/commget_test.go
index 62f4a2b..0845fb9 100644
--- a/comm/commget_test.go
+++ b/comm/commget_test.go
@@ -1,7 +1,7 @@
/*
* @Author: your name
* @Date: 2020-12-02 17:09:14
- * @LastEditTime: 2021-01-04 16:00:18
+ * @LastEditTime: 2021-01-05 17:25:02
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /commdetection/preprocessing/commget_test.go
@@ -16,39 +16,12 @@ import (
)
func TestGetCommands(t *testing.T) {
- t.Run("Get Commands From File Test", func(t *testing.T) {
- commands := GetCommands("/root/.bash_history", "")
- runCommands(commands, t)
- })
-
- t.Run("Get Commands From Net Test", func(t *testing.T) {
- commands := GetCommands("", "http://127.0.0.1:8080")
- runCommands(commands, t)
- })
-
- t.Run("Get commands from incorrect file test", func(t *testing.T) {
- commands := GetCommands("/root/aaa", "")
- runCommands(commands, t)
- })
-
- t.Run("Get commands from no-existence file test", func(t *testing.T) {
- commands := GetCommands("dsfa", "")
- runCommands(commands, t)
- })
-
- t.Run("Get commands from no-existence net", func(t *testing.T) {
- commands := GetCommands("", "sdfsagd")
- runCommands(commands, t)
- })
-
- t.Run("Get commands from nothing", func(t *testing.T) {
- commands := GetCommands("", "")
- runCommands(commands, t)
- })
-
- t.Run("Get Commands from Both Test", func(t *testing.T) {
- commands := GetCommands("/root/.bash_history", "http://127.0.0.1:8080")
- runCommands(commands, t)
+ t.Run("Get Commands From history Test", func(t *testing.T) {
+ commands, err := GetCommandsFromHist()
+ if err != nil {
+ t.Error(err)
+ }
+ checkCommands(commands, t)
})
}
@@ -76,12 +49,12 @@ func TestSplitCommandsInLine(t *testing.T) {
if !reflect.DeepEqual(commands, predict) {
fmt.Println(commands)
fmt.Println(predict)
- t.Errorf("results are not as predicted")
+ t.Log("result is not as predicted")
}
})
}
-func runCommands(commands []Command, t *testing.T) {
+func checkCommands(commands []Command, t *testing.T) {
for commNum, command := range commands {
if command.CommName == "" {
t.Errorf("command %d is empty", commNum)
@@ -91,9 +64,12 @@ func runCommands(commands []Command, t *testing.T) {
t.Errorf("command %d: %s flag %d is empty", commNum+1, command.CommName, flagNum)
}
}
+ if command.TimeStamp.Unix() <= 0.0 {
+ t.Errorf("command %d: %s timestamp is empty", commNum+1, command.CommName)
+ }
}
}
func BenchmarkGetCommands(b *testing.B) {
- GetCommands("/root/.bash_history", "")
+ GetCommands()
}
diff --git a/conf.yaml b/conf.yaml
index dd6f143..2e30593 100644
--- a/conf.yaml
+++ b/conf.yaml
@@ -1,2 +1,2 @@
ginmode: release
-port: 8000 \ No newline at end of file
+port: 8060 \ No newline at end of file
diff --git a/go.mod b/go.mod
index 3fa48bd..0c2aa9f 100644
--- a/go.mod
+++ b/go.mod
@@ -1,11 +1,10 @@
module commdetection
-go 1.15
+go 1.13
require (
github.com/ajg/form v1.5.1 // indirect
github.com/andybalholm/brotli v1.0.1 // indirect
- github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/gavv/httpexpect v2.0.0+incompatible
@@ -15,9 +14,7 @@ require (
github.com/google/go-cmp v0.5.4 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
- github.com/gorilla/websocket v1.4.2 // indirect
github.com/imkira/go-interpol v1.1.0 // indirect
- github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/klauspost/compress v1.11.4 // indirect
@@ -31,10 +28,7 @@ require (
github.com/sergi/go-diff v1.1.0 // indirect
github.com/sirupsen/logrus v1.7.0
github.com/smartystreets/assertions v1.2.0 // indirect
- github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/spf13/cobra v1.1.1
- github.com/spf13/pflag v1.0.5 // indirect
- github.com/stretchr/testify v1.6.1 // indirect
github.com/ugorji/go v1.2.2 // indirect
github.com/valyala/fasthttp v1.19.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
diff --git a/go.sum b/go.sum
index 4b522e9..32d10d4 100644
--- a/go.sum
+++ b/go.sum
@@ -25,8 +25,6 @@ github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
-github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef h1:46PFijGLmAjMPwCCCo7Jf0W6f9slllCkkv7vyc1yOSg=
-github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
@@ -264,8 +262,6 @@ github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4k
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
-github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
-github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v1.1.1 h1:KfztREH0tPxJJ+geloSLaAkaPkr4ki2Er5quFV1TDo4=
github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
@@ -293,8 +289,6 @@ github.com/ugorji/go/codec v1.2.2 h1:08Gah8d+dXj4cZNUHhtuD/S4PXD5WpVbj5B8/ClELAQ
github.com/ugorji/go/codec v1.2.2/go.mod h1:OM8g7OAy52uYl3Yk+RE/3AS1nXFn1Wh4PPLtupCxbuU=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
-github.com/valyala/fasthttp v1.18.0 h1:IV0DdMlatq9QO1Cr6wGJPVW1sV1Q8HvZXAIcjorylyM=
-github.com/valyala/fasthttp v1.18.0/go.mod h1:jjraHZVbKOXftJfsOYoAjaeygpj5hr8ermTRJNroD7A=
github.com/valyala/fasthttp v1.19.0 h1:PfTS4PeH3xDr3WomrDS2ID8lU2GskK1xS3YG6gIpibU=
github.com/valyala/fasthttp v1.19.0/go.mod h1:jjraHZVbKOXftJfsOYoAjaeygpj5hr8ermTRJNroD7A=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
@@ -401,8 +395,6 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20201218084310-7d0127a74742 h1:+CBz4km/0KPU3RGTwARGh/noP3bEwtHcq+0YcBQM2JQ=
-golang.org/x/sys v0.0.0-20201218084310-7d0127a74742/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201231184435-2d18734c6014 h1:joucsQqXmyBVxViHCPFjG3hx8JzIFSaym3l3MM/Jsdg=
golang.org/x/sys v0.0.0-20201231184435-2d18734c6014/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
diff --git a/history.sh b/history.sh
new file mode 100755
index 0000000..5f8c9cf
--- /dev/null
+++ b/history.sh
@@ -0,0 +1,15 @@
+###
+ # @Author: EnderbyEndera
+ # @Date: 2021-01-05 15:36:26
+ # @LastEditTime: 2021-01-05 17:23:34
+ # @LastEditors: Please set LastEditors
+ # @Description: In User Settings Edit
+ # @FilePath: /commdetection/history.sh
+###
+
+export HISTTIMEFORMAT="%F %T `cat /sys/class/net/eth0/address` `whoami` "
+export HISTSIZE=10000
+export HISTFILE=~/.bash_history
+history -r
+history
+history -c \ No newline at end of file
diff --git a/main.go b/main.go
index 16dcb6f..6f06a72 100644
--- a/main.go
+++ b/main.go
@@ -12,7 +12,6 @@ package main
import (
"commdetection/cmd"
"commdetection/logger"
- "commdetection/router"
"github.com/sirupsen/logrus"
)
@@ -25,5 +24,4 @@ func main() {
if err := cmd.Execute(); err != nil {
logger.Fatalln(err)
}
- router.StartRouter()
}
diff --git a/router/endpoint.go b/router/endpoint.go
index 92d2245..16033b2 100644
--- a/router/endpoint.go
+++ b/router/endpoint.go
@@ -1,7 +1,7 @@
/*
* @Author: EnderByEndera
* @Date: 2020-12-28 15:53:37
- * @LastEditTime: 2021-01-04 16:46:56
+ * @LastEditTime: 2021-01-05 11:53:02
* @LastEditors: Please set LastEditors
* @Description: endpoint.go contains all the endpoints
* @FilePath: /commdetection/router/endpoint.go
@@ -10,24 +10,14 @@
package router
import (
- "commdetection/cmd"
"commdetection/logger"
"commdetection/model"
- "encoding/json"
- "io/ioutil"
"net/http"
- "os"
"path/filepath"
"github.com/gin-gonic/gin"
)
-func verEndpoint(c *gin.Context) {
- c.JSON(http.StatusOK, gin.H{
- "ver": cmd.Ver,
- })
-}
-
// uploadCommEndpoint uploads the file storing commands to the system
func uploadCommEndpoint(c *gin.Context) {
commfile, err := c.FormFile("commfile")
@@ -46,22 +36,6 @@ func uploadCommEndpoint(c *gin.Context) {
})
}
-// startEvaluateEndpoint let the system start to evaluate commands and their args and gives the score to the user
-func startEvaluateEndpoint(c *gin.Context) {
- css := cmd.StartEvaluateCommands(cmd.GetFileName())
- jsonBuf, _ := json.Marshal(css)
- logger.Debugln("Storing result to \"static/output.json\" file")
- if err := ioutil.WriteFile(filepath.Join(StaticRoute, "output.json"), jsonBuf, os.ModeAppend); err != nil {
- handleErr(c, err)
- return
- }
-
- c.JSON(http.StatusOK, gin.H{
- "message": "ok",
- "scores": css,
- })
-}
-
// getCommrulesEndpoint gets all the command rules stored in the json file
// if error happened, handleErr will be executed
func getCommrulesEndpoint(c *gin.Context) {
diff --git a/router/endpoint_test.go b/router/endpoint_test.go
index 33872f0..2a011ce 100644
--- a/router/endpoint_test.go
+++ b/router/endpoint_test.go
@@ -1,7 +1,7 @@
/*
* @Author: your name
* @Date: 2020-12-28 16:05:24
- * @LastEditTime: 2021-01-04 19:02:57
+ * @LastEditTime: 2021-01-05 15:52:59
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /commdetection/router/endpoint_test.go
@@ -10,7 +10,6 @@
package router
import (
- "commdetection/cmd"
"commdetection/model"
"net/http"
"net/http/httptest"
@@ -59,14 +58,6 @@ func init() {
func TestEndpoint(t *testing.T) {
e = httpexpect.New(t, url)
- t.Run("Test verEndpoint", func(t *testing.T) {
- e.GET("/version").
- Expect().
- Status(http.StatusOK).
- JSON().Object().
- ValueEqual("ver", cmd.Ver)
- })
-
t.Run("Test uploadCommEndpoint", func(t *testing.T) {
e.POST("/evaluation/upload").
WithMultipart().
@@ -77,14 +68,6 @@ func TestEndpoint(t *testing.T) {
ValueEqual("message", "ok")
})
- t.Run("Test startEvaluateEndpoint", func(t *testing.T) {
- e.GET("/evaluation/start").
- Expect().
- Status(http.StatusOK).
- JSON().Object().
- ValueEqual("message", "ok")
- })
-
t.Run("Test getCommrulesEndpoint", func(t *testing.T) {
e.GET("/commrules/getrules").
Expect().
diff --git a/router/router.go b/router/router.go
index d8c84a4..2ded80b 100644
--- a/router/router.go
+++ b/router/router.go
@@ -1,7 +1,7 @@
/*
* @Author: EnderByEndera
* @Date: 2020-12-23 13:17:08
- * @LastEditTime: 2020-12-29 16:33:47
+ * @LastEditTime: 2021-01-05 17:54:33
* @LastEditors: Please set LastEditors
* @Description: router.go defines gin.Engine and its router
* @FilePath: /commdetection/route/router.go
@@ -23,15 +23,6 @@ import (
"github.com/gin-gonic/gin"
)
-var (
- addr = yaml.GetPort()
- // StaticRoute sets the base route of the gin static filesystem
- StaticRoute = filepath.Join(os.Getenv("COMMDEPATH"), "static", "base")
-
- router *gin.Engine
- srv *http.Server
-)
-
func init() {
// initilize gin-gonic backend engine
ginmode, err := yaml.GetGinMode()
@@ -53,8 +44,18 @@ func init() {
manageRouter()
}
+var (
+ addr = yaml.GetPort()
+ // StaticRoute sets the base route of the gin static filesystem
+ StaticRoute = filepath.Join(os.Getenv("COMMDEPATH"), "static", "base")
+
+ router *gin.Engine
+ srv *http.Server
+)
+
// StartRouter starts and sets ending method for gin engine
func StartRouter() {
+
go func() {
// engine start on http://127.0.0.1:8000
logger.Debugln("Server started at", srv.Addr)
@@ -81,11 +82,9 @@ func StartRouter() {
func manageRouter() {
// "/static" stores all the static file of the system
router.Static("/static", StaticRoute)
- router.GET("/version", verEndpoint)
evar := router.Group("/evaluation")
{
evar.POST("/upload", uploadCommEndpoint)
- evar.GET("/start", startEvaluateEndpoint)
}
commr := router.Group("/commrules")
{
diff --git a/router/router_test.go b/router/router_test.go
index 991e63b..41f46a9 100644
--- a/router/router_test.go
+++ b/router/router_test.go
@@ -8,19 +8,3 @@
*/
package router
-
-import (
- "net/http"
- "net/http/httptest"
- "testing"
-
- "github.com/gavv/httpexpect"
-)
-
-func TestStart(t *testing.T) {
- srv := httptest.NewServer(router)
- eng := httpexpect.New(t, srv.URL)
- eng.GET("/version").
- Expect().
- Status(http.StatusOK)
-}
diff --git a/rules/rules.go b/rules/rules.go
index 8c3ec65..4929551 100644
--- a/rules/rules.go
+++ b/rules/rules.go
@@ -1,7 +1,7 @@
/*
* @Author: EnderByEndera
* @Date: 2020-12-04 15:03:00
- * @LastEditTime: 2021-01-04 16:44:06
+ * @LastEditTime: 2021-01-05 17:58:28
* @LastEditors: Please set LastEditors
* @Description: rules provide all the rules to check the commands' availability and set score of every command
* @FilePath: /commdetection/rules/commcheck.go
diff --git a/rules/rules_test.go b/rules/rules_test.go
index f29785f..41eb131 100644
--- a/rules/rules_test.go
+++ b/rules/rules_test.go
@@ -1,7 +1,7 @@
/*
* @Author: EnderByEndera
* @Date: 2020-12-04 15:03:09
- * @LastEditTime: 2021-01-04 16:43:21
+ * @LastEditTime: 2021-01-05 17:19:26
* @LastEditors: Please set LastEditors
* @Description: Test commrules.go
* @FilePath: /commdetection/rules/commrules_test.go
@@ -127,7 +127,7 @@ func TestRule(t *testing.T) {
}
func TestInitCommScores(t *testing.T) {
- comms := comm.GetCommands("/root/.bash_history", "")
+ comms := comm.GetCommands()
comms = comm.FlushCommands(comms, []comm.Filter{comm.WhichCommandFilter})
css := InitCommScores(comms)
fmt.Println(css)
diff --git a/rules/rulestype_test.go b/rules/rulestype_test.go
index 9436e9d..d92d0c3 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: 2021-01-04 19:06:53
+ * @LastEditTime: 2021-01-05 17:19:57
* @LastEditors: Please set LastEditors
* @Description: Test UnmarshalSetting and MarshalSetting
* @FilePath: /commdetection/rules/rulestype_test.go
@@ -69,7 +69,7 @@ func TestEvaluateWebsiteRule(t *testing.T) {
}
func BenchmarkEvaluateCommandRule(b *testing.B) {
- comms := comm.GetCommands("/root/.bash_history", "")
+ comms := comm.GetCommands()
comms = comm.FlushCommands(comms, []comm.Filter{comm.WhichCommandFilter})
css := InitCommScores(comms)
b.ResetTimer()
@@ -79,7 +79,7 @@ func BenchmarkEvaluateCommandRule(b *testing.B) {
}
func BenchmarkEvaluatePathRule(b *testing.B) {
- comms := comm.GetCommands("/root/.bash_history", "")
+ comms := comm.GetCommands()
comms = comm.FlushCommands(comms, []comm.Filter{comm.WhichCommandFilter})
css := InitCommScores(comms)
b.ResetTimer()
@@ -89,7 +89,7 @@ func BenchmarkEvaluatePathRule(b *testing.B) {
}
func BenchmarkEvaluateWebsiteRule(b *testing.B) {
- comms := comm.GetCommands("/root/.bash_history", "")
+ comms := comm.GetCommands()
comms = comm.FlushCommands(comms, []comm.Filter{comm.WhichCommandFilter})
css := InitCommScores(comms)
b.ResetTimer()
diff --git a/static/rules/pathrules.json b/static/rules/pathrules.json
index 7e04441..4f8c9f0 100644
--- a/static/rules/pathrules.json
+++ b/static/rules/pathrules.json
@@ -1,4 +1 @@
-[{
- "Path": "/root/go/src/commdetection",
- "Coefficient": 0.5
-}] \ No newline at end of file
+[{"Path":"/root/go/src/commdetection","Coefficient":0.5}] \ No newline at end of file