Skip to content

Instantly share code, notes, and snippets.

@barn barn/nmap-rspec-fail.rb forked from garethr/nmap-rspec.rb
Last active Aug 29, 2015

Embed
What would you like to do?
Building on @garethr's work on nmap-rspec, this adds being able to do multiple hosts, rather than just a single IP/host. Just specify more of those blocks...
[ben@laptop:nmap-rspec2]% rspec -f d -c nmap-rspec.rb
the scan of the IP ranges
Starting Nmap 6.46 ( http://nmap.org ) at 2014-07-06 23:21 EDT
Nmap scan report for 141.101.117.49
Host is up (0.26s latency).
PORT STATE SERVICE
20/tcp filtered ftp-data
21/tcp filtered ftp
22/tcp filtered ssh
23/tcp filtered telnet
25/tcp filtered smtp
80/tcp open http
110/tcp filtered pop3
443/tcp filtered https
512/tcp filtered exec
522/tcp filtered ulp
1080/tcp filtered socks
8080/tcp open http-proxy
Nmap done: 1 IP address (1 host up) scanned in 6.49 seconds
141.101.117.49 does 80/HTTP and, oddly, 8080/SOCKS
That should be it, any other hosts are bad!
Finished in 6.53 seconds
2 examples, 0 failures
#!/usr/bin/env rspec
require 'nmap/program'
require 'nmap/xml'
require 'awesome_print'
require 'resolv'
require 'timeout'
def outdns(ip)
begin
Timeout.timeout(1) do
rdns = Resolv.new.getname ip
return "/#{rdns}" if rdns
end
rescue Timeout::Error, Resolv::ResolvError
end
end
describe 'the scan of the IP ranges' do
# I wanted this to work, but it didn't. So can someone teach me about
# helpers?
# def helpy(ips, text, ports)
# ips.each do |ip|
# it "#{ip}#{outdns(ip)} #{text} #{ports.join(' ')}" do
# @hosts_ports.should have(ports.size).items
# ports.each { |x| @hosts_ports.should include(x) }
# @found_ports[ip] = @hosts_ports[ip]
# end
# end
# end
before(:all) do
file = "nmap-scan-#{Time.new.strftime('%Y%m%d%H%M%S')}.xml"
unless File.exists? file
Nmap::Program.scan do |nmap|
nmap.xml = file
nmap.targets = '141.101.117.49'
nmap.ports = [20,21,22,23,25,80,110,443,512,522,8080,1080]
nmap.aggressive_timing = true # -T4
nmap.disable_dns = true # -n
#nmap.syn_scan = true # -sS
end
end
@hosts_ports = {}
@found_ports = {}
Nmap::XML.new(file) do |xml|
xml.each_host do |host|
host.each_port do |port|
@hosts_ports[host.ip] ||= []
@hosts_ports[host.ip] << "#{port.number}/#{port.protocol.to_s}" if port.state == :open
end
end
end
end
## Example:
#
# %w[ 1.2.3.4 5.6.7.8 ].each do |ip|
# it "#{ip}#{outdns(ip)} should just do thing/proto" do
# @hosts_ports[ip].should have(N).items
# @hosts_ports[ip].should include('port/proto')
# @found_ports[ip] = @hosts_ports[ip]
# end
# end
%w[ 141.101.117.49 ].each do |ip|
it "#{ip}#{outdns(ip)} does 80/HTTP, 8080/SOCKS and, oddly, 666/FAIL" do
@hosts_ports[ip].should include('80/tcp')
@hosts_ports[ip].should include('8080/tcp')
@hosts_ports[ip].should include('666/tcp')
@hosts_ports[ip].should have(3).items
@found_ports[ip] = @hosts_ports[ip]
end
end
# Now the found ports should be exactly the same as the scanned ports
# initially. If there's any difference, then we should error.
it "That should be it, any other hosts are bad!" do
@hosts_ports.should == @found_ports
end
## Used for testing.
# it "should output the ports we found" do
# ap @hosts_ports
# end
end
#!/usr/bin/env rspec
require 'nmap/program'
require 'nmap/xml'
require 'awesome_print'
require 'resolv'
require 'timeout'
def outdns(ip)
begin
Timeout.timeout(1) do
rdns = Resolv.new.getname ip
return "/#{rdns}" if rdns
end
rescue Timeout::Error, Resolv::ResolvError
end
end
describe 'the scan of the IP ranges' do
# I wanted this to work, but it didn't. So can someone teach me about
# helpers?
# def helpy(ips, text, ports)
# ips.each do |ip|
# it "#{ip}#{outdns(ip)} #{text} #{ports.join(' ')}" do
# @hosts_ports.should have(ports.size).items
# ports.each { |x| @hosts_ports.should include(x) }
# @found_ports[ip] = @hosts_ports[ip]
# end
# end
# end
before(:all) do
file = "nmap-scan-#{Time.new.strftime('%Y%m%d%H%M%S')}.xml"
unless File.exists? file
Nmap::Program.scan do |nmap|
nmap.xml = file
nmap.targets = '141.101.117.49'
nmap.ports = [20,21,22,23,25,80,110,443,512,522,8080,1080]
nmap.aggressive_timing = true # -T4
nmap.disable_dns = true # -n
#nmap.syn_scan = true # -sS
end
end
@hosts_ports = {}
@found_ports = {}
Nmap::XML.new(file) do |xml|
xml.each_host do |host|
host.each_port do |port|
@hosts_ports[host.ip] ||= []
@hosts_ports[host.ip] << "#{port.number}/#{port.protocol.to_s}" if port.state == :open
end
end
end
end
## Example:
#
# %w[ 1.2.3.4 5.6.7.8 ].each do |ip|
# it "#{ip}#{outdns(ip)} should just do thing/proto" do
# @hosts_ports[ip].should have(N).items
# @hosts_ports[ip].should include('port/proto')
# @found_ports[ip] = @hosts_ports[ip]
# end
# end
%w[ 141.101.117.49 ].each do |ip|
it "#{ip}#{outdns(ip)} does 80/HTTP and, oddly, 8080/SOCKS" do
@hosts_ports[ip].should have(2).items
@hosts_ports[ip].should include('80/tcp')
@hosts_ports[ip].should include('8080/tcp')
@found_ports[ip] = @hosts_ports[ip]
end
end
# Now the found ports should be exactly the same as the scanned ports
# initially. If there's any difference, then we should error.
it "That should be it, any other hosts are bad!" do
@hosts_ports.should == @found_ports
end
## Used for testing.
# it "should output the ports we found" do
# ap @hosts_ports
# end
end
[ben@laptop:nmap-rspec2]% rspec -f d -c nmap-rspec-fail.rb
the scan of the IP ranges
141.101.117.49 does 80/HTTP, 8080/SOCKS and, oddly, 666/FAIL (FAILED - 1)
That should be it, any other hosts are bad! (FAILED - 2)
Failures:
1) the scan of the IP ranges 141.101.117.49 does 80/HTTP, 8080/SOCKS and, oddly, 666/FAIL
Failure/Error: @hosts_ports[ip].should include('666/tcp')
expected ["80/tcp", "8080/tcp"] to include "666/tcp"
# ./nmap-rspec-fail.rb:76:in `block (3 levels) in <top (required)>'
2) the scan of the IP ranges That should be it, any other hosts are bad!
Failure/Error: @hosts_ports.should == @found_ports
expected: {}
got: {"141.101.117.49"=>["80/tcp", "8080/tcp"]} (using ==)
Diff:
@@ -1 +1,2 @@
+"141.101.117.49" => ["80/tcp", "8080/tcp"]
# ./nmap-rspec-fail.rb:85:in `block (2 levels) in <top (required)>'
Finished in 0.00533 seconds
2 examples, 2 failures
Failed examples:
rspec ./nmap-rspec-fail.rb:73 # the scan of the IP ranges 141.101.117.49 does 80/HTTP, 8080/SOCKS and, oddly, 666/FAIL
rspec ./nmap-rspec-fail.rb:84 # the scan of the IP ranges That should be it, any other hosts are bad!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.