-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdomainhunter2.py
executable file
·1289 lines (1110 loc) · 52.1 KB
/
domainhunter2.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
#!/usr/bin/env python3
import dns.resolver
from datetime import tzinfo, timedelta, datetime
import time
import uuid
import sys
import os
import threading
import ipaddress
from multiprocessing import Process, Queue, JoinableQueue
import warnings
from ipwhois.net import Net
from ipwhois.asn import IPASN
from pprint import pprint
import re
import sqlite3
from urllib.request import urlopen
import requests
import requests_cache
import json
from pygraphviz import *
### Classes
class Workload:
store_db = {}
mem_db = {}
def __init__(self, base_fqdn, uuid_hunt=None):
self.base_fqdn = base_fqdn
self.wildcard_canary = 'wildcardcanary' + '.' + self.base_fqdn
self.initialize_db()
self.s_dt = datetime.utcnow()
if uuid_hunt is None:
self.uuid_hunt = str(uuid.uuid4())
else:
self.uuid_hunt = uuid_hunt
def initialize_db(self):
self.mem_db['connection'] = sqlite3.connect(':memory:')
self.mem_db['connection'].isolation_level = None
# self.mem_db['connection'] = sqlite3.connect(PATH + 'db/domainhunter2.db')
self.mem_db['cursor'] = self.mem_db['connection'].cursor()
self.mem_db['connection'].execute('''CREATE TABLE fqdns (uuid_fqdn TEXT, fqdn TEXT, status TEXT, uuid_parent TEXT)''')
self.mem_db['connection'].execute('''CREATE TABLE dns_rr (uuid_rr TEXT, fqdn TEXT, r_type TEXT, value TEXT)''')
self.mem_db['connection'].execute('''CREATE TABLE asn (uuid_asn TEXT, asn TEXT, asn_description TEXT,
asn_date TEXT, asn_registry TEXT,
asn_country_code TEXT, asn_cidr TEXT
)''')
self.mem_db['connection'].execute('''CREATE TABLE ip (uuid_ip TEXT, ip TEXT, version TEXT)''')
self.mem_db['connection'].execute('''CREATE TABLE ip2asn (uuid_ip TEXT, uuid_asn TEXT)''')
self.mem_db['connection'].execute('''CREATE TABLE dns_rr_parent_child (uuid_parent TEXT, uuid_child TEXT)''')
self.mem_db['connection'].execute('''CREATE TABLE dns_rr_to_ip (uuid_rr TEXT, uuid_ip TEXT)''')
self.mem_db['connection'].execute('''CREATE TABLE redirect (uuid_redir TEXT, schema TEXT, fqdn TEXT, location TEXT)''')
self.mem_db['connection'].execute('''CREATE TABLE fqdn2redirect (uuid_fqdn TEXT, uuid_redir TEXT)''')
self.store_db['connection'] = sqlite3.connect(PATH + 'db/domainhunter2.db')
self.store_db['connection'].isolation_level = None
self.store_db['cursor'] = self.store_db['connection'].cursor()
try:
self.store_db['connection'].execute('''CREATE TABLE dns_rr_cache (fqdn TEXT, r_type TEXT, value TEXT, error TEXT)''')
except:
pass
### Disk cache of dns_rr records for speed up
### TODO: should get a (longer) TTL
def add_cache_entry(self, fqdn, r_type, value, error):
sql = ' '.join(["INSERT INTO dns_rr_cache",
"(fqdn, r_type, value, error)"
"VALUES (:fqdn, :r_type, :value, :error)"])
self.store_db['cursor'].execute(sql,
{"fqdn":fqdn,
"r_type":r_type,
"value":value,
"error":error})
return True
def has_cache_hit(self, fqdn, r_type, error):
sql = ' '.join(["SELECT count(*)"
"FROM dns_rr_cache",
"WHERE fqdn = :fqdn",
"AND r_type = :r_type",
"AND error = :error"])
self.store_db['cursor'].execute(sql,
{"fqdn":fqdn,
"r_type":r_type,
"error":error})
cnt = self.store_db['cursor'].fetchone()[0]
return cnt > 0
def get_cache_hit(self, fqdn, r_type):
sql = ' '.join(["SELECT fqdn, r_type, value, error"
"FROM dns_rr_cache",
"WHERE fqdn = :fqdn",
"AND r_type = :r_type"])
self.store_db['cursor'].execute(sql,
{"fqdn":fqdn,
"r_type":r_type})
res = self.store_db['cursor'].fetchone()
rec = {}
rec['fqdn'] = res[0]
rec['r_type'] = res[1]
rec['value'] = res[2]
rec['error'] = res[3]
return rec
### Clean up stuff, milage may vary...
def detect_none_base_fqdn_rr_wilds_for_cleanup(self):
all_recs = self.get_dns_rr()
base_fqdn_rr = self.get_dns_rr_by_fqdn(self.base_fqdn)
for ar in all_recs:
if ar['r_type'] in ['NS', 'MX', 'SOA', 'TXT']:
for bfr in base_fqdn_rr:
if bfr['r_type'] == ar['r_type'] and bfr['value'] == ar['value']:
print(bfr['r_type'], "==", ar['r_type'], "and", bfr['value'], "==", ar['value'])
# Remove from all_recs (in the db)
self.delete_dns_rr_by_fqdn_and_r_type(ar['fqdn'], ar['r_type'])
def detect_and_remove_dns_wildcard(self):
canary_recs = self.get_dns_rr_by_fqdn(self.wildcard_canary)
print("Canary rec count:", len(canary_recs), file=sys.stderr)
all_recs = self.get_dns_rr()
print("All rec count:", len(all_recs), file=sys.stderr)
# Is the data of the canary_recs is found in the all_recs, than
# remove that record from the all_recs, unless it's the base_fqdn and the wildcard_canary itself
for ar in all_recs:
for cr in canary_recs:
if ar['value'] == cr['value'] and ar['r_type'] == cr['r_type']:
# Eligable for removal
if ar['fqdn'] == self.base_fqdn or ar['fqdn'] == self.wildcard_canary:
continue
else:
# Remove from all_recs (in the db)
self.delete_dns_rr_by_fqdn_and_r_type(ar['fqdn'], ar['r_type'])
### Table: dns_rr
def delete_dns_rr_by_fqdn_and_r_type(self, g_fqdn, g_r_type):
# Remove linkages
all_recs = self.get_dns_rr()
for r in all_recs:
if r['fqdn'] == g_fqdn and r['r_type'] == g_r_type:
self.delete_dns_rr_to_ip_by_uuid_rr(r['uuid_rr'])
# Remove DNS RR
sql = ' '.join(["DELETE FROM dns_rr",
"WHERE fqdn = :fqdn",
"AND r_type = :r_type"])
self.mem_db['cursor'].execute(sql,
{"fqdn":g_fqdn,
"r_type":g_r_type})
return True
def count_dns_rr_by_r_type_and_value(self, c_r_type, c_value):
sql = ' '.join(["SELECT count(*)"
"FROM dns_rr",
"WHERE r_type = :r_type",
"AND value = :value"])
self.mem_db['cursor'].execute(sql,
{"r_type":c_r_type,
"value":c_value})
cnt = self.mem_db['cursor'].fetchone()[0]
return cnt
def add_dns_rr(self, fqdn, r_type, value):
u = str(uuid.uuid4())
sql = ' '.join(["INSERT INTO dns_rr",
"(uuid_rr, fqdn, r_type, value)",
"VALUES (:uuid_rr, :fqdn, :r_type, :value)"])
self.mem_db['cursor'].execute(sql,
{"uuid_rr":u,
"fqdn":fqdn,
"r_type": r_type,
"value": value})
return u
def get_dns_rr_by_fqdn(self, g_fqdn):
all_dns_rr = []
sql = ' '.join(["SELECT uuid_rr, fqdn, r_type, value",
"FROM dns_rr",
"WHERE fqdn = :fqdn"])
self.mem_db['cursor'].execute(sql,
{"fqdn":g_fqdn})
for (uuid_rr, fqdn, r_type, value) in self.mem_db['cursor']:
rec = {}
rec['uuid_rr'] = uuid_rr
rec['fqdn'] = fqdn
rec['r_type'] = r_type
rec['value'] = value
all_dns_rr.append(rec)
return all_dns_rr
def get_dns_rr(self):
all_dns_rr = []
sql = ' '.join(["SELECT uuid_rr, fqdn, r_type, value",
"FROM dns_rr"])
self.mem_db['cursor'].execute(sql)
for (uuid_rr, fqdn, r_type, value) in self.mem_db['cursor']:
rec = {}
rec['uuid_rr'] = uuid_rr
rec['fqdn'] = fqdn
rec['r_type'] = r_type
rec['value'] = value
all_dns_rr.append(rec)
return all_dns_rr
def count_dns_rr_by_fqdn_and_r_type(self, g_fqdn, g_r_type):
sql = ' '.join(["SELECT count(*)",
"FROM dns_rr",
"WHERE fqdn = :fqdn",
"AND r_type = :r_type"])
self.mem_db['cursor'].execute(sql,
{"fqdn":g_fqdn, "r_type":g_r_type})
cnt = self.mem_db['cursor'].fetchone()[0]
return cnt
### Table: dns_rr_to_ip
def delete_dns_rr_to_ip_by_uuid_rr(self, g_uuid_rr):
sql = ' '.join(["DELETE FROM dns_rr_to_ip",
"WHERE uuid_rr = :uuid_rr"])
self.mem_db['cursor'].execute(sql,
{"uuid_rr":g_uuid_rr})
return True
def add_dns_rr_to_ip(self, uuid_rr, uuid_ip):
sql = ' '.join(["INSERT INTO dns_rr_to_ip",
"(uuid_rr, uuid_ip)"
"VALUES (:uuid_rr, :uuid_ip)"])
self.mem_db['cursor'].execute(sql,
{"uuid_rr":uuid_rr,
"uuid_ip":uuid_ip})
return True
### Table: dns_rr_parent_child
def add_dns_rr_parent_child(self, uuid_parent, uuid_child):
sql = ' '.join(["INSERT INTO dns_rr_parent_child",
"(uuid_parent, uuid_child)",
"VALUES (:uuid_parent, :uuid_child)"])
self.mem_db['cursor'].execute(sql,
{"uuid_parent":uuid_parent,
"uuid_child":uuid_child})
return True
def get_dns_rr_parent_child(self):
dns_rr_parent_child = []
sql = ' '.join(["SELECT uuid_parent, uuid_child",
"FROM dns_rr_parent_child"])
self.mem_db['cursor'].execute(sql)
for (uuid_parent, uuid_child) in self.mem_db['cursor']:
rec = {}
rec['uuid_parent'] = uuid_parent
rec['uuid_child'] = uuid_child
dns_rr_parent_child.append(rec)
return dns_rr_parent_child
### Table: fqdns
def add_fqdn(self, fqdn, uuid_parent):
# Status: "todo", "processing", "done"
u = str(uuid.uuid4())
sql = ' '.join(["INSERT INTO fqdns",
"(uuid_fqdn, fqdn, status, uuid_parent)",
"VALUES (:uuid_fqdn, :fqdn, :status, :uuid_parent)"])
self.mem_db['cursor'].execute(sql,
{"uuid_fqdn":u,
"fqdn": fqdn,
"status": "todo",
"uuid_parent": uuid_parent})
return u
def get_fqdns_not_done(self):
records = []
sql = ' '.join(["SELECT uuid_fqdn, fqdn, status, uuid_parent",
"FROM fqdns",
"WHERE status <> :status"])
self.mem_db['cursor'].execute(sql,
{"status":"done"})
for (uuid_fqdn, fqdn, status, uuid_parent) in self.mem_db['cursor']:
rec = {}
rec['uuid'] = uuid_fqdn
rec['fqdn'] = fqdn
rec['status'] = status
rec['uuid_parent'] = uuid_parent
records.append(rec)
return records
def get_fqdns_by_fqdn(self, g_fqdn):
records = []
sql = ' '.join(["SELECT uuid_fqdn, fqdn, status, uuid_parent",
"FROM fqdns",
"WHERE fqdn = :fqdn"])
self.mem_db['cursor'].execute(sql,
{"fqdn":g_fqdn})
for (uuid_fqdn, fqdn, status, uuid_parent) in self.mem_db['cursor']:
rec = {}
rec['uuid'] = uuid_fqdn
rec['fqdn'] = fqdn
rec['status'] = status
rec['uuid_parent'] = uuid_parent
records.append(rec)
return records
def update_fqdns_status_by_fqdn(self, u_fqdn, u_status):
records = []
sql = ' '.join(["UPDATE fqdns",
"SET status = :status",
"WHERE fqdn = :fqdn"])
self.mem_db['cursor'].execute(sql,
{"fqdn": u_fqdn,
"status": u_status})
return True
def count_fqdns_by_fqdn(self, c_fqdn):
sql = ' '.join(["SELECT count(*)",
"FROM fqdns",
"WHERE fqdn = :fqdn"])
self.mem_db['cursor'].execute(sql,
{"fqdn":c_fqdn})
cnt = self.mem_db['cursor'].fetchone()[0]
return cnt
def count_fqdns_by_status(self, c_status):
sql = ' '.join(["SELECT count(status)",
"FROM fqdns",
"WHERE status = :status"])
self.mem_db['cursor'].execute(sql,
{"status":c_status})
cnt = self.mem_db['cursor'].fetchone()[0]
return cnt
### Table: asn
def add_asn(self, asn, asn_description, asn_date, asn_registry, asn_country_code, asn_cidr):
u = str(uuid.uuid4())
sql = ' '.join(["INSERT INTO asn",
"(uuid_asn, asn, asn_description,",
"asn_date, asn_registry, asn_country_code,",
"asn_cidr)",
"VALUES (:uuid_asn, :asn, :asn_description,",
":asn_date, :asn_registry, :asn_country_code,",
":asn_cidr)"])
self.mem_db['cursor'].execute(sql,
{"uuid_asn":u,
"asn":asn,
"asn_description":asn_description,
"asn_date":asn_date,
"asn_registry":asn_registry,
"asn_country_code":asn_country_code,
"asn_cidr":asn_cidr})
return u
def count_asn_by_asn_and_asn_cidr(self, c_asn, c_asn_cidr):
sql = ' '.join(["SELECT count(*)"
"FROM asn",
"WHERE asn = :asn",
"AND asn_cidr = :asn_cidr"])
self.mem_db['cursor'].execute(sql,
{"asn":c_asn, "asn_cidr":c_asn_cidr})
cnt = self.mem_db['cursor'].fetchone()[0]
return cnt
def get_asns(self):
asns = []
sql = ' '.join(["SELECT uuid_asn, asn, asn_description,",
"asn_date, asn_registry, asn_country_code,",
"asn_cidr",
"FROM asn"])
self.mem_db['cursor'].execute(sql)
for (uuid_asn, asn, asn_description, asn_date,
asn_registry, asn_country_code, asn_cidr) in self.mem_db['cursor']:
rec = {}
rec['uuid_asn'] = uuid_asn
rec['asn'] = asn
rec['asn_description'] = asn_description
rec['asn_date'] = asn_date
rec['asn_registry'] = asn_registry
rec['asn_country_code'] = asn_country_code
rec['asn_cidr'] = asn_cidr
asns.append(rec)
return asns
def get_asn_by_asn_and_asn_cidr(self, c_asn, c_asn_cidr):
sql = ' '.join(["SELECT uuid_asn, asn, asn_description,",
"asn_date, asn_registry, asn_country_code,",
"asn_cidr",
"FROM asn",
"WHERE asn = :asn",
"AND asn_cidr = :asn_cidr"])
self.mem_db['cursor'].execute(sql,
{"asn":c_asn, "asn_cidr":c_asn_cidr})
for (uuid_asn, asn, asn_description, asn_date,
asn_registry, asn_country_code, asn_cidr) in self.mem_db['cursor']:
rec = {}
rec['uuid_asn'] = uuid_asn
rec['asn'] = asn
rec['asn_description'] = asn_description
rec['asn_date'] = asn_date
rec['asn_registry'] = asn_registry
rec['asn_country_code'] = asn_country_code
rec['asn_cidr'] = asn_cidr
# Only get the first, yes, indenting matters
return rec
### Table: ip
def add_ip(self, ip, version):
u = str(uuid.uuid4())
sql = ' '.join(["INSERT INTO ip (uuid_ip, ip, version)",
"VALUES (:uuid_ip, :ip, :version)"])
self.mem_db['cursor'].execute(sql,
{"uuid_ip":u, "ip":ip, "version":version})
return u
def count_ip_by_ip(self, c_ip):
sql = ' '.join(["SELECT count(ip)",
"FROM ip",
"WHERE ip = :ip"])
self.mem_db['cursor'].execute(sql,
{"ip":c_ip})
cnt = self.mem_db['cursor'].fetchone()[0]
return cnt
def get_ip_by_ip(self, g_ip):
sql = ' '.join(["SELECT uuid_ip, ip, version",
"FROM ip",
"WHERE ip = :ip"])
self.mem_db['cursor'].execute(sql,
{"ip":g_ip})
for (uuid_ip, ip, version) in self.mem_db['cursor']:
rec = {}
rec['uuid_ip'] = uuid_ip
rec['ip'] = ip
rec['version'] = version
# Only get the first, yes, indenting matters
return rec
def get_ips(self):
all_ips = []
sql = ' '.join(["SELECT uuid_ip, ip, version",
"FROM ip"])
self.mem_db['cursor'].execute(sql)
for (uuid_ip, ip, version) in self.mem_db['cursor']:
rec = {}
rec['uuid_ip'] = uuid_ip
rec['ip'] = ip
rec['version'] = version
all_ips.append(rec)
return all_ips
### Table: ip2asn
def add_ip2asn(self, uuid_ip, uuid_asn):
sql = ' '.join(["INSERT INTO ip2asn (uuid_ip, uuid_asn)",
"VALUES (:uuid_ip, :uuid_asn)"])
self.mem_db['cursor'].execute(sql,
{"uuid_ip":uuid_ip, "uuid_asn":uuid_asn})
return True
def get_ip2asns(self):
all_ip2asns = []
sql = ' '.join(["SELECT uuid_ip, uuid_asn",
"FROM ip2asn"])
self.mem_db['cursor'].execute(sql)
for (uuid_ip, uuid_asn) in self.mem_db['cursor']:
rec = {}
rec['uuid_ip'] = uuid_ip
rec['uuid_asn'] = uuid_asn
all_ip2asns.append(rec)
return all_ip2asns
### Table: redirect
def add_redirect(self, schema, fqdn, location):
u = str(uuid.uuid4())
sql = ' '.join(["INSERT INTO redirect (uuid_redir, schema, fqdn, location)",
"VALUES (:uuid_redir, :schema, :fqdn, :location)"])
self.mem_db['cursor'].execute(sql,
{"uuid_redir":u,
"schema":schema,
"fqdn":fqdn,
"location":location})
return u
def get_redirects(self):
all_redirects = []
sql = ' '.join(["SELECT uuid_redir, schema, fqdn, location",
"FROM redirect"])
self.mem_db['cursor'].execute(sql)
for (uuid_redir, schema, fqdn, location) in self.mem_db['cursor']:
rec = {}
rec['uuid_redir'] = uuid_redir
rec['schema'] = schema
rec['fqdn'] = fqdn
rec['location'] = location
all_redirects.append(rec)
return all_redirects
### Table: fqdn2redirect
def add_fqdn2redirect(self, uuid_fqdn, uuid_redir):
sql = ' '.join(["INSERT INTO fqdn2redirect (uuid_fqdn, uuid_redir)",
"VALUES (:uuid_fqdn, :uuid_redir)"])
self.mem_db['cursor'].execute(sql,
{"uuid_fqdn":uuid_fqdn,
"uuid_redir":uuid_redir})
return True
def get_fqdn2redirects(self):
all_fqdn2redirects = []
sql = ' '.join(["SELECT uuid_fqdn, uuid_redir",
"FROM fqdn2redirect"])
self.mem_db['cursor'].execute(sql)
for (uuid_fqdn, uuid_redir) in self.mem_db['cursor']:
rec = {}
rec['uuid_fqdn'] = uuid_fqdn
rec['uuid_redir'] = uuid_redir
all_fqdn2redirects.append(rec)
return all_fqdn2redirects
### Drawing stuff
def plot(self):
self.MainGraph.add_node(self.uuid_hunt, style='filled', color='blue', fontcolor='white',
label="Main search domain is:\n" + self.base_fqdn)
# Plot the FQDN with RR results and tie that to the main node
all_dns_rr = self.get_dns_rr()
for rec in all_dns_rr:
# Color by RR type
if rec['r_type'] == "CAA":
color = 'yellow'
fontcolor = 'black'
elif rec['r_type'] == "NS":
color = 'darkgoldenrod1'
fontcolor = 'blue'
elif rec['r_type'] == "MX":
color = 'orange'
fontcolor = 'blue'
elif rec['r_type'] == "SOA":
color = 'black'
fontcolor = 'white'
elif rec['r_type'] == "A":
color = 'red'
fontcolor = 'white'
elif rec['r_type'] == "AAAA":
color = 'crimson'
fontcolor = 'white'
elif rec['r_type'] == "CNAME":
color = 'gray40'
fontcolor = 'white'
elif rec['r_type'] == "TXT":
color = 'darkviolet'
fontcolor = 'white'
else:
color = 'red'
fontcolor = 'black'
# Plot all the nodes
self.MainGraph.add_node(rec['uuid_rr'], style='filled',
color=color,
fontcolor=fontcolor,
label=rec['fqdn'] + "\n" + rec['r_type'] + "\n" + rec['value'])
# Cluster the Name Servers per domain and link them to the virtual blob
ll_ns = {}
for rec_ns in all_dns_rr:
# Color by RR type
if rec_ns['r_type'] == "NS":
for rec_ns_inner in all_dns_rr:
if rec_ns_inner['r_type'] == "NS" and rec_ns['fqdn'] == rec_ns_inner['fqdn']:
if not rec_ns['fqdn'] in ll_ns:
ll_ns[rec_ns['fqdn']] = []
if rec_ns_inner['value'] not in ll_ns[rec_ns['fqdn']]:
ll_ns[rec_ns['fqdn']].append(rec_ns_inner['value'])
for k in ll_ns.keys():
ll_label = '\n'.join(sorted(ll_ns[k]))
ll_label_start = ' '.join(['NS:', k, "\n"])
u = str(uuid.uuid4())
self.MainGraph.add_node(u, style='filled',
color='gray30',
fillcolor='cornsilk',
fontcolor='black',
label=ll_label_start + ll_label)
for l_v in sorted(ll_ns[k]):
for rec_ns in all_dns_rr:
if rec_ns['r_type'] == "NS" and rec_ns['fqdn'] == k and rec_ns['value'] == l_v:
self.MainGraph.add_edge(rec_ns['uuid_rr'], u)
# HACK: re-plot the CNAME linkage to all RR types not yet linked
for rr in self.get_dns_rr():
if rr['r_type'] == 'CNAME':
for rr_inner in self.get_dns_rr():
# Combine the CNAME value (minus the dot final character) to whatever RR
if rr['value'][:-1] == rr_inner['fqdn']:
self.add_dns_rr_parent_child(rr['uuid_rr'], rr_inner['uuid_rr'])
# Plot the DNS RR Type linkages
all_dns_rr_parent_child = self.get_dns_rr_parent_child()
for rec in all_dns_rr_parent_child:
# Link up node
self.MainGraph.add_edge(rec['uuid_parent'], rec['uuid_child'])
# Plot all IP addresses
all_ips = self.get_ips()
for rec in all_ips:
# Plot all the nodes
if rec['version'] == "6":
self.MainGraph.add_node(rec['uuid_ip'], style='filled',
color='hotpink',
fontcolor='black',
label=rec['ip'] + "\n" + "version: " + rec['version'])
elif rec['version'] == "4":
self.MainGraph.add_node(rec['uuid_ip'], style='filled',
color='lightpink1',
fontcolor='black',
label=rec['ip'] + "\n" + "version: " + rec['version'])
# Attach the IP addresses to the DNS RR records with these values
for rec_rr in all_dns_rr:
for rec_ip in all_ips:
if rec_rr['value'] == rec_ip['ip']:
self.MainGraph.add_edge(rec_rr['uuid_rr'], rec_ip['uuid_ip'])
# Plot AS Number info
main_asn = []
all_asns = self.get_asns()
for rec in all_asns:
ll = []
ll.append("AS per CIDR\n")
if rec['asn'] is not None:
ll.append(rec['asn'])
ll.append("\n")
if rec['asn_description'] is not None:
ll.append(rec['asn_description'])
ll.append("\n")
if rec['asn_date'] is not None:
ll.append(rec['asn_date'])
ll.append("\n")
if rec['asn_cidr'] is not None:
ll.append(rec['asn_cidr'])
ll.append("\n")
label = ' '.join(ll)
self.MainGraph.add_node(rec['uuid_asn'], style='filled',
color='forestgreen',
fontcolor='white',
label=label)
# Check if exists first, if not - add
if not any(d.get('asn', None) == rec['asn'] for d in main_asn):
m_asn = {}
m_asn['uuid_main_asn'] = str(uuid.uuid4())
m_asn['asn'] = rec['asn']
m_asn['asn_description'] = rec['asn_description']
m_asn['asn_registry'] = rec['asn_registry']
m_asn['asn_country_code'] = rec['asn_country_code']
main_asn.append(m_asn)
# Attach the AS Number blobs (per CIDR) to the IP address
all_ip2asns = self.get_ip2asns()
for rec in all_ip2asns:
self.MainGraph.add_edge(rec['uuid_ip'], rec['uuid_asn'])
# Bonus - stitch ASN record blobs per CIDR to eac other per ASN
for ma in main_asn:
cidrs = []
for rec in all_asns:
if rec['asn'] == ma['asn']:
if 'asn_cidr' in rec and rec['asn_cidr'] is not None:
cidrs.append(rec['asn_cidr'])
llma = []
llma.append("ASN:\n")
if ma['asn'] is not None:
llma.append(ma['asn'])
llma.append("\n")
if ma['asn_description'] is not None:
llma.append(ma['asn_description'])
llma.append("\n")
if ma['asn_registry'] is not None:
llma.append(ma['asn_registry'])
llma.append("\n")
if ma['asn_country_code'] is not None:
llma.append(ma['asn_country_code'])
llma.append("\n")
label = ' '.join(llma)
for cidr in sorted(cidrs):
label = label + "\n" + cidr
self.MainGraph.add_node(ma['uuid_main_asn'], style='filled',
color='lawngreen',
fontcolor='black',
label=label)
# Consolidate the ASN numbers by their number
for rec in all_asns:
for ma in main_asn:
if rec['asn'] == ma['asn']:
self.MainGraph.add_edge(rec['uuid_asn'], ma['uuid_main_asn'])
# URL location
redir = self.get_redirects()
for rd in redir:
label = ''.join([rd['schema'],
rd['fqdn'], '\n',
'Location', '\n',
rd['location']
])
self.MainGraph.add_node(rd['uuid_redir'], style='filled',
color='purple',
fontcolor='white',
label=label)
links = self.get_dns_rr_by_fqdn(rd['fqdn'])
for l in links:
self.MainGraph.add_edge(l['uuid_rr'], rd['uuid_redir'])
# URL link fqdn to link
# fqdn2redirects = self.get_fqdn2redirects()
# for fr in fqdn2redirects:
# self.MainGraph.add_edge(fr['uuid_fqdn'], fr['uuid_redir'])
def draw_svg(self, destination):
# Init graphviz
self.MainGraph = AGraph(overlap=False,rankdir="LR")
# Plot the map
self.plot()
# Finish lay-out and write the graph
self.MainGraph.layout()
self.MainGraph.draw(destination, prog='dot')
def draw_txt(self, destination):
f = open(destination, "w")
all_dns_rr = self.get_dns_rr()
all_dns_rr_parent_child = self.get_dns_rr_parent_child()
for rec in all_dns_rr:
for rec_pc in all_dns_rr_parent_child:
if rec_pc['uuid_parent'] == self.uuid_hunt and rec['uuid_rr'] == rec_pc['uuid_child']:
f.write(''.join([self.base_fqdn, " (base2fqdn) ", rec['fqdn'], " {", rec['r_type'], "/", rec['value'], "}", "\n"]))
for rec in all_dns_rr:
f.write(''.join([rec['fqdn'], " (", rec['r_type'], ") ", rec['value'], "\n"]))
# IP to ASN
all_ips = self.get_ips()
all_ip2asns = self.get_ip2asns()
all_asns = self.get_asns()
for rec in all_asns:
llma = []
llma.append("ASN:")
if rec['asn'] is not None:
llma.append(rec['asn'])
if rec['asn_description'] is not None:
llma.append(rec['asn_description'])
if rec['asn_registry'] is not None:
llma.append(rec['asn_registry'])
if rec['asn_country_code'] is not None:
llma.append(rec['asn_country_code'])
label = ' '.join(llma)
for rec_ip in all_ips:
for ip2asn in all_ip2asns:
if ip2asn['uuid_ip'] == rec_ip['uuid_ip']:
if ip2asn['uuid_asn'] == rec['uuid_asn']:
f.write(''.join([rec_ip['ip'], " (ip2asn) ", label, "\n"]))
f.close()
# HACK: re-plot the CNAME linkage to all RR types not yet linked
# for rr in self.get_dns_rr():
# if rr['r_type'] == 'CNAME':
# for rr_inner in self.get_dns_rr():
# # Combine the CNAME value to whatever RR
# if rr['value'] == rr_inner['fqdn']:
# self.add_dns_rr_parent_child(rr['uuid_rr'], rr_inner['uuid_rr'])
### Functions
def analyse_record2(uuid_child, uuid_parent, k, key_type, val, val_type, status, reason, dt, last_key_is_fqdn):
try:
# Remember where we came from. Required for SPF1 and DMARC
if key_type == 'FQDN':
last_key_is_fqdn = k
if val_type == 'FQDN':
# Need to resolve this FQDN again, but with the current uuid_child as its parent
if w.count_fqdns_by_fqdn(val) == 0:
w.add_fqdn(val, uuid_child)
# Planned exit or stop condition
if (key_type == 'FQDN' and (val_type == 'CAA' or val_type == 'SOA')) or \
(key_type == 'DNS_R_TYPE' and val_type == 'SPF1'):
print ("analyse_record2", "Explicit final",
'key_type', key_type,
'key', k,
'val_type', val_type,
'value', val,
file=sys.stderr)
return
elif key_type == 'FQDN' and val_type == 'NS':
# The NS value is an FQDN per default
print ("analyse_record2", "debug", 'key_type', key_type, 'key', k,
'val_type', val_type, 'value', val,
file=sys.stderr)
if w.count_fqdns_by_fqdn(val) == 0:
w.add_fqdn(val, uuid_child)
return
elif key_type == 'FQDN' and val_type == 'CNAME':
print ("analyse_record2", "debug", 'key_type', key_type, 'key', k,
'val_type', val_type, 'value', val,
file=sys.stderr)
# Clean up the result by ditching the dot
cleaned_up_val = str(val)[:-1]
# Add to main resolve if it was never on the list.
if w.count_fqdns_by_fqdn(cleaned_up_val) == 0:
# Avoid endless loops - most often this is just True
if cleaned_up_val != k:
w.add_fqdn(cleaned_up_val, uuid_child)
# Add link to existing FQDNs
# First search for other records with this FQDN. Link with this, CNAME is the parent
#parent, child
res = w.get_dns_rr_by_fqdn(cleaned_up_val)
for rr in res:
print("CNAME link to DNS RR", rr['r_type'], rr['fqdn'], file=sys.stderr)
w.add_dns_rr_parent_child(uuid_child, rr['uuid_rr'])
elif key_type == 'FQDN' and val_type == 'MX':
print ("analyse_record2", "debug", 'key_type', key_type, 'key', k,
'val_type', val_type, 'value', val,
file=sys.stderr)
priority = val.split()[0]
exchange = val.split()[1][:-1]
if w.count_fqdns_by_fqdn(exchange) == 0:
w.add_fqdn(exchange, uuid_child)
elif key_type == 'SPF1' and val_type == "INCLUDE":
print ("analyse_record2", "debug", 'key_type', key_type, 'key', k,
'val_type', val_type, 'value', val,
file=sys.stderr)
if w.count_fqdns_by_fqdn(val) == 0:
w.add_fqdn(val, uuid_child)
elif val_type == 'TXT':
print ("analyse_record2", "debug", 'key_type', key_type, 'key', k,
'val_type', val_type, 'value', val,
file=sys.stderr)
clean_value = re.sub(r'^"|"$', '', str(val))
# TXT record is already stored, see what's in the TXT and that is
# your next child node
if clean_value.lower().startswith('v=spf1'):
# Found SPFv1 record
analyse_record2(uuid_child, uuid_parent,
val,
val_type,
clean_value,
"SPF1",
"ANALYSED", "", dt,
last_key_is_fqdn)
elif val_type == 'SPF1':
print ("analyse_record2", "debug", 'key_type', key_type, 'key', k,
'val_type', val_type, 'value', val,
file=sys.stderr)
for elem in val.split():
print(elem, file=sys.stderr)
# Found an A record in the SPF
if elem.upper() == 'A' or elem.upper() == 'AAAA':
print ("analyse_record2",
"undecided what to do to avoid an endless recursion",
'key_type', key_type,
'key', k,
'val_type', val_type,
'value', val,
'elem',
elem,
'last_key_is_fqdn',
last_key_is_fqdn,
file=sys.stderr)
# Continue analysing
analyse_record2(uuid_child, uuid_parent,
elem.upper(),
"DNS_R_TYPE",
val,
val_type,
"ANALYSED", "", dt,
last_key_is_fqdn)
# Found an INCLUDE statement in the SPF
if elem.lower().startswith('include:'):
# Continue analysing
analyse_record2(uuid_child, uuid_parent,
val,
val_type,
elem.split(':')[-1],
"INCLUDE",
"ANALYSED", "", dt,
last_key_is_fqdn)
# Found an IPv4 statement in the SPF
if elem.lower().startswith('ip4:'):
# Continue analysing
analyse_record2(uuid_child, uuid_parent,
val,
val_type,
elem.split(':')[-1],
"IPV4_CIDR",
"ANALYSED", "", dt,
last_key_is_fqdn)
# Found an IPv6 statement in the SPF
if elem.lower().startswith('ip6:'):
# Continue analysing
analyse_record2(uuid_child, uuid_parent,
val,
val_type,
elem.split(':')[-1],
"IPV6_CIDR",
"ANALYSED", "", dt,
last_key_is_fqdn)
# elif key_type == 'SPF1' and val_type == "DNS_R_TYPE":
# if val == 'A' or val == 'AAAA':
# analyse_record2(uuid_child, uuid_parent,
# val,
# val_type,
# last_key_is_fqdn,
# "FQDN",
# "ANALYSED", "", dt,
# last_key_is_fqdn)
# A, AAAA or results from SPF1 and other records with an IP address in it
### Error handling CIDR notation -
### elif val_type == 'A' or val_type == 'AAAA' or val_type == "IPV4_CIDR" or val_type == "IPV6_CIDR":
elif val_type == 'A' or val_type == 'AAAA':
print ("analyse_record2", "debug", 'key_type', key_type, 'key', k,
'val_type', val_type, 'value', val, file=sys.stderr)
# Currenct RR is uuid_child, which has an A result.
# This is stored already. Need to add the IP
# and bind the IP uuid to the RR uuid, which is the child_uuid
if w.count_ip_by_ip(val) == 0:
if val_type == 'A':
uuid_ip = w.add_ip(val, 4)
elif val_type == 'AAAA':
uuid_ip = w.add_ip(val, 6)
# The new IP needs an ASN resolve and IP to ASN attachment
# Take A or AAAA value to resolve as part of an AS plus AS info
asn_result = analyse_asn(val)
# The IP is now resolved to an ASN. Did we have this one from another ASN?
# If yes, get that one, if not, create a new one.
# Result is an uuid_asn
if w.count_asn_by_asn_and_asn_cidr(asn_result['asn'],
asn_result['asn_cidr']) == 0:
uuid_asn = w.add_asn(asn_result['asn'], asn_result['asn_description'],
asn_result['asn_date'], asn_result['asn_registry'],
asn_result['asn_country_code'], asn_result['asn_cidr'])
else:
rec_asn = w.get_asn_by_asn_and_asn_cidr(asn_result['asn'],
asn_result['asn_cidr'])
uuid_asn = rec_asn['uuid_asn']
# Combine this IP address with an the ASN per CIDR
w.add_ip2asn(uuid_ip, uuid_asn)
else:
rec_ip = w.get_ip_by_ip(val)
uuid_ip = rec_ip['ip']
# in all cases, uuid_ip is the new one or the existing one
w.add_dns_rr_to_ip(uuid_child, uuid_ip)
else:
print ("analyse_record2", "Final reached",
'key_type', key_type,
'key', k,
'val_type', val_type,
'value', val,
file=sys.stderr)
except Exception as inst: