Passed
Pull Request — master (#22)
by Juan José
01:32
created

ospd_openvas.nvticache.get_nvt_timeout()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
# -*- coding: utf-8 -*-
2
# Description:
3
# Provide functions to handle NVT Info Cache
4
#
5
# Authors:
6
# Juan José Nicola <[email protected]>
7
#
8
# Copyright:
9
# Copyright (C) 2018 Greenbone Networks GmbH
10
#
11
# This program is free software; you can redistribute it and/or
12
# modify it under the terms of the GNU General Public License
13
# as published by the Free Software Foundation; either version 2
14
# of the License, or (at your option) any later version.
15
#
16
# This program is distributed in the hope that it will be useful,
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
# GNU General Public License for more details.
20
#
21
# You should have received a copy of the GNU General Public License
22
# along with this program; if not, write to the Free Software
23
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25
""" Functions related to the NVT information. """
26
27
# Needed to say that when we import ospd, we mean the package and not the
28
# module in that directory.
29
from __future__ import absolute_import
30
from __future__ import print_function
31
32
import xml.etree.ElementTree as ET
33
import ospd_openvas.openvas_db as openvas_db
34
35
36
NVTICACHE_STR = 'nvticache1.0.0'
37
QoD_TYPES = {
0 ignored issues
show
Coding Style Naming introduced by
The name QoD_TYPES does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
38
    'exploit': '100',
39
    'remote_vul': '99',
40
    'remote_app': '98',
41
    'package': '97',
42
    'registry': '97',
43
    'remote_active': '95',
44
    'remote_banner': '80',
45
    'executable_version': '80',
46
    'remote_analysis': '70',
47
    'remote_probe': '50',
48
    'remote_banner_unreliable': '30',
49
    'executable_version_unreliable': '30',
50
    'general_note': '1',
51
    'default': '70',
52
}
53
54
55
def get_feed_version():
56
    """ Get feed version.
57
    """
58
    return openvas_db.item_get_single(NVTICACHE_STR)
59
60
def get_oids():
61
    """ Get the list of NVT OIDs.
62
    """
63
    return openvas_db.get_elem_pattern_by_index('filename:*')
64
65 View Code Duplication
def get_nvt_params(oid):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
66
    """ Get NVT's preferences.
67
        @Return dictonary with preferences and timeout.
68
    """
69
    ctx = openvas_db.get_kb_context()
70
    prefs = get_nvt_prefs(ctx, oid)
71
    timeout = get_nvt_timeout(ctx, oid)
72
73
    vt_params = {}
74
    if int(timeout) > 0:
75
        vt_params['timeout'] = dict()
76
        vt_params['timeout']['type'] = 'entry'
77
        vt_params['timeout']['name'] = 'timeout'
78
        vt_params['timeout']['description'] = 'Script Timeout'
79
        vt_params['timeout']['default'] = timeout
80
81
    if prefs:
82
        for nvt_pref in prefs:
83
            elem = nvt_pref.split('|||')
84
            vt_params[elem[0]] = dict()
85
            vt_params[elem[0]]['type'] = elem[1]
86
            vt_params[elem[0]]['name'] = elem[0]
87
            vt_params[elem[0]]['description'] = 'Description'
88
            if elem[2]:
89
                vt_params[elem[0]]['default'] = elem[2]
90
            else:
91
                vt_params[elem[0]]['default'] = ''
92
93
    return vt_params
94
95 View Code Duplication
def get_nvt_metadata(oid, str_format=False):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
96
    """ Get a full NVT. Returns an XML tree with the NVT metadata.
97
    """
98
    ctx = openvas_db.get_kb_context()
99
100
    resp = ctx.lrange("nvt:%s" % oid,
101
                      openvas_db.nvt_meta_fields.index("NVT_FILENAME_POS"),
102
                      openvas_db.nvt_meta_fields.index("NVT_VERSION_POS"))
103
    if (isinstance(resp, list) and resp) is False:
104
        return None
105
106
    nvt = ET.Element('vt')
107
    nvt.set('id', oid)
108
109
    subelem = ['file_name', 'required_keys', 'mandatory_keys',
110
               'excluded_keys', 'required_udp_ports', 'required_ports',
111
               'dependencies', 'tag', 'cve', 'bid', 'xref', 'category',
112
               'timeout', 'family', 'copyright', 'name', 'version']
113
114
    for elem in subelem:
115
        ET.SubElement(nvt, elem)
116
    for child, res in zip(list(nvt), resp):
117
        child.text = res
118
119
    if str_format:
120
        itera = nvt.iter()
121
        metadata = ''
122
        for elem in itera:
123
            if elem.tag != 'vt' and elem.tag != 'file_name':
124
                metadata += (ET.tostring(elem).decode('utf-8'))
125
        return metadata
126
127
    return nvt
128
129
def get_nvt_name(ctx, oid):
130
    """ Get the NVT name of the given OID."""
131
    return ctx.lindex('nvt:%s' % oid,
132
                      openvas_db.nvt_meta_fields.index('NVT_NAME_POS'))
133
134
def get_nvt_family(ctx, oid):
135
    """ Get the NVT family of the given OID."""
136
    return ctx.lindex('nvt:%s' % oid,
137
                      openvas_db.nvt_meta_fields.index('NVT_FAMILY_POS'))
138
139
def get_nvt_prefs(ctx, oid):
140
    """ Get NVT preferences. """
141
    prefs = ctx.smembers('oid:%s:prefs' % oid)
142
    return prefs
143
144
def get_nvt_timeout(ctx, oid):
145
    """ Get NVT timeout"""
146
    timeout = ctx.lindex('nvt:%s' % oid,
147
                         openvas_db.nvt_meta_fields.index("NVT_TIMEOUT_POS"))
148
    return timeout
149
150
def get_nvt_tag(ctx, oid):
151
    """ Get a dictionary with the NVT Tags of the given OID."""
152
    tag = ctx.lindex('nvt:%s' % oid,
153
                      openvas_db.nvt_meta_fields.index('NVT_TAGS_POS'))
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (remove 1 space).
Loading history...
154
    tags = tag.split('|')
155
156
    return dict([item.split('=', 1) for item in tags])
157
158 View Code Duplication
def get_nvt_qod(ctx, tag=None, oid=None):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
159
    """ Get the NVT QoD from a tag or from the given OID.
160
    @in tag A dictionary with the NVT tags
161
    @in oid The NVT OID
162
    @return QoD value as string.
163
    """
164
    if not tag:
165
        if oid:
166
            tag = get_nvt_tag(ctx, oid)
167
        else:
168
            return 0
169
170
    if tag and 'qod_type' in tag:
171
        qodtype = tag['qod_type']
172
        return QoD_TYPES[qodtype]
173
    elif tag and 'qod' in tag:
174
        return tag['qod']
175
176
    return QoD_TYPES['default']
177
178 View Code Duplication
def get_nvt_severity(ctx, tag=None, oid=None):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
179
    """ Get the NVT Severity from a tag or from the given OID.
180
    @in tag A dictionary with the NVT tags
181
    @in oid The NVT OID
182
    @return Severity (cvess_base) value as string.
183
184
    """
185
    if not tag:
186
        if oid:
187
            tag = get_nvt_tag(ctx, oid)
188
        else:
189
            return '10'
190
191
    if tag and 'cvess_base' in tag:
192
        return tag['cvess_base']
193
194
    return ''
195