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, terminal_output: false) ⇒ 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, terminal_output: false) ⇒ PortScan
Returns a new instance of PortScan.
7 8 9 10 11 12 13 |
# File 'lib/snackhack2/portscan.rb', line 7 def initialize(display: true, delete: false, count: 10, terminal_output: false) @ip = ip @display = display @delete = delete @count = count @terminal_output = terminal_output 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
30 31 32 33 34 35 36 |
# File 'lib/snackhack2/portscan.rb', line 30 def generate_ips ips = [] @count.to_i.times do |_c| ips << Array.new(4) { rand(256) }.join('.') end ips end |
#mass_scan ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/snackhack2/portscan.rb', line 22 def mass_scan generate_ips.each do |ips| tcp = PortScan.new tcp.ip = ips tcp.run end end |
#ports_extractor(port) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/snackhack2/portscan.rb', line 38 def ports_extractor(port) ip = [] files = Dir['*_port_scan.txt'] files.each do |f| r = File.read(f) ip << f.split('_')[0] if r.include?(port) File.delete(f) if delete end File.open("#{port}_scan.txt", 'w+') { |file| file.write(ip.join("\n")) } end |
#run ⇒ Object
15 16 17 18 19 20 |
# File 'lib/snackhack2/portscan.rb', line 15 def run threads = [] ports = [*1..1000] ports.each { |i| threads << Thread.new { tcp(i) } } threads.each(&:join) end |
#tcp(i) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/snackhack2/portscan.rb', line 49 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? return unless @display open_ports.each do |port| puts "#{ip} - #{port} is open\n" end File.open("#{ip}_port_scan.txt", 'a') { |file| file.write("#{open_ports.shift}\n") } end |