2005-06-05 05:42:43 +00:00
|
|
|
module Msf
|
|
|
|
|
2006-02-03 19:55:56 +00:00
|
|
|
# this module provides instance methods to be used in overloading to do single byte sending of data
|
|
|
|
module SmallSend
|
|
|
|
def write(buf, opts = {})
|
|
|
|
warn "smallsend write"
|
|
|
|
|
|
|
|
tsent = 0; bidx = 0
|
|
|
|
|
|
|
|
if self._send_size == nil or self._send_size == 0
|
|
|
|
self._send_size = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
while (bidx < buf.length)
|
|
|
|
str = buf[bidx, _send_size]
|
|
|
|
sent = super(str, opts)
|
|
|
|
bidx += sent if sent > 0
|
|
|
|
tsent += sent
|
|
|
|
|
|
|
|
if self.is_a?(SlowSend)
|
|
|
|
sleep(self._send_delay)
|
|
|
|
else
|
|
|
|
sleep(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return tsent
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :_send_size
|
|
|
|
end
|
|
|
|
|
|
|
|
# this module provides instance mehtods to be used in overloading of Socket to insert delays inbetween each write
|
|
|
|
module SlowSend
|
|
|
|
def write(buf, opts = {})
|
|
|
|
warn 'slowsend write'
|
|
|
|
if !self.is_a?(SmallSend)
|
|
|
|
sleep(_send_delay)
|
|
|
|
end
|
|
|
|
response = super(buf, opts)
|
|
|
|
return response
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :_send_delay
|
|
|
|
end
|
|
|
|
|
2005-06-05 05:42:43 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# This module provides methods for establish a connection to a remote host and
|
|
|
|
# communicating with it.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module Exploit::Remote::Tcp
|
2005-11-15 15:11:43 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Initializes an instance of an exploit module that exploits a
|
|
|
|
# vulnerability in a TCP server.
|
|
|
|
#
|
2005-06-05 06:07:18 +00:00
|
|
|
def initialize(info = {})
|
2005-06-05 23:45:58 +00:00
|
|
|
super
|
|
|
|
|
|
|
|
register_options(
|
|
|
|
[
|
|
|
|
Opt::RHOST,
|
2007-02-18 07:02:47 +00:00
|
|
|
Opt::RPORT
|
2006-02-03 19:55:56 +00:00
|
|
|
], Msf::Exploit::Remote::Tcp
|
|
|
|
)
|
|
|
|
|
2007-02-18 07:02:47 +00:00
|
|
|
register_advanced_options(
|
|
|
|
[
|
|
|
|
Opt::SSL,
|
|
|
|
Opt::Proxies,
|
|
|
|
Opt::CPORT,
|
2007-04-24 06:27:39 +00:00
|
|
|
Opt::CHOST,
|
|
|
|
OptInt.new('ConnectTimeout', [ true, 'Maximum number of seconds to establish a TCP connection', 10])
|
2007-02-18 07:02:47 +00:00
|
|
|
], Msf::Exploit::Remote::Tcp
|
|
|
|
)
|
|
|
|
|
2006-02-03 19:55:56 +00:00
|
|
|
register_evasion_options(
|
|
|
|
[
|
|
|
|
OptInt.new('TCP::max_send_size', [false, 'Maxiumum tcp segment size. (0 = disable)', 0]),
|
|
|
|
OptInt.new('TCP::send_delay', [false, 'Delays inserted before every send. (0 = disable)', 0])
|
|
|
|
], Msf::Exploit::Remote::Tcp
|
|
|
|
)
|
2005-06-05 05:42:43 +00:00
|
|
|
end
|
|
|
|
|
2005-06-05 06:07:18 +00:00
|
|
|
#
|
|
|
|
# Establishes a TCP connection to the specified RHOST/RPORT
|
|
|
|
#
|
2007-04-14 07:16:33 +00:00
|
|
|
def connect(global = true, opts={})
|
2005-07-15 23:46:05 +00:00
|
|
|
nsock = Rex::Socket::Tcp.create(
|
2007-04-14 07:16:33 +00:00
|
|
|
'PeerHost' => opts['RHOST'] || rhost,
|
|
|
|
'PeerPort' => (opts['RPORT'] || rport).to_i,
|
|
|
|
'LocalHost' => opts['CHOST'] || chost || "0.0.0.0",
|
|
|
|
'LocalPort' => (opts['CPORT'] || cport || 0).to_i,
|
2006-08-12 23:08:20 +00:00
|
|
|
'SSL' => ssl,
|
|
|
|
'Proxies' => proxies,
|
2007-04-24 06:27:39 +00:00
|
|
|
'Timeout' => (opts['ConnectTimeout'] || connect_timeout || 10).to_i,
|
2005-11-15 21:25:23 +00:00
|
|
|
'Context' =>
|
|
|
|
{
|
|
|
|
'Msf' => framework,
|
2005-11-24 18:50:33 +00:00
|
|
|
'MsfExploit' => self,
|
2005-11-15 21:25:23 +00:00
|
|
|
})
|
2005-06-05 06:07:18 +00:00
|
|
|
|
2006-02-03 19:55:56 +00:00
|
|
|
# enable evasions on this socket
|
2006-02-04 21:04:50 +00:00
|
|
|
# XXX implement evasions!!!!
|
|
|
|
# evasions(nsock)
|
2006-02-03 19:55:56 +00:00
|
|
|
|
2005-06-05 06:07:18 +00:00
|
|
|
# Set this socket to the global socket as necessary
|
2005-07-15 23:46:05 +00:00
|
|
|
self.sock = nsock if (global)
|
2005-06-05 06:07:18 +00:00
|
|
|
|
2005-11-24 18:50:33 +00:00
|
|
|
# Add this socket to the list of sockets created by this exploit
|
|
|
|
sockets << nsock
|
|
|
|
|
2005-07-15 23:46:05 +00:00
|
|
|
return nsock
|
|
|
|
end
|
|
|
|
|
|
|
|
def handler(nsock = self.sock)
|
|
|
|
# If the handler claims the socket, then we don't want it to get closed
|
|
|
|
# during cleanup
|
2005-11-24 18:50:33 +00:00
|
|
|
if ((rv = super) == Handler::Claimed)
|
|
|
|
if (nsock == self.sock)
|
|
|
|
self.sock = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove this socket from the list of sockets so that it will not be
|
|
|
|
# aborted.
|
|
|
|
sockets.delete(nsock)
|
2005-07-15 23:46:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return rv
|
2005-06-05 06:07:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Closes the TCP connection
|
|
|
|
#
|
2005-07-15 23:46:05 +00:00
|
|
|
def disconnect(nsock = self.sock)
|
2005-11-24 19:15:24 +00:00
|
|
|
begin
|
|
|
|
if (nsock)
|
|
|
|
nsock.shutdown
|
|
|
|
nsock.close
|
|
|
|
end
|
|
|
|
rescue IOError
|
2005-06-05 06:07:18 +00:00
|
|
|
end
|
|
|
|
|
2005-07-15 23:46:05 +00:00
|
|
|
if (nsock == sock)
|
|
|
|
self.sock = nil
|
2005-06-05 06:07:18 +00:00
|
|
|
end
|
2005-11-24 18:50:33 +00:00
|
|
|
|
|
|
|
# Remove this socket from the list of sockets created by this exploit
|
|
|
|
sockets.delete(nsock)
|
2005-06-05 06:07:18 +00:00
|
|
|
end
|
|
|
|
|
2005-07-15 23:46:05 +00:00
|
|
|
#
|
|
|
|
# Performs cleanup, disconnects the socket if necessary
|
|
|
|
#
|
|
|
|
def cleanup
|
|
|
|
super
|
|
|
|
|
|
|
|
disconnect
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Wrappers for getters
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the target host
|
|
|
|
#
|
|
|
|
def rhost
|
|
|
|
datastore['RHOST']
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the remote port
|
|
|
|
#
|
|
|
|
def rport
|
|
|
|
datastore['RPORT']
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the local host
|
|
|
|
#
|
|
|
|
def lhost
|
|
|
|
datastore['LHOST']
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the local port
|
|
|
|
#
|
|
|
|
def lport
|
|
|
|
datastore['LPORT']
|
|
|
|
end
|
|
|
|
|
2006-08-12 23:08:20 +00:00
|
|
|
#
|
|
|
|
# Returns the local host for outgoing connections
|
|
|
|
#
|
|
|
|
def chost
|
|
|
|
datastore['CHOST']
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the local port for outgoing connections
|
|
|
|
#
|
|
|
|
def cport
|
|
|
|
datastore['CPORT']
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the boolean indicating SSL
|
|
|
|
#
|
|
|
|
def ssl
|
|
|
|
datastore['SSL']
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the proxy configuration
|
|
|
|
#
|
|
|
|
def proxies
|
|
|
|
datastore['Proxies']
|
|
|
|
end
|
2007-04-24 06:27:39 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the TCP connection timeout
|
|
|
|
#
|
|
|
|
def connect_timeout
|
|
|
|
datastore['ConnectTimeout']
|
|
|
|
end
|
2006-08-12 23:08:20 +00:00
|
|
|
|
2005-06-05 06:07:18 +00:00
|
|
|
protected
|
|
|
|
|
2005-07-15 23:46:05 +00:00
|
|
|
attr_accessor :sock
|
2005-06-05 06:07:18 +00:00
|
|
|
|
2005-06-05 05:42:43 +00:00
|
|
|
end
|
|
|
|
|
2005-09-21 04:48:37 +00:00
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This mixin provides a generic interface for running a TCP server of some
|
|
|
|
# sort that is designed to exploit clients. Exploits that include this mixin
|
|
|
|
# automatically take a passive stance.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module Exploit::Remote::TcpServer
|
|
|
|
|
|
|
|
def initialize(info = {})
|
|
|
|
super(update_info(info,
|
|
|
|
'Stance' => Msf::Exploit::Stance::Passive))
|
|
|
|
|
|
|
|
register_options(
|
|
|
|
[
|
2006-01-02 07:49:52 +00:00
|
|
|
OptAddress.new('SRVHOST', [ true, "The local host to listen on.", Rex::Socket.source_address ]),
|
2005-09-22 03:24:32 +00:00
|
|
|
OptPort.new('SRVPORT', [ true, "The local port to listen on.", 8080 ])
|
2005-09-21 04:48:37 +00:00
|
|
|
], Msf::Exploit::Remote::TcpServer)
|
2006-02-03 19:55:56 +00:00
|
|
|
|
|
|
|
register_evasion_options(
|
|
|
|
[
|
|
|
|
OptInt.new('TCP::max_send_size', [false, 'Maxiumum tcp segment size. (0 = disable)', 0]),
|
|
|
|
OptInt.new('TCP::send_delay', [false, 'Delays inserted before every send. (0 = disable)', 0])
|
|
|
|
], Msf::Exploit::Remote::Tcp
|
|
|
|
)
|
2005-09-21 04:48:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# This mixin overrides the exploit method so that it can initiate the
|
|
|
|
# service that corresponds with what the client has requested.
|
|
|
|
#
|
|
|
|
def exploit
|
|
|
|
start_service
|
2005-09-22 03:24:32 +00:00
|
|
|
|
|
|
|
print_status("Server started.");
|
2005-09-21 04:48:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Stops the service, if one was created.
|
|
|
|
#
|
|
|
|
def cleanup
|
|
|
|
stop_service
|
2005-09-22 03:24:32 +00:00
|
|
|
|
|
|
|
print_status("Server stopped.");
|
2005-09-21 04:48:37 +00:00
|
|
|
end
|
|
|
|
|
2006-12-14 02:20:21 +00:00
|
|
|
#
|
|
|
|
# Called when a client connects.
|
|
|
|
#
|
|
|
|
def on_client_connect(client)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Called when a client has data available for reading.
|
|
|
|
#
|
|
|
|
def on_client_data(client)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Called when a client has disconnected.
|
|
|
|
#
|
|
|
|
def on_client_close(client)
|
|
|
|
end
|
|
|
|
|
2005-09-21 04:48:37 +00:00
|
|
|
#
|
|
|
|
# Starts the service.
|
|
|
|
#
|
2005-09-22 03:24:32 +00:00
|
|
|
def start_service(*args)
|
2006-12-14 02:20:21 +00:00
|
|
|
self.service = Rex::Socket::TcpServer.create(
|
|
|
|
'LocalHost' => srvhost,
|
|
|
|
'LocalPort' => srvport,
|
|
|
|
'Context' =>
|
|
|
|
{
|
|
|
|
'Msf' => framework,
|
|
|
|
'MsfExploit' => self,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.service.on_client_connect_proc = Proc.new { |client|
|
|
|
|
on_client_connect(client)
|
|
|
|
}
|
|
|
|
self.service.on_client_data_proc = Proc.new { |client|
|
|
|
|
on_client_data(client)
|
|
|
|
}
|
|
|
|
self.service.on_client_close_proc = Proc.new { |client|
|
|
|
|
on_client_close(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
self.service.start
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Stops the service.
|
|
|
|
#
|
|
|
|
def stop_service
|
|
|
|
if (service)
|
2006-12-17 07:12:04 +00:00
|
|
|
self.service.deref if self.service.kind_of?(Rex::Service)
|
2006-12-17 06:49:04 +00:00
|
|
|
self.service.close if self.service.kind_of?(Rex::Socket)
|
2006-12-14 02:20:21 +00:00
|
|
|
self.service = nil
|
|
|
|
end
|
2005-09-21 04:48:37 +00:00
|
|
|
end
|
|
|
|
|
2006-02-03 19:55:56 +00:00
|
|
|
# Enable evasions on a given client
|
|
|
|
def evasions(socket)
|
|
|
|
# XXX - oooogly
|
|
|
|
return if socket.instance_variables.member?('@tcp_evasion')
|
|
|
|
|
|
|
|
if !socket.is_a?(SmallSend) and datastore['TCP::max_send_size'] > 0
|
|
|
|
socket.extend(SmallSend)
|
|
|
|
socket._send_size = datastore['TCP::max_send_size']
|
|
|
|
end
|
|
|
|
|
|
|
|
if !socket.is_a?(SlowSend) and datastore['TCP::send_delay'] > 0
|
|
|
|
socket.extend(SlowSend)
|
|
|
|
socket._send_delay = datastore['TCP::send_delay']
|
|
|
|
end
|
|
|
|
socket.instance_eval('@tcp_evasion = 1')
|
|
|
|
end
|
|
|
|
|
2005-09-21 04:48:37 +00:00
|
|
|
#
|
|
|
|
# Returns the local host that is being listened on.
|
|
|
|
#
|
|
|
|
def srvhost
|
|
|
|
datastore['SRVHOST']
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the local port that is being listened on.
|
|
|
|
#
|
|
|
|
def srvport
|
|
|
|
datastore['SRVPORT']
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
2005-09-22 03:24:32 +00:00
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
attr_accessor :service # :nodoc:
|
2005-09-21 04:48:37 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2005-06-05 05:42:43 +00:00
|
|
|
end
|