Class: Snackhack2::PortScan
- Inherits:
-
Object
- Object
- Snackhack2::PortScan
- Defined in:
- lib/snackhack2/portscan.rb
Instance Attribute Summary collapse
-
#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
-
#initialize(ip, display: true, delete: false) ⇒ PortScan
constructor
A new instance of PortScan.
- #ports_extractor(port) ⇒ Object
- #run ⇒ Object
- #tcp(i) ⇒ Object
Constructor Details
#initialize(ip, display: true, delete: false) ⇒ PortScan
7 8 9 10 11 |
# File 'lib/snackhack2/portscan.rb', line 7 def initialize(ip, display: true, delete: false) @ip = ip @display = display @delete = delete end |
Instance Attribute Details
#delete ⇒ Object
Returns the value of attribute delete.
6 7 8 |
# File 'lib/snackhack2/portscan.rb', line 6 def delete @delete end |
#display ⇒ Object
Returns the value of attribute display.
6 7 8 |
# File 'lib/snackhack2/portscan.rb', line 6 def display @display end |
#ip ⇒ Object
Returns the value of attribute ip.
6 7 8 |
# File 'lib/snackhack2/portscan.rb', line 6 def ip @ip end |
Instance Method Details
#ports_extractor(port) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/snackhack2/portscan.rb', line 19 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
13 14 15 16 17 18 |
# File 'lib/snackhack2/portscan.rb', line 13 def run threads = [] ports = [*1..1000] ports.each { |i| threads << Thread.new { tcp(i) } } threads.each(&:join) end |
#tcp(i) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/snackhack2/portscan.rb', line 31 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 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 |