forked from gwen001/pentest-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsexpire.py
executable file
·232 lines (178 loc) · 6.06 KB
/
dnsexpire.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
#!/usr/bin/python3
# I don't believe in license.
# You can do whatever you want with this program.
import os
import sys
import re
import socket
# import whois
import pythonwhois
import subprocess
import argparse
import tldextract
from colored import fg, bg, attr
from datetime import datetime
from threading import Thread
from queue import Queue
from multiprocessing.dummy import Pool
def banner():
print("""
_ _
__| |_ __ ___ _____ ___ __ (_)_ __ ___ _ __ _ _
/ _` | '_ \/ __|/ _ \ \/ / '_ \| | '__/ _ \ | '_ \| | | |
| (_| | | | \__ \ __/> <| |_) | | | | __/ _ | |_) | |_| |
\__,_|_| |_|___/\___/_/\_\ .__/|_|_| \___| (_) | .__/ \__, |
|_| |_| |___/
by @gwendallecoguic
""")
pass
banner()
ALERT_LIMIT = 30
parser = argparse.ArgumentParser()
parser.add_argument( "-a","--all",help="also test dead hosts and non alias", action="store_true" )
parser.add_argument( "-o","--host",help="set host, can be a file or single host" )
parser.add_argument( "-t","--threads",help="threads, default 10" )
parser.add_argument( "-v","--verbose",help="display output, can be: 0=everything, 1=only alias, 2=only possible vulnerable, default 1" )
parser.parse_args()
args = parser.parse_args()
if args.threads:
_threads = int(args.threads)
else:
_threads = 10
if args.verbose:
_verbose = int(args.verbose)
else:
_verbose = 1
if args.all:
_testall = True
else:
_testall = False
t_hosts = []
if args.host:
if os.path.isfile(args.host):
fp = open( args.host, 'r' )
t_hosts = fp.read().strip().split("\n")
fp.close()
else:
t_hosts = [args.host]
n_host = len(t_hosts)
if not n_host:
parser.error( 'hosts list missing' )
sys.stdout.write( '%s[+] %d hosts loaded: %s%s\n' % (fg('green'),n_host,args.host,attr(0)) )
sys.stdout.write( '[+] resolving...\n\n' )
def resolve( host ):
try:
cmd = 'host ' + host
# print(cmd)
output = subprocess.check_output( cmd, stderr=subprocess.STDOUT, shell=True ).decode('utf-8')
# print( output )
except Exception as e:
# sys.stdout.write( "%s[-] error occurred: %s%s\n" % (fg('red'),e,attr(0)) )
output = ''
return output
def getDomain( host ):
t_host_parse = tldextract.extract( host )
return t_host_parse.domain + '.' + t_host_parse.suffix
def getWhois( domain ):
if not domain in t_whois_history:
try:
w = pythonwhois.get_whois( domain )
# w = whois.whois( domain )
t_whois_history[ domain ] = w
except Exception as e:
sys.stdout.write( "%s[-] error occurred: %s (%s)%s\n" % (fg('red'),e,domain,attr(0)) )
return False
return t_whois_history[domain]
def getExpirationDate( domain ):
whois = getWhois( domain )
# print(type(whois))
if not type(whois) is bool and 'expiration_date' in whois:
# if type(whois.expiration_date) is list:
# return whois.expiration_date[0]
# else:
# return whois.expiration_date
if type(whois['expiration_date']) is list:
return whois['expiration_date'][0]
else:
return whois['expiration_date']
return False
else:
return False
def getColor( expiration_date ):
# expiration_date = datetime(2019, 12, 29, 6, 56, 55)
timedelta = expiration_date - datetime.now()
# print(timedelta)
if timedelta.days < -1: # to avoid false positive from smart whois who always return the current date
return 'light_red'
elif timedelta.days < ALERT_LIMIT:
return 'light_yellow'
else:
return 'light_green'
def printExpirationDate( domain ):
expiration_date = getExpirationDate( domain )
# print(type(expiration_date))
if type(expiration_date) is datetime:
color = getColor( expiration_date )
if color == 'light_red':
alert = 'TRY TAKEOVER!!'
elif color == 'light_yellow':
alert = 'WARNING!'
else:
alert = ''
return '%s%s %s%s\n' % (fg(color),expiration_date,alert,attr(0))
else:
return '%serror%s\n' % (fg('red'),attr(0))
def dnsexpire( host ):
sys.stdout.write( 'progress: %d/%d\r' % (t_multiproc['n_current'],t_multiproc['n_total']) )
t_multiproc['n_current'] = t_multiproc['n_current'] + 1
output = ''
resolution = resolve( host )
if resolution == '':
is_alias = False
if not _testall:
if not _verbose:
sys.stdout.write( '%s%s doesn\'t resolve%s\n' % (fg('dark_gray'),host,attr(0)) )
return
else:
is_alias = re.findall( r'(.*) is an alias for (.*)\.', resolution );
# print(is_alias)
if not _testall and not is_alias:
if not _verbose:
sys.stdout.write( '%s%s is not an alias%s\n' % (fg('dark_gray'),host,attr(0)) )
return
if _testall:
domain = getDomain( host )
output = output + "%s -> %s -> " % (host,domain)
output = output + printExpirationDate( domain )
if is_alias:
for alias in is_alias:
domain = getDomain( alias[1] )
output = output + ("%s is an alias for %s -> %s -> " % (alias[0],alias[1],domain))
output = output + printExpirationDate( domain )
if _verbose < 2 or ('WARNING' in output or 'TAKEOVER' in output): # remove the "progress:" text
sys.stdout.write( '%s\n%s' % (' '.rjust(100,' '),output) )
if not _testall:
sys.stdout.write( '\n' )
def doWork():
while True:
host = q.get()
dnsexpire( host )
q.task_done()
t_whois_history = {}
t_multiproc = {
'n_current': 0,
'n_total': n_host
}
q = Queue( _threads*2 )
for i in range(_threads):
t = Thread( target=doWork )
t.daemon = True
t.start()
try:
for host in t_hosts:
q.put( host )
q.join()
except KeyboardInterrupt:
sys.exit(1)
sys.stdout.write( '\n%s[+] finished%s\n' % (fg('green'),attr(0)) )
exit()