diff options
Diffstat (limited to '')
-rw-r--r-- | listener/audit.go (renamed from src/audit.go) | 0 | ||||
-rw-r--r-- | listener/basefunc.go (renamed from src/basefunc.go) | 0 | ||||
-rw-r--r-- | listener/deal.go (renamed from src/deal.go) | 200 | ||||
-rw-r--r-- | listener/global.go | 84 | ||||
-rw-r--r-- | listener/go.mod (renamed from src/go.mod) | 0 | ||||
-rw-r--r-- | listener/go.sum (renamed from src/go.sum) | 0 | ||||
-rw-r--r-- | listener/go.work (renamed from src/go.work) | 0 | ||||
-rw-r--r-- | listener/go.work.sum (renamed from src/go.work.sum) | 0 | ||||
-rw-r--r-- | listener/godo.go (renamed from src/godo.go) | 22 | ||||
-rw-r--r-- | listener/mongo.go (renamed from src/mongo.go) | 36 | ||||
m--------- | listener/netlink (renamed from src/netlink) | 0 | ||||
-rw-r--r-- | listener/organize.go (renamed from src/organize.go) | 32 | ||||
-rw-r--r-- | listener/receive.go (renamed from src/receive.go) | 0 |
13 files changed, 249 insertions, 125 deletions
diff --git a/src/audit.go b/listener/audit.go index ed48691..ed48691 100644 --- a/src/audit.go +++ b/listener/audit.go | |||
diff --git a/src/basefunc.go b/listener/basefunc.go index 2f39507..2f39507 100644 --- a/src/basefunc.go +++ b/listener/basefunc.go | |||
diff --git a/src/deal.go b/listener/deal.go index e553174..8f77431 100644 --- a/src/deal.go +++ b/listener/deal.go | |||
@@ -4,6 +4,7 @@ import ( | |||
4 | "fmt" | 4 | "fmt" |
5 | "os" | 5 | "os" |
6 | "syscall" | 6 | "syscall" |
7 | "time" | ||
7 | 8 | ||
8 | "go.mongodb.org/mongo-driver/bson" | 9 | "go.mongodb.org/mongo-driver/bson" |
9 | ) | 10 | ) |
@@ -15,10 +16,7 @@ const ( | |||
15 | fileColName string = "files" | 16 | fileColName string = "files" |
16 | ) | 17 | ) |
17 | 18 | ||
18 | // var mongoMutex sync.Mutex | ||
19 | var pidCol, fdCol, fileCol mongoClient | 19 | var pidCol, fdCol, fileCol mongoClient |
20 | |||
21 | var docRes []bson.M | ||
22 | var err error | 20 | var err error |
23 | 21 | ||
24 | func deal() { | 22 | func deal() { |
@@ -30,12 +28,12 @@ func deal() { | |||
30 | fmt.Fprintf(os.Stderr, "Error while initing the mongodb: %v\n", err) | 28 | fmt.Fprintf(os.Stderr, "Error while initing the mongodb: %v\n", err) |
31 | return | 29 | return |
32 | } | 30 | } |
33 | err = pidCol.InsertOne(bson.M{ | 31 | err = pidCol.InsertOne(Process{ |
34 | "ppid": 1, | 32 | Ppid: 1, |
35 | "pid": containerdPid, | 33 | Pid: containerdPid, |
36 | "cwd": "/", | 34 | Cwd: "/", |
37 | "children": []bson.M{}, | 35 | Children: make([]int, 0), |
38 | "daemon": true, | 36 | Star: true, |
39 | }) | 37 | }) |
40 | if err != nil { | 38 | if err != nil { |
41 | fmt.Fprintf(os.Stderr, "Error while initing the mongodb: %v\n", err) | 39 | fmt.Fprintf(os.Stderr, "Error while initing the mongodb: %v\n", err) |
@@ -50,7 +48,6 @@ func deal() { | |||
50 | fmt.Fprintf(os.Stderr, "Error while initing the mongodb: %v\n", err) | 48 | fmt.Fprintf(os.Stderr, "Error while initing the mongodb: %v\n", err) |
51 | } | 49 | } |
52 | 50 | ||
53 | fmt.Printf("Containerd: %d\n", containerdPid) | ||
54 | defer pidCol.Disconnect() | 51 | defer pidCol.Disconnect() |
55 | defer fdCol.Disconnect() | 52 | defer fdCol.Disconnect() |
56 | defer fileCol.Disconnect() | 53 | defer fileCol.Disconnect() |
@@ -81,30 +78,25 @@ func deal() { | |||
81 | } | 78 | } |
82 | 79 | ||
83 | func deletePid(cooked Event) { | 80 | func deletePid(cooked Event) { |
84 | // 先从老爹那里销户 | ||
85 | pidCol.UpdateOne(bson.M{"pid": cooked.ppid}, bson.M{ | ||
86 | "$pull": bson.M{ | ||
87 | "children": cooked.pid, | ||
88 | }, | ||
89 | }) | ||
90 | |||
91 | // 在这套逻辑里,孩子是不需要收容的,因为我们根本就不看ppid来工作 | 81 | // 在这套逻辑里,孩子是不需要收容的,因为我们根本就不看ppid来工作 |
92 | 82 | // 父节点那里也不需要销户 | |
93 | // 去了 | 83 | // 理论上这里需要闭所有文件述符,为了处理效率,留流程 |
94 | pidCol.UpdateOne(bson.M{"pid": cooked.pid}, bson.M{ | 84 | err := pidCol.UpdateOne(bson.M{"pid": cooked.pid}, bson.M{ |
95 | "$set": bson.M{ | 85 | "$set": bson.M{ |
96 | "exit_timestamp": cooked.timestamp, | 86 | "exit_timestamp": cooked.timestamp, |
97 | "exit_code": cooked.exit_code, | 87 | "exit_code": cooked.exit_code, |
98 | "exit_signal": cooked.exit_signal, | 88 | "exit_signal": cooked.exit_signal, |
99 | }, | 89 | }, |
100 | }) | 90 | }) |
101 | 91 | if err != nil { | |
102 | // 理论上这里需要关闭所有文件描述符,但为了处理效率,留给后续流程 | 92 | fmt.Fprintf(os.Stderr, "Err updating: %v\n", err) |
93 | } | ||
103 | } | 94 | } |
104 | 95 | ||
105 | func dealNewPid(cooked Event) { | 96 | func dealNewPid(cooked Event) { |
106 | // 自身是否已经记录 | 97 | // 自身是否已经记录 |
107 | docRes, err = pidCol.Finddoc(bson.M{"pid": cooked.pid}) | 98 | var docRes []Process |
99 | err = pidCol.Finddoc(bson.M{"pid": cooked.pid}, &docRes) | ||
108 | if err != nil { | 100 | if err != nil { |
109 | fmt.Fprintf(os.Stderr, "Err finding: %v\n", err) | 101 | fmt.Fprintf(os.Stderr, "Err finding: %v\n", err) |
110 | return | 102 | return |
@@ -113,97 +105,104 @@ func dealNewPid(cooked Event) { | |||
113 | if len(docRes) != 0 { | 105 | if len(docRes) != 0 { |
114 | // 进程原本就存在,换言之别的消息先到了 | 106 | // 进程原本就存在,换言之别的消息先到了 |
115 | // 所有先行抵达的消息必须保留execve/children字段 | 107 | // 所有先行抵达的消息必须保留execve/children字段 |
116 | // 此处不再更新 | 108 | docRes[0].Ppid = cooked.ppid |
117 | // 以防把原有信息更没了 | 109 | docRes[0].ParentTgid = cooked.parentTgid |
118 | pidCol.UpdateOne(bson.M{"pid": cooked.pid}, bson.M{ | 110 | docRes[0].Pid = cooked.pid |
119 | "start_timestamp": cooked.timestamp, | 111 | docRes[0].Tgid = cooked.tgid |
120 | "ppid": cooked.ppid, | 112 | docRes[0].Cwd = cooked.cwd |
121 | "parentTgid": cooked.parentTgid, | 113 | docRes[0].Comm = cooked.comm |
122 | "pid": cooked.pid, | 114 | docRes[0].Args = cooked.argv |
123 | "tgid": cooked.tgid, | 115 | |
124 | "cwd": cooked.cwd, | 116 | err := pidCol.ReplaceOne(bson.M{"pid": cooked.pid}, docRes[0]) |
125 | // "execve": []bson.M{}, | 117 | if err != nil { |
126 | "args": cooked.argv, | 118 | fmt.Fprintf(os.Stderr, "Err replaceing: %v\n", err) |
127 | // "children": []bson.M{}, | 119 | } |
128 | }) | ||
129 | } else { | 120 | } else { |
130 | // 这进程本是新修的 | 121 | // 这进程本是新修的 |
131 | pidCol.InsertOne(bson.M{ | 122 | err := pidCol.InsertOne(Process{ |
132 | "start_timestamp": cooked.timestamp, | 123 | StartTimestamp: cooked.timestamp, |
133 | "ppid": cooked.ppid, | 124 | Ppid: cooked.ppid, |
134 | "parentTgid": cooked.parentTgid, | 125 | ParentTgid: cooked.parentTgid, |
135 | "pid": cooked.pid, | 126 | Pid: cooked.pid, |
136 | "tgid": cooked.tgid, | 127 | Tgid: cooked.tgid, |
137 | "cwd": cooked.cwd, | 128 | Args: cooked.argv, |
138 | "execve": []bson.M{}, | 129 | Comm: cooked.comm, |
139 | "args": cooked.argv, | 130 | Cwd: cooked.cwd, |
140 | "children": []bson.M{}, | 131 | Execve: make([]Exec, 0), |
132 | Children: make([]int, 0), | ||
141 | }) | 133 | }) |
134 | if err != nil { | ||
135 | fmt.Fprintf(os.Stderr, "Err inserting: %v\n", err) | ||
136 | } | ||
142 | } | 137 | } |
143 | 138 | ||
144 | pidCol.UpdateOne(bson.M{"pid": cooked.ppid}, bson.M{ | 139 | err := pidCol.UpdateOne(bson.M{"pid": cooked.ppid}, bson.M{ |
145 | "$push": bson.M{ | 140 | "$push": bson.M{ |
146 | "children": cooked.pid, | 141 | "children": cooked.pid, |
147 | }, | 142 | }, |
148 | }) | 143 | }) |
144 | if err != nil { | ||
145 | fmt.Fprintf(os.Stderr, "Err updating: %v\n", err) | ||
146 | } | ||
149 | } | 147 | } |
150 | 148 | ||
151 | func dealExecve(cooked Event) { | 149 | func dealExecve(cooked Event) { |
152 | // 父进程在不在?不在扔 | 150 | var docRes []Process |
153 | docRes, err = pidCol.Finddoc(bson.M{"pid": cooked.ppid}) | ||
154 | if err != nil || len(docRes) != 1 { | ||
155 | return | ||
156 | } | ||
157 | |||
158 | // 首先检查进程是否存在,如不存在则为之创建 | 151 | // 首先检查进程是否存在,如不存在则为之创建 |
159 | docRes, err = pidCol.Finddoc(bson.M{"pid": cooked.pid}) | 152 | err = pidCol.Finddoc(bson.M{"pid": cooked.pid}, &docRes) |
160 | if err != nil { | 153 | if err != nil { |
161 | return | 154 | return |
162 | } | 155 | } |
163 | 156 | ||
164 | if len(docRes) == 1 { | 157 | if len(docRes) == 1 { |
165 | // 自身已在,直接记录 | 158 | // 自身已在,直接记录 |
166 | pidCol.UpdateOne(bson.M{"pid": cooked.pid}, bson.M{ | 159 | docRes[0].Execve = append(docRes[0].Execve, Exec{ |
167 | "$push": bson.M{ | 160 | Timestamp: cooked.timestamp, |
168 | "execve": bson.M{ | 161 | ExecArgs: cooked.argv, |
169 | "timestamp": cooked.timestamp, | ||
170 | "execArgs": cooked.argv, | ||
171 | }, | ||
172 | }, | ||
173 | }) | 162 | }) |
163 | |||
164 | err := pidCol.ReplaceOne(bson.M{"pid": cooked.pid}, docRes[0]) | ||
165 | if err != nil { | ||
166 | fmt.Fprintf(os.Stderr, "Err replacing: %v\n", err) | ||
167 | } | ||
174 | } else { | 168 | } else { |
175 | // 先fork抵达,插入 | 169 | // 先fork抵达,插入 |
176 | pidCol.InsertOne(bson.M{ | 170 | process := Process{ |
177 | "ppid": cooked.ppid, | 171 | Ppid: cooked.ppid, |
178 | "pid": cooked.pid, | 172 | Pid: cooked.pid, |
179 | "children": []bson.M{}, | 173 | Execve: make([]Exec, 0), |
180 | "execve": []bson.M{ | 174 | Children: make([]int, 0), |
181 | { | 175 | } |
182 | "timestamp": cooked.timestamp, | 176 | process.Execve = append(process.Execve, Exec{ |
183 | "execArgs": cooked.argv, | 177 | Timestamp: cooked.timestamp, |
184 | }, | 178 | ExecArgs: cooked.argv, |
185 | }, | ||
186 | }) | 179 | }) |
180 | |||
181 | err := pidCol.InsertOne(process) | ||
182 | if err != nil { | ||
183 | fmt.Fprintf(os.Stderr, "Err inserting: %v\n", err) | ||
184 | } | ||
187 | } | 185 | } |
188 | } | 186 | } |
189 | 187 | ||
190 | func fileOpen(cooked Event) { | 188 | func fileOpen(cooked Event) { |
191 | // 权限检查过了,不必再查 | 189 | // 权限检查过了,不必再查 |
192 | fdCol.InsertOne(bson.M{ | 190 | file := File{ |
193 | "timestamp": cooked.timestamp, | 191 | OpenTimestamp: cooked.timestamp, |
194 | "fileName": cooked.srcPath, | 192 | FileName: cooked.srcPath, |
195 | "pid": cooked.pid, | 193 | Pid: cooked.pid, |
196 | "fd": cooked.exit_code, | 194 | Fd: cooked.exit_code, |
197 | "flags": cooked.syscallParam, | 195 | Flags: cooked.syscallParam, |
198 | "written": []bson.M{}, | 196 | Written: make([]time.Time, 0), |
199 | }) | 197 | } |
200 | |||
201 | if cooked.syscallParam[1]&syscall.O_TRUNC != 0 { | 198 | if cooked.syscallParam[1]&syscall.O_TRUNC != 0 { |
202 | fdCol.UpdateOne(bson.M{"pid": cooked.pid, "fd": cooked.exit_code}, bson.M{ | 199 | // 文件以清空方式打开 |
203 | "$push": bson.M{ | 200 | file.Written = append(file.Written, cooked.timestamp) |
204 | "written": cooked.timestamp, | 201 | } |
205 | }, | 202 | |
206 | }) | 203 | err := fdCol.InsertOne(file) |
204 | if err != nil { | ||
205 | fmt.Fprintf(os.Stderr, "Err inserting: %v\n", err) | ||
207 | } | 206 | } |
208 | } | 207 | } |
209 | 208 | ||
@@ -213,33 +212,39 @@ func fileClose(cooked Event) { | |||
213 | return | 212 | return |
214 | } | 213 | } |
215 | res["close_timestamp"] = cooked.timestamp | 214 | res["close_timestamp"] = cooked.timestamp |
216 | if err := fileCol.InsertOne(res); err != nil { | 215 | |
216 | err = fileCol.InsertOne(res) | ||
217 | if err != nil { | ||
217 | fmt.Fprintf(os.Stderr, "Err inserting files: %v\n", err) | 218 | fmt.Fprintf(os.Stderr, "Err inserting files: %v\n", err) |
218 | } | 219 | } |
219 | } | 220 | } |
220 | 221 | ||
221 | func fileWrite(cooked Event) { | 222 | func fileWrite(cooked Event) { |
222 | res, err := fdCol.Finddoc(bson.M{ | 223 | var res []File |
223 | "pid": cooked.pid, | 224 | err := fdCol.Finddoc(bson.M{ |
224 | "fd": cooked.syscallParam[0], | 225 | "pid": cooked.pid, |
225 | "close_timestamp": bson.M{"$exists": false}, | 226 | "fd": cooked.syscallParam[0], |
226 | }) | 227 | }, &res) |
227 | if err != nil { | 228 | if err != nil { |
228 | fmt.Fprintf(os.Stderr, "Err closing fd %d of pid %d: %v\n", cooked.syscallParam[0], cooked.pid, err) | 229 | fmt.Fprintf(os.Stderr, "Err closing fd %d of pid %d: %v\n", cooked.syscallParam[0], cooked.pid, err) |
229 | } | 230 | } |
230 | if len(res) == 0 { | 231 | if len(res) == 0 { |
231 | return | 232 | return |
232 | } | 233 | } |
233 | fdCol.UpdateOne(bson.M{ | 234 | |
234 | "pid": cooked.pid, | 235 | err = fdCol.UpdateOne(bson.M{ |
235 | "fd": cooked.syscallParam[0], | 236 | "pid": cooked.pid, |
236 | "close_timestamp": bson.M{"$exists": false}, | 237 | "fd": cooked.syscallParam[0], |
237 | }, bson.M{"$push": bson.M{"written": cooked.timestamp}}) | 238 | }, bson.M{"$push": bson.M{"written": cooked.timestamp}}) |
239 | if err != nil { | ||
240 | fmt.Fprintf(os.Stderr, "Err updating: %v\n", err) | ||
241 | } | ||
238 | } | 242 | } |
239 | 243 | ||
240 | func pivotRoot(cooked Event) { | 244 | func pivotRoot(cooked Event) { |
245 | var docRes []Process | ||
241 | // docker的根目录信息,记录 | 246 | // docker的根目录信息,记录 |
242 | docRes, err := pidCol.Finddoc(bson.M{"pid": cooked.pid}) | 247 | err := pidCol.Finddoc(bson.M{"pid": cooked.pid}, &docRes) |
243 | if err != nil { | 248 | if err != nil { |
244 | fmt.Fprintf(os.Stderr, "Err finding: %v\n", err) | 249 | fmt.Fprintf(os.Stderr, "Err finding: %v\n", err) |
245 | return | 250 | return |
@@ -255,10 +260,9 @@ func pivotRoot(cooked Event) { | |||
255 | }) | 260 | }) |
256 | } else { | 261 | } else { |
257 | // 读取已有的工作目录 | 262 | // 读取已有的工作目录 |
258 | cwd := docRes[0]["cwd"] | ||
259 | pidCol.UpdateOne(bson.M{"pid": cooked.pid}, bson.M{ | 263 | pidCol.UpdateOne(bson.M{"pid": cooked.pid}, bson.M{ |
260 | "$set": bson.M{ | 264 | "$set": bson.M{ |
261 | "rootfs": cwd, | 265 | "rootfs": docRes[0].Cwd, |
262 | }, | 266 | }, |
263 | }) | 267 | }) |
264 | } | 268 | } |
diff --git a/listener/global.go b/listener/global.go new file mode 100644 index 0000000..11b18bf --- /dev/null +++ b/listener/global.go | |||
@@ -0,0 +1,84 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "sync" | ||
5 | "time" | ||
6 | ) | ||
7 | |||
8 | type eventType int | ||
9 | |||
10 | const ( | ||
11 | NEWPID eventType = iota | ||
12 | PIDEXIT | ||
13 | EXECVE | ||
14 | FILEOPEN | ||
15 | FILECLOSE | ||
16 | FILEWRITE | ||
17 | PIVOTROOT | ||
18 | TYPENUM | ||
19 | ) | ||
20 | |||
21 | func (et eventType) String() string { | ||
22 | names := []string{"NEWPID", "PIDEXIT", "EXECVE", "FILEOPEN", "FILECLOSE", "FILEWRITE", "PIVOTROOT", "TYPENUM"} | ||
23 | if et < NEWPID || et > TYPENUM { | ||
24 | return "Unknown" | ||
25 | } | ||
26 | return names[et] | ||
27 | } | ||
28 | |||
29 | type Event struct { | ||
30 | tag eventType | ||
31 | timestamp time.Time | ||
32 | pid, tgid int | ||
33 | ppid, parentTgid int | ||
34 | syscall int | ||
35 | syscallParam [4]uint64 | ||
36 | argc int | ||
37 | argv []string | ||
38 | comm string | ||
39 | cwd string | ||
40 | exit_code int | ||
41 | exit_signal int | ||
42 | srcPath string | ||
43 | destPath string | ||
44 | } | ||
45 | |||
46 | var wg sync.WaitGroup // 掌管协程 | ||
47 | var rawChan chan interface{} // 从接收到整理的管道 | ||
48 | var cookedChan chan Event // 整理好的信息的管道 | ||
49 | var syscallTable [500]string //记录一下系统调用 | ||
50 | var containerdPid int | ||
51 | |||
52 | // 插入到数据库的结构 | ||
53 | type Exec struct { | ||
54 | Timestamp time.Time `bson:"timestamp"` | ||
55 | ExecArgs []string `bson:"execArgs"` | ||
56 | } | ||
57 | |||
58 | type Process struct { | ||
59 | Star bool `bson:"star"` | ||
60 | StartTimestamp time.Time `bson:"start_timestamp"` | ||
61 | Ppid int `bson:"ppid"` | ||
62 | ParentTgid int `bson:"parentTgid"` | ||
63 | Pid int `bson:"pid"` | ||
64 | Tgid int `bson:"tgid"` | ||
65 | Args []string `bson:"args"` | ||
66 | Comm string `bson:"comm"` | ||
67 | RootFS string `bson:"rootfs"` | ||
68 | Cwd string `bson:"cwd"` | ||
69 | Children []int `bson:"children"` | ||
70 | Execve []Exec `bson:"execve"` | ||
71 | ExitCode int `bson:"exit_code"` | ||
72 | ExitSignal int `bson:"exit_signal"` | ||
73 | ExitTimestamp time.Time `bson:"exit_timestamp"` | ||
74 | } | ||
75 | |||
76 | type File struct { | ||
77 | OpenTimestamp time.Time `bson:"timestamp"` | ||
78 | FileName string `bson:"fileName"` | ||
79 | Pid int `bson:"pid"` | ||
80 | Fd int `bson:"fd"` | ||
81 | Flags [4]uint64 `bson:"flags"` | ||
82 | Written []time.Time `bson:"written"` | ||
83 | CloseTimestamp time.Time `bson:"close_timestamp"` | ||
84 | } | ||
diff --git a/src/go.mod b/listener/go.mod index ed40331..ed40331 100644 --- a/src/go.mod +++ b/listener/go.mod | |||
diff --git a/src/go.sum b/listener/go.sum index 9164cd3..9164cd3 100644 --- a/src/go.sum +++ b/listener/go.sum | |||
diff --git a/src/go.work b/listener/go.work index 5b6c957..5b6c957 100644 --- a/src/go.work +++ b/listener/go.work | |||
diff --git a/src/go.work.sum b/listener/go.work.sum index 8201e39..8201e39 100644 --- a/src/go.work.sum +++ b/listener/go.work.sum | |||
diff --git a/src/godo.go b/listener/godo.go index a30aa88..efe9585 100644 --- a/src/godo.go +++ b/listener/godo.go | |||
@@ -128,7 +128,7 @@ func procWatch() error { | |||
128 | tag: PIDEXIT, | 128 | tag: PIDEXIT, |
129 | timestamp: time.Now(), | 129 | timestamp: time.Now(), |
130 | pid: int(data.ProcessPid), | 130 | pid: int(data.ProcessPid), |
131 | exit_code: uint64(data.ExitCode), | 131 | exit_code: int(data.ExitCode), |
132 | exit_signal: int(data.ExitSignal), | 132 | exit_signal: int(data.ExitSignal), |
133 | } | 133 | } |
134 | cookedChan <- cooked | 134 | cookedChan <- cooked |
@@ -139,8 +139,8 @@ func procWatch() error { | |||
139 | } | 139 | } |
140 | 140 | ||
141 | func checkProc(pCooked *Event) { | 141 | func checkProc(pCooked *Event) { |
142 | fileName := fmt.Sprintf("/proc/%d/task/%d/cmdline", pCooked.tgid, pCooked.pid) | 142 | fileName := fmt.Sprintf("/proc/%d/task/%d/", pCooked.tgid, pCooked.pid) |
143 | fd, err := os.Open(fileName) | 143 | fd, err := os.Open(fileName + "cmdline") |
144 | if err != nil { | 144 | if err != nil { |
145 | fmt.Fprintf(os.Stderr, "Err: %v\n", err) | 145 | fmt.Fprintf(os.Stderr, "Err: %v\n", err) |
146 | return | 146 | return |
@@ -155,8 +155,20 @@ func checkProc(pCooked *Event) { | |||
155 | pCooked.argc = len(pCooked.argv) | 155 | pCooked.argc = len(pCooked.argv) |
156 | fd.Close() | 156 | fd.Close() |
157 | 157 | ||
158 | fileName = fmt.Sprintf("/proc/%d/task/%d/cwd", pCooked.tgid, pCooked.pid) | 158 | fd, err = os.Open(fileName + "comm") |
159 | pCooked.cwd, err = os.Readlink(fileName) | 159 | if err != nil { |
160 | fmt.Fprintf(os.Stderr, "Err: %v\n", err) | ||
161 | return | ||
162 | } | ||
163 | scanner = bufio.NewScanner(fd) | ||
164 | scanner.Split(bufio.ScanLines) | ||
165 | for scanner.Scan() { | ||
166 | line := scanner.Text() | ||
167 | pCooked.comm = line | ||
168 | } | ||
169 | fd.Close() | ||
170 | |||
171 | pCooked.cwd, err = os.Readlink(fileName + "cwd") | ||
160 | if err != nil { | 172 | if err != nil { |
161 | fmt.Fprintf(os.Stderr, "Err: %v\n", err) | 173 | fmt.Fprintf(os.Stderr, "Err: %v\n", err) |
162 | pCooked.cwd = "" | 174 | pCooked.cwd = "" |
diff --git a/src/mongo.go b/listener/mongo.go index 1d9f74f..a51350e 100644 --- a/src/mongo.go +++ b/listener/mongo.go | |||
@@ -2,6 +2,8 @@ package main | |||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "context" | 4 | "context" |
5 | "fmt" | ||
6 | "reflect" | ||
5 | "time" | 7 | "time" |
6 | 8 | ||
7 | "go.mongodb.org/mongo-driver/bson" | 9 | "go.mongodb.org/mongo-driver/bson" |
@@ -62,16 +64,38 @@ func (mc *mongoClient) UpdateMany(filter, update interface{}) error { | |||
62 | return err | 64 | return err |
63 | } | 65 | } |
64 | 66 | ||
65 | func (mc *mongoClient) Finddoc(filter bson.M) ([]bson.M, error) { | 67 | func (mc *mongoClient) ReplaceOne(filter, new interface{}) error { |
66 | cur, err := mc.col.Find(context.Background(), filter) | 68 | _, err := mc.col.ReplaceOne(context.Background(), filter, new) |
69 | return err | ||
70 | } | ||
71 | |||
72 | func (mc *mongoClient) Finddoc(filter bson.M, results interface{}) error { | ||
73 | sliceValue := reflect.ValueOf(results) | ||
74 | |||
75 | if sliceValue.Kind() != reflect.Ptr || sliceValue.Elem().Kind() != reflect.Slice { | ||
76 | return fmt.Errorf("Error: result argument must be pointer to slice") | ||
77 | } | ||
78 | cur, err := mc.col.Find(context.TODO(), filter) | ||
67 | if err != nil { | 79 | if err != nil { |
68 | return nil, err | 80 | return err |
69 | } | 81 | } |
82 | defer cur.Close(context.TODO()) | ||
83 | |||
84 | elemType := sliceValue.Elem().Type().Elem() | ||
70 | 85 | ||
71 | var results []bson.M | 86 | sliceValue = sliceValue.Elem() |
72 | err = cur.All(context.Background(), &results) | ||
73 | 87 | ||
74 | return results, err | 88 | for cur.Next(context.TODO()) { |
89 | elem := reflect.New(elemType).Interface() | ||
90 | err := cur.Decode(elem) | ||
91 | if err != nil { | ||
92 | return err | ||
93 | } | ||
94 | sliceValue = reflect.Append(sliceValue, reflect.ValueOf(elem).Elem()) | ||
95 | } | ||
96 | |||
97 | reflect.ValueOf(results).Elem().Set(sliceValue) | ||
98 | return nil | ||
75 | } | 99 | } |
76 | 100 | ||
77 | func (mc *mongoClient) FindOneAndDelete(filter bson.M) (bson.M, error) { | 101 | func (mc *mongoClient) FindOneAndDelete(filter bson.M) (bson.M, error) { |
diff --git a/src/netlink b/listener/netlink | |||
Subproject e53c2724725c5991cdd9ea088c26832c5c9fcf0 | Subproject e53c2724725c5991cdd9ea088c26832c5c9fcf0 | ||
diff --git a/src/organize.go b/listener/organize.go index 293371b..0c05eb4 100644 --- a/src/organize.go +++ b/listener/organize.go | |||
@@ -23,14 +23,12 @@ var event Event | |||
23 | var pEvent *Event | 23 | var pEvent *Event |
24 | var eventId, argc int | 24 | var eventId, argc int |
25 | 25 | ||
26 | // var errs [6]error | ||
27 | |||
28 | // 要用的正则匹配列表 | 26 | // 要用的正则匹配列表 |
29 | var ( | 27 | var ( |
30 | syscallRegex = regexp.MustCompile(`audit\((\d+\.\d+):(\d+)\).*?syscall=(\d+)(?:.*?exit=([-+]?\d+))?.*?ppid=(\d+) pid=(\d+).*?subj=(.*?):(.*?):(.*?):(.*?) .*?$`) | 28 | syscallRegex = regexp.MustCompile(`audit\((\d+\.\d+):(\d+)\).*?syscall=(\d+)(?:.*?exit=([-+]?\d+))?.*?ppid=(\d+) pid=(\d+).*?subj=(.*?):(.*?):(.*?):(.*?) .*?$`) |
31 | execveRegex = regexp.MustCompile(`audit\(\d+\.\d+:(\d+)\): argc=(\d+)`) | 29 | execveRegex = regexp.MustCompile(`audit\(\d+\.\d+:(\d+)\): argc=(\d+)`) |
32 | argsRegex = regexp.MustCompile(`a\d+=("(.*?)"|([0-9a-fA-F]+))`) | 30 | argsRegex = regexp.MustCompile(`a\d+=("(.*?)"|([0-9a-fA-F]+))`) |
33 | pathRegex = regexp.MustCompile(`audit\(\d+\.\d+:(\d+)\): item=(\d+) name="(.*?)"`) | 31 | pathRegex = regexp.MustCompile(`audit\(\d+\.\d+:(\d+)\): item=(\d+) name="(.*?)" .*objtype=([A-Z]+) `) |
34 | cwdRegex = regexp.MustCompile(`audit\(\d+\.\d+:(\d+)\): cwd="(.*?)"`) | 32 | cwdRegex = regexp.MustCompile(`audit\(\d+\.\d+:(\d+)\): cwd="(.*?)"`) |
35 | proctitleRegex = regexp.MustCompile(`audit\(\d+\.\d+:(\d+)\): proctitle=("(.*?)"|([0-9a-fA-F]+))$`) | 33 | proctitleRegex = regexp.MustCompile(`audit\(\d+\.\d+:(\d+)\): proctitle=("(.*?)"|([0-9a-fA-F]+))$`) |
36 | eoeRegex = regexp.MustCompile(`audit\(\d+\.\d+:(\d+)\)`) | 34 | eoeRegex = regexp.MustCompile(`audit\(\d+\.\d+:(\d+)\)`) |
@@ -112,12 +110,12 @@ func syscallRaw(rawEvent libaudit.RawAuditMessage) { | |||
112 | tag: EXECVE, | 110 | tag: EXECVE, |
113 | timestamp: event.timestamp, | 111 | timestamp: event.timestamp, |
114 | syscall: event.syscall, | 112 | syscall: event.syscall, |
115 | exit_code: a[0], | 113 | // exit_code: a[0], // 为啥这么写? |
116 | ppid: event.ppid, | 114 | ppid: event.ppid, |
117 | pid: event.pid, | 115 | pid: event.pid, |
118 | argc: 0, | 116 | argc: 0, |
119 | argv: make([]string, 0), | 117 | argv: make([]string, 0), |
120 | cwd: "", | 118 | cwd: "", |
121 | }) | 119 | }) |
122 | case "open": | 120 | case "open": |
123 | // 检查打开的权限 | 121 | // 检查打开的权限 |
@@ -129,7 +127,7 @@ func syscallRaw(rawEvent libaudit.RawAuditMessage) { | |||
129 | tag: FILEOPEN, | 127 | tag: FILEOPEN, |
130 | timestamp: event.timestamp, | 128 | timestamp: event.timestamp, |
131 | syscall: event.syscall, | 129 | syscall: event.syscall, |
132 | exit_code: uint64(exit), | 130 | exit_code: exit, |
133 | ppid: event.ppid, | 131 | ppid: event.ppid, |
134 | pid: event.pid, | 132 | pid: event.pid, |
135 | argc: 0, | 133 | argc: 0, |
@@ -143,7 +141,7 @@ func syscallRaw(rawEvent libaudit.RawAuditMessage) { | |||
143 | tag: FILEWRITE, | 141 | tag: FILEWRITE, |
144 | timestamp: event.timestamp, | 142 | timestamp: event.timestamp, |
145 | syscall: event.syscall, | 143 | syscall: event.syscall, |
146 | exit_code: uint64(exit), | 144 | exit_code: exit, |
147 | ppid: event.ppid, | 145 | ppid: event.ppid, |
148 | pid: event.pid, | 146 | pid: event.pid, |
149 | argc: 0, | 147 | argc: 0, |
@@ -157,7 +155,7 @@ func syscallRaw(rawEvent libaudit.RawAuditMessage) { | |||
157 | tag: FILECLOSE, | 155 | tag: FILECLOSE, |
158 | timestamp: event.timestamp, | 156 | timestamp: event.timestamp, |
159 | syscall: event.syscall, | 157 | syscall: event.syscall, |
160 | exit_code: uint64(exit), | 158 | exit_code: exit, |
161 | ppid: event.ppid, | 159 | ppid: event.ppid, |
162 | pid: event.pid, | 160 | pid: event.pid, |
163 | argc: 0, | 161 | argc: 0, |
@@ -271,6 +269,7 @@ func path(rawEvent libaudit.RawAuditMessage) { | |||
271 | eventId, _ = strconv.Atoi(string(match[1])) | 269 | eventId, _ = strconv.Atoi(string(match[1])) |
272 | // item, _ := strconv.Atoi(string(match[2])) | 270 | // item, _ := strconv.Atoi(string(match[2])) |
273 | name := string(match[3]) | 271 | name := string(match[3]) |
272 | objtype := string(match[4]) | ||
274 | 273 | ||
275 | tmp, ok = eventTable.Load(eventId) | 274 | tmp, ok = eventTable.Load(eventId) |
276 | if !ok { | 275 | if !ok { |
@@ -278,14 +277,15 @@ func path(rawEvent libaudit.RawAuditMessage) { | |||
278 | } | 277 | } |
279 | pEvent = tmp.(*Event) | 278 | pEvent = tmp.(*Event) |
280 | 279 | ||
281 | // 先看看是不是文件操作 | 280 | // 先看看是不是文件操作,再看是不是所在目录 |
282 | if pEvent.tag != FILEOPEN { | 281 | if pEvent.tag != FILEOPEN || objtype == "PARENT" { |
283 | return | 282 | return |
284 | } | 283 | } |
285 | 284 | ||
286 | if name[0] == '/' { | 285 | if pEvent.cwd == "/" || name[0] == '/' { |
287 | pEvent.srcPath = name | 286 | pEvent.srcPath = name |
288 | } else { | 287 | } else { |
289 | pEvent.srcPath += "/" + name | 288 | pEvent.srcPath = pEvent.cwd + "/" + name |
290 | } | 289 | } |
290 | // ATTENTION: 这里需要做路径简化,留给过滤清洗流程吧 | ||
291 | } | 291 | } |
diff --git a/src/receive.go b/listener/receive.go index c0dea00..c0dea00 100644 --- a/src/receive.go +++ b/listener/receive.go | |||