Repository navigation
Expand file tree
/
Copy pathscoper.py
More file actions
executable file
·858 lines (829 loc) · 36.5 KB
/
Copy pathscoper.py
File metadata and controls
executable file
·858 lines (829 loc) · 36.5 KB
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
#!/usr/bin/env python
# By Mike Kelly
# exfil.co
# @lixmk
# TODO: Upper to Lower All output
# TODO: Remove ISP Domains via isp list vs current method
# TODO: Parse out Zone Transfer alerts from fierce
# TODO: Add logging functionality
# TODO: Add extra output option to save all results from each test (ie: Crt.sh pages, bing pages, etc)
import argparse
import re
import subprocess
import os
import shutil
import urllib2
import ssl
import sys
import socket
import masscan
import M2Crypto
from netaddr import IPNetwork
from libnmap.process import NmapProcess
from libnmap.parser import NmapParser
from cidrize import cidrize
from time import sleep
#API Key for Shodan
apikey = ""
# Global declairations. This should be your first indication that I don't really know what I'm doing. There are many more indications throughout this file.
cidrs = [] # Input file parsed
parserr = [] # Errored parsing IPs
scope = [] # Individual IPs in scope
synack = [] # Holder for Synack Scope
nmapscope = [] # Used to break up scope into digestible sizes
nmaphostnames = [] # Hostnames discovered by Nmap
fiercetargets = [] # domains and subdomains for fierce targeting. Derived from nmaphostnames
fiercehostnames = [] # Hostnames discovered by Fierce
crtshurls = [] # List of fully developed URLs for crt.sh, Developed from nmaphostnames
crthostnames = [] # Hostnames discovered by crt.sh
crtfound = [] # Found by crt.sh to be resolve (
scopehostnames = [] # Parsed list of all inscope hostnames
argdomains = [] # Additional domains to search
binghostnames = [] # Hostnames discovered via Bing IP search
masscope = [] # Target IPs to give to masscan, broken down into 256 IP chunks
masshosts = [] # Inital masscan ips with 443 open
masshostnames = [] # discovered hostnames
shodanfound = []
basedomains = []
fullpath = "" # Output path, default ./scoper
ccodes = ['ac','ad','ae','af','ag','ai','al','am','an','ao','aq','ar','as','at','au','aw','ax','az','ba','bb','bd','be','bf','bg','bh','bi','bj','bl','bm','bn','bo','bq','br','bs','bt','bv','bw','by','bz','ca','cc','cd','cf','cg','ch','ci','ck','cl','cm','cn','co','cr','cu','cv','cw','cx','cy','cz','de','dj','dk','dm','do','dz','ec','ee','eg','eh','er','es','et','eu','fi','fj','fk','fm','fo','fr','ga','gb','gd','ge','gf','gg','gh','gi','gl','gm','gn','gp','gq','gr','gs','gt','gu','gw','gy','hk','hm','hn','hr','ht','hu','id','ie','il','im','in','io','iq','ir','is','it','je','jm','jo','jp','ke','kg','kh','ki','km','kn','kp','kr','kw','ky','kz','la','lb','lc','li','lk','lr','ls','lt','lu','lv','ly','ma','mc','md','me','mf','mg','mh','mk','ml','mm','mn','mo','mp','mq','mr','ms','mt','mu','mv','mw','mx','my','mz','na','nc','ne','nf','ng','ni','nl','no','np','nr','nu','nz','om','pa','pe','pf','pg','ph','pk','pl','pm','pn','pr','ps','pt','pw','py','qa','re','ro','rs','ru','rw','sa','sb','sc','sd','se','sg','sh','si','sj','sk','sl','sm','sn','so','sr','ss','st','su','sv','sx','sy','sz','tc','td','tf','tg','th','tj','tk','tl','tm','tn','to','tp','tr','tt','tv','tw','tz','ua','ug','uk','um','us','uy','uz','va','vc','ve','vg','vi','vn','vu','wf','ws','yt','za','zm','zw']
isps = [] # NEED TO DO THIS
# Colorize output stuff
mods = "\033[1m\033[97m[=] "
mode = "\033[0m"
good = "\033[1m\033[32m[+]\033[0m "
stat = "\033[1m\033[34m[*]\033[0m "
warn = "\033[1m\033[91m[!]\033[0m "
# Print silly banner
def printtitle():
print ""
print "\033[1m\033[91m##############################################\033[0m"
print "\033[1m\033[91m= = =\033[0m \033[1m\033[91m= = =\033[0m"
print "\033[1m\033[91m= = =\033[0m \033[1m\033[97m Scoper (TM) scope enumerator \033[0m \033[1m\033[91m= = =\033[0m"
print "\033[1m\033[91m= = =\033[0m \033[1m\033[97mBy lixmk\033[0m \033[1m\033[91m= = =\033[0m"
print "\033[1m\033[91m= = =\033[0m \033[1m\033[91m= = =\033[0m"
print "\033[1m\033[91m##############################################\033[0m"
print ""
# Parse input file, Synack C/P
def parse_synack():
print stat+"Parsing input file"
with open(filein) as f:
lines = f.readlines()
for line in lines:
cidrlines = re.findall('[0-9]{1,3}?\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,2}',line)
for cidr in cidrlines:
synack.append(cidr)
for iprange in synack:
for ip in IPNetwork(iprange):
if str(ip) not in scope:
scope.append(str(ip))
with open(fullpath+"inscope-ips.txt","a+") as f:
f.write(str(ip)+'\n')
print good+"Total in scope IP addresses: "+str(len(scope))
return scope
# Parse input file
def parse_scope():
timewarn = 0
print stat+"Parsing input file to CIDR ranges"
with open(filein) as f:
lines = f.readlines()
for line in lines:
if '-' in line:
if line.split("-")[0] == line.split("-")[1].rstrip():
line = line.split("-")[0]
try:
cidrfound = str(cidrize(line)).split("'")[1]
cidrs.append(cidrfound)
except:
parserr.append(line)
pass
if len(parserr) > 0:
print warn+"Error parsing the following lines:"
for line in parserr:
print " "+line.rstrip()
print stat+"Parsing CIDR ranges to IP addresses"
for cidr in cidrs:
if timewarn == 0 and int(cidr.split('/')[1]) < 17:
print warn+"One or more large ranges (/16+) detected"
print warn+"This is not a problem, but may take additional time to parse"
timewarn = 1
for ip in IPNetwork(cidr):
if str(ip) not in scope:
scope.append(str(ip))
with open(fullpath+"inscope-ips.txt","a+") as f:
f.write(str(ip)+'\n')
print good+"Total in scope IP addresses: "+str(len(scope))
return scope
# Add arg domains for crt.sh and fierce
def adddomains():
print stat+"Adding Custom Domains"
for domain in re.split('\,', domains):
argdomains.append(domain)
print stat+" "+domain
return argdomains
# Add domains from file
def adddomains_file():
print stat+"Adding Custom Domains"
with open(domainsfile) as f:
lines = f.readlines()
for line in lines:
domain = line.rstrip()
argdomains.append(domain.rstrip())
print stat+" "+domain
return argdomains
# Nmap reverse DNS
def nmapresolve():
ipcount = len(scope)
print stat+"Executing Nmap Reverse Lookup against "+str(ipcount)+" total IPs"
print stat+"Breaking IPs into digestible parts (<=4096)"
if ipcount > 4096:
for part in range(0, ipcount, 4096):
nmapscope.append(scope[part:part + 4096])
else:
nmapscope.append(scope)
nmapcount = len(nmapscope)
print stat+"IPs broken into "+str(nmapcount)+" parts"
count = 0
for targets in nmapscope:
count += 1
try:
sys.stdout.write("\r"+stat+"Executing part "+str(count)+" of "+str(nmapcount))
sys.stdout.flush()
nmap = NmapProcess(targets, options="-sL -R")
rc = nmap.run()
resolved = NmapParser.parse(nmap.stdout)
for host in resolved.hosts:
for hostname in host.hostnames:
hostname = hostname.lower()
if not re.findall('[0-9]{1,3}?\-[0-9]{1,3}\-[0-9]{1,3}\-[0-9]{1,3}',hostname) and not re.findall('[0-9]{1,3}?\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',hostname) and 'zayo.com' not in hostname and "ip-addr" not in hostname and "Level3" not in hostname and "in-addr" not in hostname and "q9.net" not in hostname and '.' in hostname:
nmaphostnames.append((host.address, hostname))
except (KeyboardInterrupt, SystemExit):
goodresp = 0
while goodresp == 0:
print ""
resp = raw_input(warn+'Interrupt Caught. Want to kill all Nmap? (y) or this part (n): ')
if "y" in resp:
print warn+"Killing all Nmap parts"
return nmaphostnames
elif "n" in resp:
print stat+"Continuing w/ next part"
goodresp = 1
else:
print warn+"Invalid Option..."
print ""
print good+"Nmap lookup complete. Hostnames found: "+str(len(nmaphostnames))
return nmaphostnames
# Masscan for for 443
def scanmass():
ipcount = len(scope)
print stat+"Executing Masscan against "+str(ipcount)+" total IPs"
print stat+"Breaking IPs into digestible parts (<=4096)"
if ipcount > 4096:
for part in range(0,ipcount,4096):
masscope.append(scope[part:part + 4096])
else:
masscope.append(scope)
partsnumber = len(masscope)
print stat+"IPs broken into "+str(partsnumber)+" parts"
partcount = 0
count = 0
for part in masscope:
partcount += 1
sys.stdout.flush()
sys.stdout.write("\r"+stat+"Scanning part "+str(partcount)+" of "+str(partsnumber)+" ")
sys.stdout.flush()
targets = ",".join(part)
try:
mas = masscan.PortScanner()
mas.scan(targets, ports='443', arguments='--rate=1000')
for host in mas.all_hosts:
masshosts.append(host)
count += 1
sys.stdout.write("- 443 on: "+str(count))
sys.stdout.flush()
except (masscan.masscan.NetworkConnectionError):
sys.stdout.write("- 443 on: "+str(count))
sys.stdout.flush()
pass
except (KeyboardInterrupt, SystemExit):
goodresp = 0
while goodresp == 0:
print ""
resp = raw_input(warn+'Interrupt Caught. Want to kill all Massscan? (y) or this chunk (n): ')
if "y" in resp:
print warn+"Killing all Masscan parts"
return masshosts
elif "n" in resp:
print stat+"Continuing with next part"
goodresp = 1
else:
print warn+"Invalid Option..."
print ""
print good+"Masscan complete. Total hosts with 443 open: "+str(count)
return masshosts
# Parse masscan results and pull CN + subjectAltNames
def parsemass():
ipnumber = len(masshosts)
ipcount = 0
if ipnumber > 0:
for ip in masshosts:
ipcount += 1
sys.stdout.flush()
sys.stdout.write("\r"+stat+"Checking for hostnames from SSL cert on IP "+str(ipcount)+" of "+str(ipnumber))
sys.stdout.flush()
try:
socket.setdefaulttimeout(10)
cert = ssl.get_server_certificate((ip, 443))
x509 = M2Crypto.X509.load_cert_string(cert)
cert_sub = x509.get_subject().as_text()
cname = re.sub(r'\s+', '', str(cert_sub.split('CN=')[1]).lower())
if '/' in cname:
cname = re.split('/', cname)[0]
if ',' in cname:
cname = re.split(',', cname)[0]
if ':' in cname:
cname = re.split(':', cname)[0]
if len(cname) > 0 and (ip, cname) not in masshostnames and '*' not in cname and ':' not in cname and '.' in cname:
namesplit = re.split('\.',cname)
subnum = int(len(namesplit) - 1)
if namesplit[subnum].isdigit() == False:
masshostnames.append((ip, cname))
cert_ext = str(re.sub('DNS:', '', x509.get_ext("subjectAltName").get_value())).split(', ')
for name in cert_ext:
name = re.sub(r'\s+', '', name.lower())
if '/' in name:
name = re.split('/', name)[0]
if ',' in name:
name = re.split(',', name)[0]
if ':' in cname:
cname = re.split(':', cname)[0]
if (ip, name) not in masshostnames and '*' not in name and ':' not in name and '.' in name:
namesplit = re.split('\.',name)
subnum = int(len(namesplit) - 1)
if namesplit[subnum].isdigit() == False:
masshostnames.append((ip, name))
except (TypeError, ssl.SSLEOFError, ssl.SSLError, socket.error, LookupError):
pass
except (KeyboardInterrupt, SystemExit):
print ""
goodresp = 0
while goodresp == 0:
resp = raw_input(warn+'Interrupt Caught. Want to kill all cert checks? (y|n): ')
if "y" in resp:
print warn+"Killing all cert checks"
return masshostnames
elif "n" in resp:
print stat+"Continuing cert checks"
goodresp = 1
else:
print warn+"Invalid Option..."
print "\n"+good+"Hostname check complete. Hostnames found: "+str(len(masshostnames))
else:
print warn+"Masscan found no hosts with 443 open :("
return masshostnames
# Search Bing for IP Addresses to discover hostnames
def bingip():
print stat+"Executing Bing IP searches"
bingcount = len(scope)
count=0
for ip in scope:
count +=1
sys.stdout.flush()
sys.stdout.write("\r"+stat+" IP "+str(count)+" of "+str(bingcount))
sys.stdout.flush()
bingurl = "http://www.bing.com/search?q=ip%3A"+ip
sslcontext = ssl.create_default_context()
sslcontext.check_hostname=False
sslcontext.verify_mode = ssl.CERT_NONE
# Get results
try:
datarecv = urllib2.urlopen(bingurl, context=sslcontext)
datatext = datarecv.readlines()
if '<h1>There are no results for <strong>' not in datatext:
for line in datatext:
if "Search Results" in line and "People also ask" not in line and 'bing.com' not in line:
cleanlines = re.split('\n', re.sub('</cite>', '\n', re.sub('<cite>', '\n', line)))
for newline in cleanlines:
nostrong = re.sub('</strong>', '', re.sub("<strong>", '', newline))
if '>' not in nostrong and '.' in nostrong:
hostname = str(re.split('/', re.sub('https://', '', nostrong))[0]).lower()
if ':' in hostname:
hostname = re.split(':', hostname)[0]
if (ip, hostname) not in binghostnames:
binghostnames.append((ip, hostname))
with open(fullpath+"bingfound.txt","a+") as f:
f.write(ip+" - "+hostname+"\n")
except (KeyboardInterrupt, SystemExit):
print ""
goodresp = 0
while goodresp == 0:
resp = raw_input(warn+'Interrupt Caught. Want to kill Bing searches? (y|n): ')
if "y" in resp:
print warn+"Killing all Bing searches"
return binghostnames
elif "n" in resp:
print stat+"Continuing Bing searches"
goodresp = 1
else:
print warn+"Invalid Option..."
except:
pass
print "\n"+good+"Bing IP search complete. Hosts found: "+str(len(binghostnames))
return binghostnames
###
# Loop through target domains determined from Nmap output
# Searchs Crt.sh for certs assigned to subdomains of the target domain.
# Then resolves all discovered hostnames for IP addresses
###
def checkcrtsh():
print stat+"Generating list of targets for crt.sh"
# Part one: build search URLs, ensure unique
for host in nmaphostnames:
hostsplit = re.split('\.',host[1])
subnum = int(len(hostsplit) - 1)
if subnum > 1 and hostsplit[subnum] in ccodes:
domain = hostsplit[(subnum - 2)]+"."+hostsplit[(subnum - 1)]+"."+hostsplit[subnum]
else:
domain = hostsplit[(subnum - 1)]+"."+hostsplit[subnum]
crturl = "https://crt.sh/?q=%25."+domain
if (crturl, domain) not in crtshurls:
crtshurls.append((crturl, domain))
for host in masshostnames:
hostsplit = re.split('\.',host[1])
subnum = int(len(hostsplit) - 1)
if subnum > 1 and hostsplit[subnum] in ccodes:
domain = hostsplit[(subnum - 2)]+"."+hostsplit[(subnum - 1)]+"."+hostsplit[subnum]
else:
domain = hostsplit[(subnum - 1)]+"."+hostsplit[subnum]
crturl = "https://crt.sh/?q=%25."+domain
if (crturl, domain) not in crtshurls:
crtshurls.append((crturl, domain))
for host in binghostnames:
hostsplit = re.split('\.',host[1])
subnum = int(len(hostsplit) - 1)
if subnum > 1 and hostsplit[subnum] in ccodes:
domain = hostsplit[(subnum - 2)]+"."+hostsplit[(subnum - 1)]+"."+hostsplit[subnum]
else:
domain = hostsplit[(subnum - 1)]+"."+hostsplit[subnum]
crturl = "https://crt.sh/?q=%25."+domain
if (crturl, domain) not in crtshurls:
crtshurls.append((crturl, domain))
for argdomain in argdomains:
crturl = "https://crt.sh/?q=%25."+argdomain
if (crturl, argdomain) not in crtshurls:
crtshurls.append((crturl, argdomain))
print good+"Generation Complete. Targeting the following domains: "
for pair in crtshurls:
print good+" "+pair[1]
for pair in crtshurls:
sys.stdout.flush()
sys.stdout.write("\r"+stat+"Checking crt.sh results for: "+pair[1])
sys.stdout.flush()
try:
crthostcount = 0
datarecv = urllib2.urlopen(pair[0], context=sslcontext)
dataout = datarecv.readlines()
# Parse for unique hostnames and save for resolution
for line in dataout:
if pair[1] in line and '<TD>' in line:
discovered = re.sub('\s+', '', re.sub('<TD>', '', re.sub('</TD>', '', line))).rstrip('\n')
if discovered not in crtfound:
crthostcount +=1
crtfound.append(discovered)
print " - found: "+str(crthostcount)
except (KeyboardInterrupt, SystemExit):
goodresp = 0
while goodresp == 0:
print "\n"+warn+"Interrupt Caught"
resp = raw_input(warn+'Do you want to kill all crt.sh (y|n): ')
if "y" in resp:
print warn+"Killing all crt.sh searches"
return crtfound
goodresp = 1
elif "n" in resp:
print stat+"Continuing..."
break
goodresp = 1
else:
print warn+"Invalid Option..."
except socket.gaierror:
pass
except:
print "\n"+warn+"Error on: "+pair[1]
print good+"Crt.sh search finished. Total hostnames: "+str(len(crtfound))
def resolvecrt():
print stat+"Resolving hostnames to IP"
count = 0
resolvecount = len(crtfound)
for crthost in crtfound:
crthost = crthost.lower()
count +=1
sys.stdout.flush()
sys.stdout.write("\r"+stat+" Host "+str(count)+" of "+str(resolvecount))
try:
ip = socket.gethostbyname(crthost.strip())
if (ip, crthost) not in crthostnames:
crthostnames.append((ip, crthost))
except socket.gaierror:
pass
except (KeyboardInterrupt, SystemExit):
goodresp = 0
while goodresp == 0:
print "\n"+warn+"Interrupt Caught"
resp = raw_input(warn+'Do you want to kill all resolution? (y|n): ')
if "y" in resp:
print warn+"Killing all DNS resolution"
return crthostnames
goodresp = 1
elif "n" in resp:
print stat+"Continuing..."
pass
goodresp = 1
else:
print warn+"Invalid Option..."
print "\n"+good+"Resolution complete. Hostnames resolved to IP: "+str(len(crthostnames))
return crthostnames
# Search Bing for IP Addresses to discover hostnames
def shodansearch():
print stat+"Executing Shodan searches"
shodancount = len(scope)
count=0
for ip in scope:
count +=1
sys.stdout.flush()
sys.stdout.write("\r"+stat+" IP "+str(count)+" of "+str(shodancount))
sys.stdout.flush()
shodanurl = "https://api.shodan.io/shodan/host/"+ip+"?key="+apikey
sslcontext = ssl.create_default_context()
sslcontext.check_hostname=False
sslcontext.verify_mode = ssl.CERT_NONE
# Get results
try:
datarecv = urllib2.urlopen(shodanurl, context=sslcontext)
datatext = datarecv.readlines()
ports=json.loads(datatext[0]["ports"])
names=json.loads(datatext[0]["hostnames"])
for line in datatext:
with open(fullpath+"/shodan/"+ip+".json",'a+') as f:
f.write(line)
for port in ports:
with open(fullpath+"shodan_ports.txt", 'a+') as f:
f.write(ip+":"+port+"\n")
for name in names:
with open(fullpath+"shodan_names.txt", 'a+') as f:
f.write(ip+":"+name+"\n")
if (ip,name) not in shodanfound:
shodanfound.appends((ip,name))
except (KeyboardInterrupt, SystemExit):
print ""
goodresp = 0
while goodresp == 0:
resp = raw_input(warn+'Interrupt Caught. Want to kill Shodan searches? (y|n): ')
if "y" in resp:
print warn+"Killing all Shodan searches"
return shodanhostnames
elif "n" in resp:
print stat+"Continuing Shodan searches"
goodresp = 1
else:
print warn+"Invalid Option..."
except:
pass
sleep(1)
print "\n"+good+"Shodan search complete. Hosts found: "
#return shodanhostnames
def genfierce():
print stat+"Generating target domains/subdomains for fierce"
for host in nmaphostnames:
hostsplit = re.split('\.',host[1])
subnum = int(len(hostsplit) - 1)
if subnum > 1 and hostsplit[subnum] in ccodes:
domain = hostsplit[(subnum - 2)]+"."+hostsplit[(subnum - 1)]+"."+hostsplit[subnum]
else:
domain = hostsplit[(subnum - 1)]+"."+hostsplit[subnum]
if domain not in fiercetargets:
fiercetargets.append(domain)
for host in masshostnames:
hostsplit = re.split('\.',host[1])
subnum = int(len(hostsplit) - 1)
if subnum > 1 and hostsplit[subnum] in ccodes:
domain = hostsplit[(subnum - 2)]+"."+hostsplit[(subnum - 1)]+"."+hostsplit[subnum]
else:
domain = hostsplit[(subnum - 1)]+"."+hostsplit[subnum]
if domain not in fiercetargets:
fiercetargets.append(domain)
for host in binghostnames:
hostsplit = re.split('\.',host[1])
subnum = int(len(hostsplit) - 1)
if subnum > 1 and hostsplit[subnum] in ccodes:
domain = hostsplit[(subnum - 2)]+"."+hostsplit[(subnum - 1)]+"."+hostsplit[subnum]
else:
domain = hostsplit[(subnum - 1)]+"."+hostsplit[subnum]
if domain not in fiercetargets:
fiercetargets.append(domain)
for argdomain in argdomains:
if argdomain not in fiercetargets:
fiercetargets.append(argdomain)
print good+"Generation Complete. Targeting the following (sub)domains: "
for target in fiercetargets:
print good+" "+target
return fiercetargets
###
# Loops thorugh targets in fiercetargets and executes runfierce.
###
def execfierce():
for target in fiercetargets:
try:
sys.stdout.flush()
sys.stdout.write(stat+"Executing fierce against: "+target)
sys.stdout.flush()
fierce = ['/usr/bin/fierce', '-dns', target, '-file' ,str(fullpath+"fierce/"+target)]
p = subprocess.Popen(fierce, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while(True):
retcode = p.poll()
line = p.stdout.readline()
if retcode is not None:
break
if re.findall('[0-9]{1,3}?\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',line) and "found" not in line:
line = re.sub('\s+', ':', line.rstrip('\n'))
if '.' in line[1]:
fiercehostnames.append((line.split(':')[0], line.split(':')[1].rstrip('\n')))
if "Found" in line and "entries" in line:
found = re.split(' ', line)[1]
print " - Found: "+found
except (KeyboardInterrupt, SystemExit):
goodresp = 0
while goodresp == 0:
print "\n"+warn+"Interrupt Caught"
resp = raw_input(warn+'Do you want to kill all fierce (y) or just this one (n)?: ')
if "y" in resp:
print warn+"Killing all fierce searchs"
return binghostnames
goodresp = 1
elif "n" in resp:
print warn+"Killing fierce: "+target
goodresp = 1
else:
print warn+"Invalid Option..."
print good+"Fierce complete"
return fiercehostnames
# Generate and print results
def resultsout():
print stat+"Comparing all results to in-scope IPs"
print stat+"This may take a while depending on number of hostnames found"
if noscope == 0:
nmapcount = 0
for pair in nmaphostnames:
if pair[0] in scope and pair not in scopehostnames:
scopehostnames.append(pair)
nmapcount += 1
masscount = 0
for pair in masshostnames:
if pair[0] in scope and pair not in scopehostnames:
scopehostnames.append(pair)
masscount += 1
bingcount = 0
for pair in binghostnames:
if pair[0] in scope and pair not in scopehostnames:
scopehostnames.append(pair)
bingcount =+ 1
crtcount = 0
for pair in crthostnames:
if pair[0] in scope and pair not in scopehostnames:
scopehostnames.append(pair)
crtcount += 1
fiercecount = 0
for pair in fiercehostnames:
if pair[0] in scope and pair not in scopehostnames:
scopehostnames.append(pair)
fiercecount += 1
shodancount = 0
for pair in shodanfound:
if pair[0] in scope and pair not in scopehostnames:
scopehostnames.append(pair)
shdoancount += 1
for pair in scopehostnames:
with open(fullpath+"inscope-hostnames.txt","a+") as f:
f.write(pair[0]+" - "+pair[1].rstrip('.')+'\n')
if noscope:
nmapcount = 0
for pair in nmaphostnames:
if pair not in scopehostnames:
scopehostnames.append(pair)
nmapcount += 1
masscount = 0
for pair in masshostnames:
if pair not in scopehostnames:
scopehostnames.append(pair)
masscount += 1
bingcount = 0
for pair in binghostnames:
if pair not in scopehostnames:
scopehostnames.append(pair)
bingcount =+ 1
crtcount = 0
for pair in crthostnames:
if pair not in scopehostnames:
scopehostnames.append(pair)
crtcount += 1
fiercecount = 0
for pair in fiercehostnames:
if pair not in scopehostnames:
scopehostnames.append(pair)
fiercecount += 1
shodancount = 0
for pair in shodanfound:
if pair not in scopehostnames:
scopehostnames.append(pair)
shodancount += 1
for pair in scopehostnames:
with open(fullpath+"discovered-hostnames.txt","a+") as f:
f.write(pair[0]+" - "+pair[1].rstrip('.')+'\n')
# Building List of Base Domain Names
for pair in scopehostnames:
hostsplit = re.split('\.',pair[1])
subnum = int(len(hostsplit) - 1)
if subnum > 1 and hostsplit[subnum] in ccodes:
domain = hostsplit[(subnum - 2)]+"."+hostsplit[(subnum - 1)]+"."+hostsplit[subnum]
else:
domain = hostsplit[(subnum - 1)]+"."+hostsplit[subnum]
if domain not in basedomains:
basedomains.append(domain)
for dom in basedomains:
with open(fullpath+"base-domains.txt","a+") as f:
f.write(dom+'\n')
print stat+"Results comparison complete"
print ""
print mods+"= = = = = = = = = = = = = = = ="+mode
print mods+"= = = Scoper(TM) Complete = = ="+mode
print mods+"= = = = = = = = = = = = = = = ="+mode
print ""
print stat+"All output saved to directory "+fullpath
if fierce:
print stat+"Raw fierce output in "+fullpath+"fierce/"
if bing:
print stat+"Hostnames discovered via Bing in "+fullpath+"bingfound.txt"
if noscope == False:
print good+"Total IPs in scope (inscope-ips.txt): "+str(len(scope))
if nmaprun:
print stat+"Nmap discovered hostnames: "+str(len(nmaphostnames))
if noscope == False:
print good+"Nmap discovered in scope: "+str(nmapcount)
if mass:
print stat+"Masscan discovered hostnames: "+str(len(masshostnames))
if noscope == False:
print good+"Masscan discovered in scope: "+str(masscount)
if bing:
print stat+"Bing discovered hostnames: "+str(len(binghostnames))
if noscope == False:
print good+"Bing discovered in scope: "+str(bingcount)
if crtsh:
print stat+"Crt.sh discovered hostnames: "+str(len(crthostnames))
if noscope == False:
print good+"Crt.sh discovered in scope: "+str(crtcount)
if fierce:
print stat+"Fierce discovered hostnames: "+str(len(fiercehostnames))
if noscope == False:
print good+"Fierce discovered in scope: "+str(fiercecount)
if noscope:
print good+"Total discovered hostnames (discovered-hostnames.txt): "+str(len(scopehostnames))
else:
print good+"Total in-scope hostnames (inscope-hostnames.txt): "+str(len(scopehostnames))
def main(nmaprun, mass, bing):
printtitle()
if os.path.isdir(fullpath):
goodresp = 0
while goodresp == 0:
resp = raw_input(warn+"Directory "+fullpath+" exists. Overwrite? (y|n): ")
if "y" in resp:
print warn+"Overwriting "+fullpath
shutil.rmtree(fullpath)
os.mkdir(fullpath)
goodresp = 1
elif "n" in resp:
print warn+"Not Overwriting... killing scoper.py"
sys.exit()
else:
print warn+"Invalid Option..."
print ""
else:
os.mkdir(fullpath)
# Check for Shodan Key of Shodan option is enabled
if shodan == True and len(apikey) < 1:
print warn+"Shodan requires an API Key. Please add it with the '-k' switch"
print warn+"Alternatively, you can hard code it in the 'apikey' variable on line 30"
sys.exit()
# Reseting some switches based on Noscope and filein
if noscope:
if nmaprun and not filein:
print warn+"Nmap requires target IP addresses, disabling Nmap scan"
nmaprun = False
if mass and not filein:
print warn+"Masscan requires target IP addresses, disabling Masscan"
mass = False
if bing and not filein:
print warn+"Bing search requires target IP addresses, disabling Bing search"
bing = False
if filein > 0:
if synscope == True:
parse_synack()
else:
parse_scope()
# Check for input file if noscope is not set
if noscope == False:
if not filein:
print warn+"Input file (-i) Required, unless No Scope is set (-N)"
sys.exit()
# Check for synscope and parse
print mods+"= = = IP Parser = = ="+mode
if synscope == True:
parse_synack()
else:
parse_scope()
if domains > 0:
print mods+"= = = Custom Domains = = ="+mode
adddomains()
if domainsfile > 0:
print mods+"= = = Custom Domains = = ="+mode
adddomains_file()
if nmaprun == True:
print mods+"= = = Nmap Rev Lookup = = ="+mode
nmapresolve()
if mass:
print mods+"= = = Masscan + Cert grab = = ="+mode
scanmass()
parsemass()
if bing:
print mods+"= = = Bing IP Search = = ="+mode
bingip()
if crtsh:
print mods+"= = = Crt.sh Transperency Search = = ="+mode
checkcrtsh()
resolvecrt()
if shodan:
os.mkdir(fullpath+"/shodan")
print mods+"= = = Shodan Search = = ="+mode
shodansearch()
if fierce:
os.mkdir(fullpath+"/fierce")
print mods+"= = = Fierce DNS Bruteforce = = ="+mode
genfierce()
execfierce()
print mods+"= = = Results Comparison = = ="
resultsout()
if __name__ == '__main__':
# Argument parsing
parser = argparse.ArgumentParser(usage='./scoper.py -i <input_file> -d <custom_domain> -n -m -b -c -f')
parser.add_argument('-i', '--filein', help="Input file, accepts all cidrize formats")
parser.add_argument('-d', '--domains', help="Additional domains to search")
parser.add_argument('-D', '--domainsfile', help="Additional domains from file")
parser.add_argument('-n', '--nmaprun', action='store_true', default=False, help="Use nmap for reverse DNS")
parser.add_argument('-m', '--mass', action="store_true", default=False, help="masscan for 443 and pull hostnames from certs")
parser.add_argument('-b', '--bing', action="store_true", default=False, help="Bing IP Search")
parser.add_argument('-c', '--crtsh', action='store_true', default=False, help="Check crt.sh for associated hostnames")
parser.add_argument('-s', '--shodan', action='store_true', default=False, help="Search Shodan, require API key (-k)")
parser.add_argument('-k', '--shodankey', help="API key for Shodan, can also be hard coded")
parser.add_argument('-f', '--fierce', action='store_true', default=False, help="Execute fierce against discovered (sub)domains")
parser.add_argument('-t', '--timeout', default=10, help="Socket timeout, default 10")
parser.add_argument('-o', '--outdir', default="scoper", help="Output directory, relative, no ./ needed")
parser.add_argument('-a', '--execall', action="store_true", default=False, help="same as -n -m -b -c -f")
parser.add_argument('-q', '--quick', action="store_true", default=False, help="same as -n -m -b -c")
parser.add_argument('-N', '--noscope', action="store_true", default=False, help="Disable scope comparison")
parser.add_argument('-S', '--synack', action="store_true", default=False, help="Take copy/paste from Synack scope section")
args = parser.parse_args()
filein = args.filein
domains = args.domains
domainsfile = args.domainsfile
nmaprun = args.nmaprun
mass = args.mass
bing = args.bing
crtsh = args.crtsh
shodan = args.shodan
shodankey = args.shodankey
fierce = args.fierce
timeout = int(args.timeout)
outdir = args.outdir
execall = args.execall
quick = args.quick
noscope = args.noscope
synscope = args.synack
if execall:
nmaprun = True
mass = True
bing = True
crtsh = True
fierce = True
if quick:
nmaprun = True
mass = True
bing = True
crtsh = True
if shodankey > 0:
apikey = shodankey
fullpath = os.getcwd()+"/"+outdir+"/"
# Default socket/ssl stuff
socket.setdefaulttimeout(timeout)
sslcontext = ssl.create_default_context()
sslcontext.check_hostname=False
sslcontext.verify_mode = ssl.CERT_NONE
main(nmaprun, mass, bing)