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
34 35 36 37 38 39 40 41 42 |
# File 'lib/snackhack2/portscan.rb', line 34 def generate_ips # generate a bunch of different Ips to an # array named `ips`. ips = [] @count.to_i.times do |_c| ips << Array.new(4) { rand(256) }.join('.') end ips end |
#mass_scan ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/snackhack2/portscan.rb', line 24 def mass_scan # uses the `generate_ips` to generate radom # IPs. then performs the scan generate_ips.each do |ips| tcp = PortScan.new tcp.ip = ips tcp.run end end |
#ports_extractor(port) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/snackhack2/portscan.rb', line 44 def ports_extractor(port) # Looks through the files in the folder that # match '_port_scan.txt' which extracts the # ports 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 21 22 |
# File 'lib/snackhack2/portscan.rb', line 15 def run # runs `tcp` with threads threads = [] # creates all the ports between 1 and 1000 ports = [*1..1000] ports.each { |i| threads << Thread.new { tcp(i) } } threads.each(&:join) end |
#tcp(i) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/snackhack2/portscan.rb', line 59 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 # displays the open ports and IPs 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 |