-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpolardns.py
1586 lines (1471 loc) · 67.6 KB
/
polardns.py
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
MIN_VERSION = (3, 11) # required minimal Python version
if sys.version_info < MIN_VERSION:
sys.exit(f"Python version {'.'.join(map(str, MIN_VERSION))} or later is required.")
import socketserver
import threading
import binascii
import tomllib
import random
import socket
import string
import struct
import glob
import time
import os
polardns_version = "1.5.0"
################################
stamp = str(time.time()).ljust(18, "0")
# load config
with open("polardns.toml", "rb") as f:
_config = tomllib.load(f)
config = {k:v for k,v in _config['main'].items() if k != 'known_servers'}
known_servers = {}
for line in _config['main']['known_servers'].split('\n'):
if not line:
continue
host, ip_address = line.split()
known_servers[host] = ip_address
debug = config['debug']
config_ttl = int(config['ttl'])
config_sleep = float(config['sleep'])
config_compression = int(config['compression'])
config_parse_edns0 = config['parse_edns0']
# a domain which is a 3rd party which we don't control
a3rdparty_domain = config['a3rdparty_domain']
# domains which we want to be authoritative for
OURDOMAINS = [
config['domain'],
a3rdparty_domain,
"anything.com",
"version.polar"
]
ZONEFILE = {
"ns1."+config['domain']: {"A": config['ns1']},
"ns2."+config['domain']: {"A": config['ns2']},
"end."+config['domain']: {"A": "1.2.3.4"},
config['domain']: {"NS": "ns1."+config['domain'],
"MX": "10 mail1."+config['domain'],
"TXT": "hello, this is a testing domain",
"SOA": "ns1."+config['domain']+" hostmaster."+config['domain']+" 2023052903 10800 3600 604800 3600"},
"mail1."+config['domain']: {"A": "1.2.3.4"},
"hello."+config['domain']: {"A": "1.2.3.4"},
"injected."+a3rdparty_domain: {"A": "6.6.6.0"},
"injected10."+a3rdparty_domain: {"A": "6.6.6.10"},
"injected11."+a3rdparty_domain: {"A": "6.6.6.11"},
"injected12."+a3rdparty_domain: {"A": "6.6.6.12"},
"injected13."+a3rdparty_domain: {"A": "6.6.6.13"},
"ns1."+a3rdparty_domain: {"A": config['ns1']},
"ns1."+a3rdparty_domain+"."+config['domain']: {"A": config['ns1']},
"ns1."+config['domain']+"."+a3rdparty_domain: {"A": config['ns1']}
}
DNSCLASS = {
"IN": 1,
"CH": 3,
"HS": 4
}
DNSTYPE = {
"A": 1,
"NS": 2,
"MD": 3,
"MF": 4,
"CNAME": 5,
"SOA": 6,
"MB": 7,
"MG": 8,
"MR": 9,
"NULL": 10,
"WKS": 11,
"PTR": 12,
"HINFO": 13,
"MINFO": 14,
"MX": 15,
"TXT": 16,
"RP": 17,
"AFSDB": 18,
"X25": 19,
"ISDN": 20,
"RT": 21,
"NSAP": 22,
"NSAP-PTR": 23,
"SIG": 24,
"KEY": 25,
"PX": 26,
"GPOS": 27,
"AAAA": 28,
"LOC": 29,
"NXT": 30,
"EID": 31,
"NIMLOC": 32,
"SRV": 33,
"ATMA": 34,
"NAPTR": 35,
"KX": 36,
"CERT": 37,
"A6": 38,
"DNAME": 39,
"SINK": 40,
"OPT": 41,
"APL": 42,
"DS": 43,
"SSHFP": 44,
"IPSECKEY": 45,
"RRSIG": 46,
"NSEC": 47,
"DNSKEY": 48,
"DHCID": 49,
"NSEC3": 50,
"NSEC3PARAM": 51,
"TLSA": 52,
"SMIMEA": 53,
"HIP": 55,
"NINFO": 56,
"RKEY": 57,
"TALINK": 58,
"CDS": 59,
"CDNSKEY": 60,
"OPENPGPKEY": 61,
"CSYNC": 62,
"ZONEMD": 63,
"SVCB": 64,
"HTTPS": 65,
"SPF": 99,
"UINFO": 100,
"UID": 101,
"GID": 102,
"UNSPEC": 103,
"NID": 104,
"L32": 105,
"L64": 106,
"LP": 107,
"EUI48": 108,
"EUI64": 109,
"TKEY": 249,
"TSIG": 250,
"IXFR": 251,
"AXFR": 252,
"MAILB": 253,
"MAILA": 254,
"ANY": 255,
"URI": 256,
"CAA": 257,
"AVC": 258,
"DOA": 259,
"AMTRELAY": 260,
"TA": 32768,
"DLV": 32769
}
# Function to get DNS class name (string) from code (int)
def getClassName(q):
for key, val in DNSCLASS.items():
if val == q:
return key
return "None"
# Function to get DNS class code (int) from name (string)
def getClassCode(q):
return DNSCLASS.get(q)
# Function to get binary DNS class from name (string)
def getClassBin(q):
code = DNSCLASS.get(q)
return struct.pack(">H", code)
# Create a reverse dictionary of DNS types so that look ups are very fast
DNSTYPER = {}
for key, val in DNSTYPE.items():
DNSTYPER[val] = key
# Function to get DNS type name (string) from code (int)
def getTypeName(q):
return DNSTYPER.get(q)
# Function to get DNS type code (int) from name (string)
def getTypeCode(q):
return DNSTYPE.get(q)
# Function to get binary DNS type from name (string)
def getTypeBin(q):
code = DNSTYPE.get(q)
return struct.pack(">H", code)
################################
# Function to convert domain name string to the binary form
# aka. DNS name notation
# input example: www.abcd.com
# output : \x03www\x04abcd\x3com\x00
def convDom2Bin(x):
if hasattr(resp, 'DOM_ALREADY_CONVERTED'):
delattr(resp, 'DOM_ALREADY_CONVERTED')
return x
if x == "": return b"\x00"
parts = []
append = parts.append # Local variable lookup is faster
for y in x.split('.'):
y = y.replace("<DOT>", ".")
length = bytes([len(y)])
append(length)
append(y.encode("utf-8"))
parts.append(b"\x00")
return b''.join(parts)
################################
# Function to convert data string to the binary form
# input example: somedata.something
# output : \x08somedata\x09something
def convData2Bin(x):
parts = []
append = parts.append # Local variable lookup is faster
for y in x.split('.'):
y = y.replace("<DOT>", ".")
length = bytes([len(y)])
append(length)
append(y.encode("utf-8"))
return b''.join(parts)
################################
# Name fuzzer function (nfz)
def name_fuzz(n):
rand_suffix = '{:06d}'.format(random.getrandbits(20) % 1000000)
match n:
######################
case 0:
# NULL byte(s)
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = b'\x00' * resp.nfz_sv
dom = struct.pack(">B", resp.nfz_sv) + tmp + b'\x00'
else:
dom = b'\x01\x00\x00'
resp.DOM_ALREADY_CONVERTED = 1
######################
case 1:
# <ROOT> domain
dom = b'\x00'
resp.DOM_ALREADY_CONVERTED = 1
######################
case 2:
# random printable ASCII character(s)
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want multiple random characters
dom = ''.join(random.choice(string.printable) for _ in range(resp.nfz_sv))
else:
dom = random.choice(string.printable)
######################
case 3:
# random printable ASCII character(s)
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want repeat the random character
dom = random.choice(string.printable) * resp.nfz_sv
else:
dom = random.choice(string.printable)
######################
case 4:
# random byte(s)
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want multiple random bytes
tmp = b''.join(random.getrandbits(8).to_bytes(1, 'big') for _ in range(resp.nfz_sv))
dom = struct.pack(">B", resp.nfz_sv) + tmp + b'\x00'
else:
dom = b'\x01' + random.getrandbits(8).to_bytes(1, 'big') + b'\x00'
resp.DOM_ALREADY_CONVERTED = 1
######################
case 5:
# random byte(s) - repeated
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = random.getrandbits(8).to_bytes(1, 'big') * resp.nfz_sv
dom = struct.pack(">B", resp.nfz_sv) + tmp + b'\x00'
else:
dom = b'\x01' + random.getrandbits(8).to_bytes(1, 'big') + b'\x00'
resp.DOM_ALREADY_CONVERTED = 1
######################
case 6:
# byte(s) starting from 0 to 255 (incremental)
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = b''
for _ in range(resp.nfz_sv):
tmp += resp.nfz_byte_iterator.to_bytes(1, 'big')
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
dom = struct.pack(">B", resp.nfz_sv) + tmp + b'\x00'
else:
dom = b'\x01' + resp.nfz_byte_iterator.to_bytes(1, 'big') + b'\x00'
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
resp.DOM_ALREADY_CONVERTED = 1
######################
case 7:
# byte(s) starting from 0 to 255 (repeated)
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = resp.nfz_byte_iterator.to_bytes(1, 'big') * resp.nfz_sv
dom = struct.pack(">B", resp.nfz_sv) + tmp + b'\x00'
else:
dom = b'\x01' + resp.nfz_byte_iterator.to_bytes(1, 'big') + b'\x00'
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
resp.DOM_ALREADY_CONVERTED = 1
######################
case 8:
# max label sized (63) random binary string
siz = 63
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the string more times
# Note: subvariant 4 and above will already exceed the max domain size (255)
dom = b''
for _ in range(resp.nfz_sv):
data = bytes([random.getrandbits(8) for _ in range(siz)])
dom += struct.pack(">B", len(data)) + data
dom += b"\x00"
else:
data = bytes([random.getrandbits(8) for _ in range(siz)])
dom = struct.pack(">B", len(data)) + data + b"\x00"
resp.DOM_ALREADY_CONVERTED = 1
######################
case 9:
# max label sized (63) random string made of printable characters
siz = 63
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the string more times
# Note: subvariant 4 and above will already exceed the max domain size (255)
dom = ''.join(random.choice(string.printable) for _ in range(siz))
for _ in range(resp.nfz_sv-1):
dom += "." + ''.join(random.choice(string.printable) for _ in range(siz))
else:
dom = ''.join(random.choice(string.printable) for _ in range(siz))
######################
case 10:
# max label sized (63) random string made of letters and numbers
siz = 63
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the string more times
# Note: subvariant 4 and above will already exceed the max domain size (255)
dom = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(siz))
for _ in range(resp.nfz_sv-1):
dom += "." + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(siz))
else:
dom = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(siz))
######################
case 11:
# random 1 byte long subdomain(s)
dom = b'\x01' + random.getrandbits(8).to_bytes(1, 'big')
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
for _ in range(resp.nfz_sv-1):
dom += b'\x01' + random.getrandbits(8).to_bytes(1, 'big')
dom += b'\x00'
resp.DOM_ALREADY_CONVERTED = 1
######################
case 12:
# random 1 byte long subdomain(s) made of printable character
siz = 1
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the string more times
# Note: subvariant 4 and above will already exceed the max domain size (255)
dom = ''.join(random.choice(string.printable) for _ in range(siz))
for _ in range(resp.nfz_sv-1):
dom += "." + ''.join(random.choice(string.printable) for _ in range(siz))
else:
dom = ''.join(random.choice(string.printable) for _ in range(siz))
######################
case 13:
# random 1 byte long subdomain(s) made of letters and numbers
siz = 1
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the string more times
# Note: subvariant 4 and above will already exceed the max domain size (255)
dom = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(siz))
for _ in range(resp.nfz_sv-1):
dom += "." + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(siz))
else:
dom = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(siz))
######################
case 14:
# 1 byte long subdomain(s) from \x00 to \xff (incremental)
dom = b'\x01' + resp.nfz_byte_iterator.to_bytes(1, 'big')
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
for _ in range(resp.nfz_sv-1):
dom += b'\x01' + resp.nfz_byte_iterator.to_bytes(1, 'big')
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
dom += b'\x00'
resp.DOM_ALREADY_CONVERTED = 1
######################
case 15:
# 1 byte long subdomain(s) from \x00 to \xff (repeated)
dom = b'\x01' + resp.nfz_byte_iterator.to_bytes(1, 'big')
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
for _ in range(resp.nfz_sv-1):
dom += b'\x01' + resp.nfz_byte_iterator.to_bytes(1, 'big')
dom += b'\x00'
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
resp.DOM_ALREADY_CONVERTED = 1
######################
case 16:
# nonres<######>.yourdomain.com
dom = "nonres" + rand_suffix + "." + req.sld_tld_domain
######################
case 17:
# always<######>.yourdomain.com
dom = "always" + rand_suffix + "." + req.sld_tld_domain
######################
case 18:
# always<######>.<NULL byte(s)>.yourdomain.com
dom = b'\x0c' + bytes("always" + rand_suffix, "utf-8")
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = b'\x00' * resp.nfz_sv
dom += struct.pack(">B", resp.nfz_sv) + tmp
else:
dom += b'\x01\x00'
dom += convDom2Bin(req.sld_tld_domain)
resp.DOM_ALREADY_CONVERTED = 1
######################
case 19:
# always123456.<random byte(s)>.yourdomain.com
dom = b'\x0c' + bytes("always" + rand_suffix, "utf-8")
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = b''.join(random.getrandbits(8).to_bytes(1, 'big') for _ in range(resp.nfz_sv))
dom += struct.pack(">B", resp.nfz_sv) + tmp
else:
dom += b'\x01' + random.getrandbits(8).to_bytes(1, 'big')
dom += convDom2Bin(req.sld_tld_domain)
resp.DOM_ALREADY_CONVERTED = 1
######################
case 20:
# always123456.<random byte(s)>.yourdomain.com (repeated)
dom = b'\x0c' + bytes("always" + rand_suffix, "utf-8")
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = random.getrandbits(8).to_bytes(1, 'big') * resp.nfz_sv
dom += struct.pack(">B", resp.nfz_sv) + tmp
else:
dom += b'\x01' + random.getrandbits(8).to_bytes(1, 'big')
dom += convDom2Bin(req.sld_tld_domain)
resp.DOM_ALREADY_CONVERTED = 1
######################
case 21:
# always123456.<byte(s) starting from 0 to 255>.yourdomain.com (incremental)
dom = b'\x0c' + bytes("always" + rand_suffix, "utf-8")
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = b''
for _ in range(resp.nfz_sv):
tmp += resp.nfz_byte_iterator.to_bytes(1, 'big')
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
dom += struct.pack(">B", resp.nfz_sv) + tmp
else:
dom += b'\x01' + resp.nfz_byte_iterator.to_bytes(1, 'big')
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
dom += convDom2Bin(req.sld_tld_domain)
resp.DOM_ALREADY_CONVERTED = 1
######################
case 22:
# always123456.<byte(s) starting from 0 to 255>.yourdomain.com (repeated)
dom = b'\x0c' + bytes("always" + rand_suffix, "utf-8")
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = resp.nfz_byte_iterator.to_bytes(1, 'big') * resp.nfz_sv
dom += struct.pack(">B", resp.nfz_sv) + tmp
else:
dom += b'\x01' + resp.nfz_byte_iterator.to_bytes(1, 'big')
dom += convDom2Bin(req.sld_tld_domain)
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
resp.DOM_ALREADY_CONVERTED = 1
######################
case 23:
# always.123456.<random 1 byte long subdomain(s)>.yourdomain.com
dom = b'\x0c' + bytes("always" + rand_suffix, "utf-8")
dom += b'\x01' + random.getrandbits(8).to_bytes(1, 'big')
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
for _ in range(resp.nfz_sv-1):
dom += b'\x01' + random.getrandbits(8).to_bytes(1, 'big')
dom += convDom2Bin(req.sld_tld_domain)
resp.DOM_ALREADY_CONVERTED = 1
######################
case 24:
# always123456.<random 1 byte long subdomain(s) made of printable character>.yourdomain.com
siz = 1
dom = "always" + rand_suffix + "."
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the string more times
# Note: subvariant 4 and above will already exceed the max domain size (255)
dom += ''.join(random.choice(string.printable) for _ in range(siz))
for _ in range(resp.nfz_sv-1):
dom += "." + ''.join(random.choice(string.printable) for _ in range(siz))
else:
dom += ''.join(random.choice(string.printable) for _ in range(siz))
dom += "." + req.sld_tld_domain
######################
case 25:
# always123456.<random 1 byte long subdomain(s) made of a letter or a number>.yourdomain.com
siz = 1
dom = "always" + rand_suffix + "."
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the string more times
# Note: subvariant 4 and above will already exceed the max domain size (255)
dom += ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(siz))
for _ in range(resp.nfz_sv-1):
dom += "." + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(siz))
else:
dom += ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(siz))
dom += "." + req.sld_tld_domain
######################
case 26:
# always123456.<1 byte long subdomain(s) from \x00 to \xff>.yourdomain.com (incremental)
dom = b'\x0c' + bytes("always" + rand_suffix, "utf-8")
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
for _ in range(resp.nfz_sv):
dom += b'\x01' + resp.nfz_byte_iterator.to_bytes(1, 'big')
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
dom += convDom2Bin(req.sld_tld_domain)
resp.DOM_ALREADY_CONVERTED = 1
######################
case 27:
# always123456.<1 byte long subdomain(s) from \x00 to \xff>.yourdomain.com (repeated)
dom = b'\x0c' + bytes("always" + rand_suffix, "utf-8")
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
for _ in range(resp.nfz_sv):
dom += b'\x01' + resp.nfz_byte_iterator.to_bytes(1, 'big')
dom += convDom2Bin(req.sld_tld_domain)
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
resp.DOM_ALREADY_CONVERTED = 1
######################
case 28:
# <NULL byte(s)>always123456.yourdomain.com
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = b'\x00' * resp.nfz_sv
dom = struct.pack(">B", resp.nfz_sv+12) + tmp
else:
dom = struct.pack(">B", 1+12) + b'\x00'
dom += bytes("always" + rand_suffix, "utf-8")
dom += convDom2Bin(req.sld_tld_domain)
resp.DOM_ALREADY_CONVERTED = 1
######################
case 29:
# <random byte(s)>always123456.yourdomain.com (truly random)
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = b''.join(random.getrandbits(8).to_bytes(1, 'big') for _ in range(resp.nfz_sv))
dom = struct.pack(">B", resp.nfz_sv+12) + tmp
else:
dom = struct.pack(">B", 1+12) + random.getrandbits(8).to_bytes(1, 'big')
dom += bytes("always" + rand_suffix, "utf-8")
dom += convDom2Bin(req.sld_tld_domain)
resp.DOM_ALREADY_CONVERTED = 1
######################
case 30:
# <random byte(s)>always123456.yourdomain.com (repeated)
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = random.getrandbits(8).to_bytes(1, 'big') * resp.nfz_sv
dom = struct.pack(">B", resp.nfz_sv+12) + tmp
else:
dom = struct.pack(">B", 1+12) + random.getrandbits(8).to_bytes(1, 'big')
dom += bytes("always" + rand_suffix, "utf-8")
dom += convDom2Bin(req.sld_tld_domain)
resp.DOM_ALREADY_CONVERTED = 1
######################
case 31:
# <random byte(s) starting from 0 to 255>always123456.yourdomain.com (incremental)
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = b''
for _ in range(resp.nfz_sv):
tmp += resp.nfz_byte_iterator.to_bytes(1, 'big')
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
dom = struct.pack(">B", resp.nfz_sv+12) + tmp
else:
dom = struct.pack(">B", 1+12) + resp.nfz_byte_iterator.to_bytes(1, 'big')
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
dom += bytes("always" + rand_suffix, "utf-8")
dom += convDom2Bin(req.sld_tld_domain)
resp.DOM_ALREADY_CONVERTED = 1
######################
case 32:
# <random byte(s) starting from 0 to 255>always123456.yourdomain.com (repeated)
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = resp.nfz_byte_iterator.to_bytes(1, 'big') * resp.nfz_sv
dom = struct.pack(">B", resp.nfz_sv+12) + tmp
else:
dom = struct.pack(">B", 1+12) + resp.nfz_byte_iterator.to_bytes(1, 'big')
dom += bytes("always" + rand_suffix, "utf-8")
dom += convDom2Bin(req.sld_tld_domain)
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
resp.DOM_ALREADY_CONVERTED = 1
######################
case 33:
# always123456.yourdomain.com<NULL byte(s)>
dom = b'\x0c' + bytes("always" + rand_suffix, "utf-8")
dom += struct.pack(">B", len(req.sld)) + bytes(req.sld, "utf-8")
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = b'\x00' * resp.nfz_sv
dom += struct.pack(">B", resp.nfz_sv+len(req.tld)) + bytes(req.tld, "utf-8") + tmp
else:
dom += struct.pack(">B", 1+len(req.tld)) + bytes(req.tld, "utf-8") + b'\x00'
dom += b'\x00'
resp.DOM_ALREADY_CONVERTED = 1
######################
case 34:
# always123456.yourdomain.com<random byte(s)> (truly random)
dom = b'\x0c' + bytes("always" + rand_suffix, "utf-8")
dom += struct.pack(">B", len(req.sld)) + bytes(req.sld, "utf-8")
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = b''.join(random.getrandbits(8).to_bytes(1, 'big') for _ in range(resp.nfz_sv))
dom += struct.pack(">B", resp.nfz_sv+len(req.tld)) + bytes(req.tld, "utf-8") + tmp
else:
dom += struct.pack(">B", 1+len(req.tld)) + bytes(req.tld, "utf-8") + random.getrandbits(8).to_bytes(1, 'big')
dom += b'\x00'
resp.DOM_ALREADY_CONVERTED = 1
######################
case 35:
# always123456.yourdomain.com<random byte(s)> (repeated)
dom = b'\x0c' + bytes("always" + rand_suffix, "utf-8")
dom += struct.pack(">B", len(req.sld)) + bytes(req.sld, "utf-8")
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = random.getrandbits(8).to_bytes(1, 'big') * resp.nfz_sv
dom += struct.pack(">B", resp.nfz_sv+len(req.tld)) + bytes(req.tld, "utf-8") + tmp
else:
dom += struct.pack(">B", 1+len(req.tld)) + bytes(req.tld, "utf-8") + random.getrandbits(8).to_bytes(1, 'big')
dom += b'\x00'
resp.DOM_ALREADY_CONVERTED = 1
######################
case 36:
# always123456.yourdomain.com<byte(s) starting from 0 to 255> (incremental)
dom = b'\x0c' + bytes("always" + rand_suffix, "utf-8")
dom += struct.pack(">B", len(req.sld)) + bytes(req.sld, "utf-8")
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = b''
for _ in range(resp.nfz_sv):
tmp += resp.nfz_byte_iterator.to_bytes(1, 'big')
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
dom += struct.pack(">B", resp.nfz_sv+len(req.tld)) + bytes(req.tld, "utf-8") + tmp
else:
dom += struct.pack(">B", 1+len(req.tld)) + bytes(req.tld, "utf-8") + resp.nfz_byte_iterator.to_bytes(1, 'big')
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
dom += b'\x00'
resp.DOM_ALREADY_CONVERTED = 1
######################
case 37:
# always123456.yourdomain.com<byte(s) starting from 0 to 255> (repeated)
dom = b'\x0c' + bytes("always" + rand_suffix, "utf-8")
dom += struct.pack(">B", len(req.sld)) + bytes(req.sld, "utf-8")
if hasattr(resp, "nfz_sv"):
# if there is a sub-variant, it means we want to repeat the byte more times
tmp = resp.nfz_byte_iterator.to_bytes(1, 'big') * resp.nfz_sv
dom += struct.pack(">B", resp.nfz_sv+len(req.tld)) + bytes(req.tld, "utf-8") + tmp
else:
dom += struct.pack(">B", 1+len(req.tld)) + bytes(req.tld, "utf-8") + resp.nfz_byte_iterator.to_bytes(1, 'big')
dom += b'\x00'
resp.nfz_byte_iterator = (resp.nfz_byte_iterator + 1) % 256
resp.DOM_ALREADY_CONVERTED = 1
######################
case 38:
# always123456.yourdomain.com:80
dom = "always" + rand_suffix + "." + req.sld_tld_domain + ":80"
######################
case 39:
# always123456.yourdomain.com:443
dom = "always" + rand_suffix + "." + req.sld_tld_domain + ":443"
######################
case 40:
# http://always123456.yourdomain.com/
dom = "http://always" + rand_suffix + "." + req.sld_tld_domain + "/"
######################
case 41:
# http://always123456.yourdomain.com:80/
dom = "http://always" + rand_suffix + "." + req.sld_tld_domain + ":80/"
######################
case 42:
# https://always123456.yourdomain.com/
dom = "https://always" + rand_suffix + "." + req.sld_tld_domain + "/"
######################
case 43:
# https://always123456.yourdomain.com:443/
dom = "https://always" + rand_suffix + "." + req.sld_tld_domain + ":443/"
######################
case 44:
# 1.2.3.4 (in DNS name notation as 4 labels)
dom = "1.2.3.4"
######################
case 45:
# 1.2.3.4:80 (in DNS name notation as 4 labels)
dom = "1.2.3.4:80"
######################
case 46:
# 1.2.3.4 (in DNS name notation as 1 label)
dom = "1<DOT>2<DOT>3<DOT>4"
######################
case 47:
# 1.2.3.4:80 (in DNS name notation as 1 label)
dom = "1<DOT>2<DOT>3<DOT>4:80"
######################
case 48:
# <OUR-IP-ADDRESS> (in DNS name notation as 4 labels)
dom = ZONEFILE["ns1." + req.sld_tld_domain]["A"]
######################
case 49:
# <OUR-IP-ADDRESS>:80 (in DNS name notation as 4 labels)
ourip = ZONEFILE["ns1." + req.sld_tld_domain]["A"]
dom = ourip + ":80"
######################
case _:
# hello (default case)
dom = "hello"
######################
return dom
################################
# Function to increment chainXXX if there is one
def increment_chain(req_domain):
new_subdomains = req.subdomains
# in case of domains with attribute leaves (domains prefixed with an underscore),
# do not modify the leading underscored subdomains (up to first 3 subdomains)
# e.g., '_sub._service._proto...'
skip = 0
for i in range(3):
if req.subdomains[2-i][0:1] == "_":
new_subdomains = req.subdomains[3-i:]
skip = 3-i
break
first_subdomain = new_subdomains[0]
first_subdomain_length = len(first_subdomain)
if first_subdomain_length > 5:
# how many last characters are numeric
hmlcan = 0
while True:
lastchar = first_subdomain[first_subdomain_length-(hmlcan+1):]
if lastchar.isnumeric():
hmlcan += 1
else:
break
if hmlcan >= first_subdomain_length:
break
if hmlcan > 0:
current_index = first_subdomain[first_subdomain_length-hmlcan:]
subd_wo_index = first_subdomain[0:first_subdomain_length-hmlcan]
else:
current_index = 0
subd_wo_index = first_subdomain
new_label_number = int(current_index)+1
new_subdomain = subd_wo_index + str(new_label_number)
else:
new_subdomain = "chain1"
# replace the subdomain with new incremented index (if there was no index, it will be "chain1")
new_subdomains[0] = new_subdomain
# now construct a nice full domain name and return it
new_domain_name = new_subdomain
for l in range(1, len(new_subdomains)):
new_domain_name += "." + new_subdomains[l]
# in case of domains with attribute leaves, prepend back the first N subdomains we skipped above
if skip:
tmp = ""
for i in range(skip):
tmp += req.subdomains[i] + "."
new_domain_name = tmp + new_domain_name
print("new domain name:", new_domain_name) if debug else True
return new_domain_name
################################
# Function to generate random chainXXX
def random_chain(req_domain):
new_subdomains = req.subdomains
# in case of domains with attribute leaves (domains prefixed with an underscore),
# do not modify the leading underscored subdomains (up to first 3 subdomains)
# e.g., '_sub._service._proto...'
skip = 0
for i in range(3):
if req.subdomains[2-i][0:1] == "_":
new_subdomains = req.subdomains[3-i:]
skip = 3-i
break
first_subdomain = new_subdomains[0]
first_subdomain_length = len(first_subdomain)
new_random_number = random.getrandbits(20) % 1000000
# how many last characters are numeric
hmlcan = 0
while True:
lastchar = first_subdomain[first_subdomain_length-(hmlcan+1):]
if lastchar.isnumeric():
hmlcan += 1
else:
break
if hmlcan >= first_subdomain_length:
break
if hmlcan > 0:
current_index = first_subdomain[first_subdomain_length-hmlcan:]
subd_wo_index = first_subdomain[0:first_subdomain_length-hmlcan]
else:
current_index = 0
subd_wo_index = first_subdomain
new_subdomain = subd_wo_index + str(new_random_number)
# replace the subdomain with new random index
new_subdomains[0] = new_subdomain
# now construct a nice full domain name and return it
new_domain_name = new_subdomain
for l in range(1, len(new_subdomains)):
new_domain_name += "." + new_subdomains[l]
# in case of domains with attribute leaves, prepend back the first N subdomains we skipped above
if skip:
tmp = ""
for i in range(skip):
tmp += req.subdomains[i] + "."
new_domain_name = tmp + new_domain_name
print("new domain name:", new_domain_name) if debug else True
return new_domain_name
################################
# Function for printing messages on the console
def log(m):
stamp = str(time.time()).ljust(18, "0")
end = ""
if resp.len != 0:
# custom length requested in the response? print message at the end
if proto == "tcp":
end = " (LEN:" + str(resp.len) + ")"
else:
end = " (Use LEN only in TCP!)"
try:
print("%s | %s %s %s | (%s) %s%s" % (stamp, req.info, req.type_str, req.full_domain, req.customlog, m, end))
except:
print("%s | %s %s %s | %s%s" % (stamp, req.info, req.type_str, req.full_domain, m, end))
################################
# Add custom message to the message on the console
def addcustomlog(m):
try:
req.customlog += "," + m
except AttributeError:
req.customlog = m
################################
# Send buffer with DNS message (TCP and UDP)
def send_buf(self, buffer, totallen = 0):
print(" Sending:", buffer) if debug else True
print(" Sleep:", resp.sleep) if debug else True
print(" Orig length:", len(buffer)) if debug else True
print("Custom length:", resp.len) if debug else True
time.sleep(resp.sleep)
append = b''
if hasattr(resp, "addbyte"):
append = os.urandom(resp.addcount) if resp.addbyte == "r" else bytes([resp.addbyte] * resp.addcount)
newlen = len(buffer) - getattr(resp, 'cutcount', 0)
newbuffer = buffer[:max(newlen, 0)] + append
# UDP mode
if proto == "udp":
self.wfile.write(newbuffer)
self.wfile.flush()
return
# TCP mode
# In TCP mode, we need to prepend the packet with a 2-byte length field.
# The length can be determined by one of the following methods:
# - Overridden length specified by the '.lenXXX.' modifier in the domain name
# - Overridden length provided as a parameter to this function
# - Calculated from the buffer length if neither of the above is provided
buflen = resp.len or totallen or len(buffer)
if hasattr(resp, "cutcount"):
buflen -= resp.rl * resp.cutcount # adjust the length
if hasattr(resp, "addbyte"):
buflen += resp.rl * resp.addcount # adjust the length
try:
self.request.sendall(struct.pack(">H", buflen) + newbuffer)
except Exception as e:
print(f"Error sending buffer: {e}")
return(-1)
################################
# Send buffer without length (TCP only)
def send_buf_wo_len(self, buffer):
print("Sending:", buffer) if debug else True
time.sleep(resp.sleep)
try:
self.request.sendall(buffer)
except:
return(-1)
################################
# Close connection
def close_conn(self):
if proto == "tcp":
# send proper FIN immediately
self.request.close()
else:
# In UDP this will just send nothing and close the socket.
# Nothing will be sent out.
self.rfile.close()
self.wfile.close()
# Consider sending ICMP port unreachable packet instead, but this is
# non-trivial to implement
################################
# Timeout the connection
def timeout_conn(self):
if proto == "tcp":
# Not possible to just abandon the TCP connections using socketserver.
# Workaround below:
# Wait 20 seconds and then close the socket gracefully (a resolver or
# a client will unlikely wait 20 seconds for an answer)
time.sleep(20)
self.finish()
else:
# In UDP this will just send nothing and close the socket.
# Nothing will be sent out.
self.rfile.close()
self.wfile.close()
################################
# Function to get a sample random data appropriate to the record type
def getRandomDataOfType(thetype):
databin = b''
if thetype == 1:
# send random IP address
data = '.'.join(str(random.getrandbits(8)) for _ in range(4))
databin = socket.inet_aton(data)
# send random hostname
elif thetype in (0, 2, 3, 4, 5, 7, 8, 9, 25):
data = "hello." + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(int(25))) + ".com"
databin = convDom2Bin(data)
elif thetype == 16:
# send some random data
data = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(int(253)))
databin = convData2Bin(data)
elif thetype == 6:
# SOA
# https://www.rfc-editor.org/rfc/rfc1035#section-3.3.13
pass
elif thetype == 21:
# RT / Route Through
# https://www.rfc-editor.org/rfc/rfc1183#section-3.3
pass
else:
data = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(int(253)))
databin = convData2Bin(data)
return databin