/spec/lib/rex/parser/nmap_xml_spec.rb

https://github.com/debbiemezyene/metasploit-framework · Ruby · 52 lines · 47 code · 4 blank · 1 comment · 3 complexity · 80e8a9efea018ba112212a2d46367286 MD5 · raw file

  1. # -*- coding:binary -*-
  2. require 'rex/parser/nmap_xml'
  3. xml = '
  4. <?xml version="1.0" ?>
  5. <?xml-stylesheet href="/usr/share/nmap/nmap.xsl" type="text/xsl"?>
  6. <!-- Nmap 4.76 scan initiated Thu Nov 12 19:54:47 2009 as: nmap -p22,80 -A -oX nmap.xml -T5 192.168.0.1 -->
  7. <nmaprun scanner="nmap" args="nmap -p22,80 -A -oX nmap.xml -T5 192.168.0.1" start="1258080887" startstr="Thu Nov 12 19:54:47 2009" version="4.76" xmloutputversion="1.02">
  8. <scaninfo type="connect" protocol="tcp" numservices="2" services="22,80" />
  9. <verbose level="0" />
  10. <debugging level="0" />
  11. <host starttime="1258080887" endtime="1258080893"><status state="up" reason="syn-ack"/>
  12. <address addr="192.168.0.1" addrtype="ipv4" />
  13. <hostnames><hostname name="localhost" type="PTR" /></hostnames>
  14. <ports><port protocol="tcp" portid="22"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ssh" extrainfo="protocol 2.0" servicefp="SF-Port22-TCP:V=4.76%I=7%D=11/12%Time=4AFCCA7D%P=i686-pc-linux-gnu%r(NULL,&#xa;SF:27,&quot;SSH-2\.0-OpenSSH_5\.1p1\x20Debian-5ubuntu1\r\n&quot;);" method="probed" conf="10" /></port>
  15. <port protocol="tcp" portid="80"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="http" product="Apache httpd" version="2.2.11" extrainfo="(Ubuntu) PHP/5.2.6-3ubuntu4.2 with Suhosin-Patch" method="probed" conf="10" /></port>
  16. </ports>
  17. <times srtt="119" rttvar="2882" to="50000" />
  18. </host>
  19. <runstats><finished time="1258080893" timestr="Thu Nov 12 19:54:53 2009"/><hosts up="1" down="0" total="1" />
  20. <!-- Nmap done at Thu Nov 12 19:54:53 2009; 1 IP address (1 host up) scanned in 6.43 seconds -->
  21. </runstats></nmaprun>
  22. '
  23. describe Rex::Parser::NmapXMLStreamParser do
  24. parser = Rex::Parser::NmapXMLStreamParser.new
  25. total_hosts = 0
  26. parser.on_found_host = Proc.new { |host|
  27. total_hosts += 1
  28. it "should yield a host" do
  29. host.should_not be_nil
  30. end
  31. it "should populate the host with proper keys" do
  32. host.should have_key("status")
  33. host.should have_key("ports")
  34. host.should have_key("addrs")
  35. host["ports"].should be_a(Array)
  36. host["addrs"].should be_a(Hash)
  37. end
  38. it "should find the address" do
  39. host["addrs"].keys.length.should == 1
  40. host["addrs"].should have_key("ipv4")
  41. host["addrs"]["ipv4"].should == "192.168.0.1"
  42. end
  43. }
  44. REXML::Document.parse_stream(StringIO.new(xml), parser)
  45. it "should have found exactly one host" do
  46. total_hosts.should == 1
  47. end
  48. end