-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXSScanner.py
More file actions
277 lines (248 loc) · 14.9 KB
/
XSScanner.py
File metadata and controls
277 lines (248 loc) · 14.9 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
#!/usr/bin/python3
from colorama import Fore
from flask import jsonify
from rich.console import Console
import requests, argparse, os, random, time, readline, json, datetime, re, sys
with open('VERSION', 'r', errors="ignore") as f:
version = f.readline()
def update_check():
try:
response = requests.get('https://raw.githubusercontent.com/bigzooooz/XSScanner/master/VERSION')
if response.status_code == 200:
if response.text > version:
print(Fore.MAGENTA + " New version available!")
time.sleep(1)
print(Fore.GREEN + " Add --update To Update")
time.sleep(2)
return True
else:
return False
except:
return False
return False
def ascii_art():
art = ['''\n\n /$$ /$$ /$$$$$$ /$$$$$$
| $$ / $$ /$$__ $$ /$$__ $$
| $$/ $$/| $$ \__/| $$ \__/ /$$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$
\ $$$$/ | $$$$$$ | $$$$$$ /$$_____/ |____ $$| $$__ $$| $$__ $$ /$$__ $$ /$$__ $$
>$$ $$ \____ $$ \____ $$| $$ /$$$$$$$| $$ \ $$| $$ \ $$| $$$$$$$$| $$ \__/
/$$/\ $$ /$$ \ $$ /$$ \ $$| $$ /$$__ $$| $$ | $$| $$ | $$| $$_____/| $$
| $$ \ $$| $$$$$$/| $$$$$$/| $$$$$$$| $$$$$$$| $$ | $$| $$ | $$| $$$$$$$| $$
|__/ |__/ \______/ \______/ \_______/ \_______/|__/ |__/|__/ |__/ \_______/|__/
''',
'''\n\n
,--, ,--, .--.--. .--.--.
|'. \ / .`| / / '. / / '.
; \ `\ /' / ;| : /`. /| : /`. / ,---, ,---, __ ,-.
`. \ / / .'; | |--` ; | |--` ,-+-. / | ,-+-. / | ,' ,'/ /|
\ \/ / ./ | : ;_ | : ;_ ,---. ,--.--. ,--.'|' |,--.'|' | ,---. ' | |' |
\ \.' / \ \ `. \ \ `. / \ / \ | | ,"' | | ,"' | / \ | | ,'
\ ; ; `----. \ `----. \ / / ' .--. .-. | | | / | | | / | | / / |' : /
/ \ \ \ __ \ \ | __ \ \ |. ' / \__\/: . . | | | | | | | | |. ' / || | '
; /\ \ \ / /`--' // /`--' /' ; :__ ," .--.; | | | | |/| | | |/ ' ; /|; : |
./__; \ ; \'--'. /'--'. / ' | '.'| / / ,. | | | |--' | | |--' ' | / || , ;
| : / \ \ ; `--'---' `--'---' | : :; : .' \| |/ | |/ | : | ---'
; |/ \ ' | \ \ / | , .-./'---' '---' \ \ /
`---' `--` `----' `--`---' `----'
''',
'''\n\n \o o/ o__ __o o__ __o
v\ /v /v v\ /v v\
<\ /> /> <\ /> <\
\o/ _\o____ _\o____ __o__ o__ __o/ \o__ __o \o__ __o o__ __o \o__ __o
| \_\__o__ \_\__o__ /> \ /v | | |> | |> /v |> | |>
/ \ \ \ o/ /> / \ / \ / \ / \ / \ /> // / \ < >
o/ \o \ / \ / <| \ \o/ \o/ \o/ \o/ \o/ \o o/ \o/
/v v\ o o o o \\ o | | | | | v\ /v __o |
/> <\ <\__ __/> <\__ __/> _\o__</ <\__ / \ / \ / \ / \ / \ <\/> __/> / \
''',
'''\n\n
$$\ $$\ $$$$$$\ $$$$$$\
$$ | $$ |$$ __$$\ $$ __$$\
\$$\ $$ |$$ / \__|$$ / \__| $$$$$$$\ $$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$\ $$$$$$\
\$$$$ / \$$$$$$\ \$$$$$$\ $$ _____|\____$$\ $$ __$$\ $$ __$$\ $$ __$$\ $$ __$$\
$$ $$< \____$$\ \____$$\ $$ / $$$$$$$ |$$ | $$ |$$ | $$ |$$$$$$$$ |$$ | \__|
$$ /\$$\ $$\ $$ |$$\ $$ |$$ | $$ __$$ |$$ | $$ |$$ | $$ |$$ ____|$$ |
$$ / $$ |\$$$$$$ |\$$$$$$ |\$$$$$$$\\$$$$$$$ |$$ | $$ |$$ | $$ |\$$$$$$$\ $$ |
\__| \__| \______/ \______/ \_______|\_______|\__| \__|\__| \__| \_______|\__|
''',
'''\n\n
__ __ _____ _____
\ \ / // ____/ ____|
\ V /| (___| (___ ___ __ _ _ __ _ __ ___ _ __
> < \___ \\___ \ / __/ _` | '_ \| '_ \ / _ \ '__|
/ . \ ____) |___) | (_| (_| | | | | | | | __/ |
/_/ \_\_____/_____/ \___\__,_|_| |_|_| |_|\___|_|
'''
]
return random.choice(art)
def get_php_files_from_folder(folder_path):
php_files = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".php"):
php_files.append(os.path.join(root, file))
return php_files
def scan_php_files_for_xss(folder_path):
console = Console()
files = get_php_files_from_folder(folder_path)
tasks = [f"task {n}" for n in files]
sources = ['GET','POST', 'REQUEST', "SERVER\['PHP" "SERVER\['PATH_" "SERVER\['REQUEST_U"]
sinks = ['echo', 'die', 'print', 'printf', 'print_r', 'var_dump']
vulnerable = {}
files_count = len(files)
if files_count == 0:
console.print(" \n [bold red][!] No PHP files found in the folder\n")
return False
with console.status("[bold green] Scanning {0} {1} ...".format(files_count, 'Files' if (files_count > 1) else 'File')) as status:
time.sleep(2)
for file_path in files:
scanned_path = file_path.split("/")[-1]
with open(file_path, 'r', errors="ignore") as f:
for n, line in enumerate(f, 1):
for source in sources:
if source in line:
for sink in sinks:
if sink in line:
exploitable_files = locate_exploitable_file(file_path.split("/")[-1],files)
if(exploitable_files is None): break
exploit = {}
for exploitable in exploitable_files:
exploitable_path = exploitable['file'].split("SCAN")[-1]
start = "$_"+source+"['"
end = "']"
parameter = line.lstrip().rstrip().partition(start)[2].partition(end)[0]
if source == "GET":
if exploitable_path.split('/')[1] not in exploit:
exploit[exploitable_path.split('/')[1]] = [{'file':exploitable_path.lstrip('/') +'?'+parameter+'=XSS_PAYLOAD', 'code':exploitable['code'], 'parameter':parameter}]
else:
exploit[exploitable_path.split('/')[1]].append({'file':exploitable_path.lstrip('/') +'?'+parameter+'=XSS_PAYLOAD', 'code':exploitable['code'], 'parameter':parameter})
else:
if exploitable_path.split('/')[1] not in exploit:
exploit[exploitable_path.split('/')[1]] = [{'file':exploitable_path.lstrip('/'), 'code':exploitable['code'], 'parameter':parameter}]
else:
exploit[exploitable_path.split('/')[1]].append({'file':exploitable_path.lstrip('/'), 'code':exploitable['code'], 'parameter':parameter})
if file_path.split("/")[1] not in vulnerable:
vulnerable[file_path.split("/")[1]] = [{'file': scanned_path, 'line #': n, 'code': line.lstrip().rstrip() ,'source': source, 'sink': sink, 'exploitation': exploit[file_path.split("/")[1]]}]
else:
vulnerable[file_path.split("/")[1]].append({'file': scanned_path, 'line #': n, 'code': line.lstrip().rstrip() ,'source': source, 'sink': sink, 'exploitation': exploit[file_path.split("/")[1]]})
return vulnerable
def locate_exploitable_file(file_name,files):
console = Console()
exploitable = []
for file in files:
with open(file, 'r', errors="ignore") as f:
for n, line in enumerate(f, 1):
if(re.search(file_name,line) is not None):
exploitable.append({'file': file, 'code': line.lstrip().rstrip()})
return exploitable
def scan_remote_target(target, vuln_list):
console = Console()
payload='XSS_PAYLOAD'
with console.status("[bold green] Scanning {0} for XSS ...".format(target)) as status:
time.sleep(2)
for key in vuln_list:
for v in vuln_list[key]:
for exploit in v['exploitation']:
url = '{0}{1}'.format(target,exploit['file'])
http_status_errors = {
400: "Bad Request",
401: "Unauthorized",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
408: "Request Timeout",
500: "Internal Server Error",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Gateway Timeout",
}
try:
if v['source'] == 'GET' or v['source'] == 'SERVER["PHP"]' or v['source'] == 'SERVER["PATH_"]' or v['source'] == 'SERVER["REQUEST_U"]' or v['source'] == 'REQUEST':
response = requests.get(url)
if response.status_code == 200:
if payload in response.text:
print(Fore.GREEN + "[+] {0} - Vulnerable: {1}".format(response.status_code,url))
else:
print(Fore.RED + "[!] {1} - {2} - Try Manually: {0}".format(url,response.status_code,http_status_errors[response.status_code]))
except requests.exceptions.ConnectionError:
print(Fore.RED + "[!] DNS Error, Check your internet connection or target")
return
def main(argv):
if(argv['update']):
console = Console()
with console.status("Checking for Updates...") as status:
if(update_check()):
print(Fore.CYAN + "Current Version: {0}".format(version))
status.update(Fore.GREEN + "Update Found ...")
time.sleep(2)
status.update(Fore.GREEN + " Updating...")
print(Fore.GREEN + "[+] Initiating git pull ...")
os.popen("git pull").read()
print(Fore.GREEN + "[+] Update Downloaded Successfully!")
print(Fore.GREEN + "[!] Please Restart ...")
status.update(Fore.GREEN + " Exiting...")
time.sleep(2)
exit()
else:
print(Fore.MAGENTA + "[-] No Updates Available")
print(Fore.CYAN + "[-] Current Version: {0}".format(version))
status.update(Fore.GREEN + " Exiting...")
time.sleep(2)
exit()
update_check()
if not os.path.exists('SCAN'):
os.makedirs('SCAN')
folder_path = 'SCAN/' + argv['directory'] if argv['directory'] is not None else 'SCAN'
count = 0
vulnerable = scan_php_files_for_xss(folder_path)
if vulnerable:
for vuln in vulnerable:
if(len(vuln) > 0):
count += len(vulnerable[vuln])
if not os.path.exists('Results/{0}/'.format(vuln)):
os.makedirs('Results/{0}/'.format(vuln))
with open('Results/{0}/XSScanner-{1}-output.json'.format(vuln,vuln+'-'+ datetime.datetime.now().strftime("%x-%X").replace('/','').replace(':','')), 'w+') as f:
json.dump(vulnerable[vuln],f)
if(count > 0):
print(Fore.GREEN + '\n[-] {} Potential XSS Vulnerabilities Found.\n'.format(count))
else:
print(Fore.RED + '\n[-] No Vulnerabilities Found.\n')
if(argv['output']):
if vulnerable:
for vuln in vulnerable:
for vuln in vulnerable[vuln]:
print(Fore.RESET + '-' * 40,end='\n')
print(Fore.RED + '\n[-] File: ' + vuln['file'])
print(Fore.RED + '\n[-] Line #: ' + str(vuln['line #']))
print(Fore.RED + '\n[-] Code: ' + vuln['code'])
print(Fore.RED + '\n[-] Source: ' + vuln['source'])
print(Fore.RED + '\n[-] Sink: ' + vuln['sink'], end='\n\n')
if (argv['target'] is not None):
if argv['directory'] is None:
print(Fore.RED + "[!] Please specify a directory using the flag -d if you wish to scan a remote target", end='\n\n')
print(Fore.WHITE + "\n")
parser.print_help()
print(Fore.WHITE + "\n")
exit()
target = argv['target'].rstrip(argv['directory'])
if vulnerable:
print(Fore.RESET + '-' * 40,end='\n')
scan_remote_target(target, vulnerable)
print(Fore.RESET + '-' * 40,end='\n')
print(Fore.GREEN + '\n[-] Scan Completed!')
if vulnerable: print(Fore.LIGHTCYAN_EX + '\n[+] Scan Result Saved in Results Folder')
print('\n')
if __name__ == "__main__":
banner_color = random.choice(list(vars(Fore).values()))
print(banner_color + ascii_art())
print(banner_color + 'Scanning PHP Files for XSS Vulnerabilities Never Been Any Easier!\n')
print(banner_color + 'v{} - Written By: @b4zb0z\n\n'.format(version))
print(Fore.RESET)
parser = argparse.ArgumentParser()
parser.add_argument('-d','--directory', help='Used To Define a Directory Path To Scan (all folders will be scan if not provided a name)', required=False)
parser.add_argument('-t','--target', help='Used To Define a Target URI To Test Against', type=str)
parser.add_argument("-o",'--output', help='Print Scan Output on Screen (default=false)', action='store_true',required=False, default=False)
parser.add_argument('--update', help='Update XSScanner', default=False, action='store_true')
args = vars(parser.parse_args())
readline.parse_and_bind('tab:complete')
main(args)