2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2010-09-16 12:11:30 +00:00
|
|
|
#
|
|
|
|
# sf - Sept 2010
|
|
|
|
#
|
|
|
|
require 'thread'
|
|
|
|
require 'rex/logging'
|
|
|
|
require 'rex/socket'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Proto
|
|
|
|
module Proxy
|
2011-07-27 20:50:11 +00:00
|
|
|
|
|
|
|
#
|
2010-09-16 12:11:30 +00:00
|
|
|
# A Socks4a proxy server.
|
2011-07-27 20:50:11 +00:00
|
|
|
#
|
2010-09-16 12:11:30 +00:00
|
|
|
class Socks4a
|
|
|
|
|
|
|
|
#
|
|
|
|
# A client connected to the Socks4a server.
|
|
|
|
#
|
|
|
|
class Client
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
REQUEST_VERSION = 4
|
|
|
|
REPLY_VERSION = 0
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
COMMAND_CONNECT = 1
|
|
|
|
COMMAND_BIND = 2
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
REQUEST_GRANTED = 90
|
|
|
|
REQUEST_REJECT_FAILED = 91
|
|
|
|
REQUEST_REJECT_CONNECT = 92
|
|
|
|
REQUEST_REJECT_USERID = 93
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
HOST = 1
|
|
|
|
PORT = 2
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
#
|
|
|
|
# A Socks4a packet.
|
|
|
|
#
|
|
|
|
class Packet
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
def initialize
|
|
|
|
@version = REQUEST_VERSION
|
|
|
|
@command = 0
|
|
|
|
@dest_port = 0
|
|
|
|
@dest_ip = '0.0.0.0'
|
|
|
|
@userid = ''
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
#
|
|
|
|
# A helper function to recv in a Socks4a packet byte by byte.
|
|
|
|
#
|
2011-07-27 20:50:11 +00:00
|
|
|
# sf: we could just call raw = sock.get_once but some clients
|
2010-09-16 12:11:30 +00:00
|
|
|
# seem to need reading this byte by byte instead.
|
|
|
|
#
|
|
|
|
def Packet.recv( sock, timeout=30 )
|
|
|
|
raw = ''
|
|
|
|
# read in the 8 byte header
|
|
|
|
while( raw.length < 8 )
|
|
|
|
raw << sock.read( 1 )
|
|
|
|
end
|
|
|
|
# if its a request there will be more data
|
|
|
|
if( raw[0..0].unpack( 'C' ).first == REQUEST_VERSION )
|
|
|
|
# read in the userid
|
|
|
|
while( raw[8..raw.length].index( "\x00" ) == nil )
|
|
|
|
raw << sock.read( 1 )
|
|
|
|
end
|
|
|
|
# if a hostname is going to be present, read it in
|
|
|
|
ip = raw[4..7].unpack( 'N' ).first
|
|
|
|
if( ( ip & 0xFFFFFF00 ) == 0x00000000 and ( ip & 0x000000FF ) != 0x00 )
|
|
|
|
hostname = ''
|
|
|
|
while( hostname.index( "\x00" ) == nil )
|
|
|
|
hostname += sock.read( 1 )
|
|
|
|
end
|
|
|
|
raw << hostname
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# create a packet from this raw data...
|
|
|
|
packet = Packet.new
|
|
|
|
packet.from_r( raw ) ? packet : nil
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
#
|
|
|
|
# Pack a packet into raw bytes for transmitting on the wire.
|
2011-07-27 20:50:11 +00:00
|
|
|
#
|
2010-09-16 12:11:30 +00:00
|
|
|
def to_r
|
|
|
|
raw = [ @version, @command, @dest_port, Rex::Socket.addr_atoi( @dest_ip ) ].pack( 'CCnN' )
|
|
|
|
return raw if( @userid.empty? )
|
|
|
|
return raw + [ @userid ].pack( 'Z*' )
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
#
|
|
|
|
# Unpack a raw packet into its components.
|
|
|
|
#
|
|
|
|
def from_r( raw )
|
|
|
|
return false if( raw.length < 8 )
|
|
|
|
@version = raw[0..0].unpack( 'C' ).first
|
|
|
|
return false if( @version != REQUEST_VERSION and @version != REPLY_VERSION )
|
|
|
|
@command = raw[1..1].unpack( 'C' ).first
|
|
|
|
@dest_port = raw[2..3].unpack( 'n' ).first
|
|
|
|
@dest_ip = Rex::Socket.addr_itoa( raw[4..7].unpack( 'N' ).first )
|
|
|
|
if( raw.length > 8 )
|
|
|
|
@userid = raw[8..raw.length].unpack( 'Z*' ).first
|
|
|
|
# if this is a socks4a request we can resolve the provided hostname
|
|
|
|
if( self.is_hostname? )
|
|
|
|
hostname = raw[(8+@userid.length+1)..raw.length].unpack( 'Z*' ).first
|
|
|
|
@dest_ip = self.resolve( hostname )
|
|
|
|
# fail if we couldnt resolve the hostname
|
|
|
|
return false if( not @dest_ip )
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@userid = ''
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
def is_connect?
|
|
|
|
@command == COMMAND_CONNECT ? true : false
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
def is_bind?
|
|
|
|
@command == COMMAND_BIND ? true : false
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
attr_accessor :version, :command, :dest_port, :dest_ip, :userid
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
protected
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
#
|
|
|
|
# Resolve the given hostname into a dotted IP address.
|
|
|
|
#
|
|
|
|
def resolve( hostname )
|
|
|
|
if( not hostname.empty? )
|
|
|
|
begin
|
|
|
|
return Rex::Socket.addr_itoa( Rex::Socket.gethostbyname( hostname )[3].unpack( 'N' ).first )
|
|
|
|
rescue ::SocketError
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
#
|
2011-07-27 20:50:11 +00:00
|
|
|
# As per the Socks4a spec, check to see if the provided dest_ip is 0.0.0.XX
|
2010-09-16 12:11:30 +00:00
|
|
|
# which indicates after the @userid field contains a hostname to resolve.
|
|
|
|
#
|
|
|
|
def is_hostname?
|
|
|
|
ip = Rex::Socket.addr_atoi( @dest_ip )
|
|
|
|
if( ip & 0xFFFFFF00 == 0x00000000 )
|
|
|
|
return true if( ip & 0x000000FF != 0x00 )
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
#
|
|
|
|
# A mixin for a socket to perform a relay to another socket.
|
|
|
|
#
|
|
|
|
module Relay
|
2010-09-16 18:23:03 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
#
|
|
|
|
# Relay data coming in from relay_sock to this socket.
|
|
|
|
#
|
2010-09-16 18:23:03 +00:00
|
|
|
def relay( relay_client, relay_sock )
|
2010-09-16 12:11:30 +00:00
|
|
|
@relay_client = relay_client
|
|
|
|
@relay_sock = relay_sock
|
|
|
|
# start the relay thread (modified from Rex::IO::StreamAbstraction)
|
2010-11-12 06:19:49 +00:00
|
|
|
@relay_thread = Rex::ThreadFactory.spawn("SOCKS4AProxyServerRelay", false) do
|
2010-09-16 12:11:30 +00:00
|
|
|
loop do
|
|
|
|
closed = false
|
|
|
|
buf = nil
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
begin
|
|
|
|
s = Rex::ThreadSafe.select( [ @relay_sock ], nil, nil, 0.2 )
|
|
|
|
if( s == nil || s[0] == nil )
|
|
|
|
next
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
closed = true
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
if( closed == false )
|
|
|
|
begin
|
|
|
|
buf = @relay_sock.sysread( 32768 )
|
|
|
|
closed = true if( buf == nil )
|
|
|
|
rescue
|
|
|
|
closed = true
|
|
|
|
end
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
if( closed == false )
|
|
|
|
total_sent = 0
|
|
|
|
total_length = buf.length
|
|
|
|
while( total_sent < total_length )
|
|
|
|
begin
|
2011-04-21 17:29:11 +00:00
|
|
|
data = buf[total_sent, buf.length]
|
2010-09-16 12:11:30 +00:00
|
|
|
sent = self.write( data )
|
|
|
|
if( sent > 0 )
|
|
|
|
total_sent += sent
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
closed = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
if( closed )
|
|
|
|
@relay_client.stop
|
|
|
|
::Thread.exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
#
|
|
|
|
# Create a new client connected to the server.
|
|
|
|
#
|
|
|
|
def initialize( server, sock )
|
|
|
|
@server = server
|
|
|
|
@lsock = sock
|
|
|
|
@rsock = nil
|
|
|
|
@client_thread = nil
|
|
|
|
@mutex = ::Mutex.new
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Start handling the client connection.
|
|
|
|
#
|
|
|
|
def start
|
|
|
|
# create a thread to handle this client request so as to not block the socks4a server
|
2010-11-12 06:19:49 +00:00
|
|
|
@client_thread = Rex::ThreadFactory.spawn("SOCKS4AProxyClient", false) do
|
2010-09-16 12:11:30 +00:00
|
|
|
begin
|
|
|
|
@server.add_client( self )
|
|
|
|
# get the initial client request packet
|
|
|
|
request = Packet.recv( @lsock )
|
|
|
|
raise "Invalid Socks4 request packet received." if not request
|
|
|
|
# handle the request
|
|
|
|
begin
|
|
|
|
# handle socks4a conenct requests
|
|
|
|
if( request.is_connect? )
|
|
|
|
# perform the connection request
|
2011-07-27 20:50:11 +00:00
|
|
|
params = {
|
2010-09-16 18:23:03 +00:00
|
|
|
'PeerHost' => request.dest_ip,
|
|
|
|
'PeerPort' => request.dest_port,
|
|
|
|
}
|
2011-04-18 18:17:49 +00:00
|
|
|
params['Context'] = @server.opts['Context'] if @server.opts.has_key?('Context')
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 18:23:03 +00:00
|
|
|
@rsock = Rex::Socket::Tcp.create( params )
|
2010-09-16 12:11:30 +00:00
|
|
|
# and send back success to the client
|
|
|
|
response = Packet.new
|
|
|
|
response.version = REPLY_VERSION
|
|
|
|
response.command = REQUEST_GRANTED
|
|
|
|
@lsock.put( response.to_r )
|
|
|
|
# handle socks4a bind requests
|
|
|
|
elsif( request.is_bind? )
|
|
|
|
# create a server socket for this request
|
2011-07-27 20:50:11 +00:00
|
|
|
params = {
|
2010-09-16 18:23:03 +00:00
|
|
|
'LocalHost' => '0.0.0.0',
|
|
|
|
'LocalPort' => 0,
|
|
|
|
}
|
2011-04-18 18:17:49 +00:00
|
|
|
params['Context'] = @server.opts['Context'] if @server.opts.has_key?('Context')
|
2010-09-16 18:23:03 +00:00
|
|
|
bsock = Rex::Socket::TcpServer.create( params )
|
2010-09-16 12:11:30 +00:00
|
|
|
# send back the bind success to the client
|
|
|
|
response = Packet.new
|
|
|
|
response.version = REPLY_VERSION
|
|
|
|
response.command = REQUEST_GRANTED
|
|
|
|
response.dest_ip = '0.0.0.0'
|
|
|
|
response.dest_port = bsock.getlocalname()[PORT]
|
|
|
|
@lsock.put( response.to_r )
|
|
|
|
# accept a client connection (2 minute timeout as per spec)
|
|
|
|
begin
|
|
|
|
::Timeout.timeout( 120 ) do
|
|
|
|
@rsock = bsock.accept
|
|
|
|
end
|
|
|
|
rescue ::Timeout::Error
|
|
|
|
raise "Timeout reached on accept request."
|
|
|
|
end
|
|
|
|
# close the listening socket
|
|
|
|
bsock.close
|
|
|
|
# verify the connection is from the dest_ip origionally specified by the client
|
|
|
|
rpeer = @rsock.getpeername
|
|
|
|
raise "Got connection from an invalid peer." if( rpeer[HOST] != request.dest_ip )
|
|
|
|
# send back the client connect success to the client
|
|
|
|
#
|
2011-07-27 20:50:11 +00:00
|
|
|
# sf: according to the spec we send this response back to the client, however
|
2010-09-16 12:11:30 +00:00
|
|
|
# I have seen some clients who bawk if they get this second response.
|
|
|
|
#
|
|
|
|
response = Packet.new
|
|
|
|
response.version = REPLY_VERSION
|
|
|
|
response.command = REQUEST_GRANTED
|
|
|
|
response.dest_ip = rpeer[HOST]
|
|
|
|
response.dest_port = rpeer[PORT]
|
|
|
|
@lsock.put( response.to_r )
|
|
|
|
else
|
|
|
|
raise "Unknown request command received #{request.command} received."
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
# send back failure to the client
|
|
|
|
response = Packet.new
|
|
|
|
response.version = REPLY_VERSION
|
|
|
|
response.command = REQUEST_REJECT_FAILED
|
|
|
|
@lsock.put( response.to_r )
|
|
|
|
# raise an exception to close this client connection
|
|
|
|
raise "Failed to handle the clients request."
|
|
|
|
end
|
|
|
|
# setup the two way relay for full duplex io
|
|
|
|
@lsock.extend( Relay )
|
|
|
|
@rsock.extend( Relay )
|
|
|
|
# start the socket relays...
|
2010-09-16 18:23:03 +00:00
|
|
|
@lsock.relay( self, @rsock )
|
|
|
|
@rsock.relay( self, @lsock )
|
2010-09-16 12:11:30 +00:00
|
|
|
rescue
|
|
|
|
wlog( "Client.start - #{$!}" )
|
|
|
|
self.stop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
#
|
|
|
|
# Stop handling the client connection.
|
|
|
|
#
|
|
|
|
def stop
|
|
|
|
@mutex.synchronize do
|
|
|
|
if( not @closed )
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
begin
|
|
|
|
@lsock.close if @lsock
|
|
|
|
rescue
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
begin
|
|
|
|
@rsock.close if @rsock
|
|
|
|
rescue
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
@client_thread.kill if( @client_thread and @client_thread.alive? )
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
@server.remove_client( self )
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
@closed = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create a new Socks4a server.
|
|
|
|
#
|
|
|
|
def initialize( opts={} )
|
2011-04-18 18:17:49 +00:00
|
|
|
@opts = { 'ServerHost' => '0.0.0.0', 'ServerPort' => 1080 }
|
2010-09-16 12:11:30 +00:00
|
|
|
@opts = @opts.merge( opts )
|
|
|
|
@server = nil
|
|
|
|
@clients = ::Array.new
|
|
|
|
@running = false
|
|
|
|
@server_thread = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Check if the server is running.
|
|
|
|
#
|
|
|
|
def is_running?
|
|
|
|
return @running
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
#
|
|
|
|
# Start the Socks4a server.
|
|
|
|
#
|
|
|
|
def start
|
|
|
|
begin
|
2011-04-18 18:17:49 +00:00
|
|
|
# create the servers main socket (ignore the context here because we don't want a remote bind)
|
2010-09-16 12:11:30 +00:00
|
|
|
@server = Rex::Socket::TcpServer.create( 'LocalHost' => @opts['ServerHost'], 'LocalPort' => @opts['ServerPort'] )
|
|
|
|
# signal we are now running
|
|
|
|
@running = true
|
|
|
|
# start the servers main thread to pick up new clients
|
2010-11-12 06:19:49 +00:00
|
|
|
@server_thread = Rex::ThreadFactory.spawn("SOCKS4AProxyServer", false) do
|
2010-09-16 12:11:30 +00:00
|
|
|
while( @running ) do
|
2010-09-16 18:23:03 +00:00
|
|
|
begin
|
|
|
|
# accept the client connection
|
|
|
|
sock = @server.accept
|
|
|
|
# and fire off a new client instance to handle it
|
|
|
|
Client.new( self, sock ).start
|
|
|
|
rescue
|
|
|
|
wlog( "Socks4a.start - server_thread - #{$!}" )
|
|
|
|
end
|
2010-09-16 12:11:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
wlog( "Socks4a.start - #{$!}" )
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 18:23:03 +00:00
|
|
|
#
|
|
|
|
# Block while the server is running.
|
|
|
|
#
|
|
|
|
def join
|
2011-07-27 20:50:11 +00:00
|
|
|
@server_thread.join if @server_thread
|
2010-09-16 18:23:03 +00:00
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
#
|
|
|
|
# Stop the Socks4a server.
|
|
|
|
#
|
|
|
|
def stop
|
|
|
|
if( @running )
|
|
|
|
# signal we are no longer running
|
|
|
|
@running = false
|
|
|
|
# stop any clients we have (create a new client array as client.stop will delete from @clients)
|
|
|
|
clients = []
|
|
|
|
clients.concat( @clients )
|
|
|
|
clients.each do | client |
|
|
|
|
client.stop
|
|
|
|
end
|
|
|
|
# close the server socket
|
|
|
|
@server.close if @server
|
|
|
|
# if the server thread did not terminate gracefully, kill it.
|
2010-09-16 18:23:03 +00:00
|
|
|
@server_thread.kill if( @server_thread and @server_thread.alive? )
|
2010-09-16 12:11:30 +00:00
|
|
|
end
|
|
|
|
return !@running
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
def add_client( client )
|
|
|
|
@clients << client
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
def remove_client( client )
|
|
|
|
@clients.delete( client )
|
|
|
|
end
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 18:23:03 +00:00
|
|
|
attr_reader :opts
|
2011-07-27 20:50:11 +00:00
|
|
|
|
2010-09-16 12:11:30 +00:00
|
|
|
end
|
|
|
|
|
2010-11-12 06:19:49 +00:00
|
|
|
end; end; end
|
2011-07-27 20:50:11 +00:00
|
|
|
|