forked from david415/HoneyBadger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatcher_test.go
336 lines (297 loc) · 8.27 KB
/
dispatcher_test.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
package HoneyBadger
import (
"log"
"net"
"os"
"testing"
"time"
"github.com/david415/HoneyBadger/logging"
"github.com/david415/HoneyBadger/types"
"github.com/google/gopacket/layers"
)
type MockSniffer struct {
supervisor types.Supervisor
startedChan chan bool
}
func NewMockSniffer(options *types.SnifferDriverOptions, dispatcher PacketDispatcher) types.PacketSource {
var packetSource types.PacketSource = &MockSniffer{
startedChan: make(chan bool, 0),
}
return packetSource
}
func (s *MockSniffer) Start() {
log.Print("MockSniffer Start()")
s.startedChan <- true
}
func (s *MockSniffer) Stop() {
log.Print("MockSniffer Stop()")
}
func (s *MockSniffer) SetSupervisor(supervisor types.Supervisor) {
s.supervisor = supervisor
}
func (s *MockSniffer) GetStartedChan() chan bool {
return s.startedChan
}
type MockConnection struct {
options ConnectionOptions
clientFlow types.TcpIpFlow
serverFlow types.TcpIpFlow
lastSeen time.Time
ClientStreamRing *types.Ring
packetObserverChan chan bool
receiveChan chan *types.PacketManifest
}
func (m MockConnection) Close() {
log.Print("MockConnection.Close()")
close(m.receiveChan)
}
func (m MockConnection) ReceivePacket(p *types.PacketManifest) {
m.packetObserverChan <- true
}
func (m MockConnection) GetConnectionHash() types.ConnectionHash {
return m.clientFlow.ConnectionHash()
}
func (m MockConnection) GetLastSeen() time.Time {
return m.lastSeen
}
func (m MockConnection) SetPacketLogger(l types.PacketLogger) {
log.Print("MockConnection.SetPacketLogger")
}
type mockConnFactory struct {
}
func (m *mockConnFactory) Build(options ConnectionOptions) ConnectionInterface {
c := &MockConnection{
options: options,
packetObserverChan: make(chan bool, 0),
}
return c
}
type MockPacketLoggerFactory struct {
pcapNum int
pcapSize int
}
func (f MockPacketLoggerFactory) Build(flow *types.TcpIpFlow) types.PacketLogger {
return NewMockPacketLogger("str", flow, 10, 50)
}
type MockPacketLogger struct {
packetObserverChan chan bool
}
func NewMockPacketLogger(str string, flow *types.TcpIpFlow, pcapNum int, pcapSize int) types.PacketLogger {
m := MockPacketLogger{
packetObserverChan: make(chan bool, 0),
}
return types.PacketLogger(&m)
}
func (m *MockPacketLogger) WritePacket(rawPacket []byte, timestamp time.Time) {
log.Print("MockPacketLogger.WritePacket")
m.packetObserverChan <- true
}
func (m *MockPacketLogger) Start() {
log.Print("MockPacketLogger.Start")
}
func (m *MockPacketLogger) Stop() {
log.Print("MockPacketLogger.Stop")
}
func (m *MockPacketLogger) Archive() {
}
func (m *MockPacketLogger) Remove() {
}
func SetupTestInquisitor() (*Supervisor, PacketDispatcher, types.PacketSource) {
tcpIdleTimeout, _ := time.ParseDuration("10m")
dispatcherOptions := DispatcherOptions{
BufferedPerConnection: 10,
BufferedTotal: 100,
LogDir: ".",
LogPackets: true,
TcpIdleTimeout: tcpIdleTimeout,
MaxRingPackets: 40,
Logger: logging.NewAttackMetadataJsonLogger("archives"),
DetectHijack: true,
DetectInjection: true,
DetectCoalesceInjection: true,
MaxConcurrentConnections: 100,
}
wireDuration, _ := time.ParseDuration("3s")
snifferOptions := types.SnifferDriverOptions{
Device: "myInterface",
Filename: "",
WireDuration: wireDuration,
Snaplen: 65536,
Filter: "tcp",
}
factory := mockConnFactory{}
mockPacketLoggerFactory := MockPacketLoggerFactory{}
options := SupervisorOptions{
SnifferDriverOptions: &snifferOptions,
DispatcherOptions: dispatcherOptions,
SnifferFactory: NewMockSniffer,
ConnectionFactory: &factory,
PacketLoggerFactory: mockPacketLoggerFactory,
}
supervisor := NewSupervisor(options)
go supervisor.Run()
sniffer := supervisor.GetSniffer()
startedChan := sniffer.GetStartedChan()
dispatcher := supervisor.GetDispatcher()
<-startedChan
return supervisor, dispatcher, sniffer
}
func TestInquisitorForceQuit(t *testing.T) {
supervisor, _, _ := SetupTestInquisitor()
var sig os.Signal
supervisor.forceQuitChan <- sig
}
func TestInquisitorSourceStopped(t *testing.T) {
supervisor, _, sniffer := SetupTestInquisitor()
sniffer.Stop()
supervisor.Stopped()
}
func TestInquisitorSourceReceiveOne(t *testing.T) {
_, dispatcher, sniffer := SetupTestInquisitor()
ip := layers.IPv4{
SrcIP: net.IP{1, 2, 3, 4},
DstIP: net.IP{2, 3, 4, 5},
Version: 4,
TTL: 64,
Protocol: layers.IPProtocolTCP,
}
tcp := layers.TCP{
Seq: 3,
SYN: false,
SrcPort: 1,
DstPort: 2,
}
flow := types.NewTcpIpFlowFromLayers(ip, tcp)
p := types.PacketManifest{
Timestamp: time.Now(),
Flow: flow,
IP: ip,
TCP: tcp,
Payload: []byte{1, 2, 3, 4, 5, 6, 7},
}
log.Print("before receive packet")
dispatcher.ReceivePacket(&p)
log.Print("after receive packet")
connsChan := dispatcher.GetObservedConnectionsChan(1)
log.Print("fu1")
<-connsChan
log.Print("fu2")
log.Print("after connsChan receive")
conns := dispatcher.Connections()
log.Print("fu3")
if len(conns) != 1 {
t.Fatalf("number of connections %d is not 1", len(conns))
}
conn := conns[0]
mockConn := conn.(*MockConnection)
log.Print("listen to packet observer chan")
<-mockConn.packetObserverChan
sniffer.Stop()
}
func TestInquisitorResetTwice(t *testing.T) {
_, dispatcher, sniffer := SetupTestInquisitor()
startSeq := 3
ip1 := layers.IPv4{
SrcIP: net.IP{1, 2, 3, 4},
DstIP: net.IP{2, 3, 4, 5},
Version: 4,
TTL: 64,
Protocol: layers.IPProtocolTCP,
}
tcp1 := layers.TCP{
Seq: uint32(startSeq),
SYN: false,
RST: true,
SrcPort: 1,
DstPort: 2,
}
flow1 := types.NewTcpIpFlowFromLayers(ip1, tcp1)
packet1 := types.PacketManifest{
Timestamp: time.Now(),
Flow: flow1,
IP: ip1,
TCP: tcp1,
Payload: []byte{1, 2, 3, 4, 5, 6, 7},
}
ip2 := layers.IPv4{
SrcIP: net.IP{1, 2, 3, 4},
DstIP: net.IP{2, 3, 4, 5},
Version: 4,
TTL: 64,
Protocol: layers.IPProtocolTCP,
}
tcp2 := layers.TCP{
Seq: uint32(startSeq + len(packet1.Payload)),
SYN: false,
RST: true,
SrcPort: 1,
DstPort: 2,
}
flow2 := types.NewTcpIpFlowFromLayers(ip2, tcp2)
packet2 := types.PacketManifest{
Timestamp: time.Now(),
Flow: flow2,
IP: ip2,
TCP: tcp2,
Payload: []byte{1, 2, 3, 4, 5, 6, 7},
}
connsChan := dispatcher.GetObservedConnectionsChan(1)
dispatcher.ReceivePacket(&packet1)
<-connsChan
conns := dispatcher.Connections()
if len(conns) != 1 {
t.Fatalf("number of connections %d is not 1", len(conns))
}
conn := conns[0]
mockConn := conn.(*MockConnection)
<-mockConn.packetObserverChan
conns = dispatcher.Connections()
if len(conns) != 1 {
t.Fatalf("number of connections %d is not 1", len(conns))
}
conn = conns[0]
mockConn = conn.(*MockConnection)
dispatcher.ReceivePacket(&packet2)
<-mockConn.packetObserverChan
sniffer.Stop()
}
func SetupRealConnectionInquisitor() (*Supervisor, PacketDispatcher, types.PacketSource) {
tcpIdleTimeout, _ := time.ParseDuration("10m")
dispatcherOptions := DispatcherOptions{
BufferedPerConnection: 10,
BufferedTotal: 100,
LogDir: ".",
LogPackets: true,
TcpIdleTimeout: tcpIdleTimeout,
MaxRingPackets: 40,
Logger: logging.NewAttackMetadataJsonLogger("archives"),
DetectHijack: true,
DetectInjection: true,
DetectCoalesceInjection: true,
MaxConcurrentConnections: 100,
}
wireDuration, _ := time.ParseDuration("3s")
snifferOptions := types.SnifferDriverOptions{
Device: "myInterface",
Filename: "",
WireDuration: wireDuration,
Snaplen: 65536,
Filter: "tcp",
}
factory := &DefaultConnFactory{}
mockPacketLoggerFactory := MockPacketLoggerFactory{}
options := SupervisorOptions{
SnifferDriverOptions: &snifferOptions,
DispatcherOptions: dispatcherOptions,
SnifferFactory: NewMockSniffer,
ConnectionFactory: factory,
PacketLoggerFactory: mockPacketLoggerFactory,
}
supervisor := NewSupervisor(options)
go supervisor.Run()
sniffer := supervisor.GetSniffer()
startedChan := sniffer.GetStartedChan()
dispatcher := supervisor.GetDispatcher()
<-startedChan
return supervisor, dispatcher, sniffer
}