From 3e49a044d22635157916651f0acb5a062397b34b Mon Sep 17 00:00:00 2001 From: We-unite <3205135446@qq.com> Date: Fri, 9 Aug 2024 13:56:37 +0800 Subject: Add db structure, fix filePath, start filtering This commit I made several changes: - Use structure instead of simple bson.M(interface{}). bson.M has some shortcomings: 1) It makes the database in chaos and hard to read, but this's not important; 2) Some entrys may has more or less content than others, which makes it hard to decode and filt. So I design new data structure to encode and decode. Hopes that there's no bugs. - Fix the way to calculate file path. The original method is to add all the PATH entries together, that's totally wrong! PATH entry has several types, as it shows in "objtype". I can't find it in the kernel src code, so what i know is just "PARENT" means the dir the file is in, while the filename itself has the path, so we whould ignore all "PARENT"s. When the src code is found, we should check it again. - Fix bugs in updating. The update function of mongodb is set to required to has a '$' such as 'set'/'push', so when we update a whole doc, we should use replace but not update function. And, we should never ignore the error infomation it gives us. Hope that there's no more bugs for this Big Change. Now its' time to write filter as well as viewer. Best wishes with NO BUGS! --- filter/filter.go | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 filter/filter.go (limited to 'filter/filter.go') diff --git a/filter/filter.go b/filter/filter.go new file mode 100644 index 0000000..c83fb13 --- /dev/null +++ b/filter/filter.go @@ -0,0 +1,118 @@ +package main + +import ( + "context" + "fmt" + "log" + "os" + "time" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/mongo/readpref" +) + +type Exec struct { + timestamp time.Time `bson:"timestamp"` + execArgs []string `bson:"execArgs"` +} + +type Process struct { + timestamp time.Time `bson:"start_timestamp"` + ppid int `bson:"ppid"` + parentTgid int `bson:"parentTgid"` + pid int `bson:"pid"` + tgid int `bson:"tgid"` + args []string `bson:"args"` + comm string `bson:"comm"` + cwd string `bson:"cwd"` + execve []Exec `bson:"execve"` + exit_code int `bson:"exit_code"` + exit_signal int `bson:"exit_signal"` + exit_timestamp time.Time `bson:"exit_timestamp"` +} + +func (p Process) String() string { + var res string + res = "" + res += fmt.Sprintf("timestamp\t%v\n", p.timestamp) + res += fmt.Sprintf("ppid\t%d\nparentTgid\t%d\n", p.ppid, p.parentTgid) + res += fmt.Sprintf("pid\t%d\ntgid\t%d\nargs: ", p.pid, p.tgid) + for i := 0; i < len(p.args); i++ { + res += fmt.Sprintf("%s ", p.args[i]) + } + res += fmt.Sprintf("\ncomm\t%s\ncwd\t%s\n", p.comm, p.cwd) + return res +} + +// type Process struct { +// StartTimestamp time.Time `bson:"start_timestamp"` +// Ppid *int `bson:"ppid"` +// ParentTgid *int `bson:"parentTgid"` +// Pid int `bson:"pid"` +// Tgid int `bson:"tgid"` +// Args []string `bson:"args"` +// Comm *string `bson:"comm"` +// Cwd *string `bson:"cwd"` +// Execve []Exec `bson:"execve"` +// ExitCode *int `bson:"exit_code"` +// ExitSignal *int `bson:"exit_signal"` +// ExitTimestamp *time.Time `bson:"exit_timestamp"` +// } + +// func (p Process) String() string { +// var res string +// res = "" +// res += fmt.Sprintf("timestamp\t%v\n", p.StartTimestamp) +// if p.Ppid != nil && p.ParentTgid != nil { +// res += fmt.Sprintf("ppid\t%d\nparentTgid\t%d\n", *(p.Ppid), *(p.ParentTgid)) +// } +// res += fmt.Sprintf("pid\t%d\ntgid\t%d\nargs: ", p.Pid, p.Tgid) +// for i := 0; i < len(p.Args); i++ { +// res += fmt.Sprintf("%s ", p.Args[i]) +// } +// if p.Comm != nil && p.Cwd != nil { +// res += fmt.Sprintf("\ncomm\t%s\ncwd\t%s\n", *(p.Comm), *(p.Cwd)) +// } +// return res +// } + +func main() { + client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017")) + if err != nil { + fmt.Fprintf(os.Stderr, "Err connecting mongodb: %v\n", err) + } + defer client.Disconnect(context.TODO()) + + // 检查连接 + err = client.Ping(context.TODO(), readpref.Primary()) + if err != nil { + log.Fatal(err) + } + + pidCol := client.Database("test").Collection("pids") + cur, err := pidCol.Find(context.TODO(), bson.M{}) // 查询所有文档 + if err != nil { + log.Fatal(err) + } + defer cur.Close(context.TODO()) // 确保游标被关闭 + + var res []Process + for cur.Next(context.TODO()) { + var tmp Process + // 解码到Process结构体 + if err := cur.Decode(&tmp); err != nil { + log.Fatal(err) + } + res = append(res, tmp) + } + + if err := cur.Err(); err != nil { + log.Fatal(err) + } + + for i := 0; i < len(res); i++ { + fmt.Printf("------\n%v\n", res[i]) + } +} -- cgit v1.2.3-70-g09d2