#include "parser.h" using namespace nmap; using namespace std; //just helper functions inline string _toStr(const char * inStr){ if(inStr==NULL){ return ""; } return inStr; } inline string _makeLower(string &str){ transform(str.begin(),str.end(),str.begin(),static_cast(tolower)); return str; } inline string _makeLower(const char *str){ if(str==NULL){ return ""; } string tmpStr = str; _makeLower(tmpStr); return tmpStr; } inline unsigned int _makeInt(const string &str){ if(str==""){ return 0; } std::istringstream iss(str); int i; iss >> i; return i; } inline unsigned int _makeInt(const unsigned char * str){ if(str==NULL){ return 0; } std::istringstream iss((const char*)str); unsigned int i; iss >> i; return i; } const bool nService::operator==(nService value){ if(this->_version!=value._version){ return false; } if(this->_name!=value._name){ return false; } if(this->_product!=value._product){ return false; } if(this->_method!=value._method){ return false; } return true; } const string nHost::hostname() { if(!_hostnames.empty()){ return _hostnames[0]; }else{ return ""; } } const string nHost::hostname(unsigned int index){ if(_hostnames.size()<=index){ if(!_hostnames.empty()){ return _hostnames[_hostnames.size()-1]; }else{ return ""; } }else{ return _hostnames[index]; } } nHost::nHost(){ _distance=-1; _status="unknown"; _ipv4=""; _ipv6=""; _mac=""; _vendor=""; _uptime_str=""; _uptime_sec=0; _start_time=0; _end_time=0; } bool nHost::operator ==(nHost value){ if(this->protocols() != value.protocols()){ return false; } //iterate over tcp, upd, ipproto, or whatever the hoses were scanned with set protos = this->protocols(); for(set::iterator po=protos.begin(); po!=protos.end(); po++){ if((*this).port_count(*po)!=value.port_count(*po)){ return false; } vector portListA=this->ports(*po); vector portListB=value.ports(*po); //todo: service check for(int i=value.port_count(*po)-1;i>=0;i--){ //are they the same port? string stateA =this->port_state(*po, portListA[i]); string stateB =value.port_state(*po, portListB[i]); if(portListA[i]!=portListB[i]){ return false; //do they have the same state? }else if(this->port_state(*po, portListA[i]) != value.port_state(*po, portListB[i])){ return false; }else if(!(this->service(*po, portListA[i]) == value.service(*po, portListB[i]))){ return false; } } } return true; } const vector parser::all_hosts(string status){ _makeLower(status); return _State_Hosts[status]; } const vector nHost::ports(string proto) { _makeLower(proto); return _ports[proto]; } const vector nHost::ports(string proto, string states) { _makeLower(states); _makeLower(proto); return _state_ports[proto][states]; } void parser::parse_nmaprun(XMLNode &pointer){ // Session._ARGS = _toStr(pointer.getAttribute("args",0)); Session._START_TIME = _makeInt(pointer.getAttribute("start",0)); Session._START_TIME_ASC = _toStr(pointer.getAttribute("startstr",0)); Session._XML_OUT_VER = _toStr(pointer.getAttribute("xmloutputversion",0)); Session._VERSION = _toStr(pointer.getAttribute("version",0)); Session._SCANNER = _toStr(pointer.getAttribute("scanner",0)); } void parser::parse_host_state(const XMLNode &pointer, nHost &tmpHost){ // //TODO: parse reason too? tmpHost._status = _toStr(pointer.getAttribute("state",0)); } void parser::parse_host_address(const XMLNode &pointer, nHost &tmpHost){ //
//
string addr,addrtype,vendor; addrtype=_toStr(pointer.getAttribute("addrtype",0)); addr=_toStr(pointer.getAttribute("addr",0)); vendor=_toStr(pointer.getAttribute("vendor",0)); if(addrtype=="ipv4"){ tmpHost._ipv4=addr; }else if(addrtype=="ipv6"){ tmpHost._ipv6=addr; }else if(addrtype=="mac"){ tmpHost._mac=addr; tmpHost._vendor=vendor; } } void parser::parse_host_names(const XMLNode &pointer, nHost &tmpHost){ // int num_names=pointer.nChildNode("hostname"); if(num_names>0){ int iterator=0; for(int i=0;i serv._name=_toStr(pointer.getAttribute("name")); serv._product=_toStr(pointer.getAttribute("product")); serv._version=_toStr(pointer.getAttribute("version")); serv._extrainfo=_toStr(pointer.getAttribute("extrainfo")); serv._tunnel=_toStr(pointer.getAttribute("tunnel")); serv._method=_toStr(pointer.getAttribute("method")); serv._conf=_makeInt(pointer.getAttribute("conf")); serv._ostype=_toStr(pointer.getAttribute("ostype")); serv._fingerprint=_toStr(pointer.getAttribute("fingerprint")); return true; } int parser::parse_host_ports_chidren(const XMLNode &pointer, string &pstate, nService &serv){ int state=false; //dont overwrite pstate if it the state element doesnt exist //that way the host will be reported as "unknown" if(pointer.nChildNode("state")!=0){ pstate=pointer.getChildNode("state").getAttribute("state"); } if(pointer.nChildNode("service")!=0){ state = parse_host_ports_service(pointer.getChildNode("service"),serv); } //this tells the caler if we want to store the service information in memory //which we only want to do if the service element exists and is valid return state; } void parser::parse_host_ports(const XMLNode &pointer, nHost &tmpHost){ nService service; string proto, pstate; unsigned int port; int contServ=false; int num_ports=pointer.nChildNode("port"); int iterator=0; for(int i=0;i parser::all_hosts(){ return _Hosts; } const set parser::status(){ return _states; } const vector parser::get_ips(){ vector ips; for(map >::iterator it= _State_IPs.begin(); it!=_State_IPs.end(); ++it){ ips.insert(ips.end(),(*it).second.begin(),(*it).second.end()); } return ips; } const vector parser::get_ips(string status){ return _State_IPs[status]; }