Skip to content

Commit 9e81075

Browse files
committed
Introduced a new ZAPExtended Class as wrapper and alternative to the exisiting zap python + hook implementation.
1 parent 2ce8fe8 commit 9e81075

7 files changed

Lines changed: 125 additions & 182 deletions

File tree

‎scanners/zap-extended/scanner/scbzapv2/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
from .zap_context import ZapConfigureContext
1010
from .zap_spider import ZapConfigureSpider
1111
from .zap_scanner import ZapConfigureActiveScanner
12+
from .zap_extended import ZapExtended
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import os
2+
import sys
3+
import time
4+
import json
5+
import requests
6+
import base64
7+
import collections
8+
import logging
9+
10+
from urllib.parse import urlparse
11+
from zapv2 import ZAPv2, ascan
12+
13+
from .zap_configuration import ZapConfiguration
14+
from .zap_context import ZapConfigureContext
15+
from .zap_spider import ZapConfigureSpider
16+
from .zap_scanner import ZapConfigureActiveScanner
17+
18+
class ZapExtended:
19+
"""This class configures running ZAP instance
20+
21+
Based on this opensource ZAP Python example:
22+
- https://github.com/zaproxy/zap-api-python/blob/9bab9bf1862df389a32aab15ea4a910551ba5bfc/src/examples/zap_example_api_script.py
23+
"""
24+
25+
def __init__(self, zap: ZAPv2, config_dir: str):
26+
"""Initial constructor used for this class
27+
28+
Parameters
29+
----------
30+
zap : ZAPv2
31+
The running ZAP instance to configure.
32+
config_dir : ZapConfiguration
33+
The configuration object containing all ZAP configs (based on the class ZapConfiguration).
34+
"""
35+
36+
self.__zap = zap
37+
self.__config_dir = config_dir
38+
39+
self.__config = ZapConfiguration(config_dir)
40+
41+
self.__zap_context = None
42+
self.__zap_spider = None
43+
self.__zap_scan = None
44+
45+
def scb_scan(self, target:str):
46+
47+
logging.info('Configuring ZAP Context')
48+
# Starting to configure the ZAP Instance based on the given Configuration
49+
if self.__config.has_configurations() and self.__config.has_context_configurations:
50+
self.__zap_context = ZapConfigureContext(self.__zap, self.__config)
51+
52+
logging.info('Starting ZAP Spider with target %s', target)
53+
# if a ZAP Configuration is defined start to configure the running ZAP instance (`zap`)
54+
if self.__config and self.__config.has_spider_configurations:
55+
# Starting to configure the ZAP Spider Instance based on the given Configuration
56+
self.__zap_spider = ZapConfigureSpider(self.__zap, self.__config)
57+
spider_id = self.__zap_spider.start_spider_by_url(target)
58+
59+
logging.info('Starting ZAP Scanner with target %s', target)
60+
# if a ZAP Configuration is defined start to configure the running ZAP instance (`zap`)
61+
if self.__config and self.__config.has_scan_configurations:
62+
# Starting to configure the ZAP Instance based on the given Configuration
63+
self.__zap_scan = ZapConfigureActiveScanner(self.__zap, self.__config)
64+
# Search for the corresponding context based on the given targetUrl which should correspond to defined the spider url
65+
scan_id = self.__zap_scan.start_scan_by_url(target)
66+
67+
def get_zap_context(self) -> ZapConfigureContext:
68+
return self.__zap_context
69+
70+
def get_zap_spider(self) -> ZapConfigureSpider:
71+
return self.__zap_spider
72+
73+
def get_zap_scan(self) -> ZapConfigureActiveScanner:
74+
return self.__zap_scan

‎scanners/zap-extended/scanner/scbzapv2/zap_scanner.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,15 @@ def _start_scanner(self, scanner_config: collections.OrderedDict) -> int:
167167
logging.info("ActiveScan returned: %s", scannerId)
168168

169169
if not str(scannerId).isdigit() or int(scannerId) < 0:
170-
logging.error("ActiveScan couldnt be started due to errors: %s", scannerId)
170+
logging.error("ActiveScan couldn't be started due to errors: %s", scannerId)
171+
raise RuntimeError("ActiveScan couldn't be started due to errors: %s", scannerId)
171172
else:
172173
logging.info("ActiveScan successfully started with id: %s", scannerId)
173174
# Give the scanner a chance to start
174175
time.sleep(5)
175176

177+
self.wait_until_finished(int(scannerId))
178+
176179
return scannerId
177180

178181
def get_alerts(self, baseurl, ignore_scan_rules, out_of_scope_dict):

