|
| 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) |
0 commit comments