-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathhosts_to_domains
executable file
·77 lines (67 loc) · 2.66 KB
/
hosts_to_domains
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
#!/usr/bin/env python3
###########
# IMPORTS #
###########
import os
import sys
import argparse
########
# MAIN #
########
if __name__ == '__main__':
desc = 'Extract domains from the supplied FQDNs and output a list of results.'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('file',
nargs='?',
type=argparse.FileType('r'),
action='store',
help='file containing a list of IP addresses split by a newline, otherwise read from STDIN',
metavar='FILE',
default=sys.stdin)
parser.add_argument('-s', '--suffixes',
nargs='?',
type=argparse.FileType('r'),
action='store',
help='a list of TLDs to use as suffixes to aid identification (default: %s/wordlists/dns/tlds.txt)' % os.path.dirname(os.path.realpath(__file__)),
metavar='FILE',
default="%s/wordlists/dns/tlds.txt" % os.path.dirname(os.path.realpath(__file__)))
parser.add_argument('-d', '--depth',
type=int,
action='store',
help='only list domains up to a maximum depth (default: unlimited)',
metavar='INT',
default=0)
args = parser.parse_args()
try:
hosts = [line.strip() for line in args.file if len(line.strip())>0 and line[0] != '#']
except KeyboardInterrupt:
exit()
try:
suffixes = [line.strip() for line in args.suffixes if len(line.strip())>0 and line[0] != '#']
except KeyboardInterrupt:
exit()
domains = []
for host in hosts:
elements = host.split('.')
# recursively walk through the elements
# extracting all possible (sub)domains
while len(elements) >= 2:
# account for domains stored as hosts
if len(elements) == 2:
domain = '.'.join(elements)
else:
# drop the host element
domain = '.'.join(elements[1:])
if domain not in domains + suffixes:
domains.append(domain)
del elements[0]
for domain in domains:
if args.depth < 1:
print(domain)
else:
for suffix in sorted(suffixes, key=lambda x: x.count('.'), reverse=True):
if domain.lower().endswith('.' + suffix.lower()):
sub = domain[:-(len(suffix) + 1)]
if sub.count('.') < args.depth:
print(domain)
break