forked from kung-foo/freki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
freki.go
598 lines (505 loc) · 13 KB
/
freki.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package freki
import (
"context"
"fmt"
"net"
"runtime/debug"
"strings"
"sync"
"sync/atomic"
"time"
"golang.org/x/net/bpf"
"github.com/docker/engine-api/client"
"github.com/docker/engine-api/types"
"github.com/coreos/go-iptables/iptables"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/kung-foo/freki/netfilter"
"github.com/pkg/errors"
)
const table = "raw"
var chains = []string{"PREROUTING", "OUTPUT"}
func genRuleSpec(chain, iface, protocol, queuespec string) []string {
spec := "-p,%s,-j,NFQUEUE,--queue-num,%s"
if chain == "PREROUTING" {
spec = "-i,%s," + spec
}
if chain == "OUTPUT" {
spec = "-o,%s," + spec
}
return strings.Split(fmt.Sprintf(spec, iface, protocol, queuespec), ",")
}
type iptrule struct {
table string
chain string
rulespec []string
}
func (r *iptrule) Append(ipt *iptables.IPTables) error {
return ipt.AppendUnique(r.table, r.chain, r.rulespec...)
}
func (r *iptrule) Delete(ipt *iptables.IPTables) error {
return ipt.Delete(r.table, r.chain, r.rulespec...)
}
type ConnHandlerFunc func(conn net.Conn, md *Metadata) error
type Processor struct {
rules []*Rule
ipt *iptables.IPTables
iptRules []iptrule
nfq *netfilter.Queue
cleanupOnce sync.Once
Connections *connTable
packetsProcessed uint64
shutdown chan struct{}
publicAddrs []net.IP
iface *pcap.Handle
servers map[string]Server
connHandlers map[string]ConnHandlerFunc
}
func New(ifaceName string, rules []*Rule, log Logger) (*Processor, error) {
iface, err := pcap.OpenLive(ifaceName, 1, false, time.Second)
if err != nil {
return nil, err
}
for idx, rule := range rules {
err = initRule(idx, rule, iface)
if err != nil {
return nil, errors.Wrap(err, rule.String())
}
}
nonLoopbackAddrs, err := getNonLoopbackIPs(ifaceName)
if err != nil {
return nil, err
}
// TODO: in 2.0, remove logger parameter
if log != nil {
logger = log
}
processor := &Processor{
rules: rules,
iptRules: make([]iptrule, 0),
Connections: newConnTable(),
shutdown: make(chan struct{}),
publicAddrs: nonLoopbackAddrs,
iface: iface,
servers: make(map[string]Server, 0),
connHandlers: make(map[string]ConnHandlerFunc, 0),
}
// TODO: customize protocols
for _, chain := range chains {
r := iptrule{
table: table,
chain: chain,
rulespec: genRuleSpec(chain, ifaceName, "tcp", "0"),
}
processor.iptRules = append(processor.iptRules, r)
}
return processor, nil
}
func (p *Processor) AddServer(s Server) {
p.servers[s.Type()] = s
}
func (p *Processor) RegisterConnHandler(target string, handler ConnHandlerFunc) error {
if _, ok := p.connHandlers[target]; ok {
return fmt.Errorf("conn handler already registered for %s", target)
}
p.connHandlers[target] = handler
return nil
}
func (p *Processor) initIPTables() (err error) {
for _, rule := range p.iptRules {
err = rule.Append(p.ipt)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("%+v", rule))
}
}
return
}
func (p *Processor) resetIPTables() (err error) {
for _, rule := range p.iptRules {
err = rule.Delete(p.ipt)
if err != nil {
logger.Errorf("[freki ] error deleting: %+v", rule)
}
}
return
}
func (p *Processor) Init() (err error) {
for _, rule := range p.rules {
if rule.ruleType == ProxyTCP {
if rule.targetURL.Scheme == "docker" {
logger.Debugf("[freki ] Creating Docker client with version: %v", client.DefaultVersion)
var cli *client.Client
cli, err = client.NewEnvClient()
if err != nil {
return err
}
var containers []types.Container
containers, err = cli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
return err
}
found := false
for _, container := range containers {
name := container.Names[0][1:]
if name == rule.host {
// TODO: find correct network
addr := container.NetworkSettings.Networks["bridge"].IPAddress
logger.Debugf("[freki ] mapping docker://%s:%d to tcp://%s:%d", rule.host, rule.port, addr, rule.port)
rule.targetURL.Host = fmt.Sprintf("tcp://%s:%d", addr, rule.port)
rule.host = addr
found = true
}
}
if !found {
return fmt.Errorf("unabled to find a container named: %s", rule.host)
}
}
}
}
p.ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv4)
if err != nil {
return
}
// quick permissions test
_, err = p.ipt.List(table, chains[0])
if err != nil {
return
}
// TODO: check for conflicting rules
err = p.initIPTables()
if err != nil {
return
}
for _, chain := range chains {
filters, _ := p.ipt.List(table, chain)
logger.Debugf("[freki ] %s %s %+v", table, chain, filters)
}
// TODO: set sane defaults
p.nfq, err = netfilter.New(0, 100, netfilter.NF_DEFAULT_PACKET_SIZE)
if err != nil {
return
}
for _, server := range p.servers {
go func(s Server) {
logger.Infof("[freki ] starting %s on %d", s.Type(), s.Port())
err := s.Start(p)
if err != nil {
logger.Errorf("[freki ] %v", err)
}
}(server)
}
return
}
func (p *Processor) PacketsProcessed() uint64 {
return atomic.LoadUint64(&p.packetsProcessed)
}
func (p *Processor) Shutdown() (err error) {
p.cleanupOnce.Do(func() {
close(p.shutdown)
// TODO: how to drain?
err = p.cleanup()
})
return
}
func (p *Processor) cleanup() (err error) {
logger.Debug("[freki ] Processor:cleanup()")
p.resetIPTables()
if p.nfq != nil {
p.nfq.Close()
}
for _, chain := range chains {
filters, _ := p.ipt.List(table, chain)
logger.Debugf("[freki ] %s %s %+v", table, chain, filters)
}
if p.iface != nil {
// TODO: does this really need to stay open the whole time?
p.iface.Close()
}
for _, server := range p.servers {
err = server.Shutdown()
if err != nil {
logger.Errorf("[freki ] %v", err)
err = nil
}
}
return
}
func (p *Processor) Start() (err error) {
logger.Infof("[freki ] starting freki on %v", p.publicAddrs)
go func() {
ticker := time.NewTicker(time.Second * 5)
for {
select {
case <-ticker.C:
p.Connections.FlushOlderThan(time.Second * 60)
case <-p.shutdown:
ticker.Stop()
return
}
}
}()
// TODO: discover how "Run" returns
go func() {
defer func() {
if r := recover(); r != nil {
logger.Errorf("[freki ] panic: %+v", r)
logger.Errorf("[freki ] stacktrace:\n%v", string(debug.Stack()))
p.shutdown <- struct{}{}
}
}()
p.nfq.Run()
}()
return p.loop()
}
func (p *Processor) loop() (err error) {
for {
select {
case raw := <-p.nfq.Packets():
err = p.onPacket(raw)
if err != nil {
return
}
case <-p.shutdown:
return
}
}
}
func (p *Processor) GetPublicAddresses() []net.IP {
return p.publicAddrs
}
func (p *Processor) isIPNonLoopback(ip *net.IP) bool {
for _, addr := range p.publicAddrs {
if ip.Equal(addr) {
return true
}
}
return false
}
func (p *Processor) mangle(
rawPacket *netfilter.RawPacket,
packet gopacket.Packet,
ip *layers.IPv4,
tcp *layers.TCP,
body *gopacket.Payload) error {
var err error
var buffer gopacket.SerializeBuffer
if p.isIPNonLoopback(&ip.SrcIP) {
// packets back to client
ck := NewConnKeyByEndpoints(ip.NetworkFlow().Dst(), tcp.TransportFlow().Dst())
md := p.Connections.GetByFlow(ck)
if md == nil {
// not tracking
goto accept
}
switch md.Rule.ruleType {
case Rewrite, LogTCP, LogHTTP, ProxyTCP, UserConnHandler:
tcp.SrcPort = layers.TCPPort(md.TargetPort)
goto modified
case Drop:
goto drop
case PassThrough:
goto accept
default:
logger.Errorf("[freki ] rule not implmented: %+v", md.Rule)
}
} else {
// packets to honeypots
ck := NewConnKeyByEndpoints(ip.NetworkFlow().Src(), tcp.TransportFlow().Src())
md := p.Connections.GetByFlow(ck)
if md == nil {
// not tracking
goto accept
}
var s Server
var ok bool
switch md.Rule.ruleType {
case Rewrite:
tcp.DstPort = layers.TCPPort(md.Rule.port)
goto modified
case LogTCP:
// TODO: optimize?
if s, ok = p.servers["log.tcp"]; !ok {
return fmt.Errorf("No TCPLogger installed")
}
tcp.DstPort = layers.TCPPort(s.Port())
goto modified
case LogHTTP:
// TODO: optimize?
if s, ok = p.servers["log.http"]; !ok {
return fmt.Errorf("No HTTPLogger installed")
}
tcp.DstPort = layers.TCPPort(s.Port())
goto modified
case ProxyTCP:
// TODO: optimize?
if s, ok = p.servers["proxy.tcp"]; !ok {
return fmt.Errorf("No TCPProxy installed")
}
tcp.DstPort = layers.TCPPort(s.Port())
goto modified
case UserConnHandler:
if s, ok = p.servers["user.tcp"]; !ok {
return fmt.Errorf("No ConnHandler installed")
}
tcp.DstPort = layers.TCPPort(s.Port())
goto modified
case Drop:
goto drop
case PassThrough:
goto accept
default:
logger.Errorf("[freki ] rule not implmented: %+v", md.Rule)
}
}
// default
goto accept
modified:
tcp.SetNetworkLayerForChecksum(ip)
buffer = gopacket.NewSerializeBuffer()
err = gopacket.SerializeLayers(
buffer,
gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true},
ip, tcp, body,
)
if err != nil {
// TODO: should return a verdict?
return err
}
return p.nfq.SetVerdictModifed(rawPacket, buffer.Bytes(), netfilter.NF_ACCEPT)
accept:
return p.nfq.SetVerdict(rawPacket, netfilter.NF_ACCEPT)
drop:
return p.nfq.SetVerdict(rawPacket, netfilter.NF_DROP)
}
var ethHdr = []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00,
}
func (p *Processor) onPacket(rawPacket *netfilter.RawPacket) (err error) {
// TODO: remove defer
defer func() {
atomic.AddUint64(&p.packetsProcessed, 1)
}()
// OK, so this mess is because I want to use libpcap's BPF compiler which
// emits instructions that expect an etherneet header. But, NFQUEUE only
// emits IP and down. So I need to append a fake ethernet header. Ideally
// I would have a BPF progam that could operate on the IP packet itself.
buffer := append(ethHdr, rawPacket.Data...)
// TODO: set DecodeOptions
packet := gopacket.NewPacket(
buffer,
layers.LayerTypeEthernet,
gopacket.DecodeOptions{Lazy: false, NoCopy: true},
)
// spew.Dump(packet)
var (
eth layers.Ethernet
ip layers.IPv4
tcp layers.TCP
udp layers.UDP
icmp layers.ICMPv4
body gopacket.Payload
)
parser := gopacket.NewDecodingLayerParser(
layers.LayerTypeEthernet,
ð,
&ip,
&tcp,
&udp,
&icmp,
&body)
var foundLayerTypes []gopacket.LayerType
err = parser.DecodeLayers(packet.Data(), &foundLayerTypes)
if err != nil {
logger.Errorf("[freki ] %v %v", err, foundLayerTypes)
goto accept
}
for _, layer := range foundLayerTypes {
switch layer {
case layers.LayerTypeTCP:
// TODO: validate logic
if tcp.SYN && !tcp.ACK {
var rule *Rule
srcIP := ip.NetworkFlow().Src()
srcPort := tcp.TransportFlow().Src()
logger.Debugf("[freki ] new connection %s:%s->%d", srcIP.String(), srcPort.String(), tcp.DstPort)
rule, err = p.applyRules(packet)
if err != nil {
logger.Errorf("[freki ] %v", err)
goto accept
}
if rule == nil {
// TODO: is this the correct default?
goto accept
}
// FYI: when i don't respond to a SYN, then a duplicate SYN is sent
ck := NewConnKeyByEndpoints(srcIP, srcPort)
p.Connections.Register(ck, rule, srcIP.String(), srcPort.String(), tcp.DstPort)
}
err = p.mangle(rawPacket, packet, &ip, &tcp, &body)
return
}
}
accept:
return p.nfq.SetVerdict(rawPacket, netfilter.NF_ACCEPT)
}
func (p *Processor) applyRules(packet gopacket.Packet) (*Rule, error) {
/*
if len(p.rules) == 0 {
return nil, fmt.Errorf("no rules found")
}
*/
for _, rule := range p.rules {
if rule.matcher != nil {
v, err := rule.matcher.Run(packet.Data())
if err != nil {
return nil, err
}
if v == 1 {
return rule, nil
}
}
}
return nil, nil
}
func getNonLoopbackIPs(ifaceName string) ([]net.IP, error) {
nonLoopback := []net.IP{}
ifs, err := pcap.FindAllDevs()
if err != nil {
return nonLoopback, errors.Wrap(err, "pcap.FindAllDevs()")
}
for _, iface := range ifs {
if strings.EqualFold(iface.Name, ifaceName) {
for _, addr := range iface.Addresses {
logger.Debugf("[freki ] device: %s, addr: %s, isLoopback: %v, isIPv4: %v", ifaceName, addr.IP.String(), addr.IP.IsLoopback(), addr.IP.To4() != nil)
if !addr.IP.IsLoopback() && addr.IP.To4() != nil {
nonLoopback = append(nonLoopback, addr.IP)
}
}
}
}
if len(nonLoopback) == 0 {
return nonLoopback, fmt.Errorf("unable to find any non-loopback addresses for: %s", ifaceName)
}
return nonLoopback, nil
}
func pcapBPFToXNetBPF(pcapbpf []pcap.BPFInstruction) *bpf.VM {
raw := make([]bpf.RawInstruction, len(pcapbpf))
for i, ins := range pcapbpf {
raw[i] = bpf.RawInstruction{
Op: ins.Code,
Jt: ins.Jt,
Jf: ins.Jf,
K: ins.K,
}
}
filter, _ := bpf.Disassemble(raw)
vm, err := bpf.NewVM(filter)
if err != nil {
// TODO: return error
logger.Error(err)
}
return vm
}