-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhooks-goal.py
283 lines (228 loc) · 9.29 KB
/
hooks-goal.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
"""
This is an example of a tuned scan against OWASP Juice Shop using
hooks in ZAP *-scan.py scripts
time docker run --rm -v $(pwd):/zap/wrk/:rw \
-t owasp/zap2docker-weekly zap-full-scan.py \
-d -t http://172.17.0.2:3000 -a -m 1\
-j --hook=/zap/wrk/hooks-goal.py
"""
import time
import re
from os.path import expanduser
import sys
from pip._internal import main as pip
import requests
import os
print("-- Loading hooks scripts ...")
# Adding packages to a new Docker image is better, but this works as POC
pip(['install', "-I", "-q", "--user", "selenium", "beautifulsoup4"])
home = expanduser("~")
sys.path.append(home + "/.local/lib/python2.7/site-packages/")
print("-- Finished adding extras")
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver import FirefoxOptions
def do_scan(zap, target):
print("-- Starting active scan of %s" % target)
scan_id = zap.ascan.scan(target, recurse=False)
time.sleep(5)
while(int(zap.ascan.status(scan_id)) < 100):
print('-- Active Scan progress %: ' + zap.ascan.status(scan_id))
time.sleep(5)
print('-- Active Scan complete')
def do_login(zap, driver, target):
driver.get(target)
time.sleep(2)
user = os.environ.get("TEST_USER", "[email protected]")
passwd = os.environ.get("TEST_PASS", "testtest")
print("-- Login with browser")
try:
driver.find_element_by_css_selector("#userEmail").send_keys(user)
driver.find_element_by_css_selector("#userPassword").send_keys(passwd)
driver.find_element_by_css_selector("#loginButton").click()
except:
pass
time.sleep(1)
crawl_angular(zap, target, driver)
def get_firefox(zap):
driver_bin = zap.selenium.option_firefox_driver_path
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': zap._ZAPv2__proxies['http'],
'ftpProxy': zap._ZAPv2__proxies['http'],
'sslProxy': zap._ZAPv2__proxies['http'],
'noProxy': ''
})
profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True
profile.set_preference("browser.startup.homepage_override.mstone", "ignore")
profile.set_preference("startup.homepage_welcome_url.additional", "about:blank")
opts = FirefoxOptions()
opts.add_argument("--headless")
driver = webdriver.Firefox(proxy=proxy, executable_path=driver_bin, firefox_options=opts, firefox_profile=profile)
return driver
def crawl_angular(zap, target, driver=None):
if driver is None:
driver = get_firefox(zap)
driver.get(target)
whens = _find_ng_whens(zap)
print("-- Crawling angular routes (%s)" % len(whens))
for endpoint in whens:
if 'logout' in endpoint or 'logoff' in endpoint:
continue
print("-- Navigating angular endpoint /#%s" % endpoint)
try:
driver.get(target + '/#' + endpoint)
# Taking a screenshot
# time.sleep(1)
# idx = whens.index(endpoint)
# driver.save_screenshot('/zap/wrk/screen-%s.png' % idx)
except:
pass
def _extract_when_endpoints(body):
findings = re.findall(r"\.when\([^)]+\)", body)
endpoints = []
for match in findings:
parts = match.split('"')
if len(parts) < 2:
continue
endpoints.append(match.split('"')[1])
return endpoints
def _find_ng_whens(zap):
time.sleep(1)
messages = zap.search.messages_by_response_regex('\.when\(["\']')
unique_endpoints = set()
for msg in messages:
full_msg = zap.core.message(msg["id"])
body = full_msg['responseBody']
for endpoint in _extract_when_endpoints(body):
unique_endpoints.add(endpoint)
return list(unique_endpoints)
def _list_scanners(zap, policy='Default Policy'):
all_scanners = zap.ascan.scanners(scanpolicyname=policy)
disabled_scanners = [s for s in all_scanners if s['enabled'] != 'true']
enabled_scanners = [s for s in all_scanners if s['enabled'] == 'true']
print("-- Scanners disabled (%s)" % len(disabled_scanners))
for scanner in disabled_scanners:
print(' x [' + scanner['id'] + '] ' + scanner['name'])
print("-- Scanners enabled (%s)" % len(enabled_scanners))
for scanner in enabled_scanners:
print(' + [' + scanner['id'] + '] ' + scanner['name'] + ' | ' + scanner['attackStrength'])
def start_zap(port, extra_zap_params):
return port, extra_zap_params + [
'-addoninstall', 'ascanrulesAlpha',
'-addoninstall', 'ascanrulesBeta',
'-addoninstall', 'sqliplugin',
'-addoninstall', 'domxss',
]
def zap_started(zap, target):
print("-- ZAP started!")
ignore = [
".*node_modules.*",
"%s/public.*" % target,
"%s/i18n.*" % target,
"%s/css.*" % target,
]
for endpoint in ignore:
print(("-- Ignore spider %s " % endpoint) + zap.spider.exclude_from_scan(endpoint))
print(("-- Ignore scanning %s " % endpoint) + zap.ascan.exclude_from_scan(endpoint))
def zap_spider(zap, target):
username = os.environ.get("TEST_USER", "[email protected]")
passwd = os.environ.get("TEST_PASS", "testtest")
# Register
payload = {
"email": username,
"password": passwd,
"passwordRepeat": passwd,
"securityQuestion": {
"id": 1,
"question": "Your eldest siblings middle name?",
"createdAt": "2018-11-07T21:19:32.395Z",
"updatedAt": "2018-11-07T21:19:32.395Z"
},
"securityAnswer": passwd
}
try:
requests.post("%s/api/Users/" % target, proxies=zap._ZAPv2__proxies, verify=False, json=payload)
print("-- Creating user")
except Error as err:
print(err)
# POST to the login endpoint so spider/scans have another node to attack
res = requests.post(target + '/rest/user/login', proxies=zap._ZAPv2__proxies, verify=False, json={
"email": username,
"password": passwd,
})
print("-- API Login Response " + res.text)
print("-- Setting spider duration " + zap.spider.set_option_max_duration("1"))
return zap, target
def zap_ajax_spider(zap, target, max_time):
# The regular spider already ran ... if we don't clear exclusions
# the page won't load the css/js needed to render everything
zap.spider.clear_excluded_from_scan()
zap.spider.set_option_max_duration("1")
zap.ajaxSpider.set_option_max_duration("1")
# Use selenium to login to the app and crawl real quick
driver = get_firefox(zap)
do_login(zap, driver, target)
print("-- Trying XSS ...")
try:
driver.get(target + '#/search?q=<script>alert("XSS")</script>')
time.sleep(1)
alert = driver.switch_to.alert
alert.accept()
driver.get(target + '#/track-result?id=<script>alert("XSS")</script>')
time.sleep(1)
alert = driver.switch_to.alert
alert.accept()
except:
pass
driver.quit()
return zap, target + '/#/search?q=example', 1
def zap_active_scan(zap, target, policy):
# zap.ascan.disable_all_scanners()
# zap.ascan.enable_scanners([], scanpolicyname=policy)
print("-- Ignore scanning private " + zap.ascan.exclude_from_scan("%s/private.*" % target))
print("-- Ignore scanning dist " + zap.ascan.exclude_from_scan("%s/dist.*" % target))
print("-- Ignore scanning ftp " + zap.ascan.exclude_from_scan("%s/ftp.*" % target))
print("-- Ignore scanning socket.io " + zap.ascan.exclude_from_scan("%s/socket.io.*" % target))
skippable_scanners = list(set([
'41', '43', '0', '7', '90020', '40032', '40026',
'10029', '10032', '10035', '10040', '10045', '10046',
'10047', '10049', '10050', '10051', '10052', '2011', '2',
'3192', '10053', '10056', '10057', '10058', '10061', '10095',
'64', '10094','90034', '10096', '10097', '10099', '10104',
'10105', '10107', '20014', '20015', '20016', '2012', '1823',
'20017', '2012', '1823', '20018', '20019', '30001', '30002',
'30003', '40008', '40009', '40013', '40015', '40020', '40021',
'40022', '40023', '40025', '40027', '6', '60100', '40003', '42',
'60101', '90021', '90023', '90024', '90025', '90026', '90027',
'90028', '90029', '90030', '90033','40028', '40029', '20012', '40019'
]))
print("-- Skipping scanners " + zap.ascan.disable_scanners(','.join(skippable_scanners), scanpolicyname=policy))
sql_scanners = ['40024', '40018', '90018']
for id in sql_scanners:
print(("-- Upping attack strength for id=%s " % id) + zap.ascan.set_scanner_attack_strength(id, "HIGH", policy))
do_scan(zap, target + '/rest/user/login')
# print("-- Importing policy " + zap.ascan.import_scan_policy('/zap/wrk/sample.policy'))
for id in sql_scanners:
print(("-- Setting normal attack strength for id=%s " % id) + zap.ascan.set_scanner_attack_strength(id, "DEFAULT", policy))
# High strength DOM XSS
# scanner_dom_xss = "40026"
# print("-- Upping DOM XSS strength " + zap.ascan.set_scanner_attack_strength(scanner_dom_xss, "HIGH", policy))
# Time box individual scans to a time limit
# zap.ascan.set_option_max_rule_duration_in_mins("1")
# Time box the entire active scan
zap.ascan.set_option_max_scan_duration_in_mins("10")
_list_scanners(zap)
# Increase the amount of threads if local target
if "172." in target or "127." in target or "192." in target:
new_threads = "8"
was_threads = zap.ascan.option_thread_per_host
up_threads = zap.ascan.set_option_thread_per_host(new_threads)
print("-- Upping threads per host from " + was_threads + " to " + new_threads + " " + up_threads)
return zap, target, policy
def zap_pre_shutdown(zap):
print("-- Found urls ")
# print(zap.search. zap.search.urls_by_url_regex('.*api.*'))
for endpoint in zap.core.urls():
print(" - %s" % endpoint)