Skip to content

Commit 549b29a

Browse files
committed
Introduces a complete new implementation of the ZAP-Extended scantype based on a more simple sidecar pattern.
1 parent c5adaca commit 549b29a

10 files changed

Lines changed: 335 additions & 223 deletions

File tree

‎scanners/zap-extended/examples/demo-bodgeit-scan/zap-extended-full-scan.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
labels:
66
organization: "OWASP"
77
spec:
8-
scanType: "zap-extended-full-scan"
8+
scanType: "zap-extended-scan"
99
parameters:
1010
# target URL including the protocol
1111
- "-t"
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
FROM owasp/zap2docker-stable:2.10.0
2-
COPY requirements.txt requirements.txt
3-
RUN pip3 install -r requirements.txt
4-
COPY . .
5-
# Copy doesn't respect USER directives so we need to chown and to do that we need to be root
6-
USER root
7-
RUN chown -R zap:zap /zap/*
8-
#Change back to zap at the end
9-
USER zap
1+
FROM python:3.9.0-alpine
2+
COPY . /zap-extended/
3+
RUN pip3 install -r /zap-extended/requirements.txt
4+
CMD ["/bin/sh"]
5+
WORKDIR /zap-extended
6+
ENTRYPOINT ["python3", "-m", "scbzapv2"]
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
python-owasp-zap-v2.4==0.0.18
2-
HiYaPyCo==0.4.16
3-
pytest-docker==0.10.1
2+
HiYaPyCo==0.4.16
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import argparse
2+
import json
3+
import logging
4+
import sys
5+
from pathlib import Path
6+
7+
from zapv2 import ZAPv2
8+
from .zap_extended import ZapExtended
9+
from .zap_configuration import ZapConfiguration
10+
11+
# set up logging to file - see previous section for more details
12+
logging.basicConfig(
13+
level=logging.DEBUG,
14+
format='%(asctime)s %(name)-12s %(levelname)-8s: %(message)s',
15+
datefmt='%Y-%m-%d %H:%M')
16+
17+
logger = logging.getLogger('zap-scb-extended')
18+
19+
def main():
20+
args = get_parser_args()
21+
22+
if args.target == None or len(args.target) <= 0:
23+
logger.info('Argument error: No target specified!')
24+
sys.exit(1)
25+
26+
process(args)
27+
28+
# logger.info('Write findings to file...')
29+
# write_findings_to_file(args.output_folder, findings)
30+
logger.info('Finished :-) !')
31+
32+
33+
def process(args):
34+
35+
api_key = None
36+
if not args.api_key == None and len(args.api_key) > 0:
37+
api_key = 'eor898q1luuq8054e0e5r9s3jh'
38+
39+
# MANDATORY. Define the listening address of ZAP instance
40+
zap_proxy = {
41+
"http": "http://127.0.0.1:8080",
42+
"https": "http://127.0.0.1:8080"
43+
}
44+
if not args.zap_url == None and len(args.zap_url) > 0:
45+
zap_proxy = {
46+
"http": "http://" + args.zap_url,
47+
"https": "http://" + args.zap_url
48+
}
49+
50+
try:
51+
logging.info(':: Configuring ZAP Instance with %s', zap_proxy)
52+
# Connect ZAP API client to the listening address of ZAP instance
53+
zap = ZAPv2(proxies=zap_proxy, apikey=api_key)
54+
55+
logging.info(':: Starting SCB ZAP Automation Framework with config %s', args.config_folder)
56+
zap_extended = ZapExtended(zap=zap, config_dir=args.config_folder)
57+
58+
logging.info(':: Starting SCB ZAP Scan with target %s', args.target)
59+
zap_extended.scb_scan(target=args.target)
60+
61+
alerts = zap_extended.get_zap_scan().get_alerts(args.target, [], [])
62+
logging.info(':: Found ZAP Alerts: %s', str(len(alerts)))
63+
64+
zap_extended.generate_report_file(file_path=args.output_folder, report_type=args.report_type)
65+
66+
except argparse.ArgumentError as e:
67+
logger.error(f'Argument error: {e}')
68+
sys.exit(1)
69+
except Exception as e:
70+
logger.error(f'Unexpected error: {e}')
71+
sys.exit(3)
72+
73+
def get_parser_args(args=None):
74+
parser = argparse.ArgumentParser(prog='zap-scb-extended',
75+
description='OWASP ZAP Scan extended with secureCodeBox ZAP Extended Automation Framework')
76+
parser.add_argument("-z",
77+
"--zap-url",
78+
help='The ZAP API Url used to call the ZAP API',
79+
default=None,
80+
required=True),
81+
parser.add_argument("-a",
82+
"--api-key",
83+
help='The ZAP API Key used to call the ZAP API',
84+
default=None,
85+
required=False),
86+
parser.add_argument("-c",
87+
"--config-folder",
88+
help='The path to a local folder containing the additional ZAP configuration YAMLs used to configure OWASP ZAP.',
89+
default='/home/securecodebox/configs/',
90+
required=False)
91+
parser.add_argument("-t",
92+
"--target",
93+
help="The target to scan with OWASP ZAP.",
94+
default=None,
95+
required=True),
96+
parser.add_argument("-o",
97+
"--output-folder",
98+
help='The path to a local folder used to store the output files, eg. the ZAP Report or logfiles.',
99+
default='./',
100+
required=False)
101+
parser.add_argument("-r",
102+
"--report-type",
103+
help='The OWASP ZAP Report Type u.',
104+
choices=['XML', 'JSON', 'HTML', 'MD'],
105+
default=None,
106+
required=False),
107+
parser.add_argument("-s",
108+
"--scan",
109+
help='The scan config is optionaly used to configure what type of scan ZAP is forced to. Normaly this shold be configured with the config YAML.',
110+
choices=['baseline', 'full', 'openApi', 'graphQl'],
111+
default='baseline',
112+
required=False)
113+
return parser.parse_args(args)
114+
115+
if __name__ == '__main__':
116+
main()

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

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import collections
88
import logging
99

10+
from pathlib import Path
1011
from urllib.parse import urlparse
11-
from zapv2 import ZAPv2, ascan
12+
from zapv2 import ZAPv2
1213

1314
from .zap_configuration import ZapConfiguration
1415
from .zap_context import ZapConfigureContext
@@ -64,11 +65,65 @@ def scb_scan(self, target:str):
6465
# Search for the corresponding context based on the given targetUrl which should correspond to defined the spider url
6566
scan_id = self.__zap_scan.start_scan_by_url(target)
6667

67-
def get_zap_context(self) -> ZapConfigureContext:
68+
def get_zap_context(self) -> ZapConfigureContext:
6869
return self.__zap_context
6970

7071
def get_zap_spider(self) -> ZapConfigureSpider:
7172
return self.__zap_spider
7273

7374
def get_zap_scan(self) -> ZapConfigureActiveScanner:
74-
return self.__zap_scan
75+
return self.__zap_scan
76+
77+
def __create_session(self, session_name:str):
78+
# Start the ZAP session
79+
logging.info('Creating a new ZAP session with the name: %s', session_name)
80+
self.__zap.core.new_session(name=session_name, overwrite=True)
81+
82+
def generate_report_file(self, file_path:str, report_type:str):
83+
# To retrieve ZAP report in XML or HTML format
84+
logging.info("Creating a new ZAP Report file with type '%s' at location: '%s'", report_type, file_path)
85+
86+
# To retrieve ZAP report in XML or HTML format
87+
logging.info('Creating a new ZAP Report with type %s', report_type)
88+
if report_type == None or report_type == "XML":
89+
# Save the XML report (default)
90+
self.__write_report(
91+
self.__zap.core.xmlreport(),
92+
file_path,
93+
"xml"
94+
)
95+
if report_type == None or report_type == "HTML":
96+
# Get the HTML report
97+
self.__write_report(
98+
self.__zap.core.htmlreport(),
99+
file_path,
100+
"html"
101+
)
102+
if report_type == None or report_type == "JSON":
103+
# Get the JSON report
104+
self.__write_report(
105+
self.__zap.core.jsonreport(),
106+
file_path,
107+
"json"
108+
)
109+
if report_type == None or report_type == "MD":
110+
# Get the Markdown report
111+
self.__write_report(
112+
self.__zap.core.mdreport(),
113+
file_path,
114+
"md"
115+
)
116+
117+
def __write_report(self, report, file_path:str, filetype:str):
118+
Path(file_path).mkdir(parents=True, exist_ok=True)
119+
with open(f'{file_path}/zap-results.{filetype}', mode='w') as f:
120+
f.write(report)
121+
122+
def zap_shutdown(self):
123+
""" This shutdown ZAP and prints out ZAP Scanning stats before shutting down.
124+
"""
125+
stats = self.__zap.stats.all_sites_stats()
126+
logging.info(stats)
127+
128+
logging.info("Shutting down the running ZAP Instance")
129+
self.__zap.core.shutdown()

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

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,20 @@ def start_scan_by_url(self, url: str) -> int:
4444
scannerId = -1
4545

4646
if self.__config.has_scan_configurations:
47-
logging.debug('Trying to start ActiveScan by configuration target url %s', str(url))
47+
logging.debug("Trying to start ActiveScan by configuration target url: '%s'", str(url))
4848

4949
context=self.__config.get_context_by_url(url)
5050

5151
scanner_config=None
52-
if "name" in context:
52+
if not context == None and "name" in context:
5353
scanner_config=self.__config.get_scans_by_context_name(str(context["name"]))
54+
else:
55+
logging.warning("No context configuration found for target: %s! Starting active scanning without any related context.", url)
5456

55-
scannerId = self._start_scanner(scanner_config=scanner_config)
57+
scannerId = self._start_scanner(url=url, scanner_config=scanner_config)
5658
else:
5759
logging.error("There is no scanner specific configuration found.")
58-
60+
scannerId = self._start_scanner(url=url)
5961

6062
return int(scannerId)
6163

@@ -114,7 +116,7 @@ def wait_until_finished(self, scanner_id: int):
114116
else:
115117
logging.info("ActiveScan(%s) found total: %s URLs", scanner_id, str(num_urls))
116118

117-
def _start_scanner(self, scanner_config: collections.OrderedDict) -> int:
119+
def _start_scanner(self, url: str, scanner_config: collections.OrderedDict) -> int:
118120
""" Starts a ZAP ActiveScan with the given name for the scanners configuration, based on the given configuration and ZAP instance.
119121
120122
Parameters
@@ -127,42 +129,46 @@ def _start_scanner(self, scanner_config: collections.OrderedDict) -> int:
127129
context_id = None
128130
target = None
129131

130-
if("url" in scanner_config):
131-
target = str(scanner_config['url'])
132-
else:
133-
logging.warning("The Scanner has no 'URL' target defined, trying to use the context URL")
134-
# TODO: hanlde missing url
132+
# Clear all excisting/previous scanner data
133+
logging.debug("Cleaning all existing ActiveScans")
134+
self.__zap.ascan.remove_all_scans()
135135

136+
if not scanner_config == None:
136137

137-
# "Context" is an optional config for Scanner
138-
if("context" in scanner_config):
139-
140-
context_name = str(scanner_config['context'])
141-
scanner_context_config = self.__config.get_context_by_name(context_name)
142-
context_id = int(scanner_context_config['id'])
138+
if("url" in scanner_config):
139+
target = str(scanner_config['url'])
140+
else:
141+
logging.warning("The active scanner configuration section has no specific 'url' target defined, trying to use scanType target instead with url: '%s'", url)
142+
target=url
143143

144-
# "User" is an optional config for Scanner in addition to the context
145-
if("user" in scanner_config):
144+
# "Context" is an optional config for Scanner
145+
if("context" in scanner_config):
146+
147+
context_name = str(scanner_config['context'])
148+
scanner_context_config = self.__config.get_context_by_name(context_name)
149+
context_id = int(scanner_context_config['id'])
146150

147-
user_name = str(scanner_config['user'])
148-
# search for the current ZAP Context id for the given context name
149-
user_id = int(self.__config.get_context_user_by_name(scanner_context_config, user_name)['id'])
150-
151-
# Clear all excisting/previous scanner data
152-
logging.debug("Cleaning all existing ActiveScans")
153-
self.__zap.ascan.remove_all_scans()
151+
# "User" is an optional config for Scanner in addition to the context
152+
if("user" in scanner_config):
153+
154+
user_name = str(scanner_config['user'])
155+
# search for the current ZAP Context id for the given context name
156+
user_id = int(self.__config.get_context_user_by_name(scanner_context_config, user_name)['id'])
154157

155-
# Configure HTTP ActiveScan
156-
logging.debug("Trying to configure ActiveScan with %s", scanner_config)
157-
self.__configure_scanner(self.__zap.ascan, scanner_config)
158+
# Configure HTTP ActiveScan
159+
logging.debug("Trying to configure ActiveScan with %s", scanner_config)
160+
self.__configure_scanner(self.__zap.ascan, scanner_config)
158161

159-
# ActiveScan with user
160-
if (not context_id is None) and context_id >= 0 and (not user_id is None) and user_id >= 0:
161-
logging.debug('Starting ActiveScan(url=%s, contextid=%s, userid=%s)', target, context_id, user_id)
162-
scannerId = self.__zap.ascan.scan_as_user(url=target, contextid=context_id, userid=user_id)
162+
# ActiveScan with user
163+
if (not context_id is None) and context_id >= 0 and (not user_id is None) and user_id >= 0:
164+
logging.debug('Starting ActiveScan(url=%s, contextid=%s, userid=%s)', target, context_id, user_id)
165+
scannerId = self.__zap.ascan.scan_as_user(url=target, contextid=context_id, userid=user_id)
166+
else:
167+
logging.debug('Starting ActiveScan(url=%s, contextid=%s)', target, context_id)
168+
scannerId = self.__zap.ascan.scan(url=target, contextid=context_id)
163169
else:
164-
logging.debug('Starting ActiveScan(url=%s, contextid=%s)', target, context_id)
165-
scannerId = self.__zap.ascan.scan(url=target, contextid=context_id)
170+
logging.info("Starting ActiveScan(url='%s') without any additinal scanner configuration!", url)
171+
scannerId = self.__zap.ascan.scan(url=url, contextid=None)
166172

167173
logging.info("ActiveScan returned: %s", scannerId)
168174

@@ -171,7 +177,7 @@ def _start_scanner(self, scanner_config: collections.OrderedDict) -> int:
171177
raise RuntimeError("ActiveScan couldn't be started due to errors: %s", scannerId)
172178
else:
173179
logging.info("ActiveScan successfully started with id: %s", scannerId)
174-
# Give the scanner a chance to start
180+
# Give the scanner a chance to start
175181
time.sleep(5)
176182

177183
self.wait_until_finished(int(scannerId))

0 commit comments

Comments
 (0)