-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
mongodb_parser.go
376 lines (308 loc) · 9.11 KB
/
mongodb_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package mongodb
import (
"encoding/json"
"errors"
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"gopkg.in/mgo.v2/bson"
)
func mongodbMessageParser(s *stream) (bool, bool) {
d := newDecoder(s.data)
length, err := d.readInt32()
if err != nil {
// Not even enough data to parse length of message
return true, false
}
if length > len(s.data) {
// Not yet reached the end of message
return true, false
}
// Tell decoder to only consider current message
d.truncate(length)
// fill up the header common to all messages
// see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#standard-message-header
s.message.messageLength = length
s.message.requestID, _ = d.readInt32()
s.message.responseTo, _ = d.readInt32()
code, _ := d.readInt32()
opCode := opCode(code)
if !validOpcode(opCode) {
logp.Err("Unknown operation code: %v", opCode)
return false, false
}
s.message.opCode = opCode
s.message.isResponse = false // default is that the message is a request. If not opReplyParse will set this to false
s.message.expectsResponse = false
debugf("opCode = %v", s.message.opCode)
// then split depending on operation type
s.message.event = common.MapStr{}
switch s.message.opCode {
case opReply:
s.message.isResponse = true
return opReplyParse(d, s.message)
case opMsg:
s.message.method = "msg"
return opMsgParse(d, s.message)
case opUpdate:
s.message.method = "update"
return opUpdateParse(d, s.message)
case opInsert:
s.message.method = "insert"
return opInsertParse(d, s.message)
case opQuery:
s.message.expectsResponse = true
return opQueryParse(d, s.message)
case opGetMore:
s.message.method = "getMore"
s.message.expectsResponse = true
return opGetMoreParse(d, s.message)
case opDelete:
s.message.method = "delete"
return opDeleteParse(d, s.message)
case opKillCursor:
s.message.method = "killCursors"
return opKillCursorsParse(d, s.message)
}
return false, false
}
// see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-reply
func opReplyParse(d *decoder, m *mongodbMessage) (bool, bool) {
_, err := d.readInt32() // ignore flags for now
m.event["cursorId"], err = d.readInt64()
m.event["startingFrom"], err = d.readInt32()
numberReturned, err := d.readInt32()
m.event["numberReturned"] = numberReturned
debugf("Prepare to read %d document from reply", m.event["numberReturned"])
documents := make([]interface{}, numberReturned)
for i := 0; i < numberReturned; i++ {
var document bson.M
document, err = d.readDocument()
// Check if the result is actually an error
if i == 0 {
if mongoError, present := document["$err"]; present {
m.error, err = doc2str(mongoError)
}
if writeErrors, present := document["writeErrors"]; present {
m.error, err = doc2str(writeErrors)
}
}
documents[i] = document
}
m.documents = documents
if err != nil {
logp.Err("An error occurred while parsing OP_REPLY message: %s", err)
return false, false
}
return true, true
}
func opMsgParse(d *decoder, m *mongodbMessage) (bool, bool) {
var err error
m.event["message"], err = d.readCStr()
if err != nil {
logp.Err("An error occurred while parsing OP_MSG message: %s", err)
return false, false
}
return true, true
}
func opUpdateParse(d *decoder, m *mongodbMessage) (bool, bool) {
_, err := d.readInt32() // always ZERO, a slot reserved in the protocol for future use
m.event["fullCollectionName"], err = d.readCStr()
_, err = d.readInt32() // ignore flags for now
m.event["selector"], err = d.readDocumentStr()
m.event["update"], err = d.readDocumentStr()
if err != nil {
logp.Err("An error occurred while parsing OP_UPDATE message: %s", err)
return false, false
}
return true, true
}
func opInsertParse(d *decoder, m *mongodbMessage) (bool, bool) {
_, err := d.readInt32() // ignore flags for now
m.event["fullCollectionName"], err = d.readCStr()
// TODO parse bson documents
// Not too bad if it is not done, as all recent mongodb clients send insert as a command over a query instead of this
// Find an old client to generate a pcap with legacy protocol ?
if err != nil {
logp.Err("An error occurred while parsing OP_INSERT message: %s", err)
return false, false
}
return true, true
}
func extractDocuments(query map[string]interface{}) []interface{} {
docsVi, present := query["documents"]
if !present {
return []interface{}{}
}
docs, ok := docsVi.([]interface{})
if !ok {
return []interface{}{}
}
return docs
}
// Try to guess whether this key:value pair found in
// the query represents a command.
func isDatabaseCommand(key string, val interface{}) bool {
nameExists := false
for _, cmd := range databaseCommands {
if strings.EqualFold(cmd, key) {
nameExists = true
break
}
}
if !nameExists {
return false
}
// value should be either a string or the value 1
_, ok := val.(string)
num, _ := val.(float64)
if ok || num == 1 {
return true
}
return false
}
func opQueryParse(d *decoder, m *mongodbMessage) (bool, bool) {
_, err := d.readInt32() // ignore flags for now
fullCollectionName, err := d.readCStr()
m.event["fullCollectionName"] = fullCollectionName
m.event["numberToSkip"], err = d.readInt32()
m.event["numberToReturn"], err = d.readInt32()
query, err := d.readDocument()
if d.i < len(d.in) {
m.event["returnFieldsSelector"], err = d.readDocumentStr()
}
// Actual method is either a 'find' or a command passing through a query
if strings.HasSuffix(fullCollectionName, ".$cmd") {
m.method = "otherCommand"
m.resource = fullCollectionName
for key, val := range query {
debugf("key=%v val=%s", key, val)
if isDatabaseCommand(key, val) {
debugf("is db command")
col, ok := val.(string)
if ok {
// replace $cmd with the actual collection name
m.resource = fullCollectionName[:len(fullCollectionName)-4] + col
}
delete(query, key)
m.method = key
}
}
} else {
m.method = "find"
m.resource = fullCollectionName
}
m.params = query
if err != nil {
logp.Err("An error occurred while parsing OP_QUERY message: %s", err)
return false, false
}
return true, true
}
func opGetMoreParse(d *decoder, m *mongodbMessage) (bool, bool) {
_, err := d.readInt32() // always ZERO, a slot reserved in the protocol for future use
m.event["fullCollectionName"], err = d.readCStr()
m.event["numberToReturn"], err = d.readInt32()
m.event["cursorId"], err = d.readInt64()
if err != nil {
logp.Err("An error occurred while parsing OP_GET_MORE message: %s", err)
return false, false
}
return true, true
}
func opDeleteParse(d *decoder, m *mongodbMessage) (bool, bool) {
_, err := d.readInt32() // always ZERO, a slot reserved in the protocol for future use
m.event["fullCollectionName"], err = d.readCStr()
_, err = d.readInt32() // ignore flags for now
m.event["selector"], err = d.readDocumentStr()
if err != nil {
logp.Err("An error occurred while parsing OP_DELETE message: %s", err)
return false, false
}
return true, true
}
func opKillCursorsParse(d *decoder, m *mongodbMessage) (bool, bool) {
// TODO ? Or not, content is not very interesting.
return true, true
}
// NOTE: The following functions are inspired by the source of the go-mgo/mgo project
// https://github.com/go-mgo/mgo/blob/v2/bson/decode.go
type decoder struct {
in []byte
i int
}
func newDecoder(in []byte) *decoder {
return &decoder{in, 0}
}
func (d *decoder) truncate(length int) {
d.in = d.in[:length]
}
func (d *decoder) readCStr() (string, error) {
start := d.i
end := start
l := len(d.in)
for ; end != l; end++ {
if d.in[end] == '\x00' {
break
}
}
d.i = end + 1
if d.i > l {
return "", errors.New("cstring not finished")
}
return string(d.in[start:end]), nil
}
func (d *decoder) readInt32() (int, error) {
b, err := d.readBytes(4)
if err != nil {
return 0, err
}
return int((uint32(b[0]) << 0) |
(uint32(b[1]) << 8) |
(uint32(b[2]) << 16) |
(uint32(b[3]) << 24)), nil
}
func (d *decoder) readInt64() (int, error) {
b, err := d.readBytes(8)
if err != nil {
return 0, err
}
return int((uint64(b[0]) << 0) |
(uint64(b[1]) << 8) |
(uint64(b[2]) << 16) |
(uint64(b[3]) << 24) |
(uint64(b[4]) << 32) |
(uint64(b[5]) << 40) |
(uint64(b[6]) << 48) |
(uint64(b[7]) << 56)), nil
}
func (d *decoder) readDocument() (bson.M, error) {
start := d.i
documentLength, err := d.readInt32()
d.i = start + documentLength
documentMap := bson.M{}
debugf("Parse %d bytes document from remaining %d bytes", documentLength, len(d.in)-start)
err = bson.Unmarshal(d.in[start:d.i], documentMap)
if err != nil {
debugf("Unmarshall error %v", err)
return nil, err
}
return documentMap, err
}
func doc2str(documentMap interface{}) (string, error) {
document, err := json.Marshal(documentMap)
return string(document), err
}
func (d *decoder) readDocumentStr() (string, error) {
documentMap, err := d.readDocument()
document, err := doc2str(documentMap)
return document, err
}
func (d *decoder) readBytes(length int32) ([]byte, error) {
start := d.i
d.i += int(length)
if d.i > len(d.in) {
return *new([]byte), errors.New("No byte to read")
}
return d.in[start : start+int(length)], nil
}