2014-12-12 07:20:14 +00:00
|
|
|
# -*- coding: binary -*-
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Proto
|
|
|
|
module Kerberos
|
2014-12-14 06:51:44 +00:00
|
|
|
# This class is a representation of kerberos client.
|
2014-12-12 07:20:14 +00:00
|
|
|
class Client
|
2014-12-14 06:51:44 +00:00
|
|
|
# @!attribute hostname
|
|
|
|
# @return [String] The kerberos server hostname
|
2014-12-12 07:20:14 +00:00
|
|
|
attr_accessor :hostname
|
2014-12-14 06:51:44 +00:00
|
|
|
# @!attribute port
|
|
|
|
# @return [Fixnum] The kerberos server port
|
2014-12-12 07:20:14 +00:00
|
|
|
attr_accessor :port
|
2014-12-14 06:51:44 +00:00
|
|
|
# @!attribute protocol
|
|
|
|
# @return [String] The transport protocol used (tcp/udp)
|
2014-12-12 07:20:14 +00:00
|
|
|
attr_accessor :protocol
|
2014-12-14 06:51:44 +00:00
|
|
|
# @!attribute context
|
|
|
|
# @return [Hash]
|
2014-12-12 07:20:14 +00:00
|
|
|
attr_accessor :context
|
2014-12-14 06:51:44 +00:00
|
|
|
# @!attribute connection
|
|
|
|
# @return [IO]
|
2014-12-12 07:20:14 +00:00
|
|
|
attr_accessor :connection
|
|
|
|
|
|
|
|
def initialize(opts = {})
|
|
|
|
self.hostname = opts[:hostname]
|
|
|
|
self.port = opts[:port] || 88
|
|
|
|
self.protocol = opts[:protocol] || 'tcp'
|
|
|
|
self.context = opts[:context] || {}
|
|
|
|
end
|
|
|
|
|
2014-12-14 06:51:44 +00:00
|
|
|
# Creates a connection through a Rex socket
|
|
|
|
#
|
|
|
|
# @return [Rex::Socket::Tcp]
|
|
|
|
# @raise [RuntimeError] if the connection can not be created
|
2014-12-12 07:20:14 +00:00
|
|
|
def connect
|
|
|
|
return connection if connection
|
|
|
|
|
|
|
|
#timeout = (t.nil? or t == -1) ? 0 : t
|
|
|
|
timeout = 0
|
|
|
|
|
2014-12-14 06:51:44 +00:00
|
|
|
case protocol
|
|
|
|
when 'tcp'
|
|
|
|
self.connection = create_tcp_connection
|
|
|
|
when 'udp'
|
|
|
|
raise ::RuntimeError, 'Kerberos Client: UDP unsupported'
|
|
|
|
else
|
|
|
|
raise ::RuntimeError, 'Kerberos Client: unknown transport protocol'
|
|
|
|
end
|
2014-12-12 07:20:14 +00:00
|
|
|
|
|
|
|
connection
|
|
|
|
end
|
|
|
|
|
2014-12-14 06:51:44 +00:00
|
|
|
# Closes the connection
|
2014-12-12 07:20:14 +00:00
|
|
|
def close
|
|
|
|
if connection
|
|
|
|
connection.shutdown
|
|
|
|
connection.close unless connection.closed?
|
|
|
|
end
|
|
|
|
|
|
|
|
self.connection = nil
|
|
|
|
end
|
|
|
|
|
2014-12-14 06:51:44 +00:00
|
|
|
# Sends a kerberos request through the connection
|
|
|
|
#
|
|
|
|
# @param req [Rex::Proto::Kerberos::Model::Message::KdcRequest] the request to send
|
|
|
|
# @return [Fixnum] the number of bytes sent
|
|
|
|
# @raise [RuntimeError] if the transport protocol is unknown or not supported
|
2014-12-12 07:20:14 +00:00
|
|
|
def send_request(req)
|
|
|
|
connect
|
|
|
|
|
|
|
|
sent = 0
|
|
|
|
case protocol
|
|
|
|
when 'tcp'
|
|
|
|
sent = send_request_tcp(req)
|
|
|
|
when 'udp'
|
|
|
|
sent = send_request_udp(req)
|
|
|
|
else
|
|
|
|
raise ::RuntimeError, 'Kerberos Client: unknown transport protocol'
|
|
|
|
end
|
|
|
|
|
|
|
|
sent
|
|
|
|
end
|
|
|
|
|
2014-12-14 06:51:44 +00:00
|
|
|
# Receives a kerberos response through the connection
|
|
|
|
#
|
|
|
|
# @return [Rex::Proto::Kerberos::Model::Message::KdcResponse]
|
|
|
|
# @raise [RuntimeError] if the connection isn't established
|
|
|
|
# @raise [RuntimeError] if the transport protocol is unknown or unsupported
|
|
|
|
# @raise [RuntimeError] if the response can't be parsed
|
2014-12-12 07:20:14 +00:00
|
|
|
def recv_response
|
|
|
|
if connection.nil?
|
|
|
|
raise ::RuntimeError, 'Kerberos Client: connection not established'
|
|
|
|
end
|
|
|
|
|
|
|
|
res = nil
|
|
|
|
case protocol
|
|
|
|
when 'tcp'
|
|
|
|
res = recv_response_tcp
|
|
|
|
when 'udp'
|
|
|
|
res = recv_response_udp
|
|
|
|
else
|
|
|
|
raise ::RuntimeError, 'Kerberos Client: unknown transport protocol'
|
|
|
|
end
|
|
|
|
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2014-12-14 06:51:44 +00:00
|
|
|
# Sends a kerberos request, and reads the response through the connection
|
|
|
|
#
|
|
|
|
# @param req [Rex::Proto::Kerberos::Model::Message::KdcRequest] the request to sent
|
|
|
|
# @return [Rex::Proto::Kerberos::Model::Message::KdcResponse]
|
|
|
|
# @raise [RuntimeError] if the transport protocol is unknown or unsupported
|
|
|
|
# @raise [RuntimeError] if the response can't be parsed
|
2014-12-12 07:20:14 +00:00
|
|
|
def send_recv(req)
|
|
|
|
send_request(req)
|
2014-12-13 08:46:00 +00:00
|
|
|
res = recv_response
|
2014-12-12 07:20:14 +00:00
|
|
|
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-12-14 06:51:44 +00:00
|
|
|
# Creates a TCP connection
|
|
|
|
#
|
|
|
|
# @return [Rex::Socket::Tcp]
|
|
|
|
def create_tcp_connection
|
|
|
|
self.connection = Rex::Socket::Tcp.create(
|
|
|
|
'PeerHost' => hostname,
|
|
|
|
'PeerPort' => port.to_i,
|
|
|
|
#'LocalHost' => self.local_host,
|
|
|
|
#'LocalPort' => self.local_port,
|
|
|
|
'Context' => context,
|
|
|
|
#'Proxies' => self.proxies,
|
|
|
|
'Timeout' => timeout
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-12-12 07:20:14 +00:00
|
|
|
# Sends a Kerberos Request over a tcp connection
|
|
|
|
#
|
|
|
|
# @param req [Rex::Proto::Kerberos::Model::Message::KdcRequest] the request to send
|
|
|
|
# @return [Fixnum] the number of bytes sent
|
|
|
|
# @raise [RuntimeError] if the request can't be encoded
|
|
|
|
def send_request_tcp(req)
|
|
|
|
data = req.encode
|
2014-12-14 06:51:44 +00:00
|
|
|
length = [data.length].pack('N')
|
|
|
|
connection.put(length + data)
|
2014-12-12 07:20:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def send_request_udp(req)
|
|
|
|
raise ::RuntimeError, 'Kerberos Client: UDP unsupported'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Receives a Kerberos Response over a tcp connection
|
|
|
|
#
|
|
|
|
# @return [String] the data read from the connection
|
|
|
|
# @raise [EOFError] if the response can't be read
|
|
|
|
def recv_response_tcp
|
|
|
|
res = connection.get_once(-1)
|
|
|
|
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
def recv_response_udp
|
|
|
|
raise ::RuntimeError, 'Kerberos Client: UDP unsupported'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|