/
bf-dns.go
691 lines (616 loc) · 14.6 KB
/
bf-dns.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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
package main
// Imports
import (
"os"
"fmt"
"net"
"sync"
"time"
"strings"
"io/ioutil"
"encoding/json"
"gopkg.in/yaml.v1"
"github.com/miekg/dns"
)
// Hold the global log file
var logEncoder *json.Encoder
// Define the format of the config file
type ConfigStruct struct {
Binds []struct {
Address string
Port string
}
Log string
Control string
Contact string
Questions map[string][]string
Answers struct {
Flipped struct {
A string
AAAA string
}
Correct struct {
A string
AAAA string
}
}
}
// Config will hold the actual config instance
var Config = ConfigStruct{}
// Map of flipped domain to correct domain
var flippedDomains = make(map[string]string)
// Makes a reply msg given a request
func createReply(req *dns.Msg) *dns.Msg {
// Create a response/reply msg
m := new(dns.Msg)
m.SetReply(req)
// Save our precious bandwidth if possible
m.Compress = true
// Add a disclaimer extra to every response
m.Extra = make([]dns.RR, 1)
m.Extra[0] = &dns.TXT{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeTXT,
Class: dns.ClassINET,
Ttl: 604800,
},
Txt: []string{"This server is part of an InfoSec project. You can find more information on " + Config.Control},
}
// Return it
return m
}
// Define the format of a log
type LogEntry struct {
Timestamp string `json:"timestamp"`
Type uint8 `json:"type"`
Destination string `json:"dst"`
Source string `json:"src"`
Port string `json:"port"`
QName string `json:"qName"`
QType uint16 `json:"qType"`
QClass uint16 `json:"qClass"`
}
// Logs the query to the log file
func logQuestion(w dns.ResponseWriter, req *dns.Msg, queryType uint8) {
// Grab the local address info
localAddress, _, err := net.SplitHostPort(w.LocalAddr().String())
if err != nil {
panic(err)
}
remoteAddress, remotePort, err := net.SplitHostPort(w.RemoteAddr().String())
if err != nil {
panic(err)
}
// Make a new LogEntry
log := LogEntry{
Timestamp: time.Now().Format("2006-01-02T15:04:05"),
Type: queryType,
Destination: localAddress,
Source: remoteAddress,
Port: remotePort,
QName: req.Question[0].Name,
QType: req.Question[0].Qtype,
QClass: req.Question[0].Qclass,
}
// Log it as JSON
err = logEncoder.Encode(log)
if err != nil {
panic(err)
}
}
// Answers flipped requests
func FlipServer(w dns.ResponseWriter, req *dns.Msg) {
// Attempt to log the request to the control domain
logQuestion(w, req, 3)
// Create a response/reply msg
m1 := createReply(req)
// Make a reply
if m1.Question[0].Qclass == 1 {
if m1.Question[0].Qtype == 1 {
m1.Answer = []dns.RR{
&dns.A{
Hdr: dns.RR_Header{
Name: m1.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 1,
},
A: net.ParseIP(Config.Answers.Flipped.A),
},
}
} else if m1.Question[0].Qtype == 2 {
m1.Answer = []dns.RR{
&dns.NS{
Hdr: dns.RR_Header{
Name: m1.Question[0].Name,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns1." + Config.Control + ".",
},
&dns.NS{
Hdr: dns.RR_Header{
Name: m1.Question[0].Name,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns2." + Config.Control + ".",
},
}
} else if m1.Question[0].Qtype == 6 {
m1.Answer = []dns.RR{
&dns.SOA{
Hdr: dns.RR_Header{
Name: m1.Question[0].Name,
Rrtype: dns.TypeSOA,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns1." + Config.Control + ".",
Mbox: Config.Contact + ".",
Serial: 1,
Refresh: 28800,
Retry: 7200,
Expire: 604800,
Minttl: 60,
},
}
} else if m1.Question[0].Qtype == 15 {
m1.Answer = []dns.RR{
&dns.MX{
Hdr: dns.RR_Header{
Name: m1.Question[0].Name,
Rrtype: dns.TypeMX,
Class: dns.ClassINET,
Ttl: 604800,
},
Preference: 1,
Mx: "mx." + Config.Control + ".",
},
}
} else if m1.Question[0].Qtype == 28 {
m1.Answer = []dns.RR{
&dns.AAAA{
Hdr: dns.RR_Header{
Name: m1.Question[0].Name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: 1,
},
AAAA: net.ParseIP(Config.Answers.Flipped.AAAA[1:len(Config.Answers.Flipped.AAAA)-1]),
},
}
}
}
// Send the flipped response back
w.WriteMsg(m1)
// Create a second response/reply msg
m2 := createReply(req)
// Grab the request
host_parts := strings.Split(m2.Question[0].Name, ".")
// Replace the domain with the correct value
host_parts[len(host_parts) - 3] = flippedDomains[strings.Join(host_parts[len(host_parts) - 3:len(host_parts) - 1], ".")]
// Replace the question with the correct value
m2.Question[0].Name = strings.Join(host_parts, ".")
// Make a reply
if m2.Question[0].Qclass == 1 {
if m2.Question[0].Qtype == 1 {
m2.Answer = []dns.RR{
&dns.A{
Hdr: dns.RR_Header{
Name: m2.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 1,
},
A: net.ParseIP(Config.Answers.Flipped.A),
},
}
} else if m2.Question[0].Qtype == 2 {
m2.Answer = []dns.RR{
&dns.NS{
Hdr: dns.RR_Header{
Name: m2.Question[0].Name,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns1." + Config.Control + ".",
},
&dns.NS{
Hdr: dns.RR_Header{
Name: m2.Question[0].Name,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns2." + Config.Control + ".",
},
}
} else if m2.Question[0].Qtype == 6 {
m2.Answer = []dns.RR{
&dns.SOA{
Hdr: dns.RR_Header{
Name: m2.Question[0].Name,
Rrtype: dns.TypeSOA,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns1." + Config.Control + ".",
Mbox: Config.Contact + ".",
Serial: 1,
Refresh: 28800,
Retry: 7200,
Expire: 604800,
Minttl: 60,
},
}
} else if m2.Question[0].Qtype == 15 {
m2.Answer = []dns.RR{
&dns.MX{
Hdr: dns.RR_Header{
Name: m2.Question[0].Name,
Rrtype: dns.TypeMX,
Class: dns.ClassINET,
Ttl: 604800,
},
Preference: 1,
Mx: "mx." + Config.Control + ".",
},
}
} else if m2.Question[0].Qtype == 28 {
m2.Answer = []dns.RR{
&dns.AAAA{
Hdr: dns.RR_Header{
Name: m2.Question[0].Name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: 1,
},
AAAA: net.ParseIP(Config.Answers.Correct.AAAA[1:len(Config.Answers.Correct.AAAA)-1]),
},
}
}
}
// Change the question to the 'correct' domain
w.WriteMsg(m2)
}
// Answers control domain requests
func ControlServer(w dns.ResponseWriter, req *dns.Msg) {
// Attempt to log the request to the control domain
logQuestion(w, req, 1)
// Create a response/reply msg
m := createReply(req)
// Make a reply
if m.Question[0].Qclass == 1 {
if m.Question[0].Qtype == 1 {
m.Answer = []dns.RR{
&dns.A{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 604800,
},
A: net.ParseIP(Config.Answers.Correct.A),
},
&dns.A{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 604800,
},
A: net.ParseIP(Config.Answers.Flipped.A),
},
}
m.Ns = []dns.RR{
&dns.NS{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns1." + Config.Control + ".",
},
&dns.NS{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns2." + Config.Control + ".",
},
}
} else if m.Question[0].Qtype == 2 {
m.Answer = []dns.RR{
&dns.NS{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns1." + Config.Control + ".",
},
&dns.NS{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns2." + Config.Control + ".",
},
}
} else if m.Question[0].Qtype == 6 {
m.Answer = []dns.RR{
&dns.SOA{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeSOA,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns1." + Config.Control + ".",
Mbox: Config.Contact + ".",
Serial: 1,
Refresh: 28800,
Retry: 7200,
Expire: 604800,
Minttl: 60,
},
}
} else if m.Question[0].Qtype == 15 {
m.Answer = []dns.RR{
&dns.MX{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeMX,
Class: dns.ClassINET,
Ttl: 604800,
},
Preference: 1,
Mx: "mx." + Config.Control + ".",
},
}
} else if m.Question[0].Qtype == 28 {
m.Answer = []dns.RR{
&dns.AAAA{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: 604800,
},
AAAA: net.ParseIP(Config.Answers.Correct.AAAA[1:len(Config.Answers.Correct.AAAA)-1]),
},
&dns.AAAA{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: 604800,
},
AAAA: net.ParseIP(Config.Answers.Flipped.AAAA[1:len(Config.Answers.Flipped.AAAA)-1]),
},
}
m.Ns = []dns.RR{
&dns.NS{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns1." + Config.Control + ".",
},
&dns.NS{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns2." + Config.Control + ".",
},
}
}
}
// Send back a normal response
w.WriteMsg(m)
}
// Answers unflipped domain requests
func UnflippedServer(w dns.ResponseWriter, req *dns.Msg) {
// Attempt to log the request to the control domain
logQuestion(w, req, 2)
// Create a response/reply msg
m := createReply(req)
// Make a reply
if m.Question[0].Qclass == 1 {
if m.Question[0].Qtype == 1 {
m.Answer = []dns.RR{
&dns.A{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 1,
},
A: net.ParseIP(Config.Answers.Correct.A),
},
&dns.A{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 1,
},
A: net.ParseIP(Config.Answers.Flipped.A),
},
}
} else if m.Question[0].Qtype == 2 {
m.Answer = []dns.RR{
&dns.NS{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns1." + Config.Control + ".",
},
&dns.NS{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns2." + Config.Control + ".",
},
}
} else if m.Question[0].Qtype == 6 {
m.Answer = []dns.RR{
&dns.SOA{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeSOA,
Class: dns.ClassINET,
Ttl: 604800,
},
Ns: "ns1." + Config.Control + ".",
Mbox: Config.Contact + ".",
Serial: 1,
Refresh: 28800,
Retry: 7200,
Expire: 604800,
Minttl: 60,
},
}
} else if m.Question[0].Qtype == 15 {
m.Answer = []dns.RR{
&dns.MX{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeMX,
Class: dns.ClassINET,
Ttl: 1,
},
Preference: 1,
Mx: "mx." + Config.Control + ".",
},
}
} else if m.Question[0].Qtype == 28 {
m.Answer = []dns.RR{
&dns.AAAA{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: 1,
},
AAAA: net.ParseIP(Config.Answers.Correct.AAAA[1:len(Config.Answers.Correct.AAAA)-1]),
},
&dns.AAAA{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: 1,
},
AAAA: net.ParseIP(Config.Answers.Flipped.AAAA[1:len(Config.Answers.Flipped.AAAA)-1]),
},
}
}
}
// Send back a normal response
w.WriteMsg(m)
}
// Answers unknown domain requests
func UnknownServer(w dns.ResponseWriter, req *dns.Msg) {
// Attempt to log the request to an unknown domain
logQuestion(w, req, 0)
// Create a response/reply msg
m := createReply(req)
// Refuse it since we don't know how to handle it
m.Rcode = 5
// Send back the answer
w.WriteMsg(m)
}
// Spawns a listening server given the bind info
func startListening(mux *dns.ServeMux, address string, port string) {
// Create a dns server
server := &dns.Server{
Addr: address + ":" + port,
Net: "udp",
Handler: mux,
}
// Begin listening and serving requests
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}
// Entry Point
func main() {
// If not enough args were provided, print usage to stderr and bail
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s: [configFile]\n", os.Args[0])
os.Exit(1)
}
// Try to read in the passed config file to a byte array
configFile, err := ioutil.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
// Parse the passed in yaml config
err = yaml.Unmarshal(configFile, &Config)
if err != nil {
panic(err)
}
// Update the contact email
Config.Contact = strings.Replace(Config.Contact, "@", ".", -1)
// Open the log file specified in the Config
logFile, err := os.OpenFile(Config.Log, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600);
if err != nil {
panic(err)
}
defer logFile.Close()
// Create an encoder
logEncoder = json.NewEncoder(logFile)
// Create a ServeMux instance
mux := dns.NewServeMux()
// Set the default handler to UnknownServer
mux.HandleFunc(".", UnknownServer)
// Add the control domain handler
mux.HandleFunc(Config.Control, ControlServer)
// Loop each question
for correct, flips := range Config.Questions {
// Add the unflipped domain handler
mux.HandleFunc(correct, UnflippedServer)
// Loop each possible flip
for _, flip := range flips {
// Save the correlation in flippedDomains
flippedDomains[flip] = strings.Split(correct, ".")[0]
// Add a flipped handler to the mux
mux.HandleFunc(flip, FlipServer)
}
}
// Each listener will be spawned in a new goroutine
var wg sync.WaitGroup
// Loop each bind and create a new goroutine with a dns server and begin listening
for _, bind := range Config.Binds {
// We have 1 more to wait for
wg.Add(1);
// Spawn as a goroutine
go startListening(mux, bind.Address, bind.Port)
}
// Wait for them all to exit
wg.Wait()
}