forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pgsql.go
788 lines (633 loc) · 18.6 KB
/
pgsql.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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
package main
import (
"labix.org/v2/mgo/bson"
"strings"
"time"
)
type PgsqlMessage struct {
start int
end int
isSSLResponse bool
isSSLRequest bool
toExport bool
Ts time.Time
IsRequest bool
Query string
Size uint64
Fields []string
FieldsFormat []byte
Rows [][]string
NumberOfRows int
NumberOfFields int
IsOK bool
IsError bool
ErrorInfo string
ErrorCode string
ErrorSeverity string
Direction uint8
Incomplete bool
TcpTuple TcpTuple
CmdlineTuple *CmdlineTuple
}
type PgsqlTransaction struct {
Type string
tuple TcpTuple
Src Endpoint
Dst Endpoint
ResponseTime int32
Ts int64
JsTs time.Time
ts time.Time
Pgsql bson.M
Request_raw string
Response_raw string
timer *time.Timer
}
type PgsqlStream struct {
tcpStream *TcpStream
data []byte
parseOffset int
parseState int
seenSSLRequest bool
expectSSLResponse bool
message *PgsqlMessage
}
const (
PgsqlStartState = iota
PgsqlGetDataState
)
const (
SSLRequest = iota
StartupMessage
CancelRequest
)
var pgsqlTransactionsMap = make(map[HashableTcpTuple][]*PgsqlTransaction, TransactionsHashSize)
func (stream *PgsqlStream) PrepareForNewMessage() {
stream.data = stream.data[stream.message.end:]
stream.parseState = PgsqlStartState
stream.parseOffset = 0
stream.message = nil
}
// Parse a list of commands separated by semicolon from the query
func pgsqlQueryParser(query string) []string {
array := strings.Split(query, ";")
queries := []string{}
for _, q := range array {
qt := strings.TrimSpace(q)
if len(qt) > 0 {
queries = append(queries, qt)
}
}
return queries
}
// Extract the method from a SQL query
func getQueryMethod(q string) string {
index := strings.Index(q, " ")
var method string
if index > 0 {
method = strings.ToUpper(q[:index])
} else {
method = strings.ToUpper(q)
}
return method
}
func pgsqlFieldsParser(s *PgsqlStream) {
m := s.message
// read field count (int16)
field_count := int(Bytes_Ntohs(s.data[s.parseOffset : s.parseOffset+2]))
s.parseOffset += 2
DEBUG("pgsqldetailed", "Row Description field count=%d", field_count)
fields := []string{}
fields_format := []byte{}
for i := 0; i < field_count; i++ {
// read field name (null terminated string)
field_name, err := readString(s.data[s.parseOffset:])
if err != nil {
ERR("Fail to read the column field")
}
fields = append(fields, field_name)
m.NumberOfFields += 1
s.parseOffset += len(field_name) + 1
// read Table OID (int32)
s.parseOffset += 4
// read Column Index (int16)
s.parseOffset += 2
// read Type OID (int32)
s.parseOffset += 4
// read column length (int16)
s.parseOffset += 2
// read type modifier (int32)
s.parseOffset += 4
// read format (int16)
format := Bytes_Ntohs(s.data[s.parseOffset : s.parseOffset+2])
fields_format = append(fields_format, byte(format))
s.parseOffset += 2
DEBUG("pgsqldetailed", "Field name=%s, format=%d", field_name, format)
}
m.Fields = fields
m.FieldsFormat = fields_format
if m.NumberOfFields != field_count {
ERR("Missing fields from RowDescription. Expected %d. Received %d", field_count, m.NumberOfFields)
}
}
func pgsqlRowsParser(s *PgsqlStream) {
m := s.message
// read field count (int16)
field_count := int(Bytes_Ntohs(s.data[s.parseOffset : s.parseOffset+2]))
s.parseOffset += 2
DEBUG("pgsqldetailed", "DataRow field count=%d", field_count)
row := []string{}
for i := 0; i < field_count; i++ {
// read column length (int32)
column_length := int32(Bytes_Ntohl(s.data[s.parseOffset : s.parseOffset+4]))
s.parseOffset += 4
// read column value (byten)
column_value := []byte{}
if m.FieldsFormat[i] == 0 {
// field value in text format
if column_length > 0 {
column_value = s.data[s.parseOffset : s.parseOffset+int(column_length)]
} else if column_length == -1 {
column_value = nil
}
}
row = append(row, string(column_value))
if column_length > 0 {
s.parseOffset += int(column_length)
}
DEBUG("pgsqldetailed", "Value %s, length=%d", string(column_value), column_length)
}
m.NumberOfRows += 1
m.Rows = append(m.Rows, row)
}
func pgsqlErrorParser(s *PgsqlStream) {
m := s.message
for len(s.data[s.parseOffset:]) > 0 {
// read field type(byte1)
field_type := s.data[s.parseOffset]
s.parseOffset += 1
if field_type == 0 {
break
}
// read field value(string)
field_value, err := readString(s.data[s.parseOffset:])
if err != nil {
ERR("Fail to read the column field")
}
s.parseOffset += len(field_value) + 1
if field_type == 'M' {
m.ErrorInfo = field_value
} else if field_type == 'C' {
m.ErrorCode = field_value
} else if field_type == 'S' {
m.ErrorSeverity = field_value
}
}
DEBUG("pgsqldetailed", "%s %s %s", m.ErrorSeverity, m.ErrorCode, m.ErrorInfo)
}
func isSpecialPgsqlCommand(data []byte) (bool, int) {
if len(data) < 8 {
// 8 bytes required
return false, 0
}
// read length
length := int(Bytes_Ntohl(data[0:4]))
// read command identifier
code := int(Bytes_Ntohl(data[4:8]))
if length == 16 && code == 80877102 {
// Cancel Request
DEBUG("pgsqldetailed", "Cancel Request, length=%d", length)
return true, CancelRequest
} else if length == 8 && code == 80877103 {
// SSL Request
DEBUG("pgsqldetailed", "SSL Request, length=%d", length)
return true, SSLRequest
} else if code == 196608 {
// Startup Message
DEBUG("pgsqldetailed", "Startup Message, length=%d", length)
return true, StartupMessage
}
return false, 0
}
func pgsqlMessageParser(s *PgsqlStream) (bool, bool) {
m := s.message
for s.parseOffset < len(s.data) {
switch s.parseState {
case PgsqlStartState:
if len(s.data[s.parseOffset:]) < 5 {
WARN("Postgresql Message too short. %X (length=%d). Wait for more.", s.data[s.parseOffset:], len(s.data[s.parseOffset:]))
return true, false
}
is_special, command := isSpecialPgsqlCommand(s.data[s.parseOffset:])
if is_special {
// In case of Commands: StartupMessage, SSLRequest, CancelRequest that don't have
// their type in the first byte
// read length
length := int(Bytes_Ntohl(s.data[s.parseOffset : s.parseOffset+4]))
// ignore command
if len(s.data[s.parseOffset:]) >= length {
if command == SSLRequest {
// if SSLRequest is received, expect for one byte reply (S or N)
m.start = s.parseOffset
s.parseOffset += length
m.end = s.parseOffset
m.isSSLRequest = true
return true, true
}
s.parseOffset += length
} else {
// wait for more
DEBUG("pgsqldetailed", "Wait for more data 1")
return true, false
}
} else {
// In case of Commands that have their type in the first byte
// read type
typ := byte(s.data[s.parseOffset])
if s.expectSSLResponse {
// SSLRequest was received in the other stream
if typ == 'N' || typ == 'S' {
// one byte reply to SSLRequest
DEBUG("pgsqldetailed", "Reply for SSLRequest %c", typ)
m.start = s.parseOffset
s.parseOffset += 1
m.end = s.parseOffset
m.isSSLResponse = true
return true, true
}
}
// read length
length := int(Bytes_Ntohl(s.data[s.parseOffset+1 : s.parseOffset+5]))
DEBUG("pgsqldetailed", "Pgsql type %c, length=%d", typ, length)
if typ == 'Q' {
// SimpleQuery
m.start = s.parseOffset
m.IsRequest = true
if len(s.data[s.parseOffset:]) >= length+1 {
s.parseOffset += 1 //type
s.parseOffset += length
m.end = s.parseOffset
m.Query = string(s.data[m.start+5 : m.end-1]) //without string termination
m.toExport = true
DEBUG("pgsqldetailed", "Simple Query", "%s", m.Query)
return true, true
} else {
// wait for more
DEBUG("pgsqldetailed", "Wait for more data 2")
return true, false
}
} else if typ == 'T' {
// RowDescription
m.start = s.parseOffset
m.IsRequest = false
m.IsOK = true
m.toExport = true
if len(s.data[s.parseOffset:]) >= length+1 {
s.parseOffset += 1 //type
s.parseOffset += 4 //length
pgsqlFieldsParser(s)
DEBUG("pgsqldetailed", "Fields: %s", m.Fields)
s.parseState = PgsqlGetDataState
} else {
// wait for more
DEBUG("pgsqldetailed", "Wait for more data 3")
return true, false
}
} else if typ == 'I' {
// EmptyQueryResponse, appears as a response for empty queries
// substitutes CommandComplete
DEBUG("pgsqldetailed", "EmptyQueryResponse")
m.start = s.parseOffset
m.IsOK = true
m.IsRequest = false
m.toExport = true
s.parseOffset += 5 // type + length
m.end = s.parseOffset
m.Size = uint64(m.end - m.start)
return true, true
} else if typ == 'E' {
// ErrorResponse
DEBUG("pgsqldetailed", "ErrorResponse")
m.start = s.parseOffset
m.IsRequest = false
m.IsError = true
m.toExport = true
if len(s.data[s.parseOffset:]) >= length+1 {
s.parseOffset += 1 //type
s.parseOffset += 4 //length
pgsqlErrorParser(s)
m.end = s.parseOffset
m.Size = uint64(m.end - m.start)
return true, true
} else {
// wait for more
DEBUG("pgsqldetailed", "Wait for more data 4")
return true, false
}
} else if typ == 'C' {
// CommandComplete -> Successful response
m.start = s.parseOffset
m.IsRequest = false
m.IsOK = true
m.toExport = true
if len(s.data[s.parseOffset:]) >= length+1 {
s.parseOffset += 1 //type
name := string(s.data[s.parseOffset+4 : s.parseOffset+length-1]) //without \0
DEBUG("pgsqldetailed", "CommandComplete length=%d, tag=%s", length, name)
s.parseOffset += length
m.end = s.parseOffset
m.Size = uint64(m.end - m.start)
return true, true
} else {
// wait for more
DEBUG("pgsqldetailed", "Wait for more data 5")
return true, false
}
} else if typ == 'Z' {
// ReadyForQuery -> backend ready for a new query cycle
if len(s.data[s.parseOffset:]) >= length+1 {
m.start = s.parseOffset
s.parseOffset += 1 // type
s.parseOffset += length
m.end = s.parseOffset
return true, true
} else {
// wait for more
DEBUG("pgsqldetailed", "Wait for more 5b")
return true, false
}
} else {
// TODO: add info from NoticeResponse in case there are warning messages for a query
// ignore command
if len(s.data[s.parseOffset:]) >= length+1 {
s.parseOffset += 1 //type
s.parseOffset += length
} else {
// wait for more
DEBUG("pgsqldetailed", "Wait for more data 6")
return true, false
}
}
}
break
case PgsqlGetDataState:
// The response to queries that return row sets contains:
// RowDescription
// zero or more DataRow
// CommandComplete
// ReadyForQuery
if len(s.data[s.parseOffset:]) < 5 {
WARN("Postgresql Message too short (length=%d). Wait for more.", len(s.data[s.parseOffset:]))
return true, false
}
// read type
typ := byte(s.data[s.parseOffset])
// read message length
length := int(Bytes_Ntohl(s.data[s.parseOffset+1 : s.parseOffset+5]))
if typ == 'D' {
// DataRow
if len(s.data[s.parseOffset:]) >= length+1 {
// skip type
s.parseOffset += 1
// skip length size
s.parseOffset += 4
pgsqlRowsParser(s)
} else {
// wait for more
DEBUG("pgsqldetailed", "Wait for more data 7")
return true, false
}
} else if typ == 'C' {
// CommandComplete
if len(s.data[s.parseOffset:]) >= length+1 {
// skip type
s.parseOffset += 1
name := string(s.data[s.parseOffset+4 : s.parseOffset+length-1]) //without \0
DEBUG("pgsqldetailed", "CommandComplete length=%d, tag=%s", length, name)
s.parseOffset += length
m.end = s.parseOffset
m.Size = uint64(m.end - m.start)
s.parseState = PgsqlStartState
DEBUG("pgsqldetailed", "Rows: %s", m.Rows)
return true, true
} else {
// wait for more
DEBUG("pgsqldetailed", "Wait for more data 8")
return true, false
}
} else {
// shouldn't happen
DEBUG("pgsqldetailed", "Skip command of type %c", typ)
s.parseState = PgsqlStartState
}
break
}
}
return true, false
}
func ParsePgsql(pkt *Packet, tcp *TcpStream, dir uint8) {
defer RECOVER("ParsePgsql exception")
if tcp.pgsqlData[dir] == nil {
tcp.pgsqlData[dir] = &PgsqlStream{
tcpStream: tcp,
data: pkt.payload,
message: &PgsqlMessage{Ts: pkt.ts},
}
DEBUG("pgsqldetailed", "New stream created")
} else {
// concatenate bytes
tcp.pgsqlData[dir].data = append(tcp.pgsqlData[dir].data, pkt.payload...)
DEBUG("pgsqldetailed", "Len data: %d cap data: %d", len(tcp.pgsqlData[dir].data), cap(tcp.pgsqlData[dir].data))
if len(tcp.pgsqlData[dir].data) > TCP_MAX_DATA_IN_STREAM {
DEBUG("pgsql", "Stream data too large, dropping TCP stream")
tcp.pgsqlData[dir] = nil
return
}
}
stream := tcp.pgsqlData[dir]
if tcp.pgsqlData[1-dir] != nil && tcp.pgsqlData[1-dir].seenSSLRequest {
stream.expectSSLResponse = true
}
for len(stream.data) > 0 {
if stream.message == nil {
stream.message = &PgsqlMessage{Ts: pkt.ts}
}
ok, complete := pgsqlMessageParser(tcp.pgsqlData[dir])
if !ok {
// drop this tcp stream. Will retry parsing with the next
// segment in it
tcp.pgsqlData[dir] = nil
DEBUG("pgsql", "Ignore Postgresql 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]
if stream.message.isSSLRequest {
// SSL request
stream.seenSSLRequest = true
} else if stream.message.isSSLResponse {
// SSL request answered
stream.expectSSLResponse = false
tcp.pgsqlData[1-dir].seenSSLRequest = false
} else {
if stream.message.toExport {
handlePgsql(stream.message, tcp, dir, msg)
}
}
// and reset message
stream.PrepareForNewMessage()
} else {
// wait for more data
break
}
}
}
func PgsqlMessageHasEnoughData(msg *PgsqlMessage) bool {
if msg == nil {
return false
}
if msg.isSSLRequest || msg.isSSLResponse {
return false
}
if msg.IsRequest {
return len(msg.Query) > 0
} else {
return len(msg.Rows) > 0
}
}
// Called when there's a drop packet
func GapInPgsqlStream(tcp *TcpStream, dir uint8) {
defer RECOVER("GapInPgsqlStream exception")
// If enough data was received, send it to the
// next layer but mark it as incomplete.
stream := tcp.pgsqlData[dir]
if PgsqlMessageHasEnoughData(stream.message) {
DEBUG("pgsql", "Message not complete, but sending to the next layer")
stream.message.toExport = true
stream.message.end = stream.parseOffset
stream.message.Incomplete = true
msg := stream.data[stream.message.start:stream.message.end]
handlePgsql(stream.message, tcp, dir, msg)
// and reset message
stream.PrepareForNewMessage()
}
}
var handlePgsql = func(m *PgsqlMessage, tcp *TcpStream,
dir uint8, raw_msg []byte) {
m.TcpTuple = TcpTuple{
ip_length: tcp.tuple.ip_length,
Src_ip: tcp.tuple.Src_ip,
Dst_ip: tcp.tuple.Dst_ip,
Src_port: tcp.tuple.Src_port,
Dst_port: tcp.tuple.Dst_port,
stream_id: tcp.id,
}
m.TcpTuple.ComputeHashebles()
m.Direction = dir
m.CmdlineTuple = procWatcher.FindProcessesTuple(tcp.tuple)
if m.IsRequest {
receivedPgsqlRequest(m)
} else {
receivedPgsqlResponse(m)
}
}
func receivedPgsqlRequest(msg *PgsqlMessage) {
tuple := msg.TcpTuple
// parse the query, as it might contain a list of pgsql command
// separated by ';'
queries := pgsqlQueryParser(msg.Query)
DEBUG("pgsqldetailed", "Queries (%d) :%s", len(queries), queries)
if pgsqlTransactionsMap[tuple.raw] == nil {
pgsqlTransactionsMap[tuple.raw] = []*PgsqlTransaction{}
}
for _, query := range queries {
trans := &PgsqlTransaction{Type: "pgsql", tuple: tuple}
trans.ts = msg.Ts
trans.Ts = int64(trans.ts.UnixNano() / 1000) // transactions have microseconds resolution
trans.JsTs = msg.Ts
trans.Src = Endpoint{
Ip: msg.TcpTuple.Src_ip.String(),
Port: msg.TcpTuple.Src_port,
Proc: string(msg.CmdlineTuple.Src),
}
trans.Dst = Endpoint{
Ip: msg.TcpTuple.Dst_ip.String(),
Port: msg.TcpTuple.Dst_port,
Proc: string(msg.CmdlineTuple.Dst),
}
if msg.Direction == TcpDirectionReverse {
trans.Src, trans.Dst = trans.Dst, trans.Src
}
trans.Pgsql = bson.M{
"query": query,
"query.raw": query,
"method": getQueryMethod(query),
}
trans.Request_raw = query
if trans.timer != nil {
trans.timer.Stop()
}
trans.timer = time.AfterFunc(TransactionTimeout, func() { trans.Expire() })
pgsqlTransactionsMap[tuple.raw] = append(pgsqlTransactionsMap[tuple.raw], trans)
}
}
func receivedPgsqlResponse(msg *PgsqlMessage) {
tuple := msg.TcpTuple
trans_list := pgsqlTransactionsMap[tuple.raw]
if trans_list == nil || len(trans_list) == 0 {
WARN("Response from unknown transaction. Ignoring.")
return
}
// extract the first transaction from the array
trans := removePgsqlTransaction(tuple, 0)
// check if the request was received
if len(trans.Pgsql) == 0 {
WARN("Response from unknown transaction. Ignoring.")
return
}
trans.Pgsql = bson_concat(trans.Pgsql, bson.M{
"isOK": msg.IsOK,
"iserror": msg.IsError,
"size": msg.Size,
"num_rows": msg.NumberOfRows,
"num_fields": msg.NumberOfFields,
"error_code": msg.ErrorCode,
"error_message": msg.ErrorInfo,
"error_severity": msg.ErrorSeverity,
})
trans.ResponseTime = int32(msg.Ts.Sub(trans.ts).Nanoseconds() / 1e6) // resp_time in milliseconds
trans.Response_raw = dumpInCSVFormat(msg.Fields, msg.Rows)
err := Publisher.PublishPgsqlTransaction(trans)
if err != nil {
WARN("Publish failure: %s", err)
}
DEBUG("pgsql", "Postgres transaction completed: %s\n%s", trans.Pgsql, trans.Response_raw)
if trans.timer != nil {
trans.timer.Stop()
}
}
func (trans *PgsqlTransaction) Expire() {
// TODO: Here we need to PUBLISH an incomplete/timeout transaction
// remove from map
for i, t := range pgsqlTransactionsMap[trans.tuple.raw] {
if t == trans {
removePgsqlTransaction(trans.tuple, i)
break
}
}
if len(pgsqlTransactionsMap[trans.tuple.raw]) == 0 {
delete(pgsqlTransactionsMap, trans.tuple.raw)
}
}
func removePgsqlTransaction(tuple TcpTuple, index int) *PgsqlTransaction {
trans_list := pgsqlTransactionsMap[tuple.raw]
trans := trans_list[index]
trans_list = append(trans_list[:index], trans_list[index+1:]...)
if len(trans_list) == 0 {
delete(pgsqlTransactionsMap, trans.tuple.raw)
} else {
pgsqlTransactionsMap[tuple.raw] = trans_list
}
return trans
}