cfg-gen-for-certs.gmp   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

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