forked from zorkian/mysql-sniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql-sniffer.go
556 lines (490 loc) · 13.6 KB
/
mysql-sniffer.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
/*
* mysql-sniffer.go
*
* A straightforward program for sniffing MySQL query streams and providing
* diagnostic information on the realtime queries your database is handling.
*
* FIXME: this assumes IPv4.
* FIXME: tokenizer doesn't handle negative numbers or floating points.
* FIXME: canonicalizer should collapse "IN (?,?,?,?)" and "VALUES (?,?,?,?)"
* FIXME: tokenizer breaks on '"' or similarly embedded quotes
* FIXME: tokenizer parses numbers in words wrong, i.e. s2compiled -> s?compiled
* TODO: fix the above issues or just add a parser to fix it
*
* written by Mark Smith <[email protected]>
*
* requires the gopcap library to be installed from:
* https://github.com/akrennmair/gopcap
*
*/
package main
import (
"flag"
"fmt"
"math/rand"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/akrennmair/gopcap"
)
const (
TOKEN_WORD = 0
TOKEN_QUOTE = 1
TOKEN_NUMBER = 2
TOKEN_WHITESPACE = 3
TOKEN_OTHER = 4
// Internal tuning
TIME_BUCKETS = 10000
// ANSI colors
COLOR_RED = "\x1b[31m"
COLOR_GREEN = "\x1b[32m"
COLOR_YELLOW = "\x1b[33m"
COLOR_CYAN = "\x1b[36m"
COLOR_WHITE = "\x1b[37m"
COLOR_DEFAULT = "\x1b[39m"
// MySQL packet types
COM_QUERY = 3
// These are used for formatting outputs
F_NONE = iota
F_QUERY
F_ROUTE
F_SOURCE
F_SOURCEIP
)
type packet struct {
request bool // request or response
data []byte
}
type sortable struct {
value float64
line string
}
type sortableSlice []sortable
type source struct {
src string
srcip string
synced bool
reqbuffer []byte
resbuffer []byte
reqSent *time.Time
reqTimes [TIME_BUCKETS]uint64
qbytes uint64
qdata *queryData
qtext string
}
type queryData struct {
count uint64
bytes uint64
times [TIME_BUCKETS]uint64
}
var qbuf map[string]*queryData = make(map[string]*queryData)
var querycount int
var chmap map[string]*source = make(map[string]*source)
var verbose bool = false
var noclean bool = false
var dirty bool = false
var format []interface{}
var port uint16
var times [TIME_BUCKETS]uint64
var stats struct {
packets struct {
rcvd uint64
rcvd_sync uint64
}
desyncs uint64
streams uint64
}
func main() {
var lport *int = flag.Int("P", 3306, "MySQL port to use")
var eth *string = flag.String("i", "eth0", "Interface to sniff")
var ldirty *bool = flag.Bool("u", false, "Unsanitized -- do not canonicalize queries")
var doverbose *bool = flag.Bool("v", false, "Print every query received (spammy)")
var nocleanquery *bool = flag.Bool("n", false, "no clean queries")
var formatstr *string = flag.String("f", "#s:#q", "Format for output aggregation")
flag.Parse()
verbose = *doverbose
noclean = *nocleanquery
port = uint16(*lport)
dirty = *ldirty
parseFormat(*formatstr)
rand.Seed(time.Now().UnixNano())
log.Printf("Initializing MySQL sniffing on %s:%d...", *eth, port)
iface, err := pcap.Openlive(*eth, 1024, false, 0)
if iface == nil || err != nil {
msg := "unknown error"
if err != nil {
msg = err.Error()
}
log.Fatalf("Failed to open device: %s", msg)
}
err = iface.Setfilter(fmt.Sprintf("tcp port %d", port))
//err = iface.Setfilter(fmt.Sprintf("tcp port %d and inbound", port))
if err != nil {
log.Fatalf("Failed to set port filter: %s", err.Error())
}
var pkt *pcap.Packet = nil
var rv int32 = 0
for pkt, rv = iface.NextEx(); ; pkt, rv = iface.NextEx() {
if rv == 0 {
continue
}
handlePacket(pkt)
}
}
// Do something with a packet for a source.
func processPacket(rs *source, request bool, data []byte) {
stats.packets.rcvd++
if rs.synced {
stats.packets.rcvd_sync++
}
var ptype int = -1
var pdata []byte
if request {
// If we still have response buffer, we're in some weird state and
// didn't successfully process the response.
if rs.resbuffer != nil {
// log.Printf("[%s] possibly pipelined request? %d bytes",
// rs.src, len(rs.resbuffer))
stats.desyncs++
rs.resbuffer = nil
rs.synced = false
}
rs.reqbuffer = data
ptype, pdata = carvePacket(&rs.reqbuffer)
} else {
// FIXME: For now we're not doing anything with response data, just using the first packet
// after a query to determine latency.
rs.resbuffer = nil
ptype, pdata = 0, data
}
// The synchronization logic: if we're not presently, then we want to
// keep going until we are capable of carving off of a request/query.
if !rs.synced {
if !(request && ptype == COM_QUERY) {
rs.reqbuffer, rs.resbuffer = nil, nil
return
}
rs.synced = true
}
//log.Printf("[%s] request=%b ptype=%d plen=%d", rs.src, request, ptype, len(pdata))
// No (full) packet detected yet. Continue on our way.
if ptype == -1 {
return
}
plen := uint64(len(pdata))
// If this is a response then we want to record the timing and
// store it with this channel so we can keep track of that.
var reqtime uint64
if !request {
// Keep adding the bytes we're getting, since this is probably still part of
// an earlier response
if rs.reqSent == nil {
if rs.qdata != nil {
rs.qdata.bytes += plen
}
return
}
reqtime = uint64(time.Since(*rs.reqSent).Nanoseconds())
// We keep track of per-source, global, and per-query timings.
randn := rand.Intn(TIME_BUCKETS)
rs.reqTimes[randn] = reqtime
times[randn] = reqtime
if rs.qdata != nil {
// This should never fail but it has. Probably because of a
// race condition I need to suss out, or sharing between
// two different goroutines. :(
rs.qdata.times[randn] = reqtime
rs.qdata.bytes += plen
}
rs.reqSent = nil
// If we're in verbose mode, just dump statistics from this one.
if verbose && len(rs.qtext) > 0 {
log.Printf(" %s%s %s## %sbytes: %d time: %0.2f%s\n", COLOR_GREEN, rs.qtext, COLOR_RED,
COLOR_YELLOW, rs.qbytes, float64(reqtime)/1000000, COLOR_DEFAULT)
}
return
}
// This is for sure a request, so let's count it as one.
if rs.reqSent != nil {
// log.Printf("[%s] ...sending two requests without a response?",
// rs.src)
}
tnow := time.Now()
rs.reqSent = &tnow
// Convert this request into whatever format the user wants.
querycount++
var text string
for _, item := range format {
switch item.(type) {
case int:
switch item.(int) {
case F_NONE:
log.Fatalf("F_NONE in format string")
case F_QUERY:
if dirty {
text += string(pdata)
} else {
text += cleanupQuery(pdata)
}
case F_ROUTE:
// Routes are in the query like:
// SELECT /* hostname:route */ FROM ...
// We remove the hostname so routes can be condensed.
parts := strings.SplitN(string(pdata), " ", 5)
if len(parts) >= 4 && parts[1] == "/*" && parts[3] == "*/" {
if strings.Contains(parts[2], ":") {
text += strings.SplitN(parts[2], ":", 2)[1]
} else {
text += parts[2]
}
} else {
text += "(unknown) " + cleanupQuery(pdata)
}
case F_SOURCE:
text += rs.src
case F_SOURCEIP:
text += rs.srcip
default:
log.Fatalf("Unknown F_XXXXXX int in format string")
}
case string:
text += item.(string)
default:
log.Fatalf("Unknown type in format string")
}
}
qdata, ok := qbuf[text]
if !ok {
qdata = &queryData{}
qbuf[text] = qdata
}
qdata.count++
qdata.bytes += plen
rs.qtext, rs.qdata, rs.qbytes = text, qdata, plen
log.Println(rs.qtext)
}
// carvePacket tries to pull a packet out of a slice of bytes. If so, it removes
// those bytes from the slice.
func carvePacket(buf *[]byte) (int, []byte) {
datalen := uint32(len(*buf))
if datalen < 5 {
return -1, nil
}
size := uint32((*buf)[0]) + uint32((*buf)[1])<<8 + uint32((*buf)[2])<<16
if size == 0 || datalen < size+4 {
return -1, nil
}
// Else, has some length, try to validate it.
end := size + 4
ptype := int((*buf)[4])
data := (*buf)[5 : size+4]
if end >= datalen {
*buf = nil
} else {
*buf = (*buf)[end:]
}
// log.Printf("datalen=%d size=%d end=%d ptype=%d data=%d buf=%d",
// datalen, size, end, ptype, len(data), len(*buf))
return ptype, data
}
// extract the data... we have to figure out where it is, which means extracting data
// from the various headers until we get the location we want. this is crude, but
// functional and it should be fast.
func handlePacket(pkt *pcap.Packet) {
// Ethernet frame has 14 bytes of stuff to ignore, so we start our root position here
var pos byte = 14
// Grab the src IP address of this packet from the IP header.
srcIP := pkt.Data[pos+12 : pos+16]
dstIP := pkt.Data[pos+16 : pos+20]
// The IP frame has the header length in bits 4-7 of byte 0 (relative).
pos += pkt.Data[pos] & 0x0F * 4
// Grab the source port from the TCP header.
srcPort := uint16(pkt.Data[pos])<<8 + uint16(pkt.Data[pos+1])
dstPort := uint16(pkt.Data[pos+2])<<8 + uint16(pkt.Data[pos+3])
// The TCP frame has the data offset in bits 4-7 of byte 12 (relative).
pos += byte(pkt.Data[pos+12]) >> 4 * 4
// If this is a 0-length payload, do nothing. (Any way to change our filter
// to only dump packets with data?)
if len(pkt.Data[pos:]) <= 0 {
return
}
// This is either an inbound or outbound packet. Determine by seeing which
// end contains our port. Either way, we want to put this on the channel of
// the remote end.
var src string
var request bool = false
if srcPort == port {
src = fmt.Sprintf("%d.%d.%d.%d:%d", dstIP[0], dstIP[1], dstIP[2],
dstIP[3], dstPort)
//log.Printf("response to %s", src)
} else if dstPort == port {
src = fmt.Sprintf("%d.%d.%d.%d:%d", srcIP[0], srcIP[1], srcIP[2],
srcIP[3], srcPort)
request = true
//log.Printf("request from %s", src)
} else {
log.Fatalf("got packet src = %d, dst = %d", srcPort, dstPort)
}
// Get the data structure for this source, then do something.
rs, ok := chmap[src]
if !ok {
srcip := src[0:strings.Index(src, ":")]
rs = &source{src: src, srcip: srcip, synced: false}
stats.streams++
chmap[src] = rs
}
// Now with a source, process the packet.
processPacket(rs, request, pkt.Data[pos:])
}
// scans forward in the query given the current type and returns when we encounter
// a new type and need to stop scanning. returns the size of the last token and
// the type of it.
func scanToken(query []byte) (length int, thistype int) {
if len(query) < 1 {
log.Fatalf("scanToken called with empty query")
}
//no clean queries
if verbose && noclean {
return len(query), TOKEN_OTHER
}
// peek at the first byte, then loop
b := query[0]
switch {
case b == 39 || b == 34: // '"
started_with := b
escaped := false
for i := 1; i < len(query); i++ {
switch query[i] {
case started_with:
if escaped {
escaped = false
continue
}
return i + 1, TOKEN_QUOTE
case 92:
escaped = true
default:
escaped = false
}
}
return len(query), TOKEN_QUOTE
case b >= 48 && b <= 57: // 0-9
for i := 1; i < len(query); i++ {
switch {
case query[i] >= 48 && query[i] <= 57: // 0-9
// do nothing
default:
return i, TOKEN_NUMBER
}
}
return len(query), TOKEN_NUMBER
case b == 32 || (b >= 9 && b <= 13): // whitespace
for i := 1; i < len(query); i++ {
switch {
case query[i] == 32 || (query[i] >= 9 && query[i] <= 13):
// Eat all whitespace
default:
return i, TOKEN_WHITESPACE
}
}
return len(query), TOKEN_WHITESPACE
case (b >= 65 && b <= 90) || (b >= 97 && b <= 122): // a-zA-Z
for i := 1; i < len(query); i++ {
switch {
case query[i] >= 48 && query[i] <= 57:
// Numbers, allow.
case (query[i] >= 65 && query[i] <= 90) || (query[i] >= 97 && query[i] <= 122):
// Letters, allow.
case query[i] == 36 || query[i] == 95:
// $ and _
default:
return i, TOKEN_WORD
}
}
return len(query), TOKEN_WORD
default: // everything else
return 1, TOKEN_OTHER
}
// shouldn't get here
log.Fatalf("scanToken failure: [%s]", query)
return
}
func cleanupQuery(query []byte) string {
// iterate until we hit the end of the query...
var qspace []string
for i := 0; i < len(query); {
length, toktype := scanToken(query[i:])
switch toktype {
case TOKEN_WORD, TOKEN_OTHER:
qspace = append(qspace, string(query[i:i+length]))
case TOKEN_NUMBER, TOKEN_QUOTE:
qspace = append(qspace, "?")
case TOKEN_WHITESPACE:
qspace = append(qspace, " ")
default:
log.Fatalf("scanToken returned invalid token type %d", toktype)
}
i += length
}
// Remove hostname from the route information if it's present
tmp := strings.Join(qspace, "")
parts := strings.SplitN(tmp, " ", 5)
if len(parts) >= 5 && parts[1] == "/*" && parts[3] == "*/" {
if strings.Contains(parts[2], ":") {
tmp = parts[0] + " /* " + strings.SplitN(parts[2], ":", 2)[1] + " */ " + parts[4]
}
}
return strings.Replace(tmp, "?, ", "", -1)
}
// parseFormat takes a string and parses it out into the given format slice
// that we later use to build up a string. This might actually be an overcomplicated
// solution?
func parseFormat(formatstr string) {
formatstr = strings.TrimSpace(formatstr)
if formatstr == "" {
formatstr = "#b:#k"
}
is_special := false
curstr := ""
do_append := F_NONE
for _, char := range formatstr {
if char == '#' {
if is_special {
curstr += string(char)
is_special = false
} else {
is_special = true
}
continue
}
if is_special {
switch strings.ToLower(string(char)) {
case "s":
do_append = F_SOURCE
case "i":
do_append = F_SOURCEIP
case "r":
do_append = F_ROUTE
case "q":
do_append = F_QUERY
default:
curstr += "#" + string(char)
}
is_special = false
} else {
curstr += string(char)
}
if do_append != F_NONE {
if curstr != "" {
format = append(format, curstr, do_append)
curstr = ""
} else {
format = append(format, do_append)
}
do_append = F_NONE
}
}
if curstr != "" {
format = append(format, curstr)
}
}