Class: Snackhack2::PortScan
- Inherits:
-
Object
- Object
- Snackhack2::PortScan
- Defined in:
- lib/snackhack2/portscan.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
Returns the value of attribute count.
-
#delete ⇒ Object
Returns the value of attribute delete.
-
#display ⇒ Object
Returns the value of attribute display.
-
#ip ⇒ Object
Returns the value of attribute ip.
Instance Method Summary collapse
- #generate_ips ⇒ Object
-
#initialize(display: true, delete: false, count: 10) ⇒ PortScan
constructor
A new instance of PortScan.
- #mass_scan ⇒ Object
- #ports_extractor(port) ⇒ Object
- #run ⇒ Object
- #tcp(i) ⇒ Object
Constructor Details
#initialize(display: true, delete: false, count: 10) ⇒ PortScan
Returns a new instance of PortScan.
7 8 9 10 11 12 |
# File 'lib/snackhack2/portscan.rb', line 7 def initialize(display: true, delete: false, count: 10) @ip = ip @display = display @delete = delete @count = count end |
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count.
5 6 7 |
# File 'lib/snackhack2/portscan.rb', line 5 def count @count end |
#delete ⇒ Object
Returns the value of attribute delete.
5 6 7 |
# File 'lib/snackhack2/portscan.rb', line 5 def delete @delete end |
#display ⇒ Object
Returns the value of attribute display.
5 6 7 |
# File 'lib/snackhack2/portscan.rb', line 5 def display @display end |
#ip ⇒ Object
Returns the value of attribute ip.
5 6 7 |
# File 'lib/snackhack2/portscan.rb', line 5 def ip @ip end |
Instance Method Details
#generate_ips ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/snackhack2/portscan.rb', line 29 def generate_ips ips = [] @count.to_i.times do |c| ips << Array.new(4) { rand(256) }.join('.') end ips end |
#mass_scan ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/snackhack2/portscan.rb', line 21 def mass_scan generate_ips.each do |ips| tcp = PortScan.new tcp.ip = ips tcp.run end end |
#ports_extractor(port) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/snackhack2/portscan.rb', line 37 def ports_extractor(port) ip = [] files = Dir['*_port_scan.txt'] files.each do |f| r = File.read(f) if r.include?(port) ip << f.split("_")[0] end File.delete(f) if delete end File.open("#{port}_scan.txt", 'w+') { |file| file.write(ip.join("\n")) } end |
#run ⇒ Object
14 15 16 17 18 19 |
# File 'lib/snackhack2/portscan.rb', line 14 def run threads = [] ports = [*1..1000] ports.each { |i| threads << Thread.new { tcp(i) } } threads.each(&:join) end |
#tcp(i) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/snackhack2/portscan.rb', line 50 def tcp(i) ip = @ip open_ports = [] begin Timeout.timeout(1) do s = TCPSocket.new(@ip, i) s.close open_ports << i rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH return false end rescue Timeout::Error end return if open_ports.empty? if @display open_ports.each do |port| puts "#{port} is open" end end File.open("#{ip}_port_scan.txt", 'a') { |file| file.write(open_ports.shift.to_s + "\n") } end |