Skip to content

Commit a1f025a

Browse files
authored
Merge pull request #57 from jjnicola/vt-custom-filters
Add custom date formatter to be used by the nvt filter.
2 parents ac805d8 + e7525b2 commit a1f025a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

ospd_openvas/wrapper.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
import signal
2525
import uuid
2626
from lxml.etree import tostring, SubElement, Element
27+
from datetime import datetime
2728
import psutil
2829

2930
from ospd.ospd import OSPDaemon, logger
3031
from ospd.misc import main as daemon_main
3132
from ospd.misc import target_str_to_list
3233
from ospd.cvss import CVSS
34+
from ospd.vtfilter import VtsFilter
3335
from ospd_openvas import __version__
3436

3537
from ospd_openvas.nvticache import NVTICache
@@ -201,6 +203,20 @@ def _from_bool_to_str(value):
201203
uses 1 and 0."""
202204
return 'yes' if value == 1 else 'no'
203205

206+
class OpenVasVtsFilter(VtsFilter):
207+
""" Methods to overwrite the ones in the original class.
208+
Each method formats the value to be compatible with the filter
209+
"""
210+
def format_vt_modification_time(self, value):
211+
""" Convert the datetime value in an 19 character string
212+
representing YearMonthDateHourMinuteSecond.
213+
e.g. 20190319122532
214+
"""
215+
216+
date = value[7:26].replace(" ", "")
217+
date = date.replace("-", "")
218+
date = date.replace(":","")
219+
return date
204220

205221
class OSPDopenvas(OSPDaemon):
206222

@@ -210,7 +226,8 @@ def __init__(self, certfile, keyfile, cafile):
210226
""" Initializes the ospd-openvas daemon's internal data. """
211227

212228
super(OSPDopenvas, self).__init__(certfile=certfile, keyfile=keyfile,
213-
cafile=cafile)
229+
cafile=cafile,
230+
customvtfilter=OpenVasVtsFilter())
214231
self.server_version = __version__
215232
self.scanner_info['name'] = 'openvassd'
216233
self.scanner_info['version'] = '' # achieved during self.check()

tests/testWrapper.py

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from unittest.mock import patch
2424
from ospd_openvas.wrapper import OSPD_PARAMS
2525
from tests.dummywrapper import DummyWrapper
26+
from ospd_openvas.wrapper import OpenVasVtsFilter
2627

2728
OSPD_PARAMS_OUT = {
2829
'auto_enable_dependencies': {
@@ -454,3 +455,11 @@ def test_scan_is_stopped(self, mock_nvti, mock_db):
454455
w = DummyWrapper(mock_nvti, mock_db)
455456
ret = w.scan_is_stopped('123-456')
456457
self.assertEqual(ret, True)
458+
459+
class TestFilters(unittest.TestCase):
460+
461+
def test_format_vt_modification_time(self):
462+
ovformat = OpenVasVtsFilter()
463+
td = '$Date: 2018-02-01 02:09:01 +0200 (Thu, 18 Oct 2018) $'
464+
formated = ovformat.format_vt_modification_time(td)
465+
self.assertEqual(formated, "20180201020901")

0 commit comments

Comments
 (0)