-
Notifications
You must be signed in to change notification settings - Fork 3
/
tldiscover.py
executable file
·272 lines (234 loc) · 9.11 KB
/
tldiscover.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__description__ = 'TLDiscover: A top-level-domain fuzzer!'
__version__ = '1.5'
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import time
import json
import folium
import logging
import requests
from validators import (
url as valid_url,
domain as valid_domain,
)
from tldata import tlds
from Queue import Queue
from threading import Thread
from bs4 import BeautifulSoup
from urlparse import urlparse
from datetime import datetime
from socket import gethostbyname
from collections import defaultdict
from fake_useragent import UserAgent
from colorama import Fore,Back,Style
from argparse import ArgumentParser, RawTextHelpFormatter, ArgumentTypeError
BASE_URL, USER_AGENT = None, 'tldiscover_user_agent_v{}'.format(__version__)
TLDS = Queue()
TIMEOUT = 10
DOMAINS, WORKERS = [], []
VERBOSITY, LOG = False, False
INITIAL_SIZE = 0
# console colors
F, S, BT, UN, IT = Fore.RESET, Style.RESET_ALL, Style.BRIGHT, '\033[4m', '\033[3m'
FG, FR, FC, FY, BR, FB, FM = Fore.GREEN, Fore.RED, Fore.CYAN, Fore.YELLOW, Back.RED, Fore.BLUE, Fore.MAGENTA
def console():
"""argument parser"""
parser = ArgumentParser(description="{0}{1}TLD{2}{0}discover{2} Top level domain discovery.".format(BT,FB,S),formatter_class=RawTextHelpFormatter)
parser._optionals.title = "{}arguments{}".format(BT,S)
parser.add_argument('-u', "--url", required=True, type=validateDomain,
help='Specify a domain to start with.', metavar='')
parser.add_argument('-t', "--threads", required=False, type=int, default=None, metavar='',
help="Specify number of threads to use [{0}default:{2}{1} None{2}]".format(BT,FG,S))
parser.add_argument('-v', "--verbose", action='store_true',
help="Enable verbosity [{0}default {2}{1}False{2}]".format(BT,FR,S))
parser.add_argument('-a', "--useragent", action='store_true',
help="User a random user-agent [{0}default {2}{1}False{2}]".format(BT,FR,S))
parser.add_argument('-l', "--log", action='store_true',
help="Save the results in a log file [{0}Default:{2} {1}False{2}]".format(BT, FR, S))
parser.add_argument('-m', "--map", action='store_true',
help="Create a map [{0}Default:{2} {1}False{2}]".format(BT, FR, S))
parser.add_argument("--timeout", type=int, default=10,
help="Specify HTTP connection timeout [{0}default {2}{1}10 sec{2}]".format(BT,FG,S), metavar='')
args = parser.parse_args()
return args
def ret(t):
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
time.sleep(t)
def getNetloc(url):
"""returns url netloc"""
return urlparse(url).netloc
def getBase(url):
"""returns the base url"""
parsed = urlparse(url)
return "://".join([parsed.scheme, parsed.netloc.rsplit('.',1)[0]])
def validateDomain(url):
"""check if the url is valid"""
try:
if not valid_url(url):
raise ArgumentTypeError('\n{}[x] Invalid url.{}\n'.format(BR, S))
elif not valid_domain(getNetloc(url)):
raise ArgumentTypeError('\n{}[x] Invalid domain.{}\n'.format(BR, S))
else:
return url
except Exception, e:
print e
sys.exit(0)
def get_progress(size):
"""returns progress so far..."""
return '{0:.2f}'.format(100 * (1 - float(size)/INITIAL_SIZE))
def getUA():
"""returns a fake random user-agent"""
user_agent = UserAgent()
return str(user_agent.random)
def get_title(content):
"""retrieves websites title"""
try:
title = BeautifulSoup(content, 'html.parser').title.string.strip()
return title if title else 'Unknown title'
except:
return 'Unknown title'
def resolve_ip(domain):
'''returns the IP responding to a given domain name'''
try:
return gethostbyname(domain)
except Exception, e:
return None
def craft_map(geo_info):
"""creates a simple map based on the lat,lon & domain provided"""
try:
map_osm = folium.Map(zoom_start=13)
for ip,info in geo_info.items():
marker_icon = folium.Icon(color='green') if ',' in info[2] else folium.Icon(color='blue')
folium.Marker([info[0],info[1]],popup=info[2],icon=marker_icon).add_to(map_osm)
filename = '{}.html'.format(getNetloc(BASE_URL))
map_osm.save(filename)
return filename
except Exception, e:
ret(.1)
print "{}[x] Unfortunately map couldnt be created!{}".format(BR,S)
sys.exit(0)
def geolocate(ip):
"""get lat, lon of an ip"""
resolver = "http://ip-api.com/json/{}".format(ip)
response = json.loads(requests.get(resolver).text)
if response['status']=="success":
lat, lon = response['lat'], response['lon']
if lat!=None and lon!=None:
return lat,lon
else:
return None,None
else: return None, None
def onExit(start_time, create_map):
"""prints some basic information while exiting"""
print '\n{0}[+]{1} Found {0}{2}{1} domains!'.format(FG,S,len(DOMAINS))
print '{0}[+]{1} Total duration: {0}{2}{1}'.format(FG,S,datetime.now()-start_time)
# create map if specified
if create_map:
print "{}[*]{} Creating map, please wait...".format(FY,S)
ips = defaultdict(list)
for domain in DOMAINS:
ip = resolve_ip(getNetloc(domain))
if ip:
ips[ip].append(getNetloc(domain))
if ips:
geo_info = {}
for ip, domains in ips.items():
lat, lon = geolocate(ip)
if lat and lon:
geo_info[ip] = [lat, lon, ', '.join(domains)]
filename = craft_map(geo_info)
ret(.1)
ret(.1)
print "{0}[+]{1} Map: {0}{2}{1} successfully created!".format(FG,S,filename)
else:
ret(.1)
print '{}[x] Unfortunately no IPs resolved.{}\n'.format(BR,S)
sys.exit(0)
def check(domain):
"""checks if a domain exists..."""
try:
res = requests.get(domain, timeout=TIMEOUT, headers={'User-Agent':USER_AGENT})
if res.history:
DOMAINS.append(res.url)
title = get_title(res.text)
print "{0}[+]{2} Found: {4} → {1}{3}{2} ({5})".format(FG,BT,S, res.url, domain, IT+title+S)
if LOG:
logging.info('Found: {1} → {0} Title: {2}'.format(res.url,domain,title))
else:
DOMAINS.append(domain)
title = get_title(res.text)
print "{1}[+]{2} Found: {0}{3}{2} ({4})".format(BT,FG,S, domain, IT+title+S)
if LOG:
logging.info('Found: {0} Title: {1}'.format(domain,title))
except requests.exceptions.ConnectionError:
if VERBOSITY:
print "{0}[{3}%]{1} Connection error for: {2}".format(FR,S,domain,get_progress(TLDS.qsize()))
ret(.1)
else: pass
except requests.exceptions.TooManyRedirects:
if VERBOSITY:
print "{0}[{3}%]{1} Too many redirects for: {2}".format(FB,S,domain,get_progress(TLDS.qsize()))
ret(.1)
else: pass
except requests.exceptions.Timeout:
if VERBOSITY:
print "{0}[{3}%]{1} Timeout error for: {2}".format(FY,S,domain,get_progress(TLDS.qsize()))
ret(.1)
else: pass
def discover():
while not TLDS.empty():
check(TLDS.get())
if __name__ == '__main__':
user = console()
# global variables configuration
BASE_URL = getBase(user.url)
if user.useragent: USER_AGENT = getUA()
if user.verbose: VERBOSITY = True
if user.timeout: TIMEOUT = user.timeout
if user.log:
LOG = True
logging.getLogger().setLevel(logging.INFO)
logging.basicConfig(filename='{}{}'.format(getNetloc(user.url).split()[0], '.log'), format='%(asctime)s %(message)s')
print "{}[*]{} Threads: {}".format(FC,S,FG+str(user.threads)+S)
for tld in tlds:
TLDS.put(BASE_URL+'.'+tld)
INITIAL_SIZE = TLDS.qsize()
start_time = datetime.now()
# threaded mode...
if user.threads:
if user.threads > TLDS.qsize():
print "{0}[x]{1} Invalid number of threads...".format(FR,S)
sys.exit(0)
print '{0}[+]{2} Multi-threaded mode: {1}ON{2}\n'.format(FC,FG,S)
for _ in range(user.threads):
t = Thread(target=discover)
t.daemon = True
WORKERS.append(t)
t.start()
try:
scanning = True
while scanning:
scanning = False
for _ in WORKERS:
if _.isAlive():
scanning = True
time.sleep(.1)
except KeyboardInterrupt:
for worker in WORKERS:
worker.join(.1)
finally:
onExit(start_time, user.map)
# non threaded mode ...
else:
try:
print '{0}[+]{2} Multi-threaded mode: {1}OFF{2}\n'.format(FC,FR,S)
discover()
except KeyboardInterrupt:
print '{}[!] Quiting...{}'.format(BR,S)
finally:
onExit(start_time, user.map)
# -EOF