-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
199 lines (177 loc) · 5.64 KB
/
main.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
package main
import (
"fmt"
"os"
"runtime"
log "github.com/Sirupsen/logrus"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/minio/cli"
)
func main() {
defaultInterface := "eth0"
if runtime.GOOS == "darwin" {
defaultInterface = "en0"
}
app := cli.NewApp()
app.Name = "go-memcached-sniffer"
app.Usage = "Like a dog with its nose up memcached's butt"
// Explicitly setting so that the short code for version is "V" and verbose can be "v"
cli.VersionFlag = cli.BoolFlag{
Name: "version, V",
Usage: "print the version",
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "interface, i",
Value: defaultInterface,
Usage: "the interface to sniff",
},
cli.StringFlag{
Name: "filter, f",
Value: "tcp and port 11211",
Usage: "pcap-stype filter on the incoming packets",
},
cli.IntFlag{
Name: "snaplength, s",
Value: 1600,
Usage: "maximum size to read for each packet",
},
cli.BoolFlag{
Name: "promiscuous, p",
Usage: "put the interface into promiscuous mode",
},
cli.DurationFlag{
Name: "timeout, t",
Value: pcap.BlockForever,
Usage: `timeout on a connection. defaults to 'BlockForever'
A timeout of 0 is not recommended. Some platforms, like Macs (http://www.manpages.info/macosx/pcap.3.html) say:
The read timeout is used to arrange that the read not necessarily return
immediately when a packet is seen, but that it wait for some amount of time
to allow more packets to arrive and to read multiple packets from the OS
kernel in one operation.
This means that if you only capture one packet, the kernel might decide to wait 'timeout' for more packets to batch with it before returning. A timeout of 0, then, means 'wait forever for more packets', which is... not good.
To get around this, we've introduced the following behavior: if a negative timeout is passed in, we set the positive timeout in the handle, then loop internally in ReadPacketData/ZeroCopyReadPacketData when we see timeout errors.`,
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "enable verbose logging",
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "disable logging on non-fatal events",
},
}
app.Action = appAction
app.Run(os.Args)
}
// FlowBuffer holds a buffer and the flow it came from
type FlowBuffer struct {
Flow gopacket.Flow
Buffer []byte
}
func appAction(c *cli.Context) {
if c.Bool("quiet") {
log.SetLevel(log.FatalLevel)
} else if c.Bool("verbose") {
log.SetLevel(log.DebugLevel)
}
log.WithFields(log.Fields{
"interface": c.String("interface"),
"snaplength": c.Int("snaplength"),
"promiscuous": c.Bool("promiscuous"),
"timeout": c.Duration("timeout"),
}).Debug("Opening interface")
handle, err := pcap.OpenLive(c.String("interface"), int32(c.Int("snaplength")), c.Bool("promiscuous"), c.Duration("timeout"))
if err != nil {
panic(err)
}
defer handle.Close()
if c.String("filter") != "" {
log.WithField("filter", c.String("filter")).Debug("Applying filter")
err = handle.SetBPFFilter(c.String("filter"))
if err != nil {
log.WithError(err).Fatal("Failed to set filter")
}
}
flowRemnants := map[gopacket.Flow]([]byte){}
toBeParsed := make(chan FlowBuffer)
go func() {
for fbuff := range toBeParsed {
pr := parseSession(fbuff.Buffer)
if pr.ParserOffset != pr.BufferLength || pr.ParserState != memcached_first_final {
log.WithFields(pr.ToLogFields()).WithField("flow_id", fbuff.Flow.FastHash()).Info("Parse failed on flow")
fname := fmt.Sprintf("flow_%d.bin", fbuff.Flow.FastHash())
f, err := os.Create(fname)
if err != nil {
log.WithField("fname", fname).WithError(err).Error("Failed to open file")
} else {
defer f.Close()
_, err = f.Write(fbuff.Buffer)
if err != nil {
log.WithField("fname", fname).WithError(err).Error("Failed to write file")
}
}
}
}
}()
// Use the handle as a packet source to process all packets
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
// Process packet here
ipLayer := packet.Layer(layers.LayerTypeIPv4)
if ipLayer != nil {
// ip, _ := ipLayer.(*layers.IPv4)
tcpLayer := packet.Layer(layers.LayerTypeTCP)
if tcpLayer != nil {
tcp, _ := tcpLayer.(*layers.TCP)
flow := tcp.TransportFlow()
remnant, ok := flowRemnants[flow]
if tcp.FIN {
log.WithField("hash", flow.FastHash()).Debug("Received FIN")
// close the flow
if ok {
log.WithField("hash", flow.FastHash()).Debug("Closing flow")
if len(remnant) > 0 {
toBeParsed <- FlowBuffer{flow, remnant}
}
delete(flowRemnants, flow)
log.WithField("hash", flow.FastHash()).Debug("Closed flow")
}
} else {
al := packet.ApplicationLayer()
if al != nil {
if !ok {
log.WithField("hash", flow.FastHash()).Debug("Opened")
remnant = []byte{}
flowRemnants[flow] = remnant // shouldn't be necessary
}
// fmt.Printf(
// "Application Packet #%d sent from %v:%d to %v:%d. flowhash=%d\n",
// tcp.Seq,
// flow.Src(),
// tcp.SrcPort,
// flow.Dst(),
// tcp.DstPort,
// flow.FastHash(),
// )
log.WithFields(log.Fields{
"hash": flow.FastHash(),
"length": len(al.Payload()),
}).Debug("Appending packet")
remnant = append(remnant, al.Payload()...)
flowRemnants[flow] = remnant
log.WithFields(log.Fields{
"hash": flow.FastHash(),
"length": len(al.Payload()),
}).Debug("Appended packet")
// body := string(al.Payload())
// fmt.Println(body)
}
}
}
}
}
close(toBeParsed)
}