Passed
Push — master ( dc0158...bd40ef )
by
unknown
01:39 queued 15s
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
0 ignored issues
show
Unused Code introduced by
Unused xml.etree.ElementTree imported as ET
Loading history...
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):
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
    resp = ctx.lrange("nvt:%s" % oid,
100
                      openvas_db.nvt_meta_fields.index("NVT_FILENAME_POS"),
101
                      openvas_db.nvt_meta_fields.index("NVT_VERSION_POS"))
102
    if (isinstance(resp, list) and resp) is False:
103
        return None
104
105
    subelem = ['file_name', 'required_keys', 'mandatory_keys',
106
               'excluded_keys', 'required_udp_ports', 'required_ports',
107
               'dependencies', 'tag', 'cve', 'bid', 'xref', 'category',
108
               'timeout', 'family', 'copyright', 'name', 'version',]
109
110
    custom = dict()
111
    for child, res in zip(subelem, resp):
112
        custom[child] = res
113
114
    return custom
115
116
def get_nvt_name(ctx, oid):
117
    """ Get the NVT name of the given OID."""
118
    return ctx.lindex('nvt:%s' % oid,
119
                      openvas_db.nvt_meta_fields.index('NVT_NAME_POS'))
120
121
def get_nvt_family(ctx, oid):
122
    """ Get the NVT family of the given OID."""
123
    return ctx.lindex('nvt:%s' % oid,
124
                      openvas_db.nvt_meta_fields.index('NVT_FAMILY_POS'))
125
126
def get_nvt_prefs(ctx, oid):
127
    """ Get NVT preferences. """
128
    prefs = ctx.smembers('oid:%s:prefs' % oid)
129
    return prefs
130
131
def get_nvt_timeout(ctx, oid):
132
    """ Get NVT timeout"""
133
    timeout = ctx.lindex('nvt:%s' % oid,
134
                         openvas_db.nvt_meta_fields.index("NVT_TIMEOUT_POS"))
135
    return timeout
136
137
def get_nvt_tag(ctx, oid):
138
    """ Get a dictionary with the NVT Tags of the given OID."""
139
    tag = ctx.lindex('nvt:%s' % oid,
140
                      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...
141
    tags = tag.split('|')
142
143
    return dict([item.split('=', 1) for item in tags])
144
145 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...
146
    """ Get the NVT QoD from a tag or from the given OID.
147
    @in tag A dictionary with the NVT tags
148
    @in oid The NVT OID
149
    @return QoD value as string.
150
    """
151
    if not tag:
152
        if oid:
153
            tag = get_nvt_tag(ctx, oid)
154
        else:
155
            return 0
156
157
    if tag and 'qod_type' in tag:
158
        qodtype = tag['qod_type']
159
        return QoD_TYPES[qodtype]
160
    elif tag and 'qod' in tag:
161
        return tag['qod']
162
163
    return QoD_TYPES['default']
164
165 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...
166
    """ Get the NVT Severity from a tag or from the given OID.
167
    @in tag A dictionary with the NVT tags
168
    @in oid The NVT OID
169
    @return Severity (cvess_base) value as string.
170
171
    """
172
    if not tag:
173
        if oid:
174
            tag = get_nvt_tag(ctx, oid)
175
        else:
176
            return '10'
177
178
    if tag and 'cvess_base' in tag:
179
        return tag['cvess_base']
180
181
    return ''
182