Created
March 8, 2024 05:22
-
-
Save postmodern/d927fb3669be4a02737a147476bde598 to your computer and use it in GitHub Desktop.
Proof of concept connect() port scanner using Ruby's async gems.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile do | |
gem 'async-io', '~> 1.30' | |
end | |
require 'async' | |
require 'async/queue' | |
require 'async/io' | |
pool_size = 100 | |
scan_queue = Async::LimitedQueue.new(pool_size) | |
host = 'scanme.nmap.org' | |
ports = (1..65535) | |
Async do |task| | |
task.async do | |
ports.each do |port| | |
scan_queue.enqueue([host, port]) | |
end | |
pool_size.times { scan_queue.enqueue(nil) } | |
end | |
workers = pool_size.times.map do | |
task.async do | |
while (host, port = scan_queue.dequeue) | |
endpoint = Async::IO::Endpoint.tcp(host,port) | |
begin | |
endpoint.connect do |connection| | |
puts ">>> Open port found #{host}:#{port}" | |
end | |
rescue Errno::ECONNREFUSED, | |
Errno::ETIMEDOUT | |
rescue Errno::EHOSTUNREACH | |
sleep(1) | |
retry | |
end | |
end | |
end | |
end | |
workers.each(&:wait) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment