CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/lib/rex/parser/nmap_xml_spec.rb
Views: 11655
1
# -*- coding:binary -*-
2
3
4
xml = '
5
<?xml version="1.0" ?>
6
<?xml-stylesheet href="/usr/share/nmap/nmap.xsl" type="text/xsl"?>
7
<!-- 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 -->
8
<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">
9
<scaninfo type="connect" protocol="tcp" numservices="2" services="22,80" />
10
<verbose level="0" />
11
<debugging level="0" />
12
<host starttime="1258080887" endtime="1258080893"><status state="up" reason="syn-ack"/>
13
<address addr="192.168.0.1" addrtype="ipv4" />
14
<hostnames><hostname name="localhost" type="PTR" /></hostnames>
15
<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>
16
<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>
17
</ports>
18
<times srtt="119" rttvar="2882" to="50000" />
19
</host>
20
<runstats><finished time="1258080893" timestr="Thu Nov 12 19:54:53 2009"/><hosts up="1" down="0" total="1" />
21
<!-- Nmap done at Thu Nov 12 19:54:53 2009; 1 IP address (1 host up) scanned in 6.43 seconds -->
22
</runstats></nmaprun>
23
'
24
25
RSpec.describe Rex::Parser::NmapXMLStreamParser do
26
parser = Rex::Parser::NmapXMLStreamParser.new
27
total_hosts = 0
28
parser.on_found_host = Proc.new { |host|
29
total_hosts += 1
30
it "should yield a host" do
31
expect(host).not_to be_nil
32
end
33
it "should populate the host with proper keys" do
34
expect(host).to have_key("status")
35
expect(host).to have_key("ports")
36
expect(host).to have_key("addrs")
37
expect(host["ports"]).to be_a(Array)
38
expect(host["addrs"]).to be_a(Hash)
39
end
40
it "should find the address" do
41
expect(host["addrs"].keys.length).to eq 1
42
expect(host["addrs"]).to have_key("ipv4")
43
expect(host["addrs"]["ipv4"]).to eq "192.168.0.1"
44
end
45
}
46
REXML::Document.parse_stream(StringIO.new(xml), parser)
47
it "should have found exactly one host" do
48
expect(total_hosts).to eq 1
49
end
50
end
51
52
53