Network Scanning

Network ping sweeping

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

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
9
host = ARGV[0]
10
11
def scan(host)
12
(0..1024).each do |port|
13
Thread.new {
14
begin
15
timeout(3) do # timeout of running operation
16
s = TCPSocket.new(host, port) # Create new socket
17
puts "[+] #{host} | Port #{port} open"
18
s.close
19
end
20
rescue Errno::ECONNREFUSED
21
# puts "[!] #{host} | Port #{port} closed"
22
next
23
rescue Timeout::Error
24
puts "[!] #{host} | Port #{port} timeout/filtered"
25
next
26
end
27
}.join
28
end
29
end
30
31
scan host
Copied!
Run it
1
ruby scanner.rb 45.33.32.156 # scanme.nmap.com
2
3
[+] 45.33.32.156 | Port 22 open
4
[+] 45.33.32.156 | Port 80 open
5
[!] 45.33.32.156 | Port 81 timeout
6
[!] 45.33.32.156 | Port 85 timeout
7
[!] 45.33.32.156 | Port 119 timeout
8
[!] 45.33.32.156 | Port 655 timeout
9
[!] 45.33.32.156 | Port 959 timeout
Copied!