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

cfg-gen-for-certs.gmp   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 10

3 Functions

Rating   Name   Duplication   Size   Complexity  
B create_config() 0 51 7
A check_args() 0 15 2
A main() 0 10 1
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 gvm.errors import GvmError
20
21
22
def check_args(args):
23
    len_args = len(args.script) - 1
24
    if len_args is not 1:
25
        message = """
26
        This script creates a new scan config with nvts from a given CERT-Bund!
27
        It needs one parameter after the script name.
28
29
        1. <cert>   -- Name or ID of the CERT-Bund
30
31
        Example:
32
            $ gvm-script --gmp-username name --gmp-password pass \
33
    ssh --hostname <gsm> scripts/cfg-gen-for-certs.gmp.py CB-K16/0943
34
        """
35
        print(message)
36
        quit()
37
38
39
def create_config(gmp, cert_bund_name):
40
    cert_bund_details = gmp.get_info(
41
        info_id=cert_bund_name, info_type=gmp.types.InfoType.CERT_BUND_ADV
42
    )
43
44
    list_cves = cert_bund_details.xpath(
45
        'info/cert_bund_adv/raw_data/Advisory/CVEList/CVE/text()'
46
    )
47
48
    nvt_dict = dict()
49
    counter = 0
50
51
    for cve in list_cves:
52
        # Get all nvts of this cve
53
        cve_info = gmp.get_info(info_id=cve, info_type=gmp.types.InfoType.CVE)
54
        nvts = cve_info.xpath('info/cve/nvts/nvt')
55
56
        for nvt in nvts:
57
            counter += 1
58
            oid = nvt.xpath('@oid')[0]
59
60
            # We need the nvt family to modify scan config
61
            nvt_data = gmp.get_nvt(oid)
62
            family = nvt_data.xpath('nvt/family/text()')[0]
63
64
            # Create key value map
65
            if family in nvt_dict and oid not in nvt_dict[family]:
66
                nvt_dict[family].append(oid)
67
            else:
68
                nvt_dict[family] = [oid]
69
70
    # Create new config
71
    copy_id = '085569ce-73ed-11df-83c3-002264764cea'
72
    config_name = 'scanconfig_for_%s' % cert_bund_name
73
    config_id = ''
74
75
    try:
76
        res = gmp.create_config(copy_id, config_name)
77
        config_id = res.xpath('@id')[0]
78
79
        # Modify the config with the nvts oid
80
        for family, nvt_oid in nvt_dict.items():
81
            gmp.modify_config(config_id, nvt_oids=nvt_oid, family=family)
82
83
        # This nvts must be present to work
84
        family = 'Port scanners'
85
        nvts = ['1.3.6.1.4.1.25623.1.0.14259', '1.3.6.1.4.1.25623.1.0.100315']
86
        gmp.modify_config(config_id=config_id, nvt_oids=nvts, family=family)
87
88
    except GvmError:
89
        print('Config exist')
90
91
92
def main(gmp, args):
93
    # pylint: disable=undefined-variable
94
95
    check_args(args)
96
97
    cert_bund_name = args.script[1]
98
99
    print('Creating scan config for {0}'.format(cert_bund_name))
100
101
    create_config(gmp, cert_bund_name)
102
103
104
if __name__ == '__gmp__':
105
    main(gmp, args)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable args does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable gmp does not seem to be defined.
Loading history...
106