-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathness8rest.py
1125 lines (935 loc) · 42.4 KB
/
ness8rest.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2014-2015, Tenable Network Security, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# - Neither the name of Tenable Network Security, Inc. nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, TITLE,
# NON-INFRINGEMENT, INTEGRATION, PERFORMANCE, AND ACCURACY AND ANY IMPLIED
# WARRANTIES ARISING FROM STATUTE, COURSE OF DEALING, COURSE OF PERFORMANCE, OR
# USAGE OF TRADE, ARE DISCLAIMED. IN NO EVENT SHALL TENABLE NETWORK SECURITY,
# INC., OR ANY SUCCESSOR-IN-INTEREST, BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from __future__ import print_function
'''
Module for interacting with Nessus REST interface
'''
import os
import sys
import atexit
import time
import requests
import json
import re
import collections
class SSLException(Exception):
pass
class Scanner(object):
'''
Scanner object
'''
def __init__(self, url, login='', password='', api_akey='', api_skey='',
insecure=False, ca_bundle=''):
self.api_akey = None
self.api_skey = None
self.use_api = False
self.name = ''
self.policy_name = ''
self.debug = False
self.format = ''
self.format_start = ''
self.format_end = ''
self.http_response = ''
self.plugins = {}
self.names = {}
self.files = {}
self.cisco_offline_configs = ''
self.permissions = ''
self.policy_id = ''
self.policy_object = ''
self.pref_cgi = ''
self.pref_paranoid = ''
self.pref_supplied = ''
self.pref_thorough = ''
self.pref_max_checks = ''
self.pref_receive_timeout = ''
self.set_safe_checks = ''
self.pref_verbose = ''
self.pref_silent_dependencies = ''
self.res = {}
self.scan_id = ''
self.scan_name = ''
self.scan_template_uuid = ''
self.scan_uuid = ''
self.tag_id = ''
self.tag_name = ''
self.targets = ''
self.policy_template_uuid = ''
self.token = ''
self.url = url
self.ver_feed = ''
self.ver_gui = ''
self.ver_plugins = ''
self.ver_svr = ''
self.ver_web = ''
self.ca_bundle = ca_bundle
self.insecure = insecure
self.auth = []
self.host_vulns = {}
self.plugin_output = {}
self.host_details = {}
self.host_ids = {}
if insecure and hasattr(requests, 'packages'):
requests.packages.urllib3.disable_warnings()
if (api_akey and api_skey):
self.api_akey = api_akey
self.api_skey = api_skey
self.use_api = True
else:
# Initial login to get our token for all subsequent transactions
self._login(login, password)
Register a call to the logout action automatically
atexit.register(self.action, action="session",
method="DELETE", retry=False)
self._get_permissions()
self._get_scanner_id()
################################################################################
def _login(self, login="", password=""):
if login and password:
self.auth = [login,password]
self.action(action="session",
method="POST",
extra={"username": self.auth[0], "password": self.auth[1]},
private=True,
retry=False)
try:
self.token = self.res["token"]
except KeyError:
if self.res["error"]:
print("It looks like you're trying to login into a Nessus 5")
print("instance. Exiting.")
sys.exit(0)
################################################################################
def _get_permissions(self):
'''
All development has been conducted using and administrator account which
had the permissions '128'
'''
self.action(action="session", method="GET")
self.permissions = self.res['permissions']
################################################################################
def _get_scanner_id(self):
'''
Pull in information about scanner. The ID is necessary, everything else
is "nice to have" for debugging.
'''
self.action(action="scanners", method="GET")
try:
for scanner in self.res["scanners"]:
if scanner["type"] == "local":
self.scanner_id = scanner['id']
self.ver_plugins = scanner['loaded_plugin_set']
self.ver_gui = scanner['ui_version']
self.ver_svr = scanner['engine_version']
self.ver_feed = scanner['license']['type']
except:
pass
################################################################################
def _get_token(self):
'''
This Function is added by Fahimeh Rezaei and is reponsible for extracting
user X-API-Token from /nessus6.js
'''
r = requests.get(self.url + '/nessus6.js', verify=False)
api_token = re.findall('([A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12})\"}',str(r.content)
,re.IGNORECASE)[0]
return api_token
################################################################################
def action(self, action, method, extra={}, files={}, json_req=True, download=False, private=False, retry=True):
'''
Generic actions for REST interface. The json_req may be unneeded, but
the plugin searching functionality does not use a JSON-esque request.
This is a backup setting to be able to change content types on the fly.
'''
api_token = self._get_token()
payload = {}
payload.update(extra)
if self.use_api:
headers = {'X-ApiKeys': 'accessKey=' + self.api_akey +
'; secretKey=' + self.api_skey}
else:
headers = {'X-Cookie': 'token=' + str(self.token), 'X-API-Token': api_token}
if json_req:
headers.update({'Content-type': 'application/json',
'Accept': 'text/plain'})
payload = json.dumps(payload)
url = "%s/%s" % (self.url, action)
if self.debug:
if private:
print("JSON : **JSON request hidden**")
else:
print("JSON :")
print(payload)
print("HEADERS :")
print(headers)
print("URL : %s " % url)
print("METHOD : %s" % method)
print("\n")
# Figure out if we should verify SSL connection (possibly with a user
# supplied CA bundle). Default to true.
if self.insecure:
verify = False
elif self.ca_bundle:
verify = self.ca_bundle
else:
verify = True
try:
req = requests.request(method, url, data=payload, files=files,
verify=verify, headers=headers)
if not download and req.text:
self.res = req.json()
elif not req.text:
self.res = {}
if req.status_code != 200:
print("*****************START ERROR*****************")
if private:
print("JSON : **JSON request hidden**")
else:
print("JSON :")
print(payload)
print(files)
print("HEADERS :")
print(headers)
print("URL : %s " % url)
print("METHOD : %s" % method)
print("RESPONSE: %d" % req.status_code)
print("\n")
self.pretty_print()
print("******************END ERROR******************")
if self.debug:
# This could also contain "pretty_print()" but it makes a lot of
# noise if enabled for the entire scan.
print("RESPONSE CODE: %d" % req.status_code)
if download:
return req.content
except requests.exceptions.SSLError as ssl_error:
raise SSLException('%s for %s.' % (ssl_error, url))
except requests.exceptions.ConnectionError:
raise Exception("Could not connect to %s.\nExiting!\n" % url)
if self.res and "error" in self.res and retry:
if self.res["error"] == "You need to log in to perform this request" or self.res["error"] == "Invalid Credentials":
self._login()
self.action(action=action, method=method, extra=extra, files=files,
json_req=json_req, download=download, private=private,
retry=False)
################################################################################
def _policy_template_uuid(self, name):
'''
Get the template ID. This provides the default settings for the policy.
'''
self.action(action="editor/policy/templates", method="GET")
for template in self.res["templates"]:
if template["name"] == name:
self.policy_template_uuid = template["uuid"]
break
################################################################################
def _scan_template_uuid(self, name):
'''
Get the template ID. This provides the default settings for the policy.
'''
self.action(action="editor/scan/templates", method="GET")
for template in self.res["templates"]:
if template["name"] == name:
self.scan_template_uuid = template["uuid"]
break
################################################################################
def policy_add(self, name, plugins, credentials=[], template="advanced"):
'''
Add a policy and store the returned ID. The template defaults to
"advanced" to remain compatible with the calls that occur in Nessus
5.2.x.
'''
self.policy_name = name
self._policy_template_uuid(template)
self._policy_edit_template(uuid=self.policy_template_uuid)
try:
self.policy_id = self.res["policy_id"]
# prevent duplicate names when we build the scan
self.policy_name = self.res["policy_name"]
except KeyError:
print("policy_id was not returned. Exiting")
sys.exit(1)
self.policy_add_creds(credentials=credentials)
self._policy_set_settings()
self.plugins_info(plugins=plugins)
self._enable_plugins()
################################################################################
def policy_copy(self, existing_policy_name, new_policy_name):
'''
Create a copy of an existing policy and set it to be used for a scan
'''
self.action(action="policies", method="GET")
for policy in self.res["policies"]:
if policy["name"] == existing_policy_name:
self.action(action="policies/" + str(policy["id"]) + "/copy", method="POST")
self.policy_id = self.res["id"]
'''
If there is a name conflict the rename appends a
number to the requested name.
'''
self.policy_name = new_policy_name
self.action(action="policies/" + str(self.policy_id), method="PUT",
extra={"settings":{"name": self.policy_name}})
return True
return False
################################################################################
def policy_exists(self, name):
'''
Set existing policy to use for a scan.
'''
self.policy_name = name
self.action(action="policies", method="GET")
for policy in self.res["policies"]:
if policy["name"] == name:
self.policy_id = policy["id"]
return True
return False
################################################################################
def policy_set(self, name):
'''
Set existing policy to use for a scan.
'''
self.policy_name = name
self.action(action="policies", method="GET")
for policy in self.res["policies"]:
if policy["name"] == name:
self.policy_id = policy["id"]
break
if not self.policy_id:
print("no policy with name %s found. Exiting" % name)
sys.exit(1)
################################################################################
def policy_details(self, policy_id):
'''
Retrieves details of an existing policy.
'''
self.policy_id = policy_id
self.action(action="policies/" + str(self.policy_id), method="GET")
return self.res
################################################################################
def policy_delete(self, name):
'''
Delete a policy.
'''
self.action(action="policies", method="GET")
for policy in self.res["policies"]:
if policy["name"] == name:
self.action(action="policies/" + str(policy["id"]), method="DELETE")
return True
return False
################################################################################
def _policy_edit_template(self, uuid):
'''
Using the UUID, create the base policy, which will then be manipulated.
This is easier than attempting to design an entire policy in one call.
'''
extra = {"settings": {"name": self.policy_name}, "uuid": uuid}
self.action(action="policies", method="POST", extra=extra)
################################################################################
def policy_add_ports(self, ports):
'''
Read current ports and append needed ports. The current value could have
been gathered when disabling the plugin families, but for the sake of an
extra call, it is much more clear what is occurring.
'''
discovery = {}
default_ports = ""
self.action(action="editor/policy/" + str(self.policy_id), method="GET")
for inputs in self.res["settings"]["discovery"]["groups"]:
if inputs["name"] == "network_discovery":
discovery = inputs["sections"]
for item in discovery:
for nested in item["inputs"]:
if nested["id"] == "portscan_range":
default_ports = nested["default"]
new_ports = str(default_ports) + "," + str(ports)
extra = {"settings": {"portscan_range": new_ports}}
self.action(action="policies/" + str(self.policy_id), method="PUT",
extra=extra)
###############################################################################
def policy_limit_ports(self, ports):
'''
Limit the ports scanned to the given list.
'''
extra = {"settings": {"portscan_range": str(ports)}}
self.action(action="policies/" + str(self.policy_id), method="PUT",
extra=extra)
################################################################################
def policy_add_creds(self, credentials, policy_id=""):
'''
Add a list of credentials, defined using the objects in the credential
module.
'''
if not policy_id:
policy_id = self.policy_id
creds = collections.defaultdict(lambda: collections.defaultdict(list))
for credential in credentials:
creds[credential.category][credential.name].append(credential.__dict__)
creds = {"credentials": {"add": creds}}
self.action(action="policies/" + str(policy_id),
method="PUT", extra=creds)
################################################################################
def _policy_set_settings(self):
'''
Current settings include: safe_checks, scan_webapps, report_paranoia,
provided_creds_only, thorough_tests, report_verbosity,
silent_dependencies
'''
settings = {"settings": {}}
# Default to safe checks
# Values: yes, no
if not self.set_safe_checks:
self.set_safe_checks = "yes"
# Default to not scanning webapps
# Values: yes, no
if not self.pref_cgi:
self.pref_cgi = "no"
# Default to normal paranoia levels
# Values: Avoid false alarms, Normal, Paranoid
if not self.pref_paranoid:
self.pref_paranoid = "Normal"
# Default to allow scans to check for default credentials
# Values: yes, no
if not self.pref_supplied:
self.pref_supplied = "no"
# Default to not use thorough tests
# Values: yes, no
if not self.pref_thorough:
self.pref_thorough = "no"
# Default to normal verbosity.
# Values: Quiet, Normal, Verbose
if not self.pref_verbose:
self.pref_verbose = "Normal"
# Default to normal reporting of dependencies
# Values: yes, no
if not self.pref_silent_dependencies:
self.pref_silent_dependencies = "yes"
# Plugin receive timeout limit
# Values: positive integers, passed as strings
# Nessus default: 5
if not self.pref_receive_timeout:
self.pref_receive_timeout = "5"
# Maximum concurrent checks
# Values: positive integers, passed as strings
# Nessus default: 5
if not self.pref_max_checks:
self.pref_max_checks = "5"
settings["settings"].update({"safe_checks": self.set_safe_checks})
settings["settings"].update({"scan_webapps": self.pref_cgi})
settings["settings"].update({"report_paranoia": self.pref_paranoid})
settings["settings"].update({"provided_creds_only": self.pref_supplied})
settings["settings"].update({"thorough_tests": self.pref_thorough})
settings["settings"].update({"report_verbosity": self.pref_verbose})
settings["settings"].update({"silent_dependencies":
self.pref_silent_dependencies})
settings["settings"].update({"cisco_offline_configs":
self.cisco_offline_configs})
settings["settings"].update({"network_receive_timeout":
self.pref_receive_timeout})
settings["settings"].update({"max_checks_per_host":
self.pref_max_checks})
self.action(action="policies/" + str(self.policy_id), method="PUT",
extra=settings)
################################################################################
def _policy_remove_audits(self, category, type='custom'):
'''
Removes all audit files from the policy.
'''
delete_ids = []
self.action(action="editor/policy/" + str(self.policy_id),
method="GET")
for record in self.res['compliance']['data']:
if record['name'] == category:
for audit in record['audits']:
if audit['type'] == type and 'id' in audit:
delete_ids.append(str(audit['id']))
audit = {"audits": {"custom": {"delete": []}}}
if len(delete_ids) > 0:
audit["audits"]["custom"]["delete"] = delete_ids
self.action(action="policies/" + str(self.policy_id),
method="PUT", extra=audit)
################################################################################
def _policy_add_audit(self, category, filename):
'''
Adds an audit file to the policy.
'''
audit = {"audits": {"custom": {"add": []}}}
audit["audits"]["custom"]["add"].append(
{"file": filename,
"category": category})
self.action(action="policies/" + str(self.policy_id),
method="PUT", extra=audit)
################################################################################
def plugins_info(self, plugins):
'''
Gather information on plugins for reporting. This also ensures that the
plugin exists, and exits if it does not.
'''
for plugin in plugins.split(','):
self.action(action="plugins/plugin/" + str(plugin), method="GET")
if "attributes" in self.res:
for attrib in self.res["attributes"]:
if attrib["attribute_name"] == "fname":
self.plugins.update({str(plugin):
{"fname":
attrib["attribute_value"],
"name": self.res["name"]}})
else:
# We don't want to scan with plugins that don't exist.
print ("Plugin with ID %s is not found. Exiting." % plugin)
sys.exit(1)
################################################################################
def _enable_plugins(self, plugins=[]):
'''
Disable all of the families, and then enable plugins that you need. This
builds the entire "plugins" object, and can be very large for some
families, such as "AIX", as it needs to make an entry for each plugin in
the family to set the status.
'''
families = {"plugins": {}}
updates = {}
family_id = {}
self.action(action="editor/policy/" + str(self.policy_id), method="GET")
# Build an object to disable all plugins at the family level.
for item in self.res["plugins"]["families"]:
families["plugins"].update({item: {"status": "disabled"}})
# print(json.dumps(families, sort_keys=False, indent=4))
self.action(action="policies/" + str(self.policy_id),
method="PUT", extra=families)
# Query the search interface to get the family information for the
# plugin
for plugin in self.plugins.keys():
self.action(action="editor/policy/" + str(self.policy_id) +
"/families?filter.search_type=and&" +
"filter.0.filter=plugin_id&filter.0.quality=eq&" +
"filter.0.value=" + str(plugin), method="GET")
for family in self.res["families"]:
# if family not in updates:
if family not in updates:
# Add the key if it isn't in the dict
updates.update({family: []})
# Add the plugin to the list of the family
updates[family].append(plugin)
# Track the family ID so we can request the list of plugins
family_id.update({family:
str(self.res["families"][family]["id"])})
# Build the stub for a family that has individual plugins enabled
for fam, fam_id in family_id.items():
families["plugins"][fam].update({"status": "mixed"})
families["plugins"][fam].update({"individual": {}})
self.action(action="editor/policy/" + str(self.policy_id) +
"/families/" + str(fam_id), method="GET")
# Disable every plugin in the family
all_disabled = {}
for pid in self.res["plugins"]:
all_disabled.update({str(pid["id"]): "disabled"})
# Update the "plugins" object to have all individual plugins
# disabled
families["plugins"][fam]["individual"].update(all_disabled)
# Update each of the plugins that we have selected to enable
for fam, pids in updates.items():
for pid in pids:
families["plugins"][fam]["individual"].update({str(pid):
"enabled"})
self.action(action="policies/" + str(self.policy_id),
method="PUT", extra=families)
################################################################################
def scan_add(self, targets, template="custom", name="", start=""):
'''
After building the policy, create a scan.
'''
self._scan_template_uuid(name=template)
self._scan_tag()
# This makes the targets much more readable in the GUI, as it splits
# them out to "one per line"
text_targets = targets.replace(",", "\n")
self.targets = targets.replace(",", " ")
# Figure out scan name
if name:
self.scan_name = name
else:
self.scan_name = self.policy_name
scan = {"uuid": self.scan_template_uuid}
settings = {}
# Static items- some could be dynamic, but it's overkill
settings.update({"launch": "ON_DEMAND"})
settings.update({"description": "Created with REST API"})
settings.update({"file_targets": ""})
settings.update({"filters": []})
settings.update({"emails": ""})
settings.update({"filter_type": ""})
# Dynamic items
settings.update({"scanner_id": str(self.scanner_id)})
settings.update({"name": self.scan_name})
if self.policy_id:
settings.update({"policy_id": self.policy_id})
settings.update({"folder_id": self.tag_id})
settings.update({"text_targets": text_targets})
# Start a scan at a scheduled time
if start:
settings.update({"starttime": start})
settings.update({"rrules": "FREQ=ONETIME"})
scan.update({"settings": settings})
self.action(action="scans", method="POST", extra=scan)
# This is the scan template UUID, this will be overwritten when we run
# the actual scan. Storing this value is mainly for debugging. If
# something was to go wrong, and we called "objdump", seeing
# "template-..." would be an obvious indicator of our location in
# creating the scan.
self.scan_uuid = self.res["scan"]["uuid"]
# We use the id for building the "launch" URL
self.scan_id = self.res["scan"]["id"]
################################################################################
def scan_exists(self, name):
'''
Set existing scan.
'''
self.scan_name = name
self.action(action="scans", method="GET")
if "scans" in self.res and self.res["scans"]:
for scan in self.res["scans"]:
if scan["name"] == name:
self.scan_id = scan["id"]
return True
return False
################################################################################
def scan_update_targets(self, targets):
'''
After update targets on existing scan.
'''
# This makes the targets much more readable in the GUI, as it splits
# them out to "one per line"
text_targets = targets.replace(",", "\n")
self.targets = targets.replace(",", " ")
self.action(action="scans/" + str(self.scan_id), method="GET")
#scan = {"uuid": self.scan_uuid}
scan = {}
settings = {}
# Static items- some could be dynamic, but it's overkill
# Dynamic items
settings.update({"name": self.scan_name})
settings.update({"policy_id": self.policy_id})
settings.update({"folder_id": self.tag_id})
settings.update({"text_targets": text_targets})
scan.update({"settings": settings})
self.action(action="scans/" + str(self.scan_id), method="PUT", extra=scan)
################################################################################
def scan_run(self):
'''
Start the scan and save the UUID to query the status
'''
self.action(action="scans/" + str(self.scan_id) + "/launch",
method="POST")
self.scan_uuid = self.res["scan_uuid"]
print("Scan name : %s" % self.scan_name)
# print("Scan UUID : %s" % self.scan_uuid)
################################################################################
def _scan_status(self):
'''
Check on the scan every 2 seconds.
'''
running = True
counter = 0
while running:
self.action(action="scans?folder_id=" + str(self.tag_id),
method="GET")
for scan in self.res["scans"]:
if (scan["uuid"] == self.scan_uuid
and (scan['status'] == "running" or scan['status'] == "pending")):
sys.stdout.write(".")
sys.stdout.flush()
time.sleep(2)
counter += 2
if counter % 60 == 0:
print("")
if (scan["uuid"] == self.scan_uuid
and scan['status'] != "running" and scan['status'] != "pending"):
running = False
# Yes, there are timestamps that we can use to compute the
# actual running time, however this is just a rough metric
# that's more to get a feel of how long something is taking,
# it's not meant for precision.
print("\nComplete! Run time: %d seconds." % counter)
################################################################################
def _scan_tag(self, name="CLI"):
'''
Set the 'tag' for the scan to CLI, if the tag doesn't exist, create it
and use the resulting ID
'''
# Default to "CLI"
if not self.tag_name:
self.tag_name = name
self.action(action="folders", method="GET")
# Get the numeric ID of the tag. This is used to tag where the scan will
# live in the GUI, as well as help filter the "scan_status" queries and
# limit traffic/results processing.
for tag in self.res["folders"]:
if tag["name"] == self.tag_name:
self.tag_id = tag["id"]
break
# Create the new tag if it doesn't exist
if not self.tag_id:
self.action("folders", method="POST", extra={"name": self.tag_name})
self.tag_id = self.res["id"]
################################################################################
def scan_details(self, name):
'''
Fetch the details of the requested scan
'''
# Find the scan id based on the name
self.action(action="scans", method="GET")
for scan in self.res["scans"]:
if scan["name"] == name:
self.scan_id = scan["id"]
break
if not self.scan_id:
print("no scan with name %s found. Exiting" % name)
sys.exit(1)
# Get the details of the scan
self.action(action="scans/" + str(self.scan_id), method="GET")
################################################################################
def scan_list(self):
'''
Fetch a list with scans
'''
self.action(action="scans", method="GET")
return self.res
################################################################################
def get_host_vulns(self, name):
'''
Fill in host_vulns dict with the host vulnerabilities found in a
scan
'''
# Get details of requested scan
self.scan_details(name)
for host in self.res["hosts"]:
self.action(action="scans/" + str(self.scan_id) + "/hosts/" + str(host["host_id"]), method="GET")
#print("scans/" + str(self.scan_id)+ "/hosts/" +str(host["host_id"]))
if self.scan_id not in self.host_vulns:
self.host_vulns[self.scan_id] = {}
self.host_vulns[self.scan_id][host["host_id"]]=self.res
################################################################################
def get_host_ids(self, name):
'''
List host_ids in given scan
'''
# Get details of requested scan
self.scan_details(name)
for host in self.res["hosts"]:
#print("%s" % host["host_id"])
self.host_ids[host["host_id"]]=1
################################################################################
def get_host_details(self, scan_id, host_id):
'''
Fill in host_details dict with the host vulnerabilities found in a
scan
'''
# Get details of requested scan
self.action(action="scans/" + str(scan_id) + "/hosts/" + str(host_id), method="GET")
if scan_id not in self.host_details:
self.host_details[scan_id] = {}
self.host_details[scan_id][host_id]=self.res
################################################################################
def get_plugin_output(self, scan, plugin_id):
'''
Fill in plugin_output dict with the output from a given plugin
in a given scan
'''
# Make sure the supplied plugin_id is of type int
plugin_id = int(plugin_id)
# Get list of host vulns
self.get_host_vulns(scan)
for scan_id in self.host_vulns:
for host_id in self.host_vulns[scan_id]:
for vulnerability in self.host_vulns[scan_id][host_id]["vulnerabilities"]:
if vulnerability["plugin_id"] == plugin_id:
self.action(action="scans/" + str(scan_id) + "/hosts/" + str(host_id) + "/plugins/" + str(plugin_id), method="GET")
if scan_id not in self.plugin_output:
self.plugin_output[scan_id] = {}
self.plugin_output[scan_id][host_id]=self.res
################################################################################
def _deduplicate_hosts(self, hosts):
return list({v["hostname"]: v for v in hosts}.values())
################################################################################
def download_kbs(self):
self.action("scans/" + str(self.scan_id), method="GET")
# Merge vulnerability and compliance hosts into a list, unique by
# hostname.
merged_hosts = self.res.get("hosts", []) + self.res.get("comphosts", [])
hosts = self._deduplicate_hosts(hosts=merged_hosts)
kbs = {}
for host in hosts:
kbs[host["hostname"]] = self.action("scans/" + str(self.scan_id) +
"/hosts/" + str(host["host_id"]) +
"/kb?token=" + str(self.token),
method="GET",
download=True)
return kbs
################################################################################
def download_scan(self, export_format="", chapters="", dbpasswd=""):
'''
This Function was changed by Fahimeh Rezaei in order to suppert new
endpoint and parameters for exporting
'''
running = True
counter = 0
self.action("scans/" + str(self.scan_id), method="GET")
if (export_format=="db"):
data = {"format":"db","password":dbpasswd}
elif (export_format=="html" or export_format=="pdf"):
data = {"format":export_format,"chapters":chapters}
else:
data = {'format': export_format}
self.action("scans/" + str(self.scan_id) + "/export",
method="POST",
extra=data)
token = self.res['token']
print('Download for file id '+str(self.res['file'])+'.')
while running:
time.sleep(2)
counter += 2
self.action("tokens/" + str(token) + "/status", method="GET")
running = self.res['status'] != 'ready'
sys.stdout.write(".")
sys.stdout.flush()
if counter % 60 == 0:
print("")
print("")
content = self.action("tokens/" + str(token) + "/download",
method="GET",
download=True)
return content
################################################################################
def scan_results(self):
'''
Get the list of hosts, then iterate over them and extract results
'''