Skip to content

Commit bb741f0

Browse files
committed
elastic search plugin and example code to push data
1 parent d8cdc0a commit bb741f0

3 files changed

Lines changed: 175 additions & 0 deletions

File tree

examples/es_plugin.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python
2+
3+
from libnmap.parser import NmapParser
4+
from libnmap.reportjson import ReportDecoder
5+
from libnmap.plugins.es import NmapElasticsearchPlugin
6+
from datetime import datetime
7+
import json
8+
9+
nmap_report = NmapParser.parse_fromfile('libnmap/test/files/1_hosts.xml')
10+
mindex = datetime.fromtimestamp(nmap_report.started).strftime('%Y-%m-%d')
11+
db = NmapElasticsearchPlugin(index=mindex)
12+
dbid = db.insert(nmap_report)
13+
nmap_json = db.get(dbid)
14+
15+
nmap_obj = json.loads(json.dumps(nmap_json), cls=ReportDecoder)
16+
print(nmap_obj)
17+
#print(db.getall())
18+

examples/kibana.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from libnmap.parser import NmapParser
5+
from libnmap.reportjson import ReportDecoder
6+
from libnmap.plugins.es import NmapElasticsearchPlugin
7+
from datetime import datetime
8+
import json
9+
10+
11+
def report_store(nmap_report, database):
12+
jhostlist = []
13+
for nmap_host in nmap_report.hosts:
14+
jhost = host_store(nmap_host, database)
15+
jhostlist.append(jhost)
16+
17+
for jhost in jhostlist:
18+
database.insert(jhost, doc_type="NmapHost")
19+
20+
return jhostlist
21+
22+
23+
def get_os(nmap_host):
24+
rval = {'os': '', 'accuracy': 0}
25+
if nmap_host.is_up() and nmap_host.os_fingerprinted:
26+
os_list = []
27+
for osm in nmap_host.os.osmatches:
28+
os_list.append({"os": osm.name, "accuracy": osm.accuracy})
29+
os_list.sort(key=lambda x: x['accuracy'], reverse=True)
30+
31+
if len(os_list):
32+
rval.update(os_list[0])
33+
return rval
34+
35+
36+
def host_store(nmap_host, database):
37+
host_keys = ["starttime", "endtime", "address", "hostnames",
38+
"ipv4", "ipv6", "mac", "status"]
39+
jhost = {}
40+
for hkey in host_keys:
41+
if hkey == "starttime" or hkey == "endtime":
42+
val = getattr(nmap_host, hkey)
43+
jhost[hkey] = int(val) if len(val) else 0
44+
else:
45+
jhost[hkey] = getattr(nmap_host, hkey)
46+
47+
for nmap_service in nmap_host.services:
48+
reportitems = item_store(nmap_service, database)
49+
50+
for ritem in reportitems:
51+
ritem.update(jhost)
52+
database.insert(ritem, doc_type="ReportItem")
53+
54+
jhost.update(get_os(nmap_host))
55+
return jhost
56+
57+
def item_store(nmap_service, database):
58+
service_keys = ["port", "protocol", "state"]
59+
ritems = []
60+
61+
# create report item for basic port scan
62+
jservice = {}
63+
for skey in service_keys:
64+
jservice[skey] = getattr(nmap_service, skey)
65+
jservice['type'] = 'port-scan'
66+
jservice['service'] = nmap_service.service
67+
jservice['service-data'] = nmap_service.banner
68+
ritems.append(jservice)
69+
70+
# create report items from nse script output
71+
for nse_item in nmap_service.scripts_results:
72+
jnse = {}
73+
for skey in service_keys:
74+
jnse[skey] = getattr(nmap_service, skey)
75+
jnse['type'] = 'nse-script'
76+
jnse['service'] = nse_item['id']
77+
jnse['service-data'] = nse_item['output']
78+
ritems.append(jnse)
79+
80+
return ritems
81+
82+
83+
xmlscans = ['../libnmap/test/files/1_hosts.xml', '../libnmap/test/files/full_sudo6.xml']
84+
for xmlscan in xmlscans:
85+
nmap_report = NmapParser.parse_fromfile(xmlscan)
86+
87+
if nmap_report:
88+
mindex = datetime.fromtimestamp(nmap_report.started).strftime('%Y-%m-%d')
89+
db = NmapElasticsearchPlugin(index=mindex)
90+
j = report_store(nmap_report, db)

libnmap/plugins/es.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import json
4+
5+
from libnmap.reportjson import ReportDecoder, ReportEncoder
6+
from libnmap.plugins.backendplugin import NmapBackendPlugin
7+
from elasticsearch import Elasticsearch
8+
from datetime import datetime
9+
10+
11+
class NmapElasticsearchPlugin(NmapBackendPlugin):
12+
"""
13+
This class enables the user to store and manipulate nmap reports \
14+
in a elastic search db.
15+
"""
16+
def __init__(self, index=None):
17+
if index is None:
18+
self.index = datetime.now().strftime('%Y-%m-%d')
19+
else:
20+
self.index = index
21+
self._esapi = Elasticsearch()
22+
23+
def insert(self, report, doc_type=None):
24+
"""
25+
insert NmapReport in the backend
26+
:param NmapReport:
27+
:return: str the ident of the object in the backend for
28+
future usage
29+
or None
30+
"""
31+
if doc_type is None:
32+
doc_type = 'NmapReport'
33+
j = json.dumps(report, cls=ReportEncoder)
34+
res = self._esapi.index(
35+
index=self.index,
36+
doc_type=doc_type,
37+
body=json.loads(j))
38+
rc = res['_id']
39+
return rc
40+
41+
def delete(self, id):
42+
"""
43+
delete NmapReport if the backend
44+
:param id: str
45+
"""
46+
raise NotImplementedError
47+
48+
def get(self, id):
49+
"""
50+
retreive a NmapReport from the backend
51+
:param id: str
52+
:return: NmapReport
53+
"""
54+
res = self._esapi.get(index=self.index, doc_type="NmapReport", id=id)['_source']
55+
return res
56+
57+
58+
def getall(self, filter=None):
59+
"""
60+
:return: collection of tuple (id,NmapReport)
61+
:param filter: Nice to have implement a filter capability
62+
"""
63+
rsearch = self._esapi.search(index=self.index, body={"query": {"match_all": {}}})
64+
print("--------------------")
65+
print(type(rsearch))
66+
print(rsearch)
67+
print("------------")

0 commit comments

Comments
 (0)