def _parse_xml_port(cls, scanport_data): """ Protected method parsing a scanned service from a targeted host. This protected method cannot be called directly. A <port/> tag can be directly passed to parse() and the below method will be called and return a NmapService object representing the state of the service. :param scanport_data: <port> XML tag from a nmap scan :type scanport_data: xml.ElementTree.Element or a string :return: NmapService """ xelement = cls.__format_element(scanport_data) _port = cls.__format_attributes(xelement) _portid = _port['portid'] if 'portid' in _port else None _protocol = _port['protocol'] if 'protocol' in _port else None _state = None _service = None _owner = None _service_scripts = [] _service_extras = {} for xport in xelement: if xport.tag == 'state': _state = cls.__format_attributes(xport) elif xport.tag == 'service': _service = cls.__parse_service(xport) elif xport.tag == 'owner': _owner = cls.__format_attributes(xport) elif xport.tag == 'script': _script_dict = cls.__parse_script(xport) _service_scripts.append(_script_dict) _service_extras['scripts'] = _service_scripts if(_portid is None or _protocol is None or _state is None): raise NmapParserException("XML <port> tag is incomplete. One " "of the following tags is missing: " "portid, protocol or state or tag.") nport = NmapService(_portid, _protocol, _state, _service, _owner, _service_extras) return nport
def parse_fromdict(cls, rdict): """ Strange method which transforms a python dict \ representation of a NmapReport and turns it into an \ NmapReport object. \ Needs to be reviewed and possibly removed. :param rdict: python dict representation of an NmapReport :type rdict: dict :return: NmapReport """ nreport = {} if list(rdict.keys())[0] == "__NmapReport__": r = rdict["__NmapReport__"] nreport["_runstats"] = r["_runstats"] nreport["_scaninfo"] = r["_scaninfo"] nreport["_nmaprun"] = r["_nmaprun"] hlist = [] for h in r["_hosts"]: slist = [] for s in h["__NmapHost__"]["_services"]: cname = "__NmapService__" slist.append( NmapService( portid=s[cname]["_portid"], protocol=s[cname]["_protocol"], state=s[cname]["_state"], owner=s[cname]["_owner"], service=s[cname]["_service"], ) ) nh = NmapHost( starttime=h["__NmapHost__"]["_starttime"], endtime=h["__NmapHost__"]["_endtime"], address=h["__NmapHost__"]["_address"], status=h["__NmapHost__"]["_status"], hostnames=h["__NmapHost__"]["_hostnames"], extras=h["__NmapHost__"]["_extras"], services=slist, ) hlist.append(nh) nreport["_hosts"] = hlist nmapobj = NmapReport(nreport) return nmapobj
def parse_fromdict(cls, rdict): """ Strange method which transforms a python dict \ representation of a NmapReport and turns it into an \ NmapReport object. \ Needs to be reviewed and possibly removed. :param rdict: python dict representation of an NmapReport :type rdict: dict :return: NmapReport """ nreport = {} if list(rdict.keys())[0] == '__NmapReport__': r = rdict['__NmapReport__'] nreport['_runstats'] = r['_runstats'] nreport['_scaninfo'] = r['_scaninfo'] nreport['_nmaprun'] = r['_nmaprun'] hlist = [] for h in r['_hosts']: slist = [] for s in h['__NmapHost__']['_services']: cname = '__NmapService__' slist.append(NmapService(portid=s[cname]['_portid'], protocol=s[cname]['_protocol'], state=s[cname]['_state'], owner=s[cname]['_owner'], service=s[cname]['_service'], service_extras=s[cname]['_service_extras'])) nh = NmapHost(starttime=h['__NmapHost__']['_starttime'], endtime=h['__NmapHost__']['_endtime'], address=h['__NmapHost__']['_address'], status=h['__NmapHost__']['_status'], hostnames=h['__NmapHost__']['_hostnames'], extras=h['__NmapHost__']['_extras'], services=slist) hlist.append(nh) nreport['_hosts'] = hlist nmapobj = NmapReport(nreport) return nmapobj
import libnmap import libnmap.process import libnmap.parser import libnmap.objects from libnmap.process import NmapProcess, NmapTask from libnmap.objects import NmapService, NmapHost, NmapReport from libnmap.parser import NmapParser, NmapParserException #Scanner TODO nm = libnmap.process.NmapProcess(targets='127.0.0.1', arguments='-sV -sT -oN "/tmp/libnmap"') rc = nm.run() f = open("/tmp/libnmap.xml", 'w') f.write(nm.stdout) f.close() #Parser TODO rep = libnmap.parser.NmapParser.parse_fromfile('/tmp/libnmap.xml') print("nmap {0}/{1} hosts up".format(rep.hosts_up, rep.hosts_total)) #objects TODO service report serv = NmapService()