Skip to content

Instantly share code, notes, and snippets.

@picatz
Created April 14, 2017 19:48
Show Gist options
  • Save picatz/08c74cbbffee7e632173e635f4990c01 to your computer and use it in GitHub Desktop.
Save picatz/08c74cbbffee7e632173e635f4990c01 to your computer and use it in GitHub Desktop.
Violent Ruby: Banner Grabber - Basic Socket Usage with Port Range
# coding: utf-8
# Basic socket usage to grab a banner.
# @author Kent 'picat' Gruber
require 'socket'
# Target ports... a range of all of the possible ports lol.
target_ports = 1..65535
# Grab the banner of a given +ip+ address and +port+
# to attempt to connect to.
#
# @param ip [String] Target IP address.
# @param port [Integer] Target port.
#
# @return [String]
def grab_banner(ip, ports=(1..65535))
TCPSocket.new(ip, port).recv(1024)
rescue
"Unable to establish a connection with #{ip}:#{port}"
end
# Iterate over each port, attempting to grab the banner
# and printing the result to STDOUT.
target_ports.each do |port|
puts grab_banner('localhost', port)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment