-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path__init__.py
400 lines (352 loc) · 15 KB
/
__init__.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import hashlib
import requests
class AuthenticationError(Exception):
pass
class Acunetix(requests.Session):
REPORT_TEMPLATES = {}
SCANNING_PROFILES = {}
def __init__(self, username=None, password=None, domain=None, ssl_verify=True, *args, **kwargs):
if any([not username, not password, not domain]):
raise ValueError("username, password and domain are required")
requests.packages.urllib3.disable_warnings()
super(Acunetix, self).__init__()
url = ["https://", domain]
self.verify = ssl_verify
self.timeout = 2
self.headers = {
"Accept": "application / json, text / plain, * / *",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
"Content-Type": "application/json;charset=UTF-8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9",
}
self.authenticated = False
self.max_redirects = 0
self.username = username
self.password = hashlib.sha256(password.encode("utf-8")).hexdigest()
self.url = "".join(url)
self.check_connectivity()
def request(self, *args, **kwargs):
try:
return super(Acunetix, self).request(timeout=1, *args, **kwargs)
except Exception as e:
print("[!] Error : {}".format(e.__repr__()))
return False
def login(self):
"""
This should be the first call on initialized Acunetix object
:return: Server info like license, expiry etc
"""
url = self.url + "/api/v1/me/login"
data = {"email": self.username, "password": self.password, "remember_me": False}
resp = self.post(url, json=data)
if resp.status_code == 204 and "X-Auth" in resp.headers:
self.authenticated = True
self.headers.update({"X-Auth": resp.headers['X-Auth']})
return self.me
else:
raise AuthenticationError("Failed to authenticate")
def logout(self):
"""
logout whenever required
:return: Boolean
"""
url = self.url + "/api/v1/me/logout"
resp = self.post(url, json={})
if resp.status_code == 204:
self.authenticated = False
return self.authenticated
def check_connectivity(self):
"""
Checks server connectivity by making a call to http://server/build.json
:return: build number of Acunetix app running on server
"""
try:
url = self.url + "/build.json"
resp = self.get(url)
self.build = resp.json()['build']
return self.build
except Exception as e:
return False
@property
def stats(self):
"""
Gets server stats
:return: JSON response from server
"""
url = self.url + "/api/v1/me/stats"
return self.get(url).json()
@property
def info(self):
"""
Get server info
:return: JSON response from server
"""
url = self.url + "/api/v1/info"
return self.get(url).json()
@property
def me(self):
"""
Get server license info, expiry etc
:return: JSON response from server
"""
url = self.url + "/api/v1/me"
return self.get(url).json()
@property
def license(self):
"""
Calls self.info and return license info
:return: License info from server
"""
return self.info['license']
@property
def notifications(self):
"""
TODO
:return: Notifications
"""
url = self.url + "/api/v1/notifications/count"
if self.get(url).json()['count'] > 0:
url = self.url + "/api/v1/notifications"
return self.get(url).json()['notifications']
return None
@property
def scanning_profiles(self):
"""
Get scanning profiles (scan types configurations)
:return: Scanning profiles with their ID
"""
url = self.url + "/api/v1/scanning_profiles"
profiles = self.get(url).json()["scanning_profiles"]
for profile in profiles:
self.SCANNING_PROFILES.update({profile["profile_id"]: profile["name"]})
return self.SCANNING_PROFILES
@property
def targets(self):
"""
TODO: Cursor implementation
Gets targets info from server
:return: JSON Array (list) from server response
"""
url = self.url + "/api/v1/targets?l={}".format(100)
return self.get(url).json()['targets']
def target(self, target_id, configuration=False):
"""
Gets target info for supplied target_id from server
:param target_id: str(target_id)
:param configuration: boolean (whether to return target configuration information too)
:return: JSON response from server
"""
url = self.url + "/api/v1/targets/{}".format(target_id)
target = self.get(url).json()
if configuration:
url = self.url + "/api/v1/targets/{}/configuration".format(target_id)
target.update({"configuration": self.get(url).json()})
return target
def delete_target(self, target_id):
"""
Deletes a target from server
:param target_id: str(target_id)
:return: boolean (True = Success)
"""
url = self.url + "/api/v1/targets/{}".format(target_id)
if self.delete(url).status_code == 204:
return True
return False
def create_target(self, address, description):
"""
Create a new target on server
:param address: Must be a fq URL address (eq: http://test.com)
:param description: Some description about the target
:return: JSON response from server
"""
url = self.url + "/api/v1/targets"
data = {"address": str(address), "description": str(description)}
resp = self.post(url, json=data)
if resp.status_code == 201:
return resp.json()
return False
def configure_target(self, target_id, scan_speed=None, site_login=False,
authentication=None, technologies=None, custom_headers=None, custom_cookies=None):
"""
Configure a target object with the below options
:param target_id: Server return valid target_id
:param scan_speed: Must be one of these ("sequential", "slow", "moderate", "fast")
:param site_login: Must be a list in this format ["type", "username", "password"]
For now type will be automatic only
:param authentication: Must be a list in this format ["username", "password"]
:param technologies: Must be a list containing technologies from the follow (ex: ["Python", "Perl"])
Supported technologies are ("ASP","ColdFusion/Jrun","ASP.NET","Python","PHP","Rails","Perl","FrontPage","Java/J2EE","Node.js")
:param custom_headers: Must be a list in this format ["Header: Value"]
:param custom_cookies: Must be a list in this format [["url", "cookieValue"]]
:return: Server returned configuration
"""
url = self.url + "/api/v1/targets/{}/configuration".format(target_id)
data = {}
if scan_speed and scan_speed in ["sequential", "slow", "moderate", "fast"]:
data.update({"scan_speed": scan_speed})
else:
data.update({"scan_speed": "fast"})
if site_login and isinstance(site_login, list):
if len(site_login) == 3:
data.update({"login": {"kind": "automatic", "credentials": {"enabled": True, "username": site_login[1],
"password": site_login[2]}}})
else:
data.update({"login": {"kind": "none"}})
if authentication and isinstance(authentication, list):
if len(authentication) == 2:
data.update(
{"authentication": {"enabled": True, "username": authentication[0], "password": authentication[1]}})
else:
data.update({"authentication": {"enabled": False}})
if technologies and isinstance(technologies, list):
data.update({"technologies": technologies})
else:
data.update({"technologies": []})
if custom_headers and isinstance(custom_headers, list):
data.update({"custom_headers": custom_headers})
else:
data.update({"custom_headers": []})
if custom_cookies and isinstance(custom_cookies, list):
data.update({"custom_cookies": [{"url": i[0], "cookie": i[1]} for i in custom_cookies]})
else:
data.update({"custom_cookies": []})
resp = self.patch(url, json=data)
if resp.status_code == 204:
return self.get(url).json()
@property
def scans(self):
url = self.url + "/api/v1/scans"
resp = self.get(url)
return resp
def stop_scan(self, scan_id):
"""
Abort a scan
:param scan_id: str(scan_id)
:return: response status_code
"""
url = self.url + "/api/v1/scans/{}/abort".format(str(scan_id))
resp = self.post(url, json={})
return resp.status_code
def create_scan(self, target_id, scan_type, report_templated_id=None):
"""
Create a new scan on server
:param target_id: str(target_id)
:param scan_type: str(scan_type_id) can be from the following
('11111111-1111-1111-1111-111111111111', 'Full Scan'),
('11111111-1111-1111-1111-111111111112', 'High Risk Vulnerabilities'),
('11111111-1111-1111-1111-111111111116', 'Cross-site Scripting Vulnerabilities'),
('11111111-1111-1111-1111-111111111113', 'SQL Injection Vulnerabilities'),
('11111111-1111-1111-1111-111111111115', 'Weak Passwords'),
('11111111-1111-1111-1111-111111111117', 'Crawl Only'),
:param report_templated_id: Optional, scan be from the following
('11111111-1111-1111-1111-111111111111', 'Developer'),
('11111111-1111-1111-1111-111111111112', 'Quick'),
('11111111-1111-1111-1111-111111111113', 'Executive Summary'),
('11111111-1111-1111-1111-111111111115', 'Affected Items'),
('11111111-1111-1111-1111-111111111124', 'Scan Comparison'),
('11111111-1111-1111-1111-111111111119', 'OWASP Top 10 2013'),
('11111111-1111-1111-1111-111111111125', 'OWASP Top 10 2017'),
:return: scan_id of newly created scan
"""
url = self.url + "/api/v1/scans"
data = {
"target_id": target_id,
"profile_id": scan_type,
"schedule": {
"disable": False,
"start_date": None,
"time_sensitive": False
}
}
if report_templated_id:
data.update({"report_template_id": report_templated_id})
resp = self.post(url, json=data)
scan_id = resp.headers['Location'].split('/')[-1]
return scan_id
def delete_scan(self, scan_id):
"""
TODO: verify that the scan is stopped
Abort a scan and then delete it from server
:param scan_id: str(scan_id)
:return:
"""
self.stop_scan(scan_id)
url = self.url + "/api/v1/scans/{}".format(str(scan_id))
resp = self.delete(url)
return resp.status_code
def scan_status(self, scan_id, extra_stats=False):
"""
Makes 2 calls to server in order a create a stat dict
:param scan_id: str(scan_id)
:param extra_stats: boolean (True fetches all stats, False fetches basic)
:return: dict(stats)
"""
url = self.url + "/api/v1/scans/{}".format(str(scan_id))
resp = self.get(url).json()
if 'code' in resp and resp['code'] == 404: # if scan doesn't exists on server
return None
progress = resp['current_session']['progress']
status = resp['current_session']['status']
vuln_stats = None
if status != "scheduled":
vuln_stats = resp['current_session']['severity_counts']
vuln_stats["informational"] = vuln_stats.pop("info")
data = {'progress': progress, 'status': status, 'vuln_stats': vuln_stats,
'session_id': resp['current_session']['scan_session_id']}
if extra_stats:
url = url + '/results/{}/statistics'.format(resp['current_session']['scan_session_id'])
resp = self.get(url).json()
aborted = resp['scanning_app']['wvs']['abort_requested']
start_date = resp['scanning_app']['wvs']['start_date']
end_data = resp['scanning_app']['wvs']['end_date']
data.update({'aborted': aborted, 'start_date': start_date, 'end_date': end_data})
return data
def get_scan_vulnerabilities(self, scan_id):
"""
TODO: cursor implementation (pagination)
Gets all vulnerabilities related to supplied scan_id
:param scan_id: str(scan_id)
:return: JSON response from server
"""
url = self.url + "/api/v1/scans/{}".format(str(scan_id))
resp = self.get(url).json()
url = url + '/results/{}/vulnerabilities'.format(resp['current_session']['scan_session_id'])
resp = self.get(url).json()['vulnerabilities']
return resp
def get_target_vulnerabilities(self, target_id):
"""
Gets all vulnerabilities related to supplied target_id by first getting last associated scan_id
:param target_id: str(target_id)
:return: result or None
"""
scan_id = self.target(target_id)['last_scan_session_id']
if scan_id:
return self.get_scan_vulnerabilities(scan_id)
else:
return None
def get_vulnerability_by_id(self, scan_id, vulnerability_id, scan_session_id=None):
"""
Get single vulnerability details
:param vulnerability_id: Vulnerability ID
:param scan_session_id: (optional)
:return: JSON response from server
"""
if not scan_session_id:
scan_session_id = self.scan_status(scan_id)['session_id']
url = self.url + "/api/v1/scans/{}/results/{}/vulnerabilities/{}".format(scan_id, scan_session_id,
vulnerability_id)
resp = self.get(url).json()
return resp
@property
def report_templates(self):
"""
Gets report templates from server
:return: dict(Template ID and Info)
"""
url = self.url + "/api/v1/report_templates"
resp = self.get(url)
templates = resp.json()['templates']
for i in templates:
self.REPORT_TEMPLATES.update({i['template_id']: {"name": i["name"], "group": i['group']}})
return self.REPORT_TEMPLATES