forked from rahulkadavil/pentest-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickhits.py
executable file
·375 lines (301 loc) · 10.6 KB
/
quickhits.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
#!/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 json
import time
import random
import argparse
import requests
from datetime import datetime
from threading import Thread
from queue import Queue
from functools import partial
from urllib.parse import urlparse
from colored import fg, bg, attr
from multiprocessing.dummy import Pool
from lockfile import LockFile
def banner():
print("""
_ _ _ _ _
__ _ _ _(_) ___| | _| |__ (_) |_ ___ _ __ _ _
/ _` | | | | |/ __| |/ / '_ \| | __/ __| | '_ \| | | |
| (_| | |_| | | (__| <| | | | | |_\__ \ _ | |_) | |_| |
\__, |\__,_|_|\___|_|\_\_| |_|_|\__|___/ (_) | .__/ \__, |
|_| |_| |___/
by @gwendallecoguic
""")
pass
banner()
startTime = datetime.now()
title_regexp = re.compile('<title>(.*?)</title>', re.IGNORECASE|re.DOTALL)
def testURL( url ):
time.sleep( 0.3 )
sys.stdout.write( 'progress: %d/%d\r' % (t_multiproc['n_current'],t_multiproc['n_total']) )
t_multiproc['n_current'] = t_multiproc['n_current'] + 1
if url in t_history and t_history[ url ] == 1:
return
if (t_multiproc['n_current'] % 1000) == 0:
if not lock.is_locked():
try:
lock.acquire()
with open(t_multiproc['f_session'], 'w') as json_file:
json.dump(t_history, json_file)
lock.release()
except Exception as e:
a = 1
t_urlparse = urlparse(url)
u = t_urlparse.scheme + '_' + t_urlparse.netloc
if not u in t_exceptions:
t_exceptions[u] = 0
if t_exceptions[u] >= 3:
t_history[ url ] = 1
# print("skip %s" % t_urlparse.netloc)
return
try:
r = requests.get( url, timeout=5, verify=False, stream=True )
# if t_multiproc['_grabfiles']:
# r = requests.get( url, timeout=2, verify=False, stream=True )
# else:
# r = requests.head( url, timeout=2, verify=False )
except Exception as e:
t_exceptions[u] = t_exceptions[u] + 1
# sys.stdout.write( "%s[-] error occurred: %s%s\n" % (fg('red'),e,attr(0)) )
return
t_history[ url ] = 1
if 'Content-Type' in r.headers:
content_type = r.headers['Content-Type']
else:
content_type = '-'
# if len(url) > 1:
# url = url.strip('_')
match = title_regexp.search( r.text )
title = match.group(1).strip() if match else '-'
ljust = 100
while ljust < len(url):
ljust = ljust + 50
ljust = ljust + 2
output = '%sS=%d\t\tL=%d\t\tC=%s\t\tT=%s\n' % (url.ljust(ljust),r.status_code,len(r.text),content_type,title)
# output = '%sS=%d\t\tL=%d\t\tC=%s\t\tT=%s\n' % (url.ljust(t_multiproc['u_max_length']),r.status_code,len(r.text),content_type,title)
# sys.stdout.write( '%s' % output )
fp = open( t_multiproc['f_output'], 'a+' )
fp.write( output )
fp.close()
if str(r.status_code) in t_codes:
sys.stdout.write( '%s' % output )
if t_multiproc['_grabfiles']:
saveFile( t_multiproc['d_output'], t_urlparse, r )
def saveFile( d_output, t_urlparse, r ):
# filename = t_urlparse.path.strip('/')
filename = t_urlparse.path.strip('/') + '_' + t_urlparse.query
filename = re.sub( '[^0-9a-zA-Z_\-\.]', '_', filename )
# if len(filename) > 1:
# filename = filename.strip('_')
d_output = d_output + '/' + t_urlparse.netloc
f_output = d_output + '/' + filename
# print(f_output)
if not os.path.isdir(d_output):
try:
os.makedirs( d_output )
except Exception as e:
sys.stdout.write( "%s[-] error occurred: %s%s\n" % (fg('red'),e,attr(0)) )
return
s_headers = 'HTTP/1.1 ' + str(r.status_code) + ' ' + r.reason + "\n"
for k,v in r.headers.items():
s_headers = s_headers + k + ': ' + v + "\n"
# print(s_headers)
content = s_headers + "\n" + r.text
try:
fp = open( f_output, 'w' )
fp.write( content )
fp.close()
except Exception as e:
sys.stdout.write( "%s[-] error occurred: %s%s\n" % (fg('red'),e,attr(0)) )
return
# disable "InsecureRequestWarning: Unverified HTTPS request is being made."
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
parser = argparse.ArgumentParser()
parser.add_argument( "-f","--files",help="set file list (required)" )
parser.add_argument( "-g","--no-grab",help="disable file download", action="store_true" )
parser.add_argument( "-o","--hosts",help="set host list (required or -u)" )
# parser.add_argument( "-k","--skip-resolve",help="skip host testing", action="store_true" )
# parser.add_argument( "-r","--redirect",help="follow redirection" )
parser.add_argument( "-s","--no-https",help="disable https", action="store_true" )
parser.add_argument( "-e","--code",help="display only status code separated by comma, default=none" )
parser.add_argument( "-r","--resume",help="resume a previous session by providing the session file" )
parser.add_argument( "-u","--urls",help="set url list (required or -o)" )
parser.add_argument( "-t","--threads",help="threads, default 10" )
parser.parse_args()
args = parser.parse_args()
if args.no_https:
_https = False
else:
_https = True
if args.resume:
resume_file = args.resume
else:
resume_file = False
if not resume_file:
t_hosts = []
if args.hosts:
if os.path.isfile(args.hosts):
fp = open( args.hosts, 'r' )
t_hosts = fp.read().splitlines()
fp.close()
else:
t_hosts.append( args.hosts )
n_hosts = len(t_hosts)
sys.stdout.write( '%s[+] %d hosts found: %s%s\n' % (fg('green'),n_hosts,args.urls,attr(0)) )
if not resume_file:
t_urls = []
if args.urls:
if os.path.isfile(args.urls):
fp = open( args.urls, 'r' )
t_urls = fp.read().splitlines()
fp.close()
else:
t_urls.append( args.urls )
n_urls = len(t_urls)
sys.stdout.write( '%s[+] %d urls found: %s%s\n' % (fg('green'),n_urls,args.urls,attr(0)) )
if not resume_file and n_hosts == 0 and n_urls == 0:
parser.error( 'hosts/urls list missing' )
if args.files:
t_files = []
if os.path.isfile(args.files):
fp = open( args.files, 'r' )
t_files = fp.read().splitlines()
fp.close()
else:
t_files.append( args.files )
n_files = len(t_files)
sys.stdout.write( '%s[+] %d files found: %s%s\n' % (fg('green'),n_files,args.files,attr(0)) )
elif not resume_file:
parser.error( 'files list missing' )
if args.no_grab:
_grabfiles = False
else:
_grabfiles = True
if args.code:
t_codes = args.code.split(',')
t_codes_str = ','.join(t_codes)
else:
t_codes = []
t_codes_str = 'none'
if args.threads:
_threads = int(args.threads)
else:
_threads = 10
u_max_length = 0
d_output = os.getcwd()+'/quickhits'
f_output = d_output + '/' + 'output'
if not os.path.isdir(d_output):
try:
os.makedirs( d_output )
except Exception as e:
sys.stdout.write( "%s[-] error occurred: %s%s\n" % (fg('red'),e,attr(0)) )
exit()
t_totest = []
t_history = {}
if resume_file:
if not os.path.isfile(resume_file):
sys.stdout.write( '%s[-] session file not found: %s%s\n' % (fg('red'),resume_file,attr(0)) )
exit()
sys.stdout.write( '%s[+] loading session file: %s%s\n' % (fg('green'),resume_file,attr(0)) )
f_session = resume_file
else:
f_session = d_output + '/' + 'sess_' + str(int(time.time()))
sys.stdout.write( '%s[+] session file: %s%s\n' % (fg('green'),f_session,attr(0)) )
sys.stdout.write( '%s[+] options are -> threads:%d, status_code:%s%s\n' % (fg('green'),_threads,t_codes_str,attr(0)) )
if resume_file:
sys.stdout.write( '[+] loading datas...\n' )
with open(resume_file) as json_file:
t_history = json.load( json_file )
for url,histo in t_history.items():
if histo == 0:
t_totest.append( url )
l = len(url)
if l > u_max_length:
u_max_length = l
n_totest = len(t_history)
n_current = len(t_history) - len(t_totest)
else:
sys.stdout.write( '[+] computing url and host and file list...\n' )
for url in t_urls:
for file in t_files:
u = url.strip().strip('/ ') + '/' + file.strip()
# u = url.strip().rstrip('/ ') + '/' + file.strip().lstrip('/')
t_totest.append( u )
l = len(u)
if l > u_max_length:
u_max_length = l
for host in t_hosts:
for file in t_files:
u = 'https' if _https else 'http'
u = u + '://' + host.strip('/ ') + '/' + file.strip()
# u = u + '://' + host.strip() + '/' + file.strip().lstrip('/')
t_totest.append( u )
l = len(u)
if l > u_max_length:
u_max_length = l
for url in t_totest:
t_history[ url ] = 0
n_current = 0
n_totest = len(t_totest)
sys.stdout.write( '%s[+] %d urls to call%s\n' % (fg('green'),n_totest,attr(0)) )
if n_current:
sys.stdout.write( '[+] %d already performed\n' % n_current )
sys.stdout.write( '[+] testing...\n' )
# print(n_current)
# print(t_history)
# print(t_totest)
# exit()
random.shuffle(t_totest)
# random.shuffle(t_totest)
t_exceptions = {}
t_multiproc = {
'n_current': n_current,
'n_total': n_totest,
'u_max_length': u_max_length+10,
'd_output': d_output,
'f_output': f_output,
'_grabfiles': _grabfiles,
'f_session': f_session
}
### OLD METHOD
# pool = Pool( _threads )
# pool.map( testURL, t_totest )
# pool.close()
# pool.join()
# with open(f_session, 'w') as json_file:
# json.dump(t_history, json_file)
# sys.stdout.write( '%s[+] %d urls called%s\n' % (fg('green'),n_current,attr(0)) )
# print(datetime.now() - startTime)
###
### NEW METHOD, MUCH FASTER
def doWork():
while True:
url = q.get()
testURL( url )
# doSomethingWithResult(status, url)
q.task_done()
q = Queue( _threads*2 )
lock = LockFile('./qh')
for i in range(_threads):
t = Thread( target=doWork )
t.daemon = True
t.start()
try:
for url in t_totest:
q.put( url )
q.join()
except KeyboardInterrupt:
sys.exit(1)
if lock.is_locked():
lock.release()
with open(f_session, 'w') as json_file:
json.dump(t_history, json_file)
sys.stdout.write( '%s[+] %d urls called%s\n' % (fg('green'),t_multiproc['n_current'],attr(0)) )
# print(datetime.now() - startTime)