Network Scanning

Network ping sweeping

required net-ping gem

  1. gem install net-ping
  1. #!/usr/bin/env ruby
  2. # KING SABRI | @KINGSABRI
  3. #
  4. require 'net/ping'
  5. @icmp = Net::Ping::ICMP.new(ARGV[0])
  6. rtary = []
  7. pingfails = 0
  8. repeat = 5
  9. puts 'starting to ping'
  10. (1..repeat).each do
  11. if @icmp.ping
  12. rtary << @icmp.duration
  13. puts "host replied in #{@icmp.duration}"
  14. else
  15. pingfails += 1
  16. puts "timeout"
  17. end
  18. end
  19. avg = rtary.inject(0) {|sum, i| sum + i}/(repeat - pingfails)
  20. puts "Average round-trip is #{avg}\n"
  21. puts "#{pingfails} packets were dropped"

Port Scanner

If you got what we’ve represented in Ruby Socket section, then here we wrapping up and do some application depends on it.
scanner.rb

  1. #!/usr/bin/env ruby
  2. #
  3. # KING SABRI | @KINGSABRI
  4. #
  5. require 'socket'
  6. require 'thread'
  7. require 'timeout'
  8. host = ARGV[0]
  9. def scan(host)
  10. (0..1024).each do |port|
  11. Thread.new {
  12. begin
  13. timeout(3) do # timeout of running operation
  14. s = TCPSocket.new(host, port) # Create new socket
  15. puts "[+] #{host} | Port #{port} open"
  16. s.close
  17. end
  18. rescue Errno::ECONNREFUSED
  19. # puts "[!] #{host} | Port #{port} closed"
  20. next
  21. rescue Timeout::Error
  22. puts "[!] #{host} | Port #{port} timeout/filtered"
  23. next
  24. end
  25. }.join
  26. end
  27. end
  28. scan host

Run it

  1. ruby scanner.rb 45.33.32.156 # scanme.nmap.com
  2. [+] 45.33.32.156 | Port 22 open
  3. [+] 45.33.32.156 | Port 80 open
  4. [!] 45.33.32.156 | Port 81 timeout
  5. [!] 45.33.32.156 | Port 85 timeout
  6. [!] 45.33.32.156 | Port 119 timeout
  7. [!] 45.33.32.156 | Port 655 timeout
  8. [!] 45.33.32.156 | Port 959 timeout