Skip to content

Instantly share code, notes, and snippets.

@postmodern
Created March 8, 2024 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save postmodern/d927fb3669be4a02737a147476bde598 to your computer and use it in GitHub Desktop.
Save postmodern/d927fb3669be4a02737a147476bde598 to your computer and use it in GitHub Desktop.
Proof of concept connect() port scanner using Ruby's async gems.
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