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