-
Notifications
You must be signed in to change notification settings - Fork 90
/
nvt-scan.gmp.py
110 lines (82 loc) · 2.96 KB
/
nvt-scan.gmp.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
# SPDX-FileCopyrightText: 2017-2021 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
import sys
from argparse import Namespace
from datetime import datetime
from gvm.errors import GvmError
from gvm.protocols.gmp import Gmp
def check_args(args):
len_args = len(args.script) - 1
if len_args != 2:
message = """
This script creates a new task with specific host and nvt!
It needs two parameters after the script name.
<oid> -- oid of the nvt
<target> -- scan target
Example:
$ gvm-script --gmp-username name --gmp-password pass \
ssh --hostname <gsm> 1.3.6.1.4.1.25623.1.0.106223 localhost
"""
print(message)
sys.exit()
def create_scan_config(gmp, nvt_oid) -> str:
# Create new config
copy_id = "085569ce-73ed-11df-83c3-002264764cea"
config_name = nvt_oid
config_id = ""
try:
res = gmp.create_scan_config(copy_id, config_name)
config_id = res.xpath("@id")[0]
# Modify the config with an nvt oid
nvt = gmp.get_scan_config_nvt(nvt_oid)
family = nvt.xpath("nvt/family/text()")[0]
gmp.modify_scan_config_set_nvt_selection(
config_id=config_id, nvt_oids=[nvt_oid], family=family
)
# This nvts must be present to work
family = "Port scanners"
nvts = ["1.3.6.1.4.1.25623.1.0.14259", "1.3.6.1.4.1.25623.1.0.100315"]
gmp.modify_scan_config_set_nvt_selection(
config_id, nvt_oids=nvts, family=family
)
except GvmError:
res = gmp.get_scan_configs(filter_string=f"name={config_name}")
config_id = res.xpath("config/@id")[0]
return config_id
def create_target(gmp: Gmp, name: str) -> str:
try:
res = gmp.create_target(name, hosts=[name])
target_id = res.xpath("@id")[0]
except GvmError:
res = gmp.get_targets(filter_string=f"name={name} hosts={name}")
target_id = res.xpath("target/@id")[0]
return target_id
def create_and_start_task(
gmp: Gmp, name: str, nvt_oid: str, config_id: str, target_id: str
) -> None:
# Standard Scanner OpenVAS Default
scanner_id = "08b69003-5fc2-4037-a479-93b440211c73"
date_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Create task
task_name = f"{name}_{nvt_oid}_{date_time}"
res = gmp.create_task(
name=task_name,
config_id=config_id,
target_id=target_id,
scanner_id=scanner_id,
)
task_id = res.xpath("@id")[0]
# Start the task
gmp.start_task(task_id=task_id)
print(f"\nTask {task_id} started")
def main(gmp: Gmp, args: Namespace) -> None:
# pylint: disable=undefined-variable
check_args(args)
nvt_oid = args.script[1]
target_name = args.script[2]
config_id = create_scan_config(gmp, nvt_oid)
target_id = create_target(gmp, target_name)
create_and_start_task(gmp, target_name, nvt_oid, config_id, target_id)
if __name__ == "__gmp__":
main(gmp, args)