|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# Copyright (C) 2017-2019 Greenbone Networks GmbH |
|
3
|
|
|
# |
|
4
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later |
|
5
|
|
|
# |
|
6
|
|
|
# This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
# it under the terms of the GNU General Public License as published by |
|
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
# (at your option) any later version. |
|
10
|
|
|
# |
|
11
|
|
|
# This program is distributed in the hope that it will be useful, |
|
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
# GNU General Public License for more details. |
|
15
|
|
|
# |
|
16
|
|
|
# You should have received a copy of the GNU General Public License |
|
17
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
|
|
19
|
|
|
from datetime import datetime |
|
20
|
|
|
|
|
21
|
|
|
from gvm.errors import GvmError |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def check_args(args): |
|
25
|
|
|
len_args = len(args.script) - 1 |
|
26
|
|
|
if len_args != 2: |
|
27
|
|
|
message = """ |
|
28
|
|
|
This script creates a new task with specific host and nvt! |
|
29
|
|
|
It needs two parameters after the script name. |
|
30
|
|
|
|
|
31
|
|
|
<oid> -- oid of the nvt |
|
32
|
|
|
<target> -- scan target |
|
33
|
|
|
|
|
34
|
|
|
Example: |
|
35
|
|
|
$ gvm-script --gmp-username name --gmp-password pass \ |
|
36
|
|
|
ssh --hostname <gsm> 1.3.6.1.4.1.25623.1.0.106223 localhost |
|
37
|
|
|
""" |
|
38
|
|
|
print(message) |
|
39
|
|
|
quit() |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def create_config(gmp, nvt_oid): |
|
43
|
|
|
# Create new config |
|
44
|
|
|
copy_id = '085569ce-73ed-11df-83c3-002264764cea' |
|
45
|
|
|
config_name = nvt_oid |
|
46
|
|
|
config_id = '' |
|
47
|
|
|
|
|
48
|
|
|
try: |
|
49
|
|
|
res = gmp.create_config(copy_id, config_name) |
|
50
|
|
|
config_id = res.xpath('@id')[0] |
|
51
|
|
|
|
|
52
|
|
|
# Modify the config with an nvt oid |
|
53
|
|
|
nvt = gmp.get_nvt(nvt_oid) |
|
54
|
|
|
family = nvt.xpath('nvt/family/text()')[0] |
|
55
|
|
|
|
|
56
|
|
|
gmp.modify_config( |
|
57
|
|
|
config_id, 'nvt_selection', nvt_oids=[nvt_oid], family=family |
|
58
|
|
|
) |
|
59
|
|
|
|
|
60
|
|
|
# This nvts must be present to work |
|
61
|
|
|
family = 'Port scanners' |
|
62
|
|
|
nvts = ['1.3.6.1.4.1.25623.1.0.14259', '1.3.6.1.4.1.25623.1.0.100315'] |
|
63
|
|
|
gmp.modify_config( |
|
64
|
|
|
config_id, 'nvt_selection', nvt_oids=nvts, family=family |
|
65
|
|
|
) |
|
66
|
|
|
|
|
67
|
|
|
except GvmError: |
|
68
|
|
|
res = gmp.get_configs(filter='name=%s' % config_name) |
|
69
|
|
|
config_id = res.xpath('config/@id')[0] |
|
70
|
|
|
|
|
71
|
|
|
return config_id |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
def create_target(gmp, name): |
|
75
|
|
|
try: |
|
76
|
|
|
res = gmp.create_target(name, hosts=[name]) |
|
77
|
|
|
target_id = res.xpath('@id')[0] |
|
78
|
|
|
except GvmError: |
|
79
|
|
|
res = gmp.get_targets(filter='name=%s hosts=%s' % (name, name)) |
|
80
|
|
|
target_id = res.xpath('target/@id')[0] |
|
81
|
|
|
|
|
82
|
|
|
return target_id |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
def create_and_start_task(gmp, name, nvt_oid, config_id, target_id): |
|
86
|
|
|
# Standard Scanner OpenVAS Default |
|
87
|
|
|
scanner_id = '08b69003-5fc2-4037-a479-93b440211c73' |
|
88
|
|
|
|
|
89
|
|
|
# Create task |
|
90
|
|
|
task_name = '%s_%s_%s' % ( |
|
91
|
|
|
name, |
|
92
|
|
|
nvt_oid, |
|
93
|
|
|
datetime.now().strftime('%Y-%m-%d %H:%M:%S'), |
|
94
|
|
|
) |
|
95
|
|
|
res = gmp.create_task(task_name, config_id, target_id, scanner_id) |
|
96
|
|
|
task_id = res.xpath('@id')[0] |
|
97
|
|
|
|
|
98
|
|
|
# Start the task |
|
99
|
|
|
gmp.start_task(task_id) |
|
100
|
|
|
print('\nTask %s started' % task_id) |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
def main(gmp, args): |
|
104
|
|
|
# pylint: disable=undefined-variable |
|
105
|
|
|
|
|
106
|
|
|
check_args(args) |
|
107
|
|
|
|
|
108
|
|
|
nvt_oid = args.script[1] |
|
109
|
|
|
target_name = args.script[2] |
|
110
|
|
|
|
|
111
|
|
|
config_id = create_config(gmp, nvt_oid) |
|
112
|
|
|
target_id = create_target(gmp, target_name) |
|
113
|
|
|
|
|
114
|
|
|
create_and_start_task(gmp, target_name, nvt_oid, config_id, target_id) |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
if __name__ == '__gmp__': |
|
118
|
|
|
main(gmp, args) |
|
|
|
|
|
|
119
|
|
|
|