-
Notifications
You must be signed in to change notification settings - Fork 1
/
assembler.go
310 lines (272 loc) · 8.26 KB
/
assembler.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
package reassembler
import (
"context"
"fmt"
"log"
"os"
"runtime"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
)
// Stream represents a tcp stream.
type Stream struct {
Starttime time.Time
Endtime time.Time
Endreason string
Packets []gopacket.Packet
SIP, DIP string
SPort, DPort uint16
Source string // describes the source of the pkt e.g. pcap file
}
type internalStream struct {
Stream
fins int
lastUpdated time.Time
}
// Stats is a collection of stats from the assembler
type Stats struct {
ProcessedStreams uint64 // how many streams have been processed
NotTCP uint64 // how many pkts have no TCP layer
NotIP uint64 // how many pkt have no IP layer
FinishedStreams uint64 // how many streams have been finished/reassembled
Flushes uint64 // how often flush was called (to flush "old" streams)
MissingMetadata uint64 // how often the Metadata of a packet were missing
SeenPackets uint64 // how many packets have been given to the assembler
StreamsInLastFlush int // how many open streams have been flushed in the last flush
IPv4 uint64 // how many IPv4 pkts have been seen
IPv6 uint64 // how many IPv6 pkts have been seen
}
type packetWithSrc struct {
pkt gopacket.Packet
src string // src describes where the packet was seen/found e.g. pcap file
}
// Assembler reassembles TCP streams
type Assembler struct {
input chan packetWithSrc
output chan Stream
table map[string]internalStream
ctx context.Context
ctxCancel context.CancelFunc
flushTime time.Duration
stats Stats
}
// NewAssembler creates a new assembler
func NewAssembler() *Assembler {
asm := Assembler{}
asm.input = make(chan packetWithSrc, 10000) // should be big enough
asm.output = make(chan Stream, 1000) // should be big enough
asm.table = make(map[string]internalStream, 1000000) // map will be huge... avoid resizing
ctx, cancel := context.WithCancel(context.Background())
asm.ctx = ctx
asm.ctxCancel = cancel
asm.flushTime = time.Duration(10 * time.Minute)
asm.stats = Stats{}
return &asm
}
// SetFlushTime sets the "idle-time-limit" of a stream e.g. the time no new packet for a stream was found.
// After this time the stream will be flushed.
// Needs a positive value!
// Set this value before calling Run()!
// Default: 10 --> streams with no new packets since 10 minutes will be flushed.
func (a *Assembler) SetFlushTime(value int) {
if value < 0 {
return
}
a.flushTime = time.Duration(value) * time.Minute
}
// Stats returnes the stats of the assembler.
// This call is not protected by a mutex... should work nevertheless in this case but its 'undefined' behaviour.
func (a *Assembler) Stats() Stats {
return a.stats
}
// Assemble feeds a gopacket.Packet to the assembler. Only packets which have a ipv*Layer AND a tcpLayer will be processed.
// This call might block.
func (a *Assembler) Assemble(packet gopacket.Packet, src string) {
//go func() { a.input <- packet }() // TODO: evaluate if this (go...) is a good idea or if it should block
a.input <- packetWithSrc{packet, src}
}
// Run starts the assembler. Call this only once!
func (a *Assembler) Run() {
go func() {
ticker := time.NewTicker(60 * time.Second) // check every 60 secs if a flush should be done
for {
select {
case packet, ok := <-a.input:
if !ok {
log.Println("STOPPED (via channel...)")
return
}
a.stats.SeenPackets++
var key string
var ipSrcString string
var ipDstString string
var tcp *layers.TCP
// packet layer checks
ipv4Layer := packet.pkt.Layer(layers.LayerTypeIPv4)
if ipv4Layer == nil { // its not IPv4 - check if IPv6
ipv6Layer := packet.pkt.Layer(layers.LayerTypeIPv6)
if ipv6Layer == nil { // no IPv*layer
a.stats.NotIP++
break
}
ip, ok := ipv6Layer.(*layers.IPv6)
if !ok {
a.stats.NotIP++
break
}
ipSrcString = ip.SrcIP.String()
ipDstString = ip.DstIP.String()
tcpLayer := packet.pkt.Layer(layers.LayerTypeTCP)
if tcpLayer == nil {
a.stats.NotTCP++
break
}
tcp, ok = tcpLayer.(*layers.TCP)
if !ok {
a.stats.NotTCP++
break
}
a.stats.IPv6++
key = generateKeyIPV6(tcp, ip)
} else {
ip, ok := ipv4Layer.(*layers.IPv4)
if !ok {
a.stats.NotIP++
break
}
ipSrcString = ip.SrcIP.String()
ipDstString = ip.DstIP.String()
tcpLayer := packet.pkt.Layer(layers.LayerTypeTCP)
if tcpLayer == nil {
a.stats.NotTCP++
break
}
tcp, ok = tcpLayer.(*layers.TCP)
if !ok {
a.stats.NotTCP++
break
}
a.stats.IPv4++
key = generateKeyIPV4(tcp, ip)
}
elem, ok := a.table[key]
if !ok { // new stream
a.stats.ProcessedStreams++
tmpInternalStream := internalStream{}
if packet.pkt.Metadata() != nil {
tmpInternalStream.Starttime = packet.pkt.Metadata().Timestamp
tmpInternalStream.Endtime = packet.pkt.Metadata().Timestamp
} else {
a.stats.MissingMetadata++
tmpInternalStream.Starttime = time.Now()
tmpInternalStream.Endtime = time.Now()
}
tmpInternalStream.Source = packet.src
tmpInternalStream.SIP = ipSrcString
tmpInternalStream.DIP = ipDstString
tmpInternalStream.SPort = uint16(tcp.SrcPort)
tmpInternalStream.DPort = uint16(tcp.DstPort)
tmpInternalStream.Packets = append(tmpInternalStream.Packets, packet.pkt)
tmpInternalStream.lastUpdated = time.Now()
a.table[key] = tmpInternalStream
break
}
if tcp.RST { // we found a RST for this existing Stream
elem.Packets = append(elem.Packets, packet.pkt)
if packet.pkt.Metadata() != nil {
elem.Endtime = packet.pkt.Metadata().Timestamp
} else {
a.stats.MissingMetadata++
elem.Endtime = time.Now()
}
elem.Endreason = "RST"
elem.lastUpdated = time.Now()
a.output <- elem.Stream
a.stats.FinishedStreams++
delete(a.table, key)
break
}
if tcp.FIN { // we found a FIN
elem.fins++
elem.lastUpdated = time.Now()
if elem.fins == 2 { // check if its the second one
elem.Packets = append(elem.Packets, packet.pkt)
if packet.pkt.Metadata() != nil {
elem.Endtime = packet.pkt.Metadata().Timestamp
} else {
a.stats.MissingMetadata++
elem.Endtime = time.Now()
}
elem.Endreason = "fin"
a.output <- elem.Stream
a.stats.FinishedStreams++
delete(a.table, key)
break
}
a.table[key] = elem
}
elem.Packets = append(elem.Packets, packet.pkt)
elem.lastUpdated = time.Now()
a.table[key] = elem
case <-ticker.C:
a.stats.Flushes++
x1 := len(a.table)
for k, s := range a.table {
if time.Now().After(s.lastUpdated.Add(a.flushTime)) {
// its time to flush this stream
s.Endreason = "timeout"
a.output <- s.Stream
a.stats.FinishedStreams++
delete(a.table, k)
}
}
a.stats.StreamsInLastFlush = (x1 - len(a.table))
log.Printf("Flushed: %d/%d streams\n", a.stats.StreamsInLastFlush, x1)
runtime.GC()
case <-a.ctx.Done():
log.Println("STOPPED")
return
}
}
}()
}
// Stop stops the assembler.
func (a *Assembler) Stop() {
a.ctxCancel()
close(a.input)
close(a.output)
}
// Streams returnes the next available stream or ok == false if no streams are available.
// Try to consume and recall this functions as fast as possible otherwise the reassembler might block.
func (a *Assembler) Streams() chan Stream {
return a.output
}
// DummyWorker is a dummy for testing... use Streams() for real stuff
func (a *Assembler) DummyWorker() {
//open a new pcap
n := fmt.Sprintf("flows_%d", time.Now().UnixNano()) + ".pcap"
f, err := os.Create(n)
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.Println("New file: " + n)
w := pcapgo.NewWriter(f)
w.WriteFileHeader(65535, layers.LinkTypeIPv4) // for some pcaps LinkTypeEthernet/LinkTypeIPv6 is needed
for {
select {
case stream, ok := <-a.output:
if !ok {
return
}
for _, pkt := range stream.Packets {
err := w.WritePacket(pkt.Metadata().CaptureInfo, pkt.Data())
if err != nil {
log.Fatal(err)
}
}
}
}
}