-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspider.py
executable file
·1217 lines (1115 loc) · 39 KB
/
spider.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# Copyright (C) 2021 Karim Kanso
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
## Name: Simple Spider.
##
## Description: Designed for lightweight spidering of small sites to
## gain initial visibility.
##
## TODO:
## * save output as json
## * allow for mutators to be dependent upon processing another page
## ** e.g. only apply mutators if parent has been requested and is not an index
## * use urllib.robotparser to extract urls from robots.txt
import urllib.request
from urllib.error import URLError
from urllib.parse import urlsplit, urljoin, urldefrag, SplitResult, quote
from concurrent.futures import ThreadPoolExecutor, wait, FIRST_COMPLETED
import ssl
from bs4 import BeautifulSoup
import magic
import re
import socket
import http.client
import http.cookies
import argparse
import posixpath
import logging
from colorama import Fore, Style
import inspect
import sys
import traceback
import time
import signal
import errno
import random
import itertools
inscope_urls = {}
outscope_urls = {}
exclude_urls = {}
base_url = None
default_headers = {
'User-Agent': 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'
}
def create_connection(h, *a, **kw):
global args
return socket.create_connection(args.target, *a, **kw)
def PatchHTTPConnection(cls):
def httpconn(host, cls=cls, **kw):
global args
h = cls(host, **kw)
if args.target:
h._create_connection = create_connection
return h
return httpconn
class SpiderHTTPHandler(urllib.request.AbstractHTTPHandler):
def __init__(self, debuglevel=0):
urllib.request.AbstractHTTPHandler.__init__(self, debuglevel)
def https_open(self, req):
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS)
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
ctx.set_ciphers('DEFAULT:@SECLEVEL=1')
ctx.set_alpn_protocols(['http/1.1'])
return self.do_open(
PatchHTTPConnection(http.client.HTTPSConnection),
req,
context=ctx
)
def http_open(self, req):
return self.do_open(
PatchHTTPConnection(http.client.HTTPConnection),
req
)
http_request = urllib.request.AbstractHTTPHandler.do_request_
https_request = urllib.request.AbstractHTTPHandler.do_request_
opener = urllib.request.OpenerDirector()
opener.add_handler(SpiderHTTPHandler())
recurse_types = [
'text/html',
'application/xhtml+xml'
]
# some common file types are not correctly detected by libmagic, so
# these are manual hacks to avoid spamming output with mismatches.
# key is server reported, and value is detected content type
libmagic_override = {
'application/javascript': [
'text/plain', # nominal case
'text/html' # rare cases libmagic will detect as html
],
'text/css': ['text/plain'],
'application/xml': ['text/html'],
'': ['application/x-empty']
}
class Result:
def __init__(
self,
url,
status_code,
length,
content_type,
detected_content_type
):
self.url = url
self.status_code = status_code
self.content_length = length
self.content_type = (content_type or '').lower().split(';')[0]
self.detected_content_type = detected_content_type.split(';')[0]
self.linked_resources = []
self.custom_error = None
self.tags = set()
if type(self.content_length) == str:
self.content_length = int(self.content_length)
if (detected_cts := libmagic_override.get(self.content_type, [])):
if self.detected_content_type in detected_cts:
self.detected_content_type = self.content_type
def add_resource(self, tag, link, **kw):
url = urldefrag(link).url
if not url:
return
self.linked_resources.append(
dict(
{
'tag': tag,
'link': url,
},
**kw
)
)
def set_ignore(self, ok):
self.ignore = bool(ok)
def get_resources(self, tag):
return filter(lambda x: x['tag'] == tag, self.linked_resources)
def __str__(self):
return f'{self.status_code:03d} {self.detected_content_type} {self.url}'
class FormRegistry:
def __init__(self):
self.form = set()
def add_form(self, method, params):
if method:
self.form.add((method,*params))
class RequestTrack(FormRegistry):
def __init__(
self,
depth,
guess=False,
func_is_ok=None,
tags=set(),
callback=None
):
FormRegistry.__init__(self)
self.depth = depth
self.guess = guess
self.response = None
self.function_ok = func_is_ok
self.tries_left = 5
self.last_error = None
self.func_callback = callback
self.tags = tags # tracks mutators
self.resp_tags = set() # addendum to response.tags
def set_response(self, response):
if self.response:
raise Exception('Request already completed')
self.response = response
def is_done(self):
return bool(self.response)
def is_interesting(self):
if self.is_done():
if self.function_ok:
return self.function_ok(self.response)
if not self.guess and not args.filter_all_pages:
return True
if type(self.response.ignore) == bool:
return not self.response.ignore
else:
return self.response.status_code < 400
else:
return not self.guess and self.is_pending()
def is_pending(self):
return self.depth < 0
def set_error(self, err):
self.tries_left = max(self.tries_left - 1, 0)
self.last_error = err
def callback(self):
if self.func_callback:
return self.func_callback(self)
return None
def get_response_tags(self):
if self.is_done():
return self.resp_tags | self.response.tags
else:
return self.resp_tags
def add_response_tag(self, tag):
self.resp_tags.add(tag)
def is_media(self):
if self.is_done():
macro_content_type = self.response.detected_content_type or ''
macro_content_type = macro_content_type.split('/', 1)[0]
return macro_content_type in ['image', 'audio', 'video']
class SpiderException(Exception):
pass
class OutOfScope(SpiderException):
def __init__(self):
SpiderException.__init__(self, 'out of scope')
index_title = re.compile(
'^((Index|Directory contents) of /.*)|(Directory Listing -.*)'
)
apache_link = re.compile('^\\?C=[SDMN];O=[AD]$')
def is_index(soup):
if (title := soup.find('title')) and (title := title.string):
if index_title.match(title):
return True
# IIS directory listing module
return soup.find('a', {'href': True}, string='[To Parent Directory]')
def _load_url(url, timeout=30):
global opener, args, default_headers, recurse_types, abort
if abort:
raise Exception('user aborted')
if not check_url_scope(url):
raise OutOfScope()
if type(url) == SplitResult:
url = url.geturl()
logging.debug(f'requesting {url}')
req = urllib.request.Request(
url,
headers=dict(default_headers, **dict(args.headers)),
method='GET'
)
with opener.open(req, timeout=timeout) as resp:
chunk1 = resp.read(1024)
with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m:
res = Result(
url,
resp.status,
resp.getheader('Content-Length'),
resp.getheader('Content-Type'),
m.id_buffer(chunk1)
)
if args.filter_status:
res.set_ignore(
any([ s == resp.status for s in args.filter_status ])
)
if not res.ignore and args.filter_regex:
res.set_ignore(args.filter_regex.search(chunk1))
if (redir := resp.getheader('Location')):
res.add_resource('redirect', redir)
if (cookies := resp.info().get_all('Set-Cookie')):
res.tags.add('cookie')
for cookie in cookies:
logging.info(f'found cookie {cookie} on {url}')
for c in (sc := http.cookies.SimpleCookie(cookie)):
if sc[c]['path']:
res.add_resource('cookie', sc[c]['path'])
if res.detected_content_type == 'application/javascript':
content = chunk1 + resp.read()
for mapping in re.finditer(
b'(?m:^//[#@] sourceMappingURL=(.*)$)',
content
):
if (
not mapping[1].startswith(b'data:') and
not mapping[1].startswith(b'blob:')
):
res.tags.add('sourcemap')
res.add_resource('map', mapping[1].decode('utf8'))
else:
res.tags.add('sourcemap-embedded')
return res
# if detected content type is not valid for recursion, bomb
# out as its likely that the html parser will fail. n.b. this
# covers case of empty body.
if res.detected_content_type not in recurse_types:
if (
not res.content_type or
res.detected_content_type.split('/')[0] != 'text' or
res.content_type not in recurse_types
):
return res
# for now, ignore reported content type
# if res.content_type:
# if res.content_type not in recurse_types:
# return res
content = chunk1 + resp.read()
logging.debug(f'recurse condition for {url} with {res.content_type}')
soup = BeautifulSoup(content, "html5lib")
if not res.content_length:
if (
type(res.content_length) == int and
res.content_length != len(content)
):
res.tags.add('bad-length')
logging.warning(
f'inconsistent content-length detected for {url},'
f' header reports size as {res.content_length} '
f'but actual size received is {len(content)}'
)
res.content_length = len(content)
if is_index(soup):
res.tags.add('index')
for link in soup.find_all('a'):
if 'index' in res.tags and apache_link.match(link.get('href')):
continue
res.add_resource('a', link.get('href'))
for elem in soup.find_all(src=True):
res.add_resource(elem.name, elem.get('src'))
for form in soup.find_all('form'):
params={}
for inp in form.find_all('input', {'name': True}):
params[inp.get('name')] = inp.get('type')
for inp in form.find_all(
re.compile('button|select|textarea|object'),
{'name': True}
):
params[inp.get('name')] = inp.name
res.add_resource(
'form',
form.get('action') or url_dequery(url),
method=(form.get('method') or 'get').lower(),
params=params
)
for link in soup.find_all('link'):
res.add_resource('link', link.get('href'))
return res
def load_url(*arg, **kw):
try:
return _load_url(*arg, **kw)
except Exception as e:
raise SpiderException(*arg, **kw)
def url_normalise(url, base=None):
if type(url) == str:
url = urlsplit(url)
if not url.path:
p = '/' if url.netloc else ''
else:
p = posixpath.normpath(url.path)
if url.path[-1:] == '/' and p[-1] != '/':
p += '/'
url = SplitResult(
url.scheme.lower(),
url.netloc.lower(),
p,
url.query,
''
)
if base:
return urlsplit(
urljoin(url_normalise(base).geturl(), url.geturl())
)
return url
def url_to_dir(url):
url = url_normalise(url)
f = posixpath.basename(url.path)
p = posixpath.dirname(url.path) if url.path[-1:] != '/' else url.path
return (
SplitResult(
url.scheme,
url.netloc.lower(),
p if p[-1:] == '/' else p + '/',
'',
''
),
SplitResult(
'',
'',
f,
url.query,
''
)
)
def url_to_dirs(url):
url = url_to_dir(url)[0]
subpaths = []
i = 0
while (i := url.path.find('/', i)) >= 0:
i += 1
subpaths.append(url.path[:i])
return {
SplitResult(
url.scheme,
url.netloc,
p,
'',
''
) for p in subpaths
}
def url_dequery(url):
url = url_normalise(url)
return SplitResult(
url.scheme,
url.netloc,
url.path,
'',
''
).geturl()
def is_url_prefix(base, url, same_ok=False):
base = url_normalise(base)
url = url_normalise(url)
if not base.scheme == url.scheme:
return False
if not base.netloc == url.netloc:
return False
return url.path.startswith(base.path) and (
same_ok or
url.path != base.path or
bool(url.query)
)
def random_str(l):
return ''.join(random.choices("abcdefghijklmnopqrstuvwxyz", k=l))
def url_random(base, tag):
dir_url, file_url = url_to_dir(base)
return url_normalise(f'{random_str(8)}-{tag}.html' , dir_url)
def check_url_scope(url, base=None):
if not is_url_prefix(base or base_url, url, same_ok=True):
return False
for u in exclude_urls:
if is_url_prefix(u, url, same_ok=True):
return False
return True
boring_path_segments = [
'theme', 'thirdparty', 'third_party', 'third-party', 'vendor'
]
null_mut = ([],0,None,None)
extensions = [
'~', '.save', '.sav', '.bak', '.tar.gz', '.gz', '.7z', '.cab',
'.tgz', '.gzip', '.bzip2', '.inc', '.zip', '.rar', '.jar',
'.java', '.class', '.properties', '.bak', '.bak1', '.bkp',
'.back', '.backup', '.backup1', '.old', '.old1', '.$$$', '.map'
]
base_words = [
'index', 'config', 'home', 'login', 'readme', 'README', 'robots'
]
base_extensions = [
'.php', '.asp', '.html', '.txt', '.md', '', '/'
]
basic_wordlist = [
a+b for a, b in itertools.product(base_words, base_extensions)
]
def deferred_normal_mutator_aux(track, parent_track, impl, mut):
if track.is_interesting():
parent_track.add_response_tag('acceptsall')
dir_url, file_url = url_to_dir(parent_track.response.url)
logging.info(
f'base url {dir_url.geturl()} accepts all, skipping '
f'mutator {mut}, consider using a filter or exclude path'
)
return []
else:
logging.debug(
f'base url {parent_track.response.url} appears normal with request '
f'{track.response.url}, continuing with mutator {mut}'
)
return impl()
def deferred_normal_mutator(parent_track, impl, mut):
return lambda t, p=parent_track, i=impl, m=mut: \
deferred_normal_mutator_aux(t, p, i, m)
def mut_subdirs(track):
"enumerate parent directories"
return (url_to_dirs(track.response.url), track.depth - 1, None, None)
def mut_relatedfiles(track):
"add exts. to files looking for backups"
dir_url, file_url = url_to_dir(track.response.url)
if not file_url.path:
return null_mut
if 'index' in track.get_response_tags():
raise Exception('internal error: bad index detection')
if (parent := inscope_urls.get(dir_url.geturl())):
if 'index' in parent.get_response_tags():
return null_mut
if track.is_media():
logging.debug(
f'skipping relatedfiles mutator on media file {track.response.url}'
)
return null_mut
for bps in boring_path_segments:
if bps in track.response.url.lower():
logging.debug(
f'skipping relatedfiles mutator on {track.response.url}'
)
return null_mut
return (
[ url_random(dir_url, 'relatedfiles') ]
, track.depth
# avoid redirect loop as depth is non-decrementing
, lambda req: req.status_code >= 200 and req.status_code < 300
, deferred_normal_mutator(
impl=lambda dir_url=dir_url, file_url=file_url.path: (
[ url_normalise(f'%23{file_url}%23', dir_url) ] +
[ url_normalise(file_url + e, dir_url) for e in extensions ]
),
parent_track=track,
mut='relatedfiles'
)
)
def mut_commonfiles(track):
"use wordlist in each directory"
if 'index' in track.get_response_tags():
return null_mut
dir_url, file_url = url_to_dir(track.response.url)
if file_url.path:
return null_mut
if (
(parent := inscope_urls.get(dir_url.geturl())) and
'index' in parent.get_response_tags()
):
return null_mut
if 'commonfiles' in args.mutator_wordlist:
words = args.mutator_wordlist['commonfiles']
else:
logging.info('commonfiles mutator enabled without wordlist')
words = basic_wordlist
for bps in boring_path_segments:
if bps in track.response.url.lower():
logging.debug(
f'skipping commonfiles mutator on {track.response.url}'
)
return null_mut
return (
[ url_random(dir_url, 'commonfiles') ],
track.depth - 1,
lambda req: req.status_code >= 200 and req.status_code < 300,
deferred_normal_mutator(
impl=lambda dir_url=dir_url: [
url_normalise(quote(f.lstrip('/').strip()), dir_url)
for f in words
],
parent_track=track,
mut='commonfiles'
)
)
def deferred_mut_autoexclude(track, parent_track):
global exclude_urls, inscope_urls
if track.is_interesting():
dir_url, file_url = url_to_dir(parent_track.response.url)
# try to tag parent directory, otherwise tag originator url
if (dir_url := dir_url.geturl()) in inscope_urls:
inscope_urls[dir_url].add_response_tag('acceptsall')
else:
parent_track.add_response_tag('acceptsall')
if dir_url not in exclude_urls:
logging.warning(
f'Autoexclude: {dir_url} accepts all, removing from scope. '
f'Consider using a filter to tune detection.'
)
exclude_urls[dir_url] = True
return []
def mut_autoexclude(track):
"add acceptall urls to externals"
dir_url, file_url = url_to_dir(track.response.url)
return (
[ url_random(dir_url, 'autoexclude') ]
, track.depth
, lambda req: req.status_code >= 200 and req.status_code < 300
, lambda t, p=track: deferred_mut_autoexclude(t, p)
)
all_mutators = [
obj for name,obj in inspect.getmembers(sys.modules[__name__])
if inspect.isfunction(obj) and name.startswith('mut_')
]
def mutator(x):
for f in all_mutators:
if x == f.__name__[4:]:
return f
else:
raise Exception(f'Mutator {x} not found')
def mutator_file(x):
try:
mut, fn = x.split(':',1)
except:
raise Exception('Mutator wordlist should be: "mutator:/path/to/file"')
for f in all_mutators:
if mut == f.__name__[4:]:
break
else:
raise Exception(f'Mutator {mut} not found')
return (mut, list(
filter(
lambda x: x and not x.startswith('#'),
map(
lambda y: y.rstrip('\n\r'),
open(fn,'r').readlines()
)
)
))
start_time = time.monotonic()
window_start_time = start_time
window_start_requests = 0
finished_requests = 0
abort = False
def main():
global args, abort, inscope_urls, outscope_urls, exclude_urls, base_url
def header(x):
name, value = x.split(': ', 1)
return (name, value)
parser = argparse.ArgumentParser(
description='Simple Spider.'
)
parser.add_argument(
'url',
metavar='BASE_URL',
type=urllib.parse.urlsplit,
help='Base url to start spidering from (limits scope).'
)
parser.add_argument(
'start_paths',
metavar='START_PATH',
nargs='*',
type=urllib.parse.urlsplit,
help='Paths, relative from BASE_URL to start spidering from.'
)
parser.add_argument(
'--exclude-path','-e',
dest='exclude_paths',
metavar='EXCL_PATH',
type=urllib.parse.urlsplit,
action='append',
default=[],
help='Paths, relative from BASE_URL to prune.'
)
parser.add_argument(
'--target-ip','-t',
metavar='IP',
dest='target',
type=lambda x: x.split(':', 1) if ':' in x else (x, 80),
help='Redirect connections to given "host:port" combo.'
)
parser.add_argument(
'--depth', '-d',
metavar='NUM',
type=int,
default=5,
help='Maximum number of links to follow from base url. Default 5.'
)
parser.add_argument(
'--header', '-H',
dest='headers',
action='append',
default=[],
type=header,
help='"Header: value" pairs to add to requests.'
)
parser.add_argument(
'--verbose','-v',
dest='debug',
action='count',
default=0,
help='Enable detailed logging, give twice for full debug.'
)
parser.add_argument(
'--log-file',
dest='logfile',
help='File to log to, default stderr'
)
parser.add_argument(
'--hide-external', '-he',
dest='show_external',
action='store_false',
help='Do not list urls outside of base url.'
)
parser.add_argument(
'--hide-media', '-hm',
dest='hide_media',
action='store_true',
help='Do not list urls with media mime type (detected by libmagic).'
)
parser.add_argument(
'--filter-regex', '-fr',
metavar='REGEX',
dest='filter_regex',
type=lambda x: re.compile(x.encode('utf8')),
help='Regex for error pages, if not set, fallback to -fc.'
)
parser.add_argument(
'--filter-status', '-fc',
metavar='CODE',
dest='filter_status',
nargs='*',
default=[404],
type=int,
help=(
'Status to filter (default: 404). no params: >=400 '
'(or with -fr set does nothing).'
)
)
parser.add_argument(
'--filter-all-pages',
dest='filter_all_pages',
action='store_true',
help='Apply filter to all pages (default: only guessed/fuzzed pages).'
)
parser.add_argument(
'--mutators','-m',
nargs='*',
type=mutator,
default=[
mut_autoexclude
, mut_subdirs
, mut_relatedfiles
, mut_commonfiles
],
help='Fuzzers to apply, (default: {}). available: {}'.format(
'autoexclude, subdirs, relatedfiles, commonfiles',
', '.join([
f'{f.__name__[4:]} ({f.__doc__})' for f in all_mutators
])
)
)
parser.add_argument(
'--mutator-wordlist','-wl',
dest='mutator_wordlist',
metavar='MUT:FILE',
type=mutator_file,
action='append',
default=[],
help='Wordlist used by mutator: "mutator:filename".'
)
parser.add_argument(
'--threads',
metavar='NUM',
type=int,
default=10,
help='Number of threads (default: 10).'
)
parser.add_argument(
'--show-backlinks',
dest='back_links',
action='store_true',
help='For links not guessed, show source page.'
)
args = parser.parse_args()
args.mutator_wordlist = dict(args.mutator_wordlist)
logging.basicConfig(
filename=args.logfile,
format='[%(levelname)s] %(message)s',
level=(3 - min(args.debug,2)) * 10
)
if not args.url.scheme:
raise Exception('Base URL requires scheme to be specified (http/https)')
base_url, start_url = url_to_dir(args.url)
start_url = {urljoin(base_url.geturl(), start_url.geturl())}
print(f'[*] base url: {base_url.geturl()}')
for path in args.exclude_paths:
if is_url_prefix(base_url, (url := url_normalise(path, base_url))):
exclude_urls[url.geturl()] = False
else:
logging.warning(f'out of scope exclude path {url.geturl()}')
for path in args.start_paths:
if check_url_scope((url := url_normalise(path, base_url))):
start_url.add(url.geturl())
else:
logging.warning(f'out of scope start path {url.geturl()}')
for m in args.mutators:
logging.info(f'enabled mutator {m.__name__}')
for url in start_url:
print(f'[*] start url: {url}')
inscope_urls[url] = RequestTrack(args.depth)
def enqueue(
executor,
parent_url,
url,
depth,
guess=False,
check=None,
method=None,
params=None,
tag_msg=None,
tags=set(),
callback=None,
**kw
):
if type(url) != str:
url = url.geturl()
waspending = False
if (wasnotpresent := url not in inscope_urls):
inscope_urls[url] = RequestTrack(
depth,
guess,
check,
tags,
callback
)
elif (waspending := inscope_urls[url].is_pending()):
inscope_urls[url].depth = depth
inscope_urls[url].guess &= guess
inscope_urls[url].check = check if inscope_urls[url].guess else None
inscope_urls[url].add_form(method, params)
inscope_urls[url].tags &= tags
if not inscope_urls[url].is_pending() and (wasnotpresent or waspending):
logging.debug(
f'enqueing {url} from {parent_url} at depth {depth}'
f'{" by " + tag_msg if tag_msg else ""}'
)
return { executor.submit(load_url, url) }
elif inscope_urls[url].is_pending():
logging.debug(f'max depth reached for {url} from {parent_url}')
return set()
def enqueue_all(urls, **kw):
if (to_enqueue := [
enqueue(url=u, **kw) for u in filter(check_url_scope, urls)
]):
return set.union(*to_enqueue)
return set()
def process_result(result, executor):
global finished_requests
global window_start_time
global window_start_requests
global start_time
finished_requests +=+ 1
if (window_next_time := window_start_time + 60) <= time.monotonic():
print(
f'[*] completed {finished_requests} requests, avg req/s:'
f'{int(finished_requests / (time.monotonic() - start_time)):4}'
f' req/s:'
f'{int((finished_requests - window_start_requests) / 60):4}'
)
window_start_time = window_next_time
window_start_requests = finished_requests
(track := inscope_urls[result.url]).set_response(result)
if (pack := track.callback()) or type(pack) == set:
del inscope_urls[result.url]
return pack
if track.is_interesting():
logging.info(f'found url {result.url} {result.status_code}')
futures = set()
for r in result.linked_resources:
link = url_normalise(
r['link'],
url_to_dir(result.url)[0]
)
r['link_normalised'] = (link_norm := link.geturl())
if check_url_scope(link):
futures |= enqueue(
executor,
result.url,
link,
track.depth - 1,
**r
)
elif link_norm != base_url.geturl():
if link_norm not in outscope_urls:
outscope_urls[link_norm] = FormRegistry()
if r.get('method'):
outscope_urls[link_norm].add_form(
r.get('method'),
r.get('params')
)
if not track.is_interesting():
return futures
# iterate over mutators and add new urls to spider
for mut in args.mutators:
if mut.__name__ in track.tags:
# break out of branches with just mutators
continue
urls, depth, check, hook = mut(track)
base_args={
'executor': executor,
'parent_url': result.url,
'depth': depth,
'guess': True,
'check': check,
'tag_msg': f'mutator {mut.__name__}',
'tags': {mut.__name__} | track.tags,
}
if hook:
hook = lambda track, hook=hook, kw=base_args : enqueue_all(
urls=hook(track), **kw
)
futures |= enqueue_all(
urls=urls,
callback=hook,
**base_args
)
return futures
def sigint(*a):
global abort
abort = True
signal.signal(signal.SIGINT, signal.SIG_DFL)
print('\n[!] ctrl-c received, waiting for connections to close')
signal.signal(signal.SIGINT, sigint)
def failed(executor, error, url):
track = inscope_urls[url]
logging.debug(f'Error while loading {failed_url}')
for l in traceback.format_exception(
type(track.last_error),
track.last_error,
track.last_error.__traceback__
):
for m in l.rstrip('\n').split('\n'):
logging.debug(m)
if not track.tries_left or error not in ['timeout']:
if error in ['outofscope']:
if url not in outscope_urls:
if not track.guess:
outscope_urls[url] = track