-
Notifications
You must be signed in to change notification settings - Fork 2
/
scan.py
202 lines (159 loc) · 8.24 KB
/
scan.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
#!/usr/bin/python
from gvm.connections import TLSConnection
from gvm.protocols.gmp import Gmp
from gvm.protocols.gmpv214 import SeverityLevel
from gvm.protocols.gmpv214 import AliveTest
from gvm.protocols.gmpv214 import CredentialType
from gvm.transforms import EtreeTransform
import logging
import os, sys, time, random
import optparse
from lxml import etree
from pathlib import Path
import base64
usage = "usage: %prog [options] hosts reportname"
parser = optparse.OptionParser(usage)
parser.add_option('-n', '--no-ping',
action="store_true", dest="consider_alive",
help="Consider all hosts as alive", default=False)
parser.add_option('--ssh-username',
action="store", dest="ssh_username",
help="SSH Username", default=None)
parser.add_option('--ssh-password',
action="store", dest="ssh_password",
help="SSH Password", default=None)
parser.add_option('--ssh-private-key',
action="store", dest="ssh_private_key",
help="SSH Private Key", default=None)
parser.add_option('--ssh-key-phrase',
action="store", dest="ssh_private_key_phrase",
help="SSH Private Key Phrase", default=None)
parser.add_option('--ssh-port',
action="store", dest="ssh_port",
help="SSH Port", default=22)
parser.add_option('-s', '--scan-config',
action="store", dest="scan_config",
help="Scan Configuration, Base or Full and fast", default="Base")
parser.add_option('-l', '--loglevel',
action="store", dest="loglevel",
help="Set loglevel", default="INFO")
options, args = parser.parse_args()
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', datefmt='[%Y-%m-%d %H:%M:%S]', level=options.loglevel.upper())
if len(args) != 2:
parser.print_help()
sys.exit()
try:
hosts = args[0]
outputfile = args[1]
logging.info('Hosts: {}'.format(hosts))
logging.info('Outputfile: {}'.format(outputfile))
connection = TLSConnection()
transform = EtreeTransform()
logging.info('Connecting to GMP')
with Gmp(connection, transform=transform) as gmp:
gmp.authenticate('admin', 'adminpassword')
logging.info('Authenticated')
config_id = "d21f6c81-2b88-4ac1-b7b4-a2a9f2ad4663" # Base
scan_config = options.scan_config.lower()
if scan_config == "full and fast":
config_id = "daba56c8-73ec-11df-a475-002264764cea" # Full and Fast
logging.info('Starting scan with config: {}'.format(config_id))
# Don't report outdated GVM
gmp.create_override(nvt_oid="1.3.6.1.4.1.25623.1.0.108560", text="dont report", new_threat=SeverityLevel.LOG)
credential_id = None
if options.ssh_username:
if not options.ssh_password and not options.ssh_private_key:
logging.error("SSH Username is provided, but no password or private key.")
sys.exit()
credential_name = "Credentials-{}".format(random.randint(1,99999))
create_credential = gmp.create_credential(name=credential_name,
credential_type=CredentialType.USERNAME_PASSWORD,
allow_insecure=True,
login=options.ssh_username,
password=options.ssh_password,
private_key=options.ssh_private_key,
key_phrase=options.ssh_private_key_phrase)
credential_id = create_credential.xpath("//create_credential_response")[0].get("id")
logging.info("Created credential: {}".format(credential_id))
alive_tests = AliveTest.ICMP_TCP_ACK_SERVICE_AND_ARP_PING
if options.consider_alive:
alive_tests = AliveTest.CONSIDER_ALIVE
port_list_id = "33d0cd82-57c6-11e1-8ed1-406186ea4fc5" # All IANA assigned TCP
scan_name = "scan-{}".format(random.randint(1,99999))
host_list = hosts.split(",")
create_target = gmp.create_target(name = scan_name,
hosts = host_list,
alive_test = alive_tests,
port_list_id=port_list_id,
ssh_credential_port=options.ssh_port,
ssh_credential_id=credential_id)
target_id = create_target.xpath("//create_target_response")[0].get("id")
if not target_id:
logging.error("Could not create target, response: " + etree.tostring(create_target).decode('utf-8'))
sys.exit()
logging.info("Created target: {}".format(target_id))
logging.info("Alive test: {}".format(alive_tests))
logging.info("Port list: {}".format(port_list_id))
logging.info("Scan name: {}".format(scan_name))
logging.info("Host list: {}".format(host_list))
default_scanner_id = "08b69003-5fc2-4037-a479-93b440211c73" # OpenVAS Default Scanner
create_task = gmp.create_task(name = scan_name,
scanner_id = default_scanner_id,
config_id = config_id,
target_id = target_id)
task_id = create_task.xpath("//create_task_response")[0].get("id")
if not task_id:
logging.error("Could not create task, response: " + etree.tostring(create_task).decode('utf-8'))
sys.exit()
logging.info("Created task: {}".format(task_id))
start_task = gmp.start_task(task_id = task_id)
report_id = start_task.xpath("/start_task_response/report_id")[0].text
logging.info("Started task with report: {}".format(report_id))
progress = 0
status = ""
while progress != -1 and status != "Interrupted":
get_task = gmp.get_task(task_id = task_id)
status = get_task.xpath("//status")[0].text
progress = int(get_task.xpath("//progress")[0].text)
logging.info("Progress: {} {}%".format(status, progress))
time.sleep(10)
try:
openvaslog = open("/usr/local/var/log/gvm/openvas.log", "r").read()
logging.debug("openvas.log: {}".format(openvaslog))
except FileNotFoundError as ex:
logging.error(ex)
report_formats = [("pdf", "c402cc3e-b531-11e1-9163-406186ea4fc5"), ("xml", "a994b278-1f62-11e1-96ac-406186ea4fc5")]
for report_format, report_format_id in report_formats:
logging.info("Building report: {}".format(report_format))
report_filename = os.path.split(outputfile)[1]
export_path = "/var/reports/{}.{}".format(report_filename, report_format)
get_report = gmp.get_report(report_id = report_id,
report_format_id=report_format_id,
details=True,
ignore_pagination=True,
filter_string="apply_overrides=1")
try:
if report_format == "pdf":
report_content = get_report.xpath("//report[@id='{}']/text()".format(report_id))[0]
binary_base64_encoded_pdf = report_content.encode('ascii')
binary_pdf = base64.b64decode(binary_base64_encoded_pdf)
pdf_path = Path(export_path).expanduser()
pdf_path.write_bytes(binary_pdf)
if report_format == "xml":
report_content = get_report.xpath("(//report[@id='{}'])[2]".format(report_id))[0]
report_content = etree.tostring(report_content).decode('utf-8')
f = open(export_path, 'w')
f.write(report_content)
f.close()
logging.info("Written {} report to: {}".format(report_format.upper(), export_path))
except Exception as ex:
logging.error(ex)
# Cleanup
gmp.delete_task(task_id = task_id, ultimate=True)
gmp.delete_target(target_id = target_id, ultimate=True)
gmp.delete_report(report_id = report_id)
if credential_id:
gmp.delete_credential(credential_id = credential_id, ultimate=True)
except Exception as ex:
logging.error(ex)
logging.info("Done!")