‎scanners/zap-extended/scanner/scbzapv2/zap_spider.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def start_spider_by_url(self, url: str) -> int:
5353
spider_config=None
5454
if "name" in context:
5555
spider_config = self.__config.get_spider_by_context_name(str(context["name"]))
56-
ajax = bool(spider_config["ajax"]) if "ajax" in spider_config else False
56+
ajax = True if "ajax" in spider_config else False
5757

5858
logging.info('Trying to start Spider (Ajax: %s) by configuration target url %s', str(ajax), url)
5959
spiderId = self._start_spider(spider_config=spider_config, ajax=ajax_config)
@@ -78,7 +78,7 @@ def start_spider_by_index(self, index: int) -> int:
7878

7979
if self.__config.has_spider_configurations:
8080
spider_config = self.__config.get_spider_by_index(index)
81-
ajax = bool(spider_config["ajax"]) if "ajax" in spider_config else False
81+
ajax = True if "ajax" in spider_config else False
8282

8383
logging.debug('Trying to start Spider (Ajax: %s) by configuration index %s', str(ajax), str(index))
8484
spiderId = self._start_spider(spider_config=spider_config, ajax=ajax)
@@ -100,8 +100,8 @@ def start_spider_by_name(self, name: str) -> int:
100100

101101
if self.__config.has_spider_configurations:
102102
spider_config = self.__config.get_spider_by_name(name)
103-
ajax = bool(spider_config["ajax"]) if "ajax" in spider_config else False
104-
103+
ajax = True if "ajax" in spider_config else False
104+
105105
logging.debug('Trying to start Spider (Ajax: %s) by configuration index %s', str(ajax), str(index))
106106
spiderId = self._start_spider(spider_config=spider_config, ajax=ajax)
107107

@@ -215,22 +215,29 @@ def _start_spider(self, spider_config: collections.OrderedDict, ajax: bool) -> i
215215

216216
if ("OK" != str(spiderId)):
217217
logging.error("Spider couldn't be started due to errors: %s", spiderId)
218+
raise RuntimeError("Spider couldn't be started due to errors: %s", spiderId)
218219
else:
219220
# due to the fact that there can be only one ajax spider at once the id is "pinned" to 1
220221
spiderId = 1
221222
logging.info("Spider successfully started with id: %s", spiderId)
222223
# Give the scanner a chance to start
223224
time.sleep(5)
225+
226+
self.wait_until_ajax_spider_finished()
227+
224228
else:
225229
logging.info('Trying to start "traditional" Spider with config: %s', spider_config)
226230
spiderId = self.__start_spider_http(spider_config, target, context_id, context_name, user_id)
227231

228232
if (not str(spiderId).isdigit()) or int(spiderId) < 0:
229233
logging.error("Spider couldn't be started due to errors: %s", spiderId)
234+
raise RuntimeError("Spider couldn't be started due to errors: %s", spiderId)
230235
else:
231236
logging.info("Spider successfully started with id: %s", spiderId)
232237
# Give the scanner a chance to start
233238
time.sleep(5)
239+
240+
self.wait_until_http_spider_finished(int(spiderId))
234241

235242
return spiderId
236243

‎scanners/zap-extended/scanner/tests/test_integration_docker_local.py‎

Lines changed: 15 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from scbzapv2.zap_context import ZapConfigureContext
1111
from scbzapv2.zap_spider import ZapConfigureSpider
1212
from scbzapv2.zap_scanner import ZapConfigureActiveScanner
13+
from scbzapv2.zap_extended import ZapExtended
1314

