-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlscanner.py
145 lines (123 loc) · 4.49 KB
/
urlscanner.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
import argparse
import requests
import re
from http.client import responses
from os import path
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from time import sleep
urlcount = 0
verbose = 0
failedurls = 0
outstring = ""
firefoxpath = "c:\Program Files\Mozilla Firefox\firefox.exe"
interval = 3
def printbanner():
banner = " ____ _____________.____\n"\
"| | \\______ \\ |\n"\
"| | /| _/ |\n"\
"| | / | | \\ |___ \n"\
"|______/ |____|_ /_______ \\\n"\
" ______ ____ ___\\/ ____\\/ ____ ___________\n"\
" / ___// ___\\\\__ \\ / \\ / \\_/ __ \\_ __ \\\n"\
" \\___ \\\\ \\___ / __ \\| | \\ | \\ ___/| | \\/\n"\
"/____ >\\___ >____ /___| /___| /\\___ >__|\n"\
" \\/ \\/ \\/ \\/ \\/ \\/\n"\
"URL scanner\r\nPeter Berends - 2021\n"
print(banner)
def statuscode(url):
try:
response = requests.head(url)
return response.status_code
except Exception as err:
if verbose:
print("Request failed for %s (%s)." % (url, err))
return None
def formaturl(url):
if not re.match('(?:http|https)://', url):
if verbose:
print("Prepend https:// to line (%s)." % url)
return 'https://{}'.format(url)
return url
def checkdir(dir):
return path.exists(dir)
printbanner()
parser = argparse.ArgumentParser()
parser.add_argument("--inputfile", "-i", help="specify input file with URLs per line")
parser.add_argument("--outputdir", "-d", help="specify output directory")
parser.add_argument("--outputfile", "-o", help="save results to file (CSV format)")
parser.add_argument("--verbose", "-v", help="verbose mode", action="store_true")
parser.add_argument("--append", "-a", help="append a string to the urls to check")
parser.add_argument("--firefoxpath", "-f", help="location where the executable of Firefox is found")
parser.add_argument("--screenshots", "-s", help="create screenshots of URLs", action="store_true")
args = parser.parse_args()
if not args.inputfile:
print("No input file specified, got nothing to work with. Bye.")
exit(1)
if args.screenshots == True and not args.outputdir:
print("No output directory specified for screenshot storage. Bye.")
exit(1)
if args.outputdir and not checkdir(args.outputdir):
print("Invalid output directory specified. Bye.")
exit(1)
if args.verbose:
print("Verbose mode on")
verbose = 1
if args.firefoxpath:
firefoxpath = args.firefoxpath
if verbose:
print("Location to firefox given: %s" % args.firefoxpath)
if args.screenshots:
print("Taking screenshots (will be stored in '%s')" % args.outputdir)
if verbose:
print("Activating Firefox as screenshot driver")
print("Using Firefox location: %s" % firefoxpath)
try:
Options = Options()
Options.headless = True
browser = webdriver.Firefox(options=Options, firefox_binary=firefoxpath)
except Exception as err:
print("Could not activate screenshot driver (%s). Try setting the path to Firefox using the --firefoxpath flag." % err)
exit(1)
print("Reading input file (%s)..." % args.inputfile)
try:
inputfile = open(args.inputfile, 'r')
lines = inputfile.readlines()
except:
print("ERROR: Input file specified is invalid. Bye.")
exit(1)
for line in lines:
line = re.sub(r"[\n\t\s\r]*", "", line)
line = formaturl(line)
if args.append:
line = line + args.append
urlcount += 1
if verbose:
print("Processing: %s" % line)
result = statuscode(line)
if verbose:
print("Taking screenshot of %s" % line)
try:
browser.get(line)
sleep(interval)
browser.get_screenshot_as_file(args.outputdir + '/' + line[7:] + '.png')
except Exception as err:
if verbose:
print("Could not create screenshot (%s)" % err)
if result:
outline = str(result) + "," + responses[result] + "," + line
outstring += outline + "\n"
print("[%s][%s] - %s" % (result, responses[result], line))
else:
failedurls += 1
if args.outputfile:
print("Saving results to %s" % args.outputfile)
try:
outfile = open(args.outputfile, 'w')
outfile.write(outstring)
outfile.close()
except Exception as err:
print("ERROR: Could not save results (%s)." % err)
if args.screenshots:
browser.quit()
print("Done. Processed %i URLs (%i failed)." % (urlcount, failedurls))