-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbrute-dns-hostnames
executable file
·168 lines (143 loc) · 5.37 KB
/
brute-dns-hostnames
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
#!/usr/bin/env python2.7
###########
# IMPORTS #
###########
import os
import sys
import argparse
import socket
import dns
import dns.resolver
import dns.reversename
import time
from multiprocessing import Pool
#from multiprocessing.dummy import Pool # utilise threads rather than subprocesses
import signal
####################
# GLOBAL VARIABLES #
####################
global nameserver
global addresses
global wildcards
#############
# FUNCTIONS #
#############
def resolve(hostname):
max_attempts = 3
resolver = dns.resolver.get_default_resolver()
resolver.nameservers = [nameserver]
resolver.lifetime = 3
attempt = 0
while attempt < max_attempts:
try:
answers = resolver.query(hostname)
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer, dns.resolver.NoNameservers, dns.name.EmptyLabel) as e:
#sys.stderr.write('%s => No record found.\n' % (domain))
pass
except dns.resolver.Timeout:
attempt += 1
if attempt == max_attempts:
sys.stderr.write('%s => Request timed out.\n' % (hostname))
continue
else:
for i in range(0, len(answers)):
address = answers[i].address
if address != wildcards[domain]:
if addresses is None or address in addresses:
sys.stdout.write('%s,%s\n' % (address, hostname))
sys.stdout.flush()
# break out of the loop
attempt = max_attempts
def check_wildcard(domain):
max_attempts = 3
resolver = dns.resolver.get_default_resolver()
resolver.nameservers = [nameserver]
resolver.lifetime = 3
attempt = 0
while attempt < max_attempts:
wildcard = None
try:
epochtime = str(int(time.time()))
answers = resolver.query('a%s.%s' % (epochtime, domain))
wildcard = answers.response.answer[0][0]
sys.stderr.write('%s => Wildcard entry found, %s\n' % (domain, wildcard))
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer, dns.resolver.NoNameservers):
#sys.stderr.write('%s => No record found.\n' % (hostname))
pass
except dns.resolver.Timeout:
attempt += 1
if attempt == max_attempts:
sys.stderr.write('%s => Request timed out querying for wildcard entry.\n' % (domain))
continue
return wildcard
def initializer():
"""Ignore CTRL+C in the worker process."""
signal.signal(signal.SIGINT, signal.SIG_IGN)
########
# MAIN #
########
if __name__ == '__main__':
desc = 'Brute force host names using a wordlist and supplied domains and output results in CSV format.'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('-n', '--nameserver',
action='store',
help='nameserver to use for lookups (default: 8.8.8.8)',
metavar='NS',
default='8.8.8.8')
parser.add_argument('-w', '--wordlist',
action='store',
help='a list of words to use for brute force attack (default: %s/wordlists/dns/hostnames.txt)' % os.path.dirname(os.path.realpath(__file__)),
metavar='FILE',
default="%s/wordlists/dns/hostnames.txt" % os.path.dirname(os.path.realpath(__file__)))
parser.add_argument('-f', '--filter',
action='store',
help='a list of IP addresses to filter against, only matching results are printed',
metavar='FILE',
default=None)
parser.add_argument('file',
nargs='?',
type=argparse.FileType('r'),
action='store',
help='file containing a list of domains split by a newline, otherwise read from STDIN',
metavar='FILE',
default=sys.stdin)
args = parser.parse_args()
global nameserver
nameserver = args.nameserver
# compile wordlist for hostname brute force
with open(args.wordlist) as fp:
wordlist = [line.strip().lower() for line in fp if len(line)>0 and line[0] is not '#']
# compile filter (if supplied)
global addresses
if args.filter:
with open(args.filter) as fp:
addresses = [line.strip().lower() for line in fp if len(line)>0 and line[0] is not '#']
else:
addresses = None
# compile list of domains to brute force
try:
domains = [line.strip() for line in args.file]
except KeyboardInterrupt:
exit()
# remove duplicates and sort
domains = list(set(domains))
domains = sorted(domains)
# check for a wildcard entry for each domain
global wildcards
wildcards = dict()
for domain in domains:
wildcards[domain] = check_wildcard(domain)
# compile a list of brute force canidates (hostname + domain)
candidates = list()
for domain in domains:
for word in wordlist:
candidate = '%s.%s' % (word, domain)
candidates.append(candidate.lower())
pool = Pool(processes=10, initializer=initializer)
try:
pool.map(resolve, candidates)
pool.close()
pool.join()
except KeyboardInterrupt:
pool.terminate()
pool.join()