forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
567 lines (477 loc) · 16.2 KB
/
mysql.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
package main
import (
"bytes"
"encoding/csv"
"labix.org/v2/mgo/bson"
"strings"
"time"
)
// Packet types
const (
MYSQL_CMD_QUERY = 3
)
type MysqlMessage struct {
start int
end int
Ts time.Time
IsRequest bool
PacketLength uint32
Seq uint8
Typ uint8
NumberOfRows int
NumberOfFields int
Size uint64
Fields []string
Rows [][]string
Tables string
IsOK bool
AffectedRows int
InsertId int
IsError bool
ErrorCode int
ErrorInfo string
Query string
Stream_id uint32
Direction uint8
IsTruncated bool
Tuple *IpPortTuple
CmdlineTuple *CmdlineTuple
Raw []byte
}
type MysqlTransaction struct {
Type string
tuple TcpTuple
Src Endpoint
Dst Endpoint
ResponseTime int32
Ts int64
JsTs time.Time
ts time.Time
Mysql bson.M
Request_raw string
Response_raw string
timer *time.Timer
}
type MysqlStream struct {
tcpStream *TcpStream
data []byte
parseOffset int
parseState int
isClient bool
message *MysqlMessage
}
const (
TransactionsHashSize = 2 ^ 16
TransactionTimeout = 10 * 1e9
)
const (
MysqlStateStart = iota
MysqlStateEatMessage
MysqlStateEatFields
MysqlStateEatRows
)
var mysqlTransactionsMap = make(map[TcpTuple]*MysqlTransaction, TransactionsHashSize)
func (stream *MysqlStream) PrepareForNewMessage() {
stream.data = stream.data[stream.message.end:]
stream.parseState = MysqlStateStart
stream.parseOffset = 0
stream.isClient = false
stream.message = nil
}
func mysqlMessageParser(s *MysqlStream) (bool, bool) {
m := s.message
for s.parseOffset < len(s.data) {
switch s.parseState {
case MysqlStateStart:
m.start = s.parseOffset
if len(s.data[s.parseOffset:]) < 5 {
WARN("MySQL Message too short. Ignore it.")
return false, false
}
hdr := s.data[s.parseOffset : s.parseOffset+5]
m.PacketLength = uint32(hdr[0]) | uint32(hdr[1])<<8 | uint32(hdr[2])<<16
m.Seq = uint8(hdr[3])
m.Typ = uint8(hdr[4])
DEBUG("mysqldetailed", "MySQL Header: Packet length %d, Seq %d, Type=%d", m.PacketLength, m.Seq, m.Typ)
if m.Seq == 0 {
// starts Command Phase
if m.Typ == MYSQL_CMD_QUERY {
// parse request
m.IsRequest = true
m.start = s.parseOffset
s.parseState = MysqlStateEatMessage
} else {
// ignore command
s.parseState = MysqlStateEatMessage
}
if !s.isClient {
s.isClient = true
}
} else if !s.isClient {
// parse response
m.IsRequest = false
if uint8(hdr[4]) == 0x00 {
DEBUG("mysqldetailed", "Received OK response")
m.start = s.parseOffset
s.parseState = MysqlStateEatMessage
m.IsOK = true
} else if uint8(hdr[4]) == 0xff {
DEBUG("mysqldetailed", "Received ERR response")
m.start = s.parseOffset
s.parseState = MysqlStateEatMessage
m.IsError = true
} else if m.PacketLength == 1 {
DEBUG("mysqldetailed", "Query response. Number of fields %d", uint8(hdr[4]))
m.NumberOfFields = int(hdr[4])
m.start = s.parseOffset
s.parseOffset += 5
s.parseState = MysqlStateEatFields
} else {
// something else. ignore
s.parseState = MysqlStateEatMessage
}
} else {
// something else, not expected
WARN("Unexpected MySQL message of type %d received.", m.Typ)
return false, false
}
break
case MysqlStateEatMessage:
if len(s.data[s.parseOffset:]) >= int(m.PacketLength)+4 {
s.parseOffset += 4 //header
s.parseOffset += int(m.PacketLength)
m.end = s.parseOffset
if m.IsRequest {
m.Query = string(s.data[m.start+5 : m.end])
return true, true
} else if m.IsOK {
m.AffectedRows = int(s.data[m.start+5])
m.InsertId = int(s.data[m.start+6])
return true, true
} else if m.IsError {
m.ErrorCode = int(uint16(s.data[m.start+6])<<8 | uint16(s.data[m.start+7]))
m.ErrorInfo = string(s.data[m.start+9:m.start+14]) + ": " + string(s.data[m.start+15:])
return true, true
}
} else {
// wait for more
return true, false
}
break
case MysqlStateEatFields:
if len(s.data[s.parseOffset:]) < 4 {
// wait for more
return true, false
}
hdr := s.data[s.parseOffset : s.parseOffset+4]
m.PacketLength = uint32(hdr[0]) | uint32(hdr[1])<<8 | uint32(hdr[2])<<16
m.Seq = uint8(hdr[3])
DEBUG("mysqldetailed", "Fields: packet length %d, packet number %d", m.PacketLength, m.Seq)
if len(s.data[s.parseOffset:]) >= int(m.PacketLength)+4 {
s.parseOffset += 4 // header
if uint8(s.data[s.parseOffset]) == 0xfe {
DEBUG("mysqldetailed", "Received EOF packet")
// EOF marker
s.parseOffset += int(m.PacketLength)
s.parseState = MysqlStateEatRows
} else {
_ /* catalog */, off := read_lstring(s.data, s.parseOffset)
db /*schema */, off := read_lstring(s.data, off)
table /* table */, off := read_lstring(s.data, off)
db_table := string(db) + "." + string(table)
if len(m.Tables) == 0 {
m.Tables = db_table
} else if !strings.Contains(m.Tables, db_table) {
m.Tables = m.Tables + ", " + db_table
}
DEBUG("mysqldetailed", "db=%s, table=%s", db, table)
s.parseOffset += int(m.PacketLength)
// go to next field
}
} else {
// wait for more
return true, false
}
break
case MysqlStateEatRows:
if len(s.data[s.parseOffset:]) < 4 {
// wait for more
return true, false
}
hdr := s.data[s.parseOffset : s.parseOffset+4]
m.PacketLength = uint32(hdr[0]) | uint32(hdr[1])<<8 | uint32(hdr[2])<<16
m.Seq = uint8(hdr[3])
DEBUG("mysqldetailed", "Rows: packet length %d, packet number %d", m.PacketLength, m.Seq)
if len(s.data[s.parseOffset:]) >= int(m.PacketLength)+4 {
s.parseOffset += 4 //header
if uint8(s.data[s.parseOffset]) == 0xfe {
DEBUG("mysqldetailed", "Received EOF packet")
// EOF marker
s.parseOffset += int(m.PacketLength)
if m.end == 0 {
m.end = s.parseOffset
} else {
m.IsTruncated = true
}
m.Size = uint64(s.parseOffset - m.start)
if !m.IsError {
// in case the response was sent successfully
m.IsOK = true
}
return true, true
} else {
s.parseOffset += int(m.PacketLength)
if m.end == 0 && s.parseOffset > MAX_PAYLOAD_SIZE {
// only send up to here, but read until the end
m.end = s.parseOffset
}
m.NumberOfRows += 1
// go to next row
}
} else {
// wait for more
return true, false
}
break
}
}
return true, false
}
func ParseMysql(pkt *Packet, tcp *TcpStream, dir uint8) {
defer RECOVER("ParseMysql exception")
if tcp.mysqlData[dir] == nil {
tcp.mysqlData[dir] = &MysqlStream{
tcpStream: tcp,
data: pkt.payload,
message: &MysqlMessage{Ts: pkt.ts},
}
} else {
// concatenate bytes
tcp.mysqlData[dir].data = append(tcp.mysqlData[dir].data, pkt.payload...)
if len(tcp.mysqlData[dir].data) > TCP_MAX_DATA_IN_STREAM {
DEBUG("mysql", "Stream data too large, dropping TCP stream")
tcp.mysqlData[dir] = nil
return
}
}
stream := tcp.mysqlData[dir]
for len(stream.data) > 0 {
if stream.message == nil {
stream.message = &MysqlMessage{Ts: pkt.ts}
}
ok, complete := mysqlMessageParser(tcp.mysqlData[dir])
if !ok {
// drop this tcp stream. Will retry parsing with the next
// segment in it
tcp.mysqlData[dir] = nil
DEBUG("mysql", "Ignore MySQL message. Drop tcp stream. Try parsing with the next segment")
return
}
if complete {
// all ok, ship it
msg := stream.data[stream.message.start:stream.message.end]
handleMysql(stream.message, tcp, dir, msg)
// and reset message
stream.PrepareForNewMessage()
} else {
// wait for more data
break
}
}
}
var handleMysql = func(m *MysqlMessage, tcp *TcpStream,
dir uint8, raw_msg []byte) {
m.Stream_id = tcp.id
m.Tuple = tcp.tuple
m.Direction = dir
m.CmdlineTuple = procWatcher.FindProcessesTuple(tcp.tuple)
m.Raw = raw_msg
if m.IsRequest {
receivedMysqlRequest(m)
} else {
receivedMysqlResponse(m)
}
}
func receivedMysqlRequest(msg *MysqlMessage) {
// Add it to the HT
tuple := TcpTuple{
Src_ip: msg.Tuple.Src_ip,
Dst_ip: msg.Tuple.Dst_ip,
Src_port: msg.Tuple.Src_port,
Dst_port: msg.Tuple.Dst_port,
stream_id: msg.Stream_id,
}
trans := mysqlTransactionsMap[tuple]
if trans != nil {
if len(trans.Mysql) != 0 {
WARN("Two requests without a Response. Dropping old request: %s", trans.Mysql)
}
} else {
trans = &MysqlTransaction{Type: "mysql", tuple: tuple}
mysqlTransactionsMap[tuple] = trans
}
trans.ts = msg.Ts
trans.Ts = int64(trans.ts.UnixNano() / 1000) // transactions have microseconds resolution
trans.JsTs = msg.Ts
trans.Src = Endpoint{
Ip: Ipv4_Ntoa(tuple.Src_ip),
Port: tuple.Src_port,
Proc: string(msg.CmdlineTuple.Src),
}
trans.Dst = Endpoint{
Ip: Ipv4_Ntoa(tuple.Dst_ip),
Port: tuple.Dst_port,
Proc: string(msg.CmdlineTuple.Dst),
}
index := strings.Index(msg.Query, " ")
var method string
if index > 0 {
method = strings.ToUpper(msg.Query[:index])
} else {
method = strings.ToUpper(msg.Query)
}
trans.Mysql = bson.M{
"query": msg.Query,
"query.raw": msg.Query,
"method": method,
}
// save Raw message
trans.Request_raw = msg.Query
if trans.timer != nil {
trans.timer.Stop()
}
trans.timer = time.AfterFunc(TransactionTimeout, func() { trans.Expire() })
}
func receivedMysqlResponse(msg *MysqlMessage) {
tuple := TcpTuple{
Src_ip: msg.Tuple.Src_ip,
Dst_ip: msg.Tuple.Dst_ip,
Src_port: msg.Tuple.Src_port,
Dst_port: msg.Tuple.Dst_port,
stream_id: msg.Stream_id,
}
trans := mysqlTransactionsMap[tuple]
if trans == nil {
WARN("Response from unknown transaction. Ignoring.")
return
}
// check if the request was received
if len(trans.Mysql) == 0 {
WARN("Response from unknown transaction. Ignoring.")
return
}
// save json details
trans.Mysql = bson_concat(trans.Mysql, bson.M{
"isok": msg.IsOK,
"affected_rows": msg.AffectedRows,
"insert_id": msg.InsertId,
"tables": msg.Tables,
"num_rows": msg.NumberOfRows,
"size": msg.Size,
"num_fields": msg.NumberOfFields,
"iserror": msg.IsError,
"error_code": msg.ErrorCode,
"error_message": msg.ErrorInfo,
})
trans.ResponseTime = int32(msg.Ts.Sub(trans.ts).Nanoseconds() / 1e6) // resp_time in milliseconds
// save Raw message
if len(msg.Raw) > 0 {
fields, rows := parseMysqlResponse(msg.Raw)
trans.Response_raw = dumpInCSVFormat(fields, rows)
}
err := Publisher.PublishMysqlTransaction(trans)
if err != nil {
WARN("Publish failure: %s", err)
}
DEBUG("mysql", "Mysql transaction completed: %s", trans.Mysql)
DEBUG("mysql", "%s", trans.Response_raw)
// remove from map
delete(mysqlTransactionsMap, trans.tuple)
if trans.timer != nil {
trans.timer.Stop()
}
}
func (trans *MysqlTransaction) Expire() {
// TODO: Here we need to PUBLISH an incomplete/timeout transaction
// remove from map
delete(mysqlTransactionsMap, trans.tuple)
}
func dumpInCSVFormat(fields []string, rows [][]string) string {
var buf bytes.Buffer
writer := csv.NewWriter(&buf)
for i, field := range fields {
fields[i] = strings.Replace(field, "\n", "\\n", -1)
}
if len(fields) > 0 {
writer.Write(fields)
}
for _, row := range rows {
for i, field := range row {
field = strings.Replace(field, "\n", "\\n", -1)
field = strings.Replace(field, "\r", "\\r", -1)
row[i] = field
}
writer.Write(row)
}
writer.Flush()
csv := buf.String()
return csv
}
func parseMysqlResponse(data []byte) ([]string, [][]string) {
length := read_length(data, 0)
if length < 1 {
WARN("Warning: Skipping empty Response")
return []string{}, [][]string{}
}
fields := []string{}
rows := [][]string{}
if uint8(data[4]) == 0x00 {
// OK response
} else if uint8(data[4]) == 0xff {
// Error response
} else {
offset := 5
// Read fields
for {
length = read_length(data, offset)
if uint8(data[offset+4]) == 0xfe {
// EOF
offset += length + 4
break
}
_ /* catalog */, off := read_lstring(data, offset+4)
_ /*database*/, off = read_lstring(data, off)
_ /*table*/, off = read_lstring(data, off)
_ /*org table*/, off = read_lstring(data, off)
name, off := read_lstring(data, off)
_ /* org name */, off = read_lstring(data, off)
fields = append(fields, string(name))
offset += length + 4
}
// Read rows
for offset < len(data) {
var row []string
if uint8(data[offset+4]) == 0xfe {
// EOF
offset += length + 4
break
}
length = read_length(data, offset)
off := offset + 4 // skip length + packet number
start := off
for off < start+length {
var text []byte
if uint8(data[off]) == 0xfb {
text = []byte("NULL")
off++
} else {
text, off = read_lstring(data, off)
}
row = append(row, string(text))
}
rows = append(rows, row)
offset += length + 4
}
}
return fields, rows
}