module Ronin::Support::Network::TLS::Mixin

Provides helper methods for communicating with TLS-enabled services.

Public Instance Methods

tls_accept(**kwargs, &block) click to toggle source

Creates a new SSL socket listening on a given host and port, accepts only one client and then stops listening.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {#tls_server_socket}.

@!macro server_kwargs

@yield [client]

The given block will be passed the newly connected client.
After the block has finished, both the client and the server will
be closed.

@yieldparam [OpenSSL::SSL::SSLSocket] client

The newly connected client.

@return [nil]

@example

tls_accept(1337) do |client|
  client.puts 'lol'
end

@example Using a self-signed certificate:

# $ openssl genrsa -out ssl.key 1024
# $ openssl req -new -key ssl.key -x509 -days 3653 -out ssl.crt
# $ cat ssl.key ssl.crt > ssl.pem
# $ chmod 600 ssl.key ssl.pem
tls_accept(port: 1337, cert: 'ssl.crt', key: 'ssl.key') do |client|
  client.puts 'lol'
end

@api public

@see TLS.accept

# File lib/ronin/support/network/tls/mixin.rb, line 489
def tls_accept(**kwargs, &block)
  TLS.accept(**kwargs, &block)
end
tls_banner(host,port,**kwargs, &block) click to toggle source

Reads the banner from the service running on the given host and port.

@param [String] host

The host to connect to.

@param [Integer] port

The port to connect to.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {#tls_connect}.

@!macro connect_kwargs

@yield [banner]

If a block is given, it will be passed the grabbed banner.

@yieldparam [String] banner

The grabbed banner.

@return [String]

The grabbed banner.

@example

tls_banner('smtp.gmail.com',465)
# => "220 mx.google.com ESMTP c20sm3096959rvf.1"

@api public

@see TLS.banner

# File lib/ronin/support/network/tls/mixin.rb, line 280
def tls_banner(host,port,**kwargs, &block)
  TLS.banner(host,port,**kwargs, &block)
end
tls_cert(host,port,**kwargs) click to toggle source

Connects to the host and port and returns the server’s certificate.

@param [String] host

The host to connect to.

@param [Integer] port

The port to connect to.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {#tls_connect}.

@!macro connect_kwargs

@return [OpenSSL::X509::Certificate]

The server's certificate.

@see TLS.get_cert

# File lib/ronin/support/network/tls/mixin.rb, line 244
def tls_cert(host,port,**kwargs)
  TLS.get_cert(host,port,**kwargs)
end
tls_connect(host,port,**kwargs, &block) click to toggle source

Establishes a SSL connection.

@param [String] host

The host to connect to.

@param [Integer] port

The port to connect to.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {#tls_socket}.

@!macro connect_kwargs

@yield [tls_socket]

The given block will be passed the new SSL socket. Once the block
returns the SSL socket will be closed.

@yieldparam [OpenSSL::SSL::SSLSocket] tls_socket

The new SSL Socket.

@return [OpenSSL::SSL::SSLSocket, nil]

The new SSL Socket. If a block is given, then `nil` will be
returned.

@example

socket = tls_connect('twitter.com',443)

@example

tls_connect('twitter.com',443) do |sock|
  sock.write("GET / HTTP/1.1\n\r\n\r")

  sock.each_line { |line| puts line }
end

@see rubydoc.info/stdlib/openssl/OpenSSL/SSL/SSLSocket

@api public

@see TLS.connect

# File lib/ronin/support/network/tls/mixin.rb, line 190
def tls_connect(host,port,**kwargs, &block)
  TLS.connect(host,port,**kwargs, &block)
end
tls_connect_and_send(data,host,port,**kwargs, &block) click to toggle source

Creates a new SSL connection and sends the given data.

@param [String] data

The data to send through the connection.

@param [String] host

The host to connect to.

@param [Integer] port

The port to connect to.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {#tls_connect}.

@!macro connect_kwargs

@yield [tls_socket]

The given block will be passed the newly created TLS Socket.

@yieldparam [OpenSSL::SSL::SSLSocket] tls_socket

The newly created SSL Socket.

@api public

@see TLS.connect_and_send

# File lib/ronin/support/network/tls/mixin.rb, line 221
def tls_connect_and_send(data,host,port,**kwargs, &block)
  TLS.connect_and_send(data,host,port,**kwargs, &block)
end
tls_context(**kwargs) click to toggle source

Creates a new TLS Context.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {TLS.context}.

@!macro context_kwargs

@return [OpenSSL::SSL::SSLContext]

The newly created SSL Context.

@api semipublic

# File lib/ronin/support/network/tls/mixin.rb, line 81
def tls_context(**kwargs)
  TLS.context(**kwargs)
end
tls_open?(host,port,**kwargs) click to toggle source

Tests whether a remote SSLed TCP port is open.

@param [String] host

The host to connect to.

@param [Integer] port

The port to connect to.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {#tls_connect}.

@!macro connect_kwargs

@return [Boolean, nil]

Specifies whether the remote SSLed TCP port is open.
If the connection was not accepted, `nil` will be returned.

@example

tls_open?('www.bankofamerica.com',443)

@example Using a timeout:

tls_open?('example.com',80, timeout: 5)
# => nil

@api public

@see TLS.open?

# File lib/ronin/support/network/tls/mixin.rb, line 145
def tls_open?(host,port,**kwargs)
  TLS.open?(host,port,**kwargs)
end
tls_send(data,host,port,**kwargs) click to toggle source

Connects to a specified host and port, sends the given data and then closes the connection.

@param [String] data

The data to send through the connection.

@param [String] host

The host to connect to.

@param [Integer] port

The port to connect to.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {#tls_connect}.

@!macro connect_kwargs

@return [true]

The data was successfully sent.

@example

buffer = "GET /#{'A' * 4096}\n\r"
tls_send(buffer,'victim.com',443)
# => true

@api public

@see TLS.send

# File lib/ronin/support/network/tls/mixin.rb, line 314
def tls_send(data,host,port,**kwargs)
  TLS.send(data,host,port,**kwargs)
end
tls_server(**kwargs, &block) click to toggle source

Creates a new TLS server listening on a given host and port.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {#tls_context}.

@!macro server_kwargs

@yield [server]

The given block will be passed the newly created SSL server.

@yieldparam [OpenSSL::SSL::SSLServer] server

The newly created SSL server.

@return [OpenSSL::SSL::SSLServer]

The newly created SSL server.

@api public

@since 1.1.0

# File lib/ronin/support/network/tls/mixin.rb, line 386
def tls_server(**kwargs, &block)
  TLS.server(**kwargs, &block)
end
tls_server_loop(**kwargs,&block) click to toggle source

Creates a new SSL socket listening on a given host and port, accepting clients in a loop.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {#tls_server_socket}.

@!macro server_kwargs

@yield [client]

The given block will be passed the newly connected client.
After the block has finished, the client will be closed.

@yieldparam [OpenSSL::SSL::SSLSocket] client

A newly connected client.

@return [nil]

@example

# $ openssl genrsa -out ssl.key 1024
# $ openssl req -new -key ssl.key -x509 -days 3653 -out ssl.crt
# $ cat ssl.key ssl.crt > ssl.pem
# $ chmod 600 ssl.key ssl.pem
tls_server_loop(port: 1337, cert: 'ssl.crt', key: 'ssl.key') do |sock|
  sock.puts 'lol'
end

@api public

@see TLS.server_loop

# File lib/ronin/support/network/tls/mixin.rb, line 448
def tls_server_loop(**kwargs,&block)
  TLS.server_loop(**kwargs,&block)
end
tls_server_session(**kwargs, &block) click to toggle source

Creates a new temporary TLS server listening on a given host and port.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {#tls_context}.

@!macro server_kwargs

@yield [server]

The given block will be passed the newly created SSL server.

@yieldparam [OpenSSL::SSL::SSLServer] server

The newly created SSL server. Once the block has finished,
the server will be closed.

@return [OpenSSL::SSL::SSLServer]

The newly created SSL server.

@api public

@since 1.1.0

# File lib/ronin/support/network/tls/mixin.rb, line 413
def tls_server_session(**kwargs, &block)
  TLS.server_session(**kwargs, &block)
end
tls_server_socket(socket,**kwargs) click to toggle source

Accepts an SSL session from an existing TCP socket.

@param [TCPSocket] socket

The existing TCP socket.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {#tls_socket}.

@!macro server_context_kwargs

@return [OpenSSL::SSL::SSLSocket]

The new SSL Socket.

@api public

@see TLS.server_socket

# File lib/ronin/support/network/tls/mixin.rb, line 347
def tls_server_socket(socket,**kwargs)
  TLS.server_socket(socket,**kwargs)
end
tls_socket(socket,**kwargs) click to toggle source

Initiates an SSL session with an existing TCP socket.

@param [TCPSocket] socket

The existing TCP socket.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {TLS.socket}.

@!macro context_kwargs

@return [OpenSSL::SSL::SSLSocket]

The new SSL Socket.

@api public

# File lib/ronin/support/network/tls/mixin.rb, line 101
def tls_socket(socket,**kwargs)
  TLS.socket(socket,**kwargs)
end