forked from bodik/python-libnmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreportjson.py
More file actions
55 lines (47 loc) · 1.54 KB
/
reportjson.py
File metadata and controls
55 lines (47 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from libnmap.objects import NmapHost, NmapReport, NmapService
from libnmap.objects.os import (
CPE,
NmapOSClass,
NmapOSFingerprint,
NmapOSMatch,
OSFPPortUsed,
)
from libnmap.parser import NmapParser
class ReportEncoder(json.JSONEncoder):
"""
ReportEncoder is a internal class used mostly by plugins to convert
NmapReport objects in json format.
e.g.:
nmapreport_obj = NmapParser.parse_fromfile(
"libnmap/test/files/1_hosts.xml"
)
nmapreport_json = json.dumps(nmapreport_obj, cls=ReportEncoder)
"""
def default(self, obj):
otype = {
"NmapHost": NmapHost,
"NmapOSFingerprint": NmapOSFingerprint,
"NmapOSMatch": NmapOSMatch,
"NmapOSClass": NmapOSClass,
"CPE": CPE,
"OSFPPortUsed": OSFPPortUsed,
"NmapService": NmapService,
"NmapReport": NmapReport,
}
if isinstance(obj, tuple(otype.values())):
key = ("__{0}__").format(obj.__class__.__name__)
return {key: obj.__dict__}
return json.JSONEncoder.default(self, obj)
class ReportDecoder(json.JSONDecoder):
"""
ReportDecoder is a internal class used mostly by plugins to convert
json nmap report in to NmapReport objects.
e.g.:
nmap_report_obj = json.loads(nmap_report_json, cls=ReportDecoder)
"""
def decode(self, json_str):
r = NmapParser.parse_fromdict(json.loads(json_str))
return r