2005-09-29 20:18:24 +00:00
|
|
|
require 'thread'
|
|
|
|
require 'rex/socket'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Services
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This service acts as a local TCP relay whereby clients can connect to a
|
|
|
|
# local listener that forwards to an arbitrary remote endpoint. Interaction
|
|
|
|
# with the remote endpoint socket requires that it implement the
|
|
|
|
# Rex::IO::Stream interface.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class LocalRelay
|
|
|
|
|
|
|
|
include Rex::Service
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This module is used to extend streams such that they can be associated
|
|
|
|
# with a relay context and the other side of the stream.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module Stream
|
|
|
|
|
|
|
|
#
|
|
|
|
# This method is called when the other side has data that has been read
|
|
|
|
# in.
|
|
|
|
#
|
|
|
|
def on_other_data(data)
|
|
|
|
if (relay.on_other_data_proc)
|
|
|
|
relay.on_other_data_proc.call(relay, self, data)
|
|
|
|
else
|
2009-12-10 05:40:26 +00:00
|
|
|
put(data)
|
2005-09-29 20:18:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :relay
|
|
|
|
attr_accessor :other_stream
|
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This module is used to extend stream servers such that they can be
|
|
|
|
# associated with a relay context.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module StreamServer
|
|
|
|
|
|
|
|
#
|
|
|
|
# This method is called when the stream server receives a local
|
|
|
|
# connection such that the remote half can be allocated. The return
|
|
|
|
# value of the callback should be a Stream instance.
|
|
|
|
#
|
|
|
|
def on_local_connection(relay, lfd)
|
|
|
|
if (relay.on_local_connection_proc)
|
|
|
|
relay.on_local_connection_proc.call(relay, lfd)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :relay
|
|
|
|
end
|
|
|
|
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2005-09-29 20:18:24 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class acts as an instance of a given local relay.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Relay
|
|
|
|
|
|
|
|
def initialize(name, listener, opts = {})
|
|
|
|
self.name = name
|
|
|
|
self.listener = listener
|
|
|
|
self.opts = opts
|
|
|
|
self.on_local_connection_proc = opts['OnLocalConnection']
|
2005-12-12 07:07:19 +00:00
|
|
|
self.on_conn_close_proc = opts['OnConnectionClose']
|
2005-09-29 20:18:24 +00:00
|
|
|
self.on_other_data_proc = opts['OnOtherData']
|
2009-12-06 18:09:27 +00:00
|
|
|
if (not $dispatcher['rex'])
|
|
|
|
register_log_source('rex', $dispatcher['core'], get_log_level('core'))
|
|
|
|
end
|
2005-09-29 20:18:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def shutdown
|
2009-12-10 06:09:15 +00:00
|
|
|
begin
|
|
|
|
listener.shutdown if (listener)
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
2005-09-29 20:18:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
2009-12-10 06:09:15 +00:00
|
|
|
begin
|
|
|
|
listener.close if (listener)
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
2005-09-29 20:18:24 +00:00
|
|
|
listener = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :name, :listener, :opts
|
|
|
|
attr_accessor :on_local_connection_proc
|
2005-12-12 07:07:19 +00:00
|
|
|
attr_accessor :on_conn_close_proc
|
2005-09-29 20:18:24 +00:00
|
|
|
attr_accessor :on_other_data_proc
|
|
|
|
protected
|
|
|
|
attr_writer :name, :listener, :opts
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Initializes the local tcp relay monitor.
|
|
|
|
#
|
|
|
|
def initialize
|
|
|
|
self.relays = Hash.new
|
|
|
|
self.rfds = Array.new
|
|
|
|
self.relay_thread = nil
|
|
|
|
self.relay_mutex = Mutex.new
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Service interface implementors
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2006-08-08 03:29:26 +00:00
|
|
|
#
|
|
|
|
# Returns the hardcore alias for the local relay service.
|
|
|
|
#
|
|
|
|
def self.hardcore_alias(*args)
|
2008-12-19 07:11:08 +00:00
|
|
|
"__#{args}"
|
2006-08-08 03:29:26 +00:00
|
|
|
end
|
|
|
|
|
2005-09-29 20:18:24 +00:00
|
|
|
#
|
|
|
|
# Returns the alias for this service.
|
|
|
|
#
|
|
|
|
def alias
|
|
|
|
super || "Local Relay"
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Starts the thread that monitors the local relays.
|
|
|
|
#
|
|
|
|
def start
|
2005-12-12 07:07:19 +00:00
|
|
|
if (!self.relay_thread)
|
2009-12-08 18:32:26 +00:00
|
|
|
self.relay_thread = Thread.new {
|
2005-12-12 07:07:19 +00:00
|
|
|
begin
|
|
|
|
monitor_relays
|
|
|
|
rescue ::Exception
|
|
|
|
elog("Error in #{self} monitor_relays: #{$!}", 'rex')
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2005-09-29 20:18:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Stops the thread that monitors the local relays and destroys all local
|
|
|
|
# listeners.
|
|
|
|
#
|
|
|
|
def stop
|
|
|
|
if (self.relay_thread)
|
|
|
|
self.relay_thread.kill
|
|
|
|
self.relay_thread = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
self.relay_mutex.synchronize {
|
|
|
|
self.relays.delete_if { |k, v|
|
|
|
|
v.shutdown
|
|
|
|
v.close
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Flush the relay list and read fd list
|
|
|
|
self.relays.clear
|
|
|
|
self.rfds.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Adding/removing local tcp relays
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
|
|
|
# Starts a local TCP relay.
|
|
|
|
#
|
|
|
|
def start_tcp_relay(lport, opts = {})
|
|
|
|
# Make sure our options are valid
|
2005-12-12 07:07:19 +00:00
|
|
|
if ((opts['PeerHost'] == nil or opts['PeerPort'] == nil) and (opts['Stream'] != true))
|
2005-09-29 20:18:24 +00:00
|
|
|
raise ArgumentError, "Missing peer host or peer port.", caller
|
|
|
|
end
|
2007-01-30 04:48:35 +00:00
|
|
|
|
2005-09-29 20:18:24 +00:00
|
|
|
listener = Rex::Socket.create_tcp_server(
|
|
|
|
'LocalHost' => opts['LocalHost'],
|
|
|
|
'LocalPort' => lport)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2005-09-29 20:18:24 +00:00
|
|
|
opts['LocalPort'] = lport
|
|
|
|
opts['__RelayType'] = 'tcp'
|
|
|
|
|
|
|
|
start_relay(listener, lport.to_s + (opts['LocalHost'] || '0.0.0.0'), opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Starts a local relay on the supplied local port. This listener will call
|
|
|
|
# the supplied callback procedures when various events occur.
|
|
|
|
#
|
|
|
|
def start_relay(stream_server, name, opts = {})
|
|
|
|
# Create a Relay instance with the local stream and remote stream
|
|
|
|
relay = Relay.new(name, stream_server, opts)
|
|
|
|
|
|
|
|
# Extend the stream_server so that we can associate it with this relay
|
|
|
|
stream_server.extend(StreamServer)
|
|
|
|
stream_server.relay = relay
|
|
|
|
|
|
|
|
# Add the stream associations the appropriate lists and hashes
|
|
|
|
self.relay_mutex.synchronize {
|
|
|
|
self.relays[name] = relay
|
|
|
|
|
|
|
|
self.rfds << stream_server
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Stops relaying on a given local port.
|
|
|
|
#
|
|
|
|
def stop_tcp_relay(lport, lhost = nil)
|
|
|
|
stop_relay(lport.to_s + (lhost || '0.0.0.0'))
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Stops a relay with a given name.
|
|
|
|
#
|
|
|
|
def stop_relay(name)
|
|
|
|
rv = false
|
|
|
|
|
|
|
|
self.relay_mutex.synchronize {
|
|
|
|
relay = self.relays[name]
|
|
|
|
|
|
|
|
if (relay)
|
2009-12-08 18:32:26 +00:00
|
|
|
close_relay(relay)
|
2005-09-29 20:18:24 +00:00
|
|
|
rv = true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
rv
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Enumerate each TCP relay
|
|
|
|
#
|
|
|
|
def each_tcp_relay(&block)
|
|
|
|
self.relays.each_pair { |name, relay|
|
|
|
|
next if (relay.opts['__RelayType'] != 'tcp')
|
|
|
|
|
|
|
|
yield(
|
2009-12-08 18:32:26 +00:00
|
|
|
relay.opts['LocalHost'] || '0.0.0.0',
|
2005-09-29 20:18:24 +00:00
|
|
|
relay.opts['LocalPort'],
|
2009-12-08 18:32:26 +00:00
|
|
|
relay.opts['PeerHost'],
|
2005-09-29 20:18:24 +00:00
|
|
|
relay.opts['PeerPort'],
|
|
|
|
relay.opts)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
attr_accessor :relays, :relay_thread, :relay_mutex
|
|
|
|
attr_accessor :rfds
|
|
|
|
|
|
|
|
#
|
|
|
|
# Closes an cleans up a specific relay
|
|
|
|
#
|
|
|
|
def close_relay(relay)
|
|
|
|
self.rfds.delete(relay.listener)
|
|
|
|
self.relays.delete(relay.name)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-08-10 23:09:00 +00:00
|
|
|
begin
|
2009-12-08 18:32:26 +00:00
|
|
|
relay.shutdown
|
2007-08-10 23:09:00 +00:00
|
|
|
relay.close
|
|
|
|
rescue IOError
|
|
|
|
end
|
2005-09-29 20:18:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Closes a specific relay connection without tearing down the actual relay
|
|
|
|
# itself.
|
|
|
|
#
|
|
|
|
def close_relay_conn(fd)
|
|
|
|
relay = fd.relay
|
|
|
|
ofd = fd.other_stream
|
|
|
|
|
|
|
|
self.rfds.delete(fd)
|
|
|
|
|
|
|
|
begin
|
2005-12-12 07:07:19 +00:00
|
|
|
if (relay.on_conn_close_proc)
|
|
|
|
relay.on_conn_close_proc.call(fd)
|
|
|
|
end
|
|
|
|
|
2005-09-29 20:18:24 +00:00
|
|
|
fd.shutdown
|
|
|
|
fd.close
|
|
|
|
rescue IOError
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2005-09-29 20:18:24 +00:00
|
|
|
if (ofd)
|
|
|
|
self.rfds.delete(ofd)
|
|
|
|
|
|
|
|
begin
|
2005-12-12 07:07:19 +00:00
|
|
|
if (relay.on_conn_close_proc)
|
|
|
|
relay.on_conn_close_proc.call(ofd)
|
|
|
|
end
|
|
|
|
|
2005-09-29 20:18:24 +00:00
|
|
|
ofd.shutdown
|
|
|
|
ofd.close
|
|
|
|
rescue IOError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Accepts a client connection on a local relay.
|
|
|
|
#
|
|
|
|
def accept_relay_conn(srvfd)
|
|
|
|
relay = srvfd.relay
|
|
|
|
|
|
|
|
begin
|
|
|
|
dlog("Accepting relay client connection...", 'rex', LEV_3)
|
|
|
|
|
|
|
|
# Accept the child connection
|
|
|
|
lfd = srvfd.accept
|
|
|
|
dlog("Got left side of relay: #{lfd}", 'rex', LEV_3)
|
|
|
|
|
|
|
|
# Call the relay's on_local_connection method which should return a
|
|
|
|
# remote connection on success
|
|
|
|
rfd = srvfd.on_local_connection(relay, lfd)
|
2005-12-12 07:07:19 +00:00
|
|
|
|
|
|
|
dlog("Got right side of relay: #{rfd}", 'rex', LEV_3)
|
2005-09-29 20:18:24 +00:00
|
|
|
rescue
|
|
|
|
wlog("Failed to get remote half of local connection on relay #{relay.name}: #{$!}", 'rex')
|
2009-12-10 05:40:26 +00:00
|
|
|
lfd.close
|
|
|
|
return
|
2005-09-29 20:18:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# If we have both sides, then we rock. Extend the instances, associate
|
|
|
|
# them with the relay, associate them with each other, and add them to
|
|
|
|
# the list of polling file descriptors
|
|
|
|
if (lfd and rfd)
|
|
|
|
lfd.extend(Stream)
|
|
|
|
rfd.extend(Stream)
|
|
|
|
|
|
|
|
lfd.relay = relay
|
|
|
|
rfd.relay = relay
|
|
|
|
|
|
|
|
lfd.other_stream = rfd
|
|
|
|
rfd.other_stream = lfd
|
|
|
|
|
|
|
|
self.rfds << lfd
|
2009-12-08 18:32:26 +00:00
|
|
|
self.rfds << rfd
|
|
|
|
|
2005-09-29 20:18:24 +00:00
|
|
|
# Otherwise, we don't have both sides, we'll close them.
|
|
|
|
else
|
|
|
|
close_relay_conn(lfd)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Monitors the relays for data and passes it in both directions.
|
|
|
|
#
|
|
|
|
def monitor_relays
|
2008-01-26 22:17:59 +00:00
|
|
|
begin
|
|
|
|
# Helps with latency
|
|
|
|
Thread.current.priority = 2
|
2005-09-29 20:18:24 +00:00
|
|
|
|
|
|
|
# Poll all the streams...
|
|
|
|
begin
|
2010-05-20 20:42:17 +00:00
|
|
|
socks = ::IO.select(rfds, nil, nil, 0.25)
|
2005-09-29 20:18:24 +00:00
|
|
|
rescue StreamClosedError => e
|
2009-12-10 05:40:26 +00:00
|
|
|
dlog("monitor_relays: closing stream #{e.stream}", 'rex', LEV_3)
|
2005-09-29 20:18:24 +00:00
|
|
|
|
|
|
|
# Close the relay connection that is associated with the stream
|
|
|
|
# closed error
|
|
|
|
if (e.stream.kind_of?(Stream))
|
|
|
|
close_relay_conn(e.stream)
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2005-09-29 20:18:24 +00:00
|
|
|
dlog("monitor_relays: closed stream #{e.stream}", 'rex', LEV_3)
|
|
|
|
|
|
|
|
next
|
2009-12-08 18:32:26 +00:00
|
|
|
rescue
|
2005-09-29 20:18:24 +00:00
|
|
|
elog("Error in #{self} monitor_relays select: #{$!}", 'rex')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# If socks is nil, go again.
|
|
|
|
next unless socks
|
|
|
|
|
|
|
|
# Process read-ready file descriptors, if any.
|
|
|
|
socks[0].each { |rfd|
|
|
|
|
|
|
|
|
# If this file descriptor is a server, accept the connection
|
|
|
|
if (rfd.kind_of?(StreamServer))
|
|
|
|
accept_relay_conn(rfd)
|
|
|
|
# Otherwise, it's a relay connection, read data from one side
|
|
|
|
# and write it to the other
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
# Pass the data onto the other fd, most likely writing it.
|
2010-03-27 15:44:33 +00:00
|
|
|
data = rfd.sysread(65536)
|
2005-09-29 20:18:24 +00:00
|
|
|
rfd.other_stream.on_other_data(data)
|
2009-12-10 05:40:26 +00:00
|
|
|
# If we catch an error, close the connection
|
|
|
|
rescue ::Exception
|
2005-09-29 20:18:24 +00:00
|
|
|
elog("Error in #{self} monitor_relays read: #{$!}", 'rex')
|
2009-12-10 05:40:26 +00:00
|
|
|
close_relay_conn(rfd)
|
2009-12-08 18:32:26 +00:00
|
|
|
end
|
2005-09-29 20:18:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
} if (socks[0])
|
|
|
|
|
|
|
|
end while true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-12-06 18:09:27 +00:00
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|