2005-06-05 05:42:43 +00:00
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Tcp
|
|
|
|
# ---
|
|
|
|
#
|
|
|
|
# This module provides methods for establish a connection to a remote host and
|
|
|
|
# communicating with it.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module Exploit::Remote::Tcp
|
|
|
|
|
2005-06-05 06:07:18 +00:00
|
|
|
def initialize(info = {})
|
2005-06-05 23:45:58 +00:00
|
|
|
super
|
|
|
|
|
|
|
|
register_options(
|
|
|
|
[
|
|
|
|
Opt::RHOST,
|
|
|
|
Opt::RPORT,
|
|
|
|
], 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
|
|
|
|
#
|
|
|
|
def connect(global = true)
|
2005-07-15 23:46:05 +00:00
|
|
|
nsock = Rex::Socket::Tcp.create(
|
2005-06-05 06:07:18 +00:00
|
|
|
'PeerHost' => datastore['RHOST'],
|
|
|
|
'PeerPort' => datastore['RPORT'].to_i,
|
2005-07-16 07:32:11 +00:00
|
|
|
'LocalHost' => datastore['CHOST'] || "0.0.0.0",
|
|
|
|
'LocalPort' => datastore['CPORT'] ? datastore['CPORT'].to_i : 0)
|
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-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
|
|
|
|
if (((rv = super) == Handler::Claimed) and
|
|
|
|
(nsock == self.sock))
|
|
|
|
self.sock = nil
|
|
|
|
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)
|
|
|
|
if (nsock)
|
|
|
|
nsock.shutdown
|
|
|
|
nsock.close
|
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
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
end
|