Nmap

  1. gem install ruby-nmap ronin-scanners gems

As far as you understand how to use nmap and how basically it works, you’ll find this lib is easy to use. You can do most of nmap functionality

Basic Scan

Ruby-nmap gem is a Ruby interface to nmap, the exploration tool and security / port scanner.

  • Provides a Ruby interface for running nmap.
  • Provides a Parser for enumerating nmap XML scan files.

let’s see how it dose work.

  1. require 'nmap'
  2. scan = Nmap::Program.scan(:targets => '192.168.0.15', :verbose => true)

SYN Scan

  1. require 'nmap/program'
  2. Nmap::Program.scan do |nmap|
  3. nmap.syn_scan = true
  4. nmap.service_scan = true
  5. nmap.os_fingerprint = true
  6. nmap.xml = 'scan.xml'
  7. nmap.verbose = true
  8. nmap.ports = [20,21,22,23,25,80,110,443,512,522,8080,1080,4444,3389]
  9. nmap.targets = '192.168.1.*'
  10. end

each option like nmap.syn_scan or nmap.xml is considered as a Task. Documentation shows the list of scan tasks/options that are supported by the lib.

Comprehensive scan

  1. #!/usr/bin/env ruby
  2. # KING SABRI | @KINGSABRI
  3. require 'nmap/program'
  4. Nmap::Program.scan do |nmap|
  5. # Target
  6. nmap.targets = '192.168.0.1'
  7. # Verbosity and Debugging
  8. nmap.verbose = true
  9. nmap.show_reason = true
  10. # Port Scanning Techniques:
  11. nmap.syn_scan = true # You can use nmap.all like -A in nmap
  12. # Service/Version Detection:
  13. nmap.service_scan = true
  14. nmap.os_fingerprint = true
  15. nmap.version_all = true
  16. # Script scanning
  17. nmap.script = "all"
  18. nmap.all_ports # nmap.ports = (0..65535).to_a
  19. # Firewall/IDS Evasion and Spoofing:
  20. nmap.decoys = ["google.com","yahoo.com","hotmail.com","facebook.com"]
  21. nmap.spoof_mac = "00:11:22:33:44:55"
  22. # Timing and Performance
  23. nmap.min_parallelism = 30
  24. nmap.max_parallelism = 130
  25. # Scan outputs
  26. nmap.output_all = 'rubyfu_scan'
  27. end

Parsing nmap XML scan file

I made an aggressive scan on scanme.nmap.org

  1. nmap -n -v -A scanme.nmap.org -oX scanme.nmap.org.xml

I quoted the code from official documentation (https://github.com/sophsec/ruby-nmap)

  1. require 'nmap/xml'
  2. Nmap::XML.new(ARGV[0]) do |xml|
  3. xml.each_host do |host|
  4. puts "[#{host.ip}]"
  5. # Print: Port/Protocol port_status service_name
  6. host.each_port do |port|
  7. puts " #{port.number}/#{port.protocol}\t#{port.state}\t#{port.service}"
  8. end
  9. end
  10. end

Returns

  1. [45.33.32.156]
  2. 22/tcp open ssh
  3. 80/tcp open http
  4. 9929/tcp open nping-echo

https://github.com/ronin-ruby/ronin-scanners