-
Notifications
You must be signed in to change notification settings - Fork 39
/
pcap_integration_test.go
139 lines (117 loc) · 3.07 KB
/
pcap_integration_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
package HoneyBadger
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/david415/HoneyBadger/types"
)
type DummyPacketLogger struct {
}
func NewDummyPacketLogger(str string, flow *types.TcpIpFlow, pcapNum int, pcapSize int) types.PacketLogger {
m := DummyPacketLogger{}
return types.PacketLogger(&m)
}
type DummyPacketLoggerFactory struct {
}
func (f DummyPacketLoggerFactory) Build(flow *types.TcpIpFlow) types.PacketLogger {
return NewDummyPacketLogger("", flow, 10, 100)
}
func (m *DummyPacketLogger) WritePacket(rawPacket []byte, timestamp time.Time) {
}
func (m DummyPacketLogger) SetFileWriter(writer io.WriteCloser) {
}
func (m DummyPacketLogger) Start() {
}
func (m DummyPacketLogger) Stop() {
}
func (m DummyPacketLogger) Archive() {
}
func (m DummyPacketLogger) Remove() {
}
type TestLogger struct {
eventList []types.Event
count int
}
func NewTestLogger() TestLogger {
return TestLogger{}
}
func (t *TestLogger) Log(event *types.Event) {
t.count += 1
}
func (t *TestLogger) Archive() {
}
func SetupAttackDetectionPcapInquisitor(pcapPath string, attackLogger *TestLogger) {
tcpIdleTimeout, _ := time.ParseDuration("10m")
dispatcherOptions := DispatcherOptions{
BufferedPerConnection: 10,
BufferedTotal: 100,
LogDir: "",
LogPackets: true,
TcpIdleTimeout: tcpIdleTimeout,
MaxRingPackets: 40,
Logger: types.Logger(attackLogger),
DetectHijack: true,
DetectInjection: true,
DetectCoalesceInjection: true,
MaxConcurrentConnections: 100,
}
wireDuration, _ := time.ParseDuration("3s")
snifferOptions := types.SnifferDriverOptions{
DAQ: "libpcap",
Device: "",
Filename: pcapPath,
WireDuration: wireDuration,
Snaplen: 65536,
Filter: "tcp",
}
factory := &DefaultConnFactory{}
dummyPacketLoggerFactory := DummyPacketLoggerFactory{}
options := SupervisorOptions{
SnifferDriverOptions: &snifferOptions,
DispatcherOptions: dispatcherOptions,
SnifferFactory: NewSniffer,
ConnectionFactory: factory,
PacketLoggerFactory: dummyPacketLoggerFactory,
}
supervisor := NewSupervisor(options)
supervisor.Run()
return
}
func PcapIsDetectInjection(pcapPath string) bool {
logger := NewTestLogger()
SetupAttackDetectionPcapInquisitor(pcapPath, &logger)
if logger.count == 0 {
return false
} else {
return true
}
}
func TestAllPcapFiles(t *testing.T) {
root := "pcap_archive/"
absPathSymLink, err := filepath.Abs(root)
if err != nil {
panic(err)
}
var path string
path, err = filepath.EvalSymlinks(absPathSymLink)
if err != nil {
t.Skip("skipping test because pcap_archive symlink is missing.")
}
walkpath := func(path string, f os.FileInfo, err error) error {
if strings.HasSuffix(path, ".pcap") {
fmt.Printf("HoneyBadger integration test with: %s\n", path)
if !PcapIsDetectInjection(path) {
t.Fatalf("No injection attack detected in pcap file: %s\n", path)
}
}
return nil
}
err = filepath.Walk(path, walkpath)
if err != nil {
panic(err)
}
}