@@ -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