Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/metasploit/framework/tcp/client.rb
Views: 11784
module Metasploit1module Framework2module Tcp34module EvasiveTCP5attr_accessor :_send_size, :_send_delay, :evasive67def denagle8begin9setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)10rescue ::Exception11end12end1314def write(buf, opts={})1516return super(buf, opts) if not @evasive1718ret = 019idx = 020len = @_send_size || buf.length2122while(idx < buf.length)2324if(@_send_delay and idx > 0)25::IO.select(nil, nil, nil, @_send_delay)26end2728pkt = buf[idx, len]2930res = super(pkt, opts)31flush()3233idx += len34ret += res if res35end36ret37end38end3940module Client4142extend ActiveSupport::Concern4344# @!attribute max_send_size45# @return [Integer] The max size of the data to encapsulate in a single packet46attr_accessor :max_send_size47# @!attribute send_delay48# @return [Integer] The delay between sending packets49attr_accessor :send_delay5051included do52include ActiveModel::Validations53validates :max_send_size,54presence: true,55numericality: {56only_integer: true,57greater_than_or_equal_to: 058}5960validates :send_delay,61presence: true,62numericality: {63only_integer: true,64greater_than_or_equal_to: 065}6667end6869#70# Establishes a TCP connection to the specified RHOST/RPORT71#72# @see Rex::Socket::Tcp73# @see Rex::Socket::Tcp.create74def connect(global = true, opts={})75dossl = false76if(opts.has_key?('SSL'))77dossl = opts['SSL']78else79dossl = ssl80end8182nsock = Rex::Socket::Tcp.create(83'PeerHost' => opts['RHOST'] || rhost,84'PeerHostname' => opts['SSLServerNameIndication'] || opts['RHOSTNAME'],85'PeerPort' => (opts['RPORT'] || rport).to_i,86'LocalHost' => opts['CHOST'] || chost || "0.0.0.0",87'LocalPort' => (opts['CPORT'] || cport || 0).to_i,88'SSL' => dossl,89'SSLVersion' => opts['SSLVersion'] || ssl_version,90'SSLVerifyMode' => opts['SSLVerifyMode'] || ssl_verify_mode,91'SSLCipher' => opts['SSLCipher'] || ssl_cipher,92'Proxies' => proxies,93'Timeout' => (opts['ConnectTimeout'] || connection_timeout || 10).to_i,94'Context' => { 'Msf' => framework, 'MsfExploit' => framework_module }95)96# enable evasions on this socket97set_tcp_evasions(nsock)9899# Set this socket to the global socket as necessary100self.sock = nsock if (global)101102return nsock103end104105# Enable evasions on a given client106def set_tcp_evasions(socket)107108if( max_send_size.to_i == 0 and send_delay.to_i == 0)109return110end111112return if socket.respond_to?('evasive')113114socket.extend(EvasiveTCP)115116if ( max_send_size.to_i > 0)117socket._send_size = max_send_size118socket.denagle119socket.evasive = true120end121122if ( send_delay.to_i > 0)123socket._send_delay = send_delay124socket.evasive = true125end126end127128#129# Closes the TCP connection130#131def disconnect(nsock = self.sock)132begin133if (nsock)134nsock.shutdown135nsock.close136end137rescue IOError138end139140if (nsock == sock)141self.sock = nil142end143144end145146##147#148# Wrappers for getters149#150##151152#153# Returns the target host154#155def rhost156raise NotImplementedError157end158159#160# Returns the remote port161#162def rport163raise NotImplementedError164end165166#167# Returns the local host for outgoing connections168#169def chost170raise NotImplementedError171end172173#174# Returns the local port for outgoing connections175#176def cport177raise NotImplementedError178end179180#181# Returns the boolean indicating SSL182#183def ssl184raise NotImplementedError185end186187#188# Returns the string indicating SSLVersion189#190def ssl_version191raise NotImplementedError192end193194#195# Returns the proxy configuration196#197def proxies198raise NotImplementedError199end200201attr_accessor :sock202203end204end205end206end207208209