Passed
Push — master ( bd40ef...f1f17f )
by
unknown
02:07 queued 34s
created

ospd_openvas.nvticache.get_nvt_family()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
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
        if child not in ['cve', 'bid', 'xref', ]:
113
            custom[child] = res
114
115
    return custom
116
117 View Code Duplication
def get_nvt_refs(oid):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
118
    """ Get a full NVT. Returns an XML tree with the NVT references.
119
    """
120
    ctx = openvas_db.get_kb_context()
121
    resp = ctx.lrange("nvt:%s" % oid,
122
                      openvas_db.nvt_meta_fields.index("NVT_CVES_POS"),
123
                      openvas_db.nvt_meta_fields.index("NVT_XREFS_POS"))
124
    if (isinstance(resp, list) and resp) is False:
125
        return None
126
127
    subelem = ['cve', 'bid', 'xref',]
128
129
    refs = dict()
130
    for child, res in zip(subelem, resp):
131
        refs[child] = res.split(", ")
132
133
    return refs
134
135
def get_nvt_name(ctx, oid):
136
    """ Get the NVT name of the given OID."""
137
    return ctx.lindex('nvt:%s' % oid,
138
                      openvas_db.nvt_meta_fields.index('NVT_NAME_POS'))
139
140
def get_nvt_family(ctx, oid):
141
    """ Get the NVT family of the given OID."""
142
    return ctx.lindex('nvt:%s' % oid,
143
                      openvas_db.nvt_meta_fields.index('NVT_FAMILY_POS'))
144
145
def get_nvt_prefs(ctx, oid):
146
    """ Get NVT preferences. """
147
    prefs = ctx.smembers('oid:%s:prefs' % oid)
148
    return prefs
149
150
def get_nvt_timeout(ctx, oid):
151
    """ Get NVT timeout"""
152
    timeout = ctx.lindex('nvt:%s' % oid,
153
                         openvas_db.nvt_meta_fields.index("NVT_TIMEOUT_POS"))
154
    return timeout
155
156
def get_nvt_tag(ctx, oid):
157
    """ Get a dictionary with the NVT Tags of the given OID."""
158
    tag = ctx.lindex('nvt:%s' % oid,
159
                      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...
160
    tags = tag.split('|')
161
162
    return dict([item.split('=', 1) for item in tags])
163
164 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...
165
    """ Get the NVT QoD from a tag or from the given OID.
166
    @in tag A dictionary with the NVT tags
167
    @in oid The NVT OID
168
    @return QoD value as string.
169
    """
170
    if not tag:
171
        if oid:
172
            tag = get_nvt_tag(ctx, oid)
173
        else:
174
            return 0
175
176
    if tag and 'qod_type' in tag:
177
        qodtype = tag['qod_type']
178
        return QoD_TYPES[qodtype]
179
    elif tag and 'qod' in tag:
180
        return tag['qod']
181
182
    return QoD_TYPES['default']
183
184 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...
185
    """ Get the NVT Severity from a tag or from the given OID.
186
    @in tag A dictionary with the NVT tags
187
    @in oid The NVT OID
188
    @return Severity (cvess_base) value as string.
189
190
    """
191
    if not tag:
192
        if oid:
193
            tag = get_nvt_tag(ctx, oid)
194
        else:
195
            return '10'
196
197
    if tag and 'cvess_base' in tag:
198
        return tag['cvess_base']
199
200
    return ''
201