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