2005-11-15 20:33:55 +00:00
|
|
|
require 'singleton'
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'rex/socket'
|
|
|
|
require 'rex/socket/tcp'
|
|
|
|
require 'rex/socket/ssl_tcp'
|
|
|
|
require 'rex/socket/udp'
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Local communication class factory.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Rex::Socket::Comm::Local
|
|
|
|
|
2005-11-15 20:33:55 +00:00
|
|
|
include Singleton
|
2005-09-30 05:59:44 +00:00
|
|
|
include Rex::Socket::Comm
|
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
#
|
|
|
|
# Creates an instance of a socket using the supplied parameters.
|
|
|
|
#
|
|
|
|
def self.create(param)
|
|
|
|
case param.proto
|
|
|
|
when 'tcp'
|
2005-06-03 04:51:51 +00:00
|
|
|
return create_by_type(param, ::Socket::SOCK_STREAM, ::Socket::IPPROTO_TCP)
|
|
|
|
when 'udp'
|
|
|
|
return create_by_type(param, ::Socket::SOCK_DGRAM, ::Socket::IPPROTO_UDP)
|
2005-06-02 07:52:17 +00:00
|
|
|
else
|
2005-06-04 20:38:49 +00:00
|
|
|
raise Rex::UnsupportedProtocol.new(param.proto), caller
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Creates a socket using the supplied Parameter instance.
|
2005-06-02 07:52:17 +00:00
|
|
|
#
|
2005-06-03 04:51:51 +00:00
|
|
|
def self.create_by_type(param, type, proto = 0)
|
2005-11-15 20:33:55 +00:00
|
|
|
# Notify handlers of the before socket create event.
|
|
|
|
self.instance.notify_before_socket_create(self, param)
|
|
|
|
|
2005-06-03 07:32:17 +00:00
|
|
|
# Create the socket
|
2005-06-03 04:51:51 +00:00
|
|
|
sock = ::Socket.new(::Socket::AF_INET, type, proto)
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
# Bind to a given local address and/or port if they are supplied
|
|
|
|
if (param.localhost || param.localport)
|
2005-06-04 20:38:49 +00:00
|
|
|
begin
|
|
|
|
if (param.server?)
|
|
|
|
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, 1)
|
|
|
|
end
|
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
|
|
|
|
|
2005-06-04 20:38:49 +00:00
|
|
|
sock.bind(Rex::Socket.to_sockaddr(param.localhost, param.localport))
|
2005-11-27 18:42:44 +00:00
|
|
|
|
2005-06-04 20:38:49 +00:00
|
|
|
rescue Errno::EADDRINUSE
|
2005-11-27 18:42:44 +00:00
|
|
|
sock.close
|
2005-06-04 20:38:49 +00:00
|
|
|
raise Rex::AddressInUse.new(param.localhost, param.localport), caller
|
|
|
|
end
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# If a server TCP instance is being created...
|
|
|
|
if (param.server?)
|
|
|
|
sock.listen(32)
|
|
|
|
|
|
|
|
return sock if (param.bare?)
|
|
|
|
|
2005-09-27 05:31:48 +00:00
|
|
|
sock.extend(Rex::Socket::TcpServer)
|
|
|
|
|
|
|
|
sock.initsock(param)
|
2005-06-02 07:52:17 +00:00
|
|
|
# Otherwise, if we're creating a client...
|
|
|
|
else
|
2005-12-18 02:19:21 +00:00
|
|
|
chain = []
|
|
|
|
|
2005-06-03 04:51:51 +00:00
|
|
|
# If we were supplied with host information
|
|
|
|
if (param.peerhost)
|
2005-06-04 20:38:49 +00:00
|
|
|
begin
|
2005-12-18 02:19:21 +00:00
|
|
|
if param.proxies
|
|
|
|
chain = param.proxies.dup
|
|
|
|
chain.push(['host',param.peerhost,param.peerport])
|
|
|
|
ip = chain[0][1]
|
|
|
|
port = chain[0][2].to_i
|
|
|
|
sock.connect(Rex::Socket.to_sockaddr(ip, port))
|
|
|
|
else
|
|
|
|
sock.connect(Rex::Socket.to_sockaddr(param.peerhost, param.peerport))
|
|
|
|
end
|
2005-06-04 20:38:49 +00:00
|
|
|
rescue Errno::ECONNREFUSED
|
2005-11-27 18:42:44 +00:00
|
|
|
sock.close
|
2005-06-04 20:38:49 +00:00
|
|
|
raise Rex::ConnectionRefused.new(param.peerhost, param.peerport), caller
|
|
|
|
end
|
2005-06-03 04:51:51 +00:00
|
|
|
end
|
2005-06-02 07:52:17 +00:00
|
|
|
|
2005-11-15 20:33:55 +00:00
|
|
|
if (param.bare? == false)
|
|
|
|
case param.proto
|
|
|
|
when 'tcp'
|
|
|
|
klass = Rex::Socket::Tcp
|
|
|
|
|
|
|
|
if (param.ssl)
|
|
|
|
klass = Rex::Socket::SslTcp
|
|
|
|
end
|
|
|
|
|
|
|
|
sock.extend(klass)
|
|
|
|
|
|
|
|
sock.initsock(param)
|
|
|
|
when 'udp'
|
|
|
|
sock.extend(Rex::Socket::Udp)
|
|
|
|
|
|
|
|
sock.initsock(param)
|
|
|
|
end
|
|
|
|
end
|
2005-12-18 02:19:21 +00:00
|
|
|
|
|
|
|
if chain.size > 1
|
|
|
|
chain.each_with_index {
|
|
|
|
|proxy, i|
|
|
|
|
next_hop = chain[i + 1]
|
|
|
|
if next_hop
|
|
|
|
proxy(sock, proxy[0], next_hop[1], next_hop[2])
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2005-11-15 20:33:55 +00:00
|
|
|
end
|
2005-06-03 22:51:09 +00:00
|
|
|
|
2005-11-15 20:33:55 +00:00
|
|
|
# Notify handlers that a socket has been created.
|
|
|
|
self.instance.notify_socket_created(self, sock, param)
|
2005-09-27 05:31:48 +00:00
|
|
|
|
2005-11-15 20:33:55 +00:00
|
|
|
sock
|
|
|
|
end
|
2005-12-18 02:19:21 +00:00
|
|
|
|
|
|
|
def self.proxy (sock, type, host, port)
|
|
|
|
if type == 'socks4'
|
|
|
|
setup = [4,1,port.to_i].pack('CCn') + Socket.gethostbyname(host)[3] + "bmc\x00"
|
|
|
|
size = sock.put(setup)
|
|
|
|
if size != setup.length
|
|
|
|
raise 'ack, we did not write as much as expected!'
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
ret = sock.get_once(8, 30)
|
|
|
|
rescue IOError
|
|
|
|
raise Rex::ConnectionRefused.new(host, port), caller
|
|
|
|
end
|
|
|
|
|
|
|
|
if (ret.nil? or ret.length < 8)
|
|
|
|
raise 'ack, sock4 server did not respond with a socks4 response'
|
|
|
|
end
|
|
|
|
if ret[1] != 90
|
|
|
|
raise "ack, socks4 server responded with error code #{ret[0]}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise 'unsupported socks protocol', caller
|
|
|
|
end
|
|
|
|
end
|
2005-09-27 05:31:48 +00:00
|
|
|
|
2005-11-15 20:33:55 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Registration
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
def self.register_event_handler(handler) # :nodoc:
|
|
|
|
self.instance.register_event_handler(handler)
|
|
|
|
end
|
2005-09-27 05:31:48 +00:00
|
|
|
|
2005-11-15 20:33:55 +00:00
|
|
|
def self.deregister_event_handler(handler) # :nodoc:
|
|
|
|
self.instance.deregister_event_handler(handler)
|
|
|
|
end
|
2005-09-27 05:31:48 +00:00
|
|
|
|
2005-11-15 20:33:55 +00:00
|
|
|
def self.each_event_handler(handler) # :nodoc:
|
|
|
|
self.instance.each_event_handler(handler)
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|