/
main.go
384 lines (312 loc) · 7.71 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
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
// Copyright 2018 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
package main
import (
"bufio"
"flag"
"io"
"net"
"os"
"os/signal"
"runtime/pprof"
"strings"
"time"
log "pkg/minilog"
"pkg/p0f"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
var (
f_hosts = flag.String("hosts", "", "hosts output filename")
f_p0f = flag.String("p0f", "", "file containing p0f fingerprints")
f_dot1q = flag.Bool("dot1q", false, "enable 802.1q (VLAN) analysis")
f_icmp4 = flag.Bool("icmp4", false, "enable icmp4 analysis")
f_dns = flag.Bool("dns", false, "enable dns analysis")
f_arp = flag.Bool("arp", false, "enable arp analysis")
f_dhcp = flag.Bool("dhcp", false, "enable dhcp analysis")
f_profile = flag.String("profile", "", "write cpu profile to file")
f_push = flag.String("push", "", "read hosts output and push to specified server")
)
// used for live capture to signal when to stop
var CAUGHT_SIGNAL bool
var TCPSigs []*p0f.TCPSignature
func main() {
flag.Parse()
log.Init()
if *f_push != "" {
if *f_hosts == "" {
log.Fatal("must specify host file when pushing to server")
}
pushHosts()
return
}
if flag.NArg() == 0 {
log.Fatal("must specify at least one input PCAP")
}
if *f_p0f != "" {
if err := parseFingerprints(*f_p0f); err != nil {
log.Fatal("unable to parse p0f fingerprints: %v", err)
}
}
var hostsOut = os.Stdout
if *f_hosts != "" {
f, err := os.Create(*f_hosts)
if err != nil {
log.Fatal("unable to open hosts output file: %v", err)
}
hostsOut = f
}
if *f_profile != "" {
log.Info("Enabling CPU profiling, writing to: %s", *f_profile)
f, err := os.Create(*f_profile)
if err != nil {
log.Fatal(err.Error())
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
state := State{
events: make(chan Event),
}
state.DecodingLayerParser = gopacket.NewDecodingLayerParser(
layers.LayerTypeEthernet,
&state.eth,
&state.ip4,
&state.ip6,
)
if *f_dot1q {
state.AddDecodingLayer(&state.dot1q)
}
if *f_icmp4 {
state.AddDecodingLayer(&state.icmp4)
}
if *f_dns {
state.AddDecodingLayer(&state.udp)
state.AddDecodingLayer(&state.tcp)
state.AddDecodingLayer(&state.dns)
}
if *f_dhcp {
state.AddDecodingLayer(&state.udp)
state.AddDecodingLayer(&state.dhcp)
}
if *f_arp {
state.AddDecodingLayer(&state.arp)
}
go func() {
defer close(state.events)
for _, in := range flag.Args() {
state.Run(in)
}
}()
// graceful exit
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
CAUGHT_SIGNAL = true
}()
inference := NewInference(dedupStream(state.events))
inference.Run()
inference.WriteHostsJSON(hostsOut)
log.Info("%v", inference.stats)
}
func (s *State) SrcIP() net.IP {
if s.internet == layers.LayerTypeIPv4 {
return s.ip4.SrcIP
} else if s.internet == layers.LayerTypeIPv6 {
return s.ip6.SrcIP
} else {
return nil
}
}
func (s *State) DstIP() net.IP {
if s.internet == layers.LayerTypeIPv4 {
return s.ip4.DstIP
} else if s.internet == layers.LayerTypeIPv6 {
return s.ip6.DstIP
} else {
return nil
}
}
func (s *State) GuessDistance() uint8 {
var ttl uint8
if s.internet == layers.LayerTypeIPv4 {
ttl = s.ip4.TTL
} else if s.internet == layers.LayerTypeIPv6 {
ttl = s.ip6.HopLimit
}
for i := uint8(0); i < 3; i++ {
if max := uint8(32 << i); ttl <= max {
return max - ttl
}
}
return 255 - ttl
}
func (s *State) Run(in string) {
log.Info("Processing %v", in)
// Record how long it took us to process the file
start := time.Now()
defer func() {
log.Info("Processed %v in %v", in, time.Now().Sub(start))
}()
packets, err := pcap.OpenOffline(in)
if err != nil {
// not a file, maybe an interface?
packets, err = pcap.OpenLive(in, 1600, true, pcap.BlockForever)
if err != nil {
log.Fatal("Failed to open pcap %v -- %v", in, err)
}
}
decodeFailed := map[string]int{}
decodedLayers := []gopacket.LayerType{}
for !CAUGHT_SIGNAL {
data, ci, err := packets.ReadPacketData()
if err != nil {
if err != io.EOF {
log.Error("Error reading packet data: ", err)
}
break
}
s.captureInfo = ci
// for testing
// err = s.DecodeLayers(data, &decodedLayers)
// for _, typ := range decodedLayers {
// fmt.Println(" successfully decoded layer type", typ)
// }
// fmt.Println("err: ", err)
// fmt.Println("eth can decode: ", s.eth.CanDecode())
if err := s.DecodeLayers(data, &decodedLayers); err != nil {
switch err := err.(type) {
case gopacket.UnsupportedLayerType:
decodeFailed[gopacket.LayerType(err).String()] += 1
default:
log.Error("Error parsing packet: %v", err)
continue
}
}
// Process all the decoded layers
for _, typ := range decodedLayers {
s.HandleLayer(typ)
}
if s.Truncated {
// TODO: Do we care? Probably could look for frequently truncated
// packets from misbehaving hosts...
//fmt.Println(" Packet has been truncated")
}
}
if len(decodeFailed) > 0 {
log.Info("Failed to decode:")
}
for layer, count := range decodeFailed {
log.Info(" %v: %v", layer, count)
}
}
func (s *State) HandleLayer(typ gopacket.LayerType) {
switch typ {
case layers.LayerTypeEthernet:
s.link = typ
case layers.LayerTypeDot1Q:
s.HandleDot1Q()
case layers.LayerTypeIPv4:
s.internet = typ
s.HandleIP()
case layers.LayerTypeIPv6:
s.internet = typ
s.HandleIP()
case layers.LayerTypeICMPv4:
s.HandleICMPv4()
case layers.LayerTypeARP:
s.HandleARP()
case layers.LayerTypeTCP:
s.transport = layers.LayerTypeTCP
s.HandleTCP()
case layers.LayerTypeUDP:
s.transport = layers.LayerTypeUDP
s.HandleUDP()
case layers.LayerTypeDNS:
s.HandleDNS()
case layers.LayerTypeDHCPv4:
s.HandleDHCP()
}
}
func dedupStream(in chan Event) chan Event {
out := make(chan Event)
type EventCounter struct {
Hash uint64
Event Event
Count uint
}
go func() {
defer close(out)
deduper := make([]EventCounter, 1<<20)
for e := range in {
hash := e.Hash()
index := hash % uint64(len(deduper))
counter := &deduper[index]
// New event is not the same as the stored event
if counter.Hash != hash {
// Re-emit the old event, if there's a non-zero count
if counter.Count > 0 {
counter.Event.SetWeight(counter.Count)
out <- counter.Event
}
// Emit and track new event
e.SetWeight(1)
out <- e
counter.Hash = hash
counter.Event = e
counter.Count = 0
} else {
counter.Count += 1
}
}
// Emit final set of events that have non-zero counts
for _, counter := range deduper {
if counter.Count > 0 {
out <- counter.Event
}
}
}()
return out
}
func parseFingerprints(fname string) error {
log.Debug("Parsing p0f fingerprints from %v", fname)
f, err := os.Open(fname)
if err != nil {
return err
}
defer f.Close()
label := ""
interested := false
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
// Skip blank lines/comments
if strings.HasPrefix(line, ";") {
continue
} else if len(line) == 0 {
continue
}
// New section, only interested in the two tcp sections
if strings.HasPrefix(line, "[") {
interested = (line == "[tcp:request]" || line == "[tcp:response]")
}
if !interested {
continue
}
if strings.HasPrefix(line, "label") {
label = strings.TrimSpace(strings.Split(line, "=")[1])
} else if strings.HasPrefix(line, "sig") {
s := strings.TrimSpace(strings.Split(line, "=")[1])
sig, err := p0f.ParseTCPSignature(label, s)
if err != nil {
return err
}
TCPSigs = append(TCPSigs, sig)
}
}
log.Debug("Parsed %v TCP fingerprints", len(TCPSigs))
return scanner.Err()
}