1415
def is_responsive(url):
1516
try:
@@ -91,103 +92,31 @@ def test_bodgeit_scan(get_bodgeit_url, get_juiceshop_url, get_zap_instance: ZAPv
9192

9293
zap = get_zap_instance
9394
test_config_yaml = "./tests/mocks/scan-full-bodgeit-docker/"
94-
95-
config = ZapConfiguration(test_config_yaml)
96-
target = "http://bodgeit:8080/"
97-
98-
logging.info('Configuring ZAP Context')
99-
# Starting to configure the ZAP Instance based on the given Configuration
100-
if config.has_configurations() and config.has_context_configurations:
101-
local_zap_context = ZapConfigureContext(zap, config)
102-
103-
configured_context = zap.context.context("scb-bodgeit-context")
104-
assert configured_context["name"] == "scb-bodgeit-context"
105-
106-
logging.info('Starting ZAP Spider with target %s', target)
107-
# if a ZAP Configuration is defined start to configure the running ZAP instance (`zap`)
108-
if config and config.has_spider_configurations:
109-
# Starting to configure the ZAP Spider Instance based on the given Configuration
110-
zap_spider = ZapConfigureSpider(zap, config)
111-
spider_id = zap_spider.start_spider_by_url(target)
112-
113-
assert int(zap.spider.status(spider_id)) > 0
114-
115-
zap_spider.wait_until_http_spider_finished(spider_id)
116-
result_urls = len(zap.core.urls())
95+
test_target = "http://bodgeit:8080/"
11796

118-
assert int(result_urls) > 10
119-
120-
logging.info('Starting ZAP Scanner with target %s', target)
121-
# if a ZAP Configuration is defined start to configure the running ZAP instance (`zap`)
122-
if config and config.has_scan_configurations:
123-
# Starting to configure the ZAP Instance based on the given Configuration
124-
zap_scan = ZapConfigureActiveScanner(zap, config)
125-
# Search for the corresponding context based on the given targetUrl which should correspond to defined the spider url
126-
scan_id = zap_scan.start_scan_by_url(target)
127-
128-
assert int(zap.ascan.status(scan_id)) > 0
129-
130-
zap_scan.wait_until_finished(scan_id)
131-
result_urls = len(zap.core.urls())
132-
133-
assert int(result_urls) > 10
134-
135-
alerts = zap_scan.get_alerts(target, [], [])
97+
zap_extended = ZapExtended(zap=zap, config_dir=test_config_yaml)
98+
zap_extended.scb_scan(target=test_target)
99+
100+
alerts = zap_extended.get_zap_scan().get_alerts(test_target, [], [])
136101

137-
logging.info('Found ZAP Alerts: %s', str(len(alerts)))
102+
logging.info('Found ZAP Alerts: %s', str(len(alerts)))
138103

139-
assert int(len(alerts)) >= 5
104+
assert int(len(alerts)) >= 5
140105

141106
def test_juiceshop_scan(get_bodgeit_url, get_juiceshop_url, get_zap_instance: ZAPv2):
142107

143108
zap = get_zap_instance
144109
test_config_yaml = "./tests/mocks/scan-full-juiceshop-docker/"
145-
146-
config = ZapConfiguration(test_config_yaml)
147-
target = "http://juiceshop:3000/"
148-
149-
logging.info('Configuring ZAP Context')
150-
# Starting to configure the ZAP Instance based on the given Configuration
151-
if config.has_configurations() and config.has_context_configurations:
152-
local_zap_context = ZapConfigureContext(zap, config)
153-
154-
configured_context = zap.context.context("scb-juiceshop-context")
155-
assert configured_context["name"] == "scb-juiceshop-context"
156-
157-
logging.info('Starting ZAP Spider with target %s', target)
158-
# if a ZAP Configuration is defined start to configure the running ZAP instance (`zap`)
159-
if config and config.has_spider_configurations:
160-
# Starting to configure the ZAP Spider Instance based on the given Configuration
161-
zap_spider = ZapConfigureSpider(zap, config)
162-
spider_id = zap_spider.start_spider_by_url(target)
163-
164-
assert zap.ajaxSpider.status == 'running'
165-
166-
zap_spider.wait_until_ajax_spider_finished()
167-
result_urls = len(zap.core.urls())
110+
test_target = "http://juiceshop:3000/"
168111

169-
assert int(result_urls) > 10
170-
171-
logging.info('Starting ZAP Scanner with target %s', target)
172-
# if a ZAP Configuration is defined start to configure the running ZAP instance (`zap`)
173-
if config and config.has_scan_configurations:
174-
# Starting to configure the ZAP Instance based on the given Configuration
175-
zap_scan = ZapConfigureActiveScanner(zap, config)
176-
# Search for the corresponding context based on the given targetUrl which should correspond to defined the spider url
177-
scan_id = zap_scan.start_scan_by_url(target)
178-
179-
assert int(zap.ascan.status(scan_id)) > 0
180-
181-
zap_scan.wait_until_finished(scan_id)
182-
result_urls = len(zap.core.urls())
183-
184-
assert int(result_urls) > 10
185-
186-
alerts = zap_scan.get_alerts(target, [], [])
112+
zap_extended = ZapExtended(zap=zap, config_dir=test_config_yaml)
113+
zap_extended.scb_scan(target=test_target)
114+
115+
alerts = zap_extended.get_zap_scan().get_alerts(test_target, [], [])
187116

188-
logging.info('Found ZAP Alerts: %s', str(len(alerts)))
117+
logging.info('Found ZAP Alerts: %s', str(len(alerts)))
189118

190-
assert int(len(alerts)) >= 3
119+
assert int(len(alerts)) >= 3
191120

192121
# # https://medium.com/opsops/deepdive-into-pytest-parametrization-cb21665c05b9
193122
# @pytest.mark.parametrize("config_folder", ["./tests/mocks/scan-full-bodgeit/", "./tests/mocks/scan-full-juiceshop/"])

0 commit comments

Comments
 (0)