-
Notifications
You must be signed in to change notification settings - Fork 1
/
dpiblock.go
605 lines (508 loc) · 15.9 KB
/
dpiblock.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
599
600
601
602
603
604
605
package netem
//
// DPI: rules to block flows
//
import (
"bytes"
"net"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/miekg/dns"
)
// DPIResetTrafficForTLSSNI is a [DPIRule] that spoofs a RST TCP segment
// after it sees a given TLS SNI. The zero value is invalid; please, fill
// all the fields marked as MANDATORY.
//
// Note: this rule assumes that there is a router in the path that
// can generate a spoofed RST segment. If there is no router in the
// path, no RST segment will ever be generated.
//
// Note: this rule relies on a race condition. For consistent results
// you MUST set some delay in the router<->server link.
type DPIResetTrafficForTLSSNI struct {
// Logger is the MANDATORY logger.
Logger Logger
// SNI is the MANDATORY offending SNI.
SNI string
}
var _ DPIRule = &DPIResetTrafficForTLSSNI{}
// Filter implements DPIRule
func (r *DPIResetTrafficForTLSSNI) Filter(
direction DPIDirection, packet *DissectedPacket) (*DPIPolicy, bool) {
// short circuit for the return path
if direction != DPIDirectionClientToServer {
return nil, false
}
// short circuit for UDP packets
if packet.TransportProtocol() != layers.IPProtocolTCP {
return nil, false
}
// short circuit in case of misconfiguration
if r.SNI == "" {
return nil, false
}
// try to obtain the SNI
sni, err := packet.parseTLSServerName()
if err != nil {
return nil, false
}
// if the packet is not offending, accept it
if sni != r.SNI {
return nil, false
}
// generate the frame to spoof
spoofed, err := reflectDissectedTCPSegmentWithRSTFlag(packet)
if err != nil {
return nil, false
}
// tell the user we're asking the router to RST the flow.
r.Logger.Infof(
"netem: dpi: asking to send RST to flow %s:%d %s:%d/%s because SNI==%s",
packet.SourceIPAddress(),
packet.SourcePort(),
packet.DestinationIPAddress(),
packet.DestinationPort(),
packet.TransportProtocol(),
sni,
)
// make sure the router knows it should spoof
policy := &DPIPolicy{
Delay: 0,
Flags: FrameFlagSpoof,
PLR: 0,
Spoofed: [][]byte{spoofed},
}
return policy, true
}
// DPIResetTrafficForString is a [DPIRule] that spoofs a RST TCP segment
// after it sees a given string in the payload for a given offending server
// endpoint. The zero value is invalid; please, fill all the fields
// marked as MANDATORY.
//
// Note: this rule assumes that there is a router in the path that
// can generate a spoofed RST segment. If there is no router in the
// path, no RST segment will ever be generated.
//
// Note: this rule relies on a race condition. For consistent results
// you MUST set some delay in the router<->server link.
type DPIResetTrafficForString struct {
// Logger is the MANDATORY logger.
Logger Logger
// ServerIPAddress is the MANDATORY server endpoint IP address.
ServerIPAddress string
// ServerPort is the MANDATORY server endpoint port.
ServerPort uint16
// String is the MANDATORY offending string.
String string
}
var _ DPIRule = &DPIResetTrafficForString{}
// Filter implements DPIRule
func (r *DPIResetTrafficForString) Filter(
direction DPIDirection, packet *DissectedPacket) (*DPIPolicy, bool) {
// short circuit for the return path
if direction != DPIDirectionClientToServer {
return nil, false
}
// short circuit for UDP packets
if packet.TransportProtocol() != layers.IPProtocolTCP {
return nil, false
}
// make sure the remote server is filtered
if !packet.MatchesDestination(layers.IPProtocolTCP, r.ServerIPAddress, r.ServerPort) {
return nil, false
}
// short circuit in case of misconfiguration
if r.String == "" {
return nil, false
}
// if the packet is not offending, accept it
if !bytes.Contains(packet.TCP.Payload, []byte(r.String)) {
return nil, false
}
// generate the frame to spoof
spoofed, err := reflectDissectedTCPSegmentWithRSTFlag(packet)
if err != nil {
return nil, false
}
// tell the user we're asking the router to RST the flow.
r.Logger.Infof(
"netem: dpi: asking to send RST to flow %s:%d %s:%d/%s because it contains %s",
packet.SourceIPAddress(),
packet.SourcePort(),
packet.DestinationIPAddress(),
packet.DestinationPort(),
packet.TransportProtocol(),
r.String,
)
// make sure the router knows it should spoof
policy := &DPIPolicy{
Delay: 0,
Flags: FrameFlagSpoof,
PLR: 0,
Spoofed: [][]byte{spoofed},
}
return policy, true
}
// DPISpoofDNSResponse is a [DPIRule] that spoofs a DNS response after it
// sees a given DNS request. The zero value is invalid; please, fill all
// the fields marked as MANDATORY.
//
// Note: this rule assumes that there is a router in the path that
// can generate a spoofed RST segment. If there is no router in the
// path, no RST segment will ever be generated.
//
// Note: this rule relies on a race condition. For consistent results
// you MUST set some delay in the router<->server link.
type DPISpoofDNSResponse struct {
// Addresses contains the OPTIONAL addresses to include
// in the spoofed response. If this field is empty, we
// will return a NXDOMAIN response to the user.
Addresses []string
// Logger is the MANDATORY logger.
Logger Logger
// Domain is the MANDATORY offending SNI.
Domain string
}
var _ DPIRule = &DPISpoofDNSResponse{}
// Filter implements DPIRule
func (r *DPISpoofDNSResponse) Filter(
direction DPIDirection, packet *DissectedPacket) (*DPIPolicy, bool) {
// short circuit for the return path
if direction != DPIDirectionClientToServer {
return nil, false
}
// short circuit for TCP packets
if packet.TransportProtocol() != layers.IPProtocolUDP {
return nil, false
}
// short circuit for non-DNS traffic
if packet.DestinationPort() != 53 {
return nil, false
}
// short circuit in case of misconfiguration
if r.Domain == "" {
return nil, false
}
// try to parse the DNS request
request := &dns.Msg{}
if err := request.Unpack(packet.UDP.Payload); err != nil {
return nil, false
}
// if the packet is not offending, accept it
if len(request.Question) != 1 {
return nil, false
}
question := request.Question[0]
if question.Name != dns.CanonicalName(r.Domain) {
return nil, false
}
// create a DNS record for preparing a response
dnsRecord := &DNSRecord{
A: []net.IP{},
CNAME: "",
}
for _, addr := range r.Addresses {
if ip := net.ParseIP(addr); ip != nil {
dnsRecord.A = append(dnsRecord.A, ip)
}
}
// generate raw DNS response
rawResponse, err := dnsServerNewResponse(request, question, len(dnsRecord.A) > 0, dnsRecord)
if err != nil {
return nil, false
}
// generate the frame to spoof
spoofed, err := reflectDissectedUDPDatagramWithPayload(packet, rawResponse)
if err != nil {
return nil, false
}
// make sure the router knows it should spoof
policy := &DPIPolicy{
Delay: 0,
Flags: FrameFlagSpoof,
PLR: 0,
Spoofed: [][]byte{spoofed},
}
// tell the user we're asking the router to spoof a response
r.Logger.Infof(
"netem: dpi: asking to spoof DNS reply for flow %s:%d %s:%d/%s because domain==%s",
packet.SourceIPAddress(),
packet.SourcePort(),
packet.DestinationIPAddress(),
packet.DestinationPort(),
packet.TransportProtocol(),
question.Name,
)
return policy, true
}
// DPICloseConnectionForTLSSNI is a [DPIRule] that spoofs a FIN|ACK TCP segment
// after it sees a given TLS SNI. The zero value is invalid; please, fill
// all the fields marked as MANDATORY.
//
// Note: this rule assumes that there is a router in the path that
// can generate a spoofed RST segment. If there is no router in the
// path, no RST segment will ever be generated.
//
// Note: this rule relies on a race condition. For consistent results
// you MUST set some delay in the router<->server link.
type DPICloseConnectionForTLSSNI struct {
// Logger is the MANDATORY logger.
Logger Logger
// SNI is the MANDATORY offending SNI.
SNI string
}
var _ DPIRule = &DPICloseConnectionForTLSSNI{}
// Filter implements DPIRule
func (r *DPICloseConnectionForTLSSNI) Filter(
direction DPIDirection, packet *DissectedPacket) (*DPIPolicy, bool) {
// short circuit for the return path
if direction != DPIDirectionClientToServer {
return nil, false
}
// short circuit for UDP packets
if packet.TransportProtocol() != layers.IPProtocolTCP {
return nil, false
}
// short circuit in case of misconfiguration
if r.SNI == "" {
return nil, false
}
// try to obtain the SNI
sni, err := packet.parseTLSServerName()
if err != nil {
return nil, false
}
// if the packet is not offending, accept it
if sni != r.SNI {
return nil, false
}
// generate the frame to spoof
spoofed, err := reflectDissectedTCPSegmentWithFINACKFlag(packet)
if err != nil {
return nil, false
}
// tell the user we're asking the router to FIN|ACK the flow.
r.Logger.Infof(
"netem: dpi: asking to send FIN|ACK to flow %s:%d %s:%d/%s because SNI==%s",
packet.SourceIPAddress(),
packet.SourcePort(),
packet.DestinationIPAddress(),
packet.DestinationPort(),
packet.TransportProtocol(),
sni,
)
// make sure the router knows it should spoof
policy := &DPIPolicy{
Delay: 0,
Flags: FrameFlagSpoof,
PLR: 0,
Spoofed: [][]byte{spoofed},
}
return policy, true
}
// DPICloseConnectionForServerEndpoint is a [DPIRule] that spoofs a FIN|ACK TCP segment
// after it sees a given TCP connect attempt. The zero value is invalid; please, fill
// all the fields marked as MANDATORY.
//
// Note: this rule assumes that there is a router in the path that
// can generate a spoofed RST segment. If there is no router in the
// path, no RST segment will ever be generated.
//
// Note: this rule relies on a race condition. For consistent results
// you MUST set some delay in the router<->server link.
type DPICloseConnectionForServerEndpoint struct {
// Logger is the MANDATORY logger.
Logger Logger
// ServerIPAddress is the MANDATORY server endpoint IP address.
ServerIPAddress string
// ServerPort is the MANDATORY server endpoint port.
ServerPort uint16
}
var _ DPIRule = &DPICloseConnectionForServerEndpoint{}
// Filter implements DPIRule
func (r *DPICloseConnectionForServerEndpoint) Filter(
direction DPIDirection, packet *DissectedPacket) (*DPIPolicy, bool) {
// make sure the packet is TCP and for the proper endpoint
if !packet.MatchesDestination(layers.IPProtocolTCP, r.ServerIPAddress, r.ServerPort) {
return nil, false
}
// generate the frame to spoof
spoofed, err := reflectDissectedTCPSegmentWithSetter(packet, func(tcp *layers.TCP) {
tcp.RST = true
tcp.ACK = true
})
// make sure we could spoof the packet
if err != nil {
return nil, false
}
// tell the user we're asking the router to RST|ACK the flow.
r.Logger.Infof(
"netem: dpi: asking to send RST|ACK to flow %s:%d %s:%d/%s because it is filtered",
packet.SourceIPAddress(),
packet.SourcePort(),
packet.DestinationIPAddress(),
packet.DestinationPort(),
packet.TransportProtocol(),
)
// make sure the router knows it should spoof
policy := &DPIPolicy{
Delay: 0,
Flags: FrameFlagSpoof,
PLR: 0,
Spoofed: [][]byte{spoofed},
}
return policy, true
}
// DPICloseConnectionForString is a [DPIRule] that spoofs a FIN|ACK TCP segment
// after it sees a given string in the payload. The zero value is invalid; please, fill
// all the fields marked as MANDATORY.
//
// Note: this rule assumes that there is a router in the path that
// can generate a spoofed RST segment. If there is no router in the
// path, no RST segment will ever be generated.
//
// Note: this rule relies on a race condition. For consistent results
// you MUST set some delay in the router<->server link.
type DPICloseConnectionForString struct {
// Logger is the MANDATORY logger.
Logger Logger
// ServerIPAddress is the MANDATORY server endpoint IP address.
ServerIPAddress string
// ServerPort is the MANDATORY server endpoint port.
ServerPort uint16
// SNI is the MANDATORY offending string.
String string
}
var _ DPIRule = &DPICloseConnectionForString{}
// Filter implements DPIRule
func (r *DPICloseConnectionForString) Filter(
direction DPIDirection, packet *DissectedPacket) (*DPIPolicy, bool) {
// short circuit for the return path
if direction != DPIDirectionClientToServer {
return nil, false
}
// short circuit for UDP packets
if packet.TransportProtocol() != layers.IPProtocolTCP {
return nil, false
}
// make sure the remote server is filtered
if !packet.MatchesDestination(layers.IPProtocolTCP, r.ServerIPAddress, r.ServerPort) {
return nil, false
}
// short circuit in case of misconfiguration
if r.String == "" {
return nil, false
}
// if the packet is not offending, accept it
if !bytes.Contains(packet.TCP.Payload, []byte(r.String)) {
return nil, false
}
// generate the frame to spoof
spoofed, err := reflectDissectedTCPSegmentWithFINACKFlag(packet)
if err != nil {
return nil, false
}
// tell the user we're asking the router to FIN|ACK the flow.
r.Logger.Infof(
"netem: dpi: asking to send FIN|ACK to flow %s:%d %s:%d/%s because it contains %s",
packet.SourceIPAddress(),
packet.SourcePort(),
packet.DestinationIPAddress(),
packet.DestinationPort(),
packet.TransportProtocol(),
r.String,
)
// make sure the router knows it should spoof
policy := &DPIPolicy{
Delay: 0,
Flags: FrameFlagSpoof,
PLR: 0,
Spoofed: [][]byte{spoofed},
}
return policy, true
}
// DPISpoofBlockpageForString is a [DPIRule] that spoofs a blockpage
// after it sees a given string in the payload. The zero value is invalid; please, fill
// all the fields marked as MANDATORY.
//
// Note: this rule assumes that there is a router in the path that
// can generate a spoofed RST segment. If there is no router in the
// path, no RST segment will ever be generated.
//
// Note: this rule relies on a race condition. For consistent results
// you MUST set some delay in the router<->server link.
//
// Note: this rule requires the blockpage to be very small.
type DPISpoofBlockpageForString struct {
// HTTPResponse is the MANDATORY blockpage content prefix with HTTP
// headers (use DPIFormatHTTPResponse to produce this field).
HTTPResponse []byte
// Logger is the MANDATORY logger.
Logger Logger
// ServerIPAddress is the MANDATORY server endpoint IP address.
ServerIPAddress string
// ServerPort is the MANDATORY server endpoint port.
ServerPort uint16
// SNI is the MANDATORY offending string.
String string
}
var _ DPIRule = &DPISpoofBlockpageForString{}
// Filter implements DPIRule
func (r *DPISpoofBlockpageForString) Filter(
direction DPIDirection, packet *DissectedPacket) (*DPIPolicy, bool) {
// short circuit for the return path
if direction != DPIDirectionClientToServer {
return nil, false
}
// short circuit for UDP packets
if packet.TransportProtocol() != layers.IPProtocolTCP {
return nil, false
}
// make sure the remote server is filtered
if !packet.MatchesDestination(layers.IPProtocolTCP, r.ServerIPAddress, r.ServerPort) {
return nil, false
}
// short circuit in case of misconfiguration
if r.String == "" {
return nil, false
}
// if the packet is not offending, accept it
if !bytes.Contains(packet.TCP.Payload, []byte(r.String)) {
return nil, false
}
// generate the frame to spoof
reflected, err := packet.reflectSegment()
if err != nil {
return nil, false
}
reflected.tcp.ACK = true
reflected.tcp.FIN = true
spoofed, err := reflected.serialize(gopacket.Payload(r.HTTPResponse))
if err != nil {
return nil, false
}
// tell the user we're asking the router to spoof a blockpage.
r.Logger.Infof(
"netem: dpi: spoofing blockpage to flow %s:%d %s:%d/%s because it contains %s",
packet.SourceIPAddress(),
packet.SourcePort(),
packet.DestinationIPAddress(),
packet.DestinationPort(),
packet.TransportProtocol(),
r.String,
)
// make sure the router knows it should spoof
policy := &DPIPolicy{
Delay: 0,
Flags: FrameFlagSpoof,
PLR: 0,
Spoofed: [][]byte{spoofed},
}
return policy, true
}
// DPIFormatHTTPResponse formats an HTTP response for a blockpage.
func DPIFormatHTTPResponse(blockpage []byte) (output []byte) {
output = append(output, []byte("HTTP/1.0 200 OK\r\n\r\n")...)
output = append(output, blockpage...)
return
}