greenbone /
gvm-tools
| 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 | from datetime import datetime |
||
| 21 | |||
| 22 | from gvm.errors import GvmError |
||
| 23 | |||
| 24 | |||
| 25 | def check_args(args): |
||
| 26 | len_args = len(args.script) - 1 |
||
| 27 | if len_args != 2: |
||
| 28 | message = """ |
||
| 29 | This script creates a new task with specific host and nvt! |
||
| 30 | It needs two parameters after the script name. |
||
| 31 | |||
| 32 | <oid> -- oid of the nvt |
||
| 33 | <target> -- scan target |
||
| 34 | |||
| 35 | Example: |
||
| 36 | $ gvm-script --gmp-username name --gmp-password pass \ |
||
| 37 | ssh --hostname <gsm> 1.3.6.1.4.1.25623.1.0.106223 localhost |
||
| 38 | """ |
||
| 39 | print(message) |
||
| 40 | sys.exit() |
||
| 41 | |||
| 42 | |||
| 43 | def create_config(gmp, nvt_oid): |
||
| 44 | # Create new config |
||
| 45 | copy_id = '085569ce-73ed-11df-83c3-002264764cea' |
||
| 46 | config_name = nvt_oid |
||
| 47 | config_id = '' |
||
| 48 | |||
| 49 | try: |
||
| 50 | res = gmp.create_config(copy_id, config_name) |
||
| 51 | config_id = res.xpath('@id')[0] |
||
| 52 | |||
| 53 | # Modify the config with an nvt oid |
||
| 54 | nvt = gmp.get_nvt(nvt_oid) |
||
| 55 | family = nvt.xpath('nvt/family/text()')[0] |
||
| 56 | |||
| 57 | gmp.modify_config( |
||
| 58 | config_id, 'nvt_selection', nvt_oids=[nvt_oid], family=family |
||
| 59 | ) |
||
| 60 | |||
| 61 | # This nvts must be present to work |
||
| 62 | family = 'Port scanners' |
||
| 63 | nvts = ['1.3.6.1.4.1.25623.1.0.14259', '1.3.6.1.4.1.25623.1.0.100315'] |
||
| 64 | gmp.modify_config( |
||
| 65 | config_id, 'nvt_selection', nvt_oids=nvts, family=family |
||
| 66 | ) |
||
| 67 | |||
| 68 | except GvmError: |
||
| 69 | res = gmp.get_configs(filter='name=%s' % config_name) |
||
| 70 | config_id = res.xpath('config/@id')[0] |
||
| 71 | |||
| 72 | return config_id |
||
| 73 | |||
| 74 | |||
| 75 | def create_target(gmp, name): |
||
| 76 | try: |
||
| 77 | res = gmp.create_target(name, hosts=[name]) |
||
| 78 | target_id = res.xpath('@id')[0] |
||
| 79 | except GvmError: |
||
| 80 | res = gmp.get_targets(filter='name=%s hosts=%s' % (name, name)) |
||
| 81 | target_id = res.xpath('target/@id')[0] |
||
| 82 | |||
| 83 | return target_id |
||
| 84 | |||
| 85 | |||
| 86 | def create_and_start_task(gmp, name, nvt_oid, config_id, target_id): |
||
| 87 | # Standard Scanner OpenVAS Default |
||
| 88 | scanner_id = '08b69003-5fc2-4037-a479-93b440211c73' |
||
| 89 | |||
| 90 | # Create task |
||
| 91 | task_name = '%s_%s_%s' % ( |
||
| 92 | name, |
||
| 93 | nvt_oid, |
||
| 94 | datetime.now().strftime('%Y-%m-%d %H:%M:%S'), |
||
| 95 | ) |
||
| 96 | res = gmp.create_task(task_name, config_id, target_id, scanner_id) |
||
| 97 | task_id = res.xpath('@id')[0] |
||
| 98 | |||
| 99 | # Start the task |
||
| 100 | gmp.start_task(task_id) |
||
| 101 | print('\nTask %s started' % task_id) |
||
| 102 | |||
| 103 | |||
| 104 | def main(gmp, args): |
||
| 105 | # pylint: disable=undefined-variable |
||
| 106 | |||
| 107 | check_args(args) |
||
| 108 | |||
| 109 | nvt_oid = args.script[1] |
||
| 110 | target_name = args.script[2] |
||
| 111 | |||
| 112 | config_id = create_config(gmp, nvt_oid) |
||
| 113 | target_id = create_target(gmp, target_name) |
||
| 114 | |||
| 115 | create_and_start_task(gmp, target_name, nvt_oid, config_id, target_id) |
||
| 116 | |||
| 117 | |||
| 118 | if __name__ == '__gmp__': |
||
| 119 | main(gmp, args) |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
Comprehensibility
Best Practice
introduced
by
|
|||
| 120 |