-
Notifications
You must be signed in to change notification settings - Fork 39
/
dispatcher.go
258 lines (233 loc) · 7.81 KB
/
dispatcher.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
/*
* HoneyBadger core library for detecting TCP injection attacks
*
* Copyright (C) 2014, 2015 David Stainton
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package HoneyBadger
import (
"log"
"time"
"github.com/google/gopacket/layers"
"github.com/david415/HoneyBadger/types"
)
type TimedRawPacket struct {
Timestamp time.Time
RawPacket []byte
}
// InquisitorOptions are user set parameters for specifying the
// details of how to proceed with honey_bager's TCP connection monitoring.
// More parameters should soon be added here!
type DispatcherOptions struct {
BufferedPerConnection int
BufferedTotal int
LogDir string
LogPackets bool
MaxPcapLogRotations int
MaxPcapLogSize int
TcpIdleTimeout time.Duration
MaxRingPackets int
Logger types.Logger
DetectHijack bool
DetectInjection bool
DetectCoalesceInjection bool
MaxConcurrentConnections int
}
// Inquisitor sets up the connection pool and is an abstraction layer for dealing
// with incoming packets weather they be from a pcap file or directly off the wire.
type Dispatcher struct {
options DispatcherOptions
connectionFactory ConnectionFactory
observeConnectionCount int
observeConnectionChan chan bool
dispatchPacketChan chan *types.PacketManifest
stopDispatchChan chan bool
closeConnectionChan chan ConnectionInterface
pageCache *pageCache
PacketLoggerFactory types.PacketLoggerFactory
poolTcpIpv4 map[types.HashedTcpIpv4Flow]ConnectionInterface
poolTcpIpv6 map[types.HashedTcpIpv6Flow]ConnectionInterface
}
// NewInquisitor creates a new Inquisitor struct
func NewDispatcher(options DispatcherOptions, connectionFactory ConnectionFactory, packetLoggerFactory types.PacketLoggerFactory) *Dispatcher {
i := Dispatcher{
PacketLoggerFactory: packetLoggerFactory,
connectionFactory: connectionFactory,
options: options,
dispatchPacketChan: make(chan *types.PacketManifest),
stopDispatchChan: make(chan bool),
closeConnectionChan: make(chan ConnectionInterface),
pageCache: newPageCache(),
observeConnectionChan: make(chan bool, 0),
poolTcpIpv4: make(map[types.HashedTcpIpv4Flow]ConnectionInterface),
poolTcpIpv6: make(map[types.HashedTcpIpv6Flow]ConnectionInterface),
}
return &i
}
func (i *Dispatcher) GetObservedConnectionsChan(count int) chan bool {
i.observeConnectionCount = count
return i.observeConnectionChan
}
// Start... starts the TCP attack inquisition!
func (i *Dispatcher) Start() {
go i.dispatchPackets()
}
// Stop... stops the TCP attack inquisition!
func (i *Dispatcher) Stop() {
i.stopDispatchChan <- true
closedConns := i.CloseAllConnections()
log.Printf("%d connection(s) closed.", closedConns)
}
// connectionsLocked returns a slice of Connection pointers.
func (i *Dispatcher) Connections() []ConnectionInterface {
return i.connections()
}
func (i *Dispatcher) connections() []ConnectionInterface {
conns := make([]ConnectionInterface, 0, len(i.poolTcpIpv4) + len(i.poolTcpIpv6))
for _, conn := range i.poolTcpIpv4 {
conns = append(conns, conn)
}
for _, conn := range i.poolTcpIpv6 {
conns = append(conns, conn)
}
return conns
}
func (i *Dispatcher) ReceivePacket(p *types.PacketManifest) {
i.dispatchPacketChan <- p
}
// CloseOlderThan takes a Time argument and closes all the connections
// that have not received packet since that specified time
func (i *Dispatcher) CloseOlderThan(t time.Time) int {
conns := i.connections()
if conns == nil {
return 0
}
closeList := make([]ConnectionInterface,0)
for _, conn := range conns {
lastSeen := conn.GetLastSeen()
if lastSeen.Equal(t) || lastSeen.Before(t) {
conns = append(conns, conn)
}
}
return i.closeConnectionList(closeList)
}
// CloseAllConnections closes all connections in the pool.
func (i *Dispatcher) CloseAllConnections() int {
conns := i.connections()
if conns == nil {
return 0
}
return i.closeConnectionList(conns)
}
func (i *Dispatcher) closeConnectionList(conns []ConnectionInterface) int {
count := 0
for _, conn := range conns {
tcpip_flow := conn.GetClientFlow()
netFlow, _ := tcpip_flow.Flows()
eType := netFlow.EndpointType()
if eType == layers.EndpointIPv4 {
delete(i.poolTcpIpv4, types.NewHashedTcpIpv4Flow(tcpip_flow))
count += 1
} else if eType == layers.EndpointIPv6 {
delete(i.poolTcpIpv6, types.NewHashedTcpIpv6Flow(tcpip_flow))
count += 1
} else {
panic("wtf")
}
conn.Close()
}
return count
}
func (i *Dispatcher) setupNewConnection(flow *types.TcpIpFlow) ConnectionInterface {
options := ConnectionOptions{
MaxBufferedPagesTotal: i.options.BufferedTotal,
MaxBufferedPagesPerConnection: i.options.BufferedPerConnection,
MaxRingPackets: i.options.MaxRingPackets,
PageCache: i.pageCache,
LogDir: i.options.LogDir,
AttackLogger: i.options.Logger,
LogPackets: i.options.LogPackets,
DetectHijack: i.options.DetectHijack,
DetectInjection: i.options.DetectInjection,
DetectCoalesceInjection: i.options.DetectCoalesceInjection,
}
conn := i.connectionFactory.Build(options)
if i.options.LogPackets {
packetLogger := i.PacketLoggerFactory.Build(flow)
conn.SetPacketLogger(packetLogger)
packetLogger.Start()
}
ipFlow, _ := flow.Flows()
eType := ipFlow.EndpointType()
if eType == layers.EndpointIPv4 {
i.poolTcpIpv4[types.NewHashedTcpIpv4Flow(flow)] = conn
} else if eType == layers.EndpointIPv6 {
i.poolTcpIpv6[types.NewHashedTcpIpv6Flow(flow)] = conn
} else {
panic("wtf")
}
if i.observeConnectionCount != 0 && i.observeConnectionCount == len(i.connections()) {
i.observeConnectionChan <- true
}
return conn
}
func (i *Dispatcher) dispatchPackets() {
var conn ConnectionInterface
timeout := i.options.TcpIdleTimeout
ticker := time.Tick(timeout)
for {
select {
case <-ticker:
closed := i.CloseOlderThan(time.Now().Add(timeout * -1))
if closed != 0 {
log.Printf("timeout closed %d connections\n", closed)
}
case <-i.stopDispatchChan:
return
case packetManifest := <-i.dispatchPacketChan:
ipFlow, _ := packetManifest.Flow.Flows()
eType := ipFlow.EndpointType()
if eType == layers.EndpointIPv4 {
_, ok := i.poolTcpIpv4[types.NewHashedTcpIpv4Flow(packetManifest.Flow)]
if ok {
conn = i.poolTcpIpv4[types.NewHashedTcpIpv4Flow(packetManifest.Flow)]
} else {
if i.options.MaxConcurrentConnections != 0 {
if len(i.poolTcpIpv4) >= i.options.MaxConcurrentConnections {
continue
}
}
conn = i.setupNewConnection(packetManifest.Flow)
}
} else if eType == layers.EndpointIPv6 {
connectionHash := types.NewHashedTcpIpv6Flow(packetManifest.Flow)
_, ok := i.poolTcpIpv6[connectionHash]
if ok {
conn = i.poolTcpIpv6[connectionHash]
} else {
if i.options.MaxConcurrentConnections != 0 {
if len(i.poolTcpIpv6) >= i.options.MaxConcurrentConnections {
continue
}
}
conn = i.setupNewConnection(packetManifest.Flow)
}
} else {
panic("wtf")
}
conn.ReceivePacket(packetManifest)
}
}
}