Passed
Pull Request — master (#244)
by
unknown
01:35
created

start-nvt-scan.gmp.create_and_start_task()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 6
dl 0
loc 11
rs 10
c 0
b 0
f 0
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
20
def check_args(args):
21
    len_args = len(args.script) - 1
22
    if len_args != 2:
23
        message = """
24
        This script creates a new task with specific host and nvt!
25
        It needs two parameters after the script name.
26
        First one is the oid of the nvt and the second one is the
27
        chosen scan target.
28
29
        Example:
30
            $ gvm-script --gmp-username name --gmp-password pass \
31
ssh --hostname <gsm> scripts/start-nvt-scan.gmp.py \
32
    1.3.6.1.4.1.25623.1.0.106223 localhost
33
        """
34
        print(message)
35
        quit()
36
37
38
def get_config(gmp, nvt_oid):
39
    # Choose from existing config, which to copy or create new config
40
    res = gmp.get_configs()
41
42
    config_ids = res.xpath('config/@id')
43
44
    for i, conf in enumerate(res.xpath('config')):
45
        config_id = conf.xpath('@id')[0]
46
        name = conf.xpath('name/text()')[0]
47
        print('\n({0}) {1}: ({2})'.format(i, name, config_id))
48
49
    while True:
50
        chosen_config = input(
51
            '\nChoose your config or create new one[0-{len} | n]: '.format(
52
                len=len(config_ids) - 1
53
            )
54
        )
55
56
        if chosen_config == 'n':
57
            chosen_copy_config = int(
58
                input(
59
                    'Which config to copy? [0-{len}]: '.format(
60
                        len=len(config_ids) - 1
61
                    )
62
                )
63
            )
64
            config_name = input('Enter new Name for config: ')
65
66
            copy_id = config_ids[chosen_copy_config]
67
68
            res = gmp.clone_config(copy_id)
69
70
            config_id = res.xpath('@id')[0]
71
72
            # Modify the config with an nvt oid
73
            if len(nvt_oid) is 0:
74
                nvt_oid = input('NVT OID: ')
75
76
            nvt = gmp.get_nvt(nvt_oid=nvt_oid)
77
            family = nvt.xpath('nvt/family/text()')[0]
78
79
            gmp.modify_config(
80
                config_id,
81
                'nvt_selection',
82
                name=config_name,
83
                nvt_oids=[nvt_oid],
84
                family=family,
85
            )
86
87
            # This nvts must be present to work
88
            family = 'Port scanners'
89
            nvts = [
90
                '1.3.6.1.4.1.25623.1.0.14259',
91
                '1.3.6.1.4.1.25623.1.0.100315',
92
            ]
93
94
            gmp.modify_config(
95
                config_id, 'nvt_selection', nvt_oids=nvts, family=family
96
            )
97
            return config_id
98
99
        if 0 <= int(chosen_config) < len(config_ids):
100
            return config_ids[int(chosen_config)]
101
102
103
def get_target(gmp, hosts):
104
    # create a new target or use an existing
105
    targets = gmp.get_targets()
106
    target_ids = targets.xpath('target/@id')
107
108
    for i, target in enumerate(targets.xpath('target')):
109
        name = target.xpath('name/text()')[0]
110
        print('\n({0}) {1}'.format(i, name))
111
112
    while True:
113
        if target_ids:
114
            chosen_target = input(
115
                '\nChoose your target or create new one[0-{len} | n]: '.format(
116
                    len=len(target_ids) - 1
117
                )
118
            )
119
        else:
120
            chosen_target = 'n'
121
122
        if chosen_target == 'n':
123
            if len(hosts) is 0:
124
                hosts = input('Target hosts (comma separated): ')
125
126
            name = input('Name of target: ')
127
128
            res = gmp.create_target(name, hosts=hosts.split(','))
129
            return res.xpath('@id')[0]
130
131
        if 0 <= int(chosen_target) < len(target_ids):
132
            return target_ids[int(chosen_target)]
133
134
135
def get_scanner(gmp):
136
    res = gmp.get_scanners()
137
    scanner_ids = res.xpath('scanner/@id')
138
139
    for i, scanner in enumerate(res.xpath('scanner')):
140
        scanner_id = scanner.xpath('@id')[0]
141
        name = scanner.xpath('name/text()')[0]
142
        # configs[id] = name
143
        print("\n({0})\n{1}: ({2})".format(i, name, scanner_id))
144
145
    while True:
146
        chosen_scanner = int(
147
            input(
148
                '\nChoose your scanner [0-{len}]: '.format(
149
                    len=len(scanner_ids) - 1
150
                )
151
            )
152
        )
153
        if 0 <= chosen_scanner < len(scanner_ids):
154
            return scanner_ids[chosen_scanner]
155
156
157
def create_and_start_task(
158
    gmp, task_name, task_comment, config_id, target_id, scanner_id
159
):
160
    res = gmp.create_task(
161
        task_name, config_id, target_id, scanner_id, comment=task_comment
162
    )
163
164
    # Start the task
165
    task_id = res.xpath('@id')[0]
166
    gmp.start_task(task_id)
167
    print('Task started')
168
169
170
def main(gmp, args):
171
    # pylint: disable=undefined-variable
172
173
    check_args(args)
174
175
    nvt_oid = args.script[1]
176
    hosts = args.script[2]
177
178
    task_name = input('Task name: ')
179
    task_comment = input('Task comment: ')
180
181
    config_id = get_config(gmp, nvt_oid)
182
    target_id = get_target(gmp, hosts)
183
    scanner_id = get_scanner(gmp)
184
185
    create_and_start_task(
186
        gmp, task_name, task_comment, config_id, target_id, scanner_id
187
    )
188
189
190
if __name__ == '__gmp__':
191
    main(gmp, args)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gmp does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable args does not seem to be defined.
Loading history...
192