Module: Ronin::Support::Network::SSL::Mixin

Included in:
Mixin, Ronin::Support::Network::SMTP::Mixin, TLS::Mixin
Defined in:
lib/ronin/support/network/ssl/mixin.rb

Overview

Provides helper methods for communicating with SSL-enabled services.

Instance Method Summary collapse

Instance Method Details

#ssl_accept(**kwargs) {|client| ... } ⇒ nil

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

Examples:

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

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
ssl_accept(port: 1337, cert: 'ssl.crt', key: 'ssl.key') do |client|
  client.puts 'lol'
end

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #ssl_context.

Options Hash (**kwargs):

  • :port (Integer) — default: 0

    The local port to listen on.

  • :host (String, nil)

    The host to bind to.

  • :backlog (Integer) — default: 5

    The maximum backlog of pending connections.

  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil) — default: SSL.key

    The RSA key to use for the SSL context.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil) — default: SSL.cert

    The X509 certificate to use for the SSL context.

  • :version (1, 1.1, 1.2, 1.3, Symbol, nil)

    The SSL version to use.

  • :verify (Symbol, Boolean)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :fail_if_no_peer_cert
    • :client_once
  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil)

    The RSA key to use for the SSL context.

  • :key_file (String)

    The path to the SSL .key file.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil)

    The X509 certificate to use for the SSL context.

  • :cert_file (String)

    The path to the SSL .crt file.

  • :ca_bundle (String)

    Path to the CA certificate file or directory.

Yields:

  • (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.

Yield Parameters:

  • client (OpenSSL::SSL::SSLSocket)

    The newly connected client.

Returns:

  • (nil)

See Also:

Since:

  • 0.6.0



504
505
506
# File 'lib/ronin/support/network/ssl/mixin.rb', line 504

def ssl_accept(**kwargs,&block)
  SSL.accept(**kwargs,&block)
end

#ssl_banner(host, port, **kwargs) {|banner| ... } ⇒ String

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

Examples:

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

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #ssl_connect.

Options Hash (**kwargs):

  • :bind_host (String)

    The local host to bind to.

  • :bind_port (Integer)

    The local port to bind to.

  • :version (1, 1.1, 1.2, 1.3, Symbol, nil)

    The SSL version to use.

  • :verify (Symbol, Boolean)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :fail_if_no_peer_cert
    • :client_once
  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil)

    The RSA key to use for the SSL context.

  • :key_file (String)

    The path to the SSL .key file.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil)

    The X509 certificate to use for the SSL context.

  • :cert_file (String)

    The path to the SSL .crt file.

  • :ca_bundle (String)

    Path to the CA certificate file or directory.

Yields:

  • (banner)

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

Yield Parameters:

  • banner (String)

    The grabbed banner.

Returns:

  • (String)

    The grabbed banner.

See Also:

Since:

  • 0.6.0



287
288
289
# File 'lib/ronin/support/network/ssl/mixin.rb', line 287

def ssl_banner(host,port,**kwargs,&block)
  SSL.banner(host,port,**kwargs,&block)
end

#ssl_cert(host, port, **kwargs) ⇒ Crypto::Cert

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

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments #ssl_connect.

Options Hash (**kwargs):

  • :bind_host (String)

    The local host to bind to.

  • :bind_port (Integer)

    The local port to bind to.

  • :version (1, 1.1, 1.2, 1.3, Symbol, nil)

    The SSL version to use.

  • :verify (Symbol, Boolean)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :fail_if_no_peer_cert
    • :client_once
  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil)

    The RSA key to use for the SSL context.

  • :key_file (String)

    The path to the SSL .key file.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil)

    The X509 certificate to use for the SSL context.

  • :cert_file (String)

    The path to the SSL .crt file.

  • :ca_bundle (String)

    Path to the CA certificate file or directory.

Returns:

See Also:



249
250
251
# File 'lib/ronin/support/network/ssl/mixin.rb', line 249

def ssl_cert(host,port,**kwargs)
  SSL.get_cert(host,port,**kwargs)
end

#ssl_connect(host, port, **kwargs) {|ssl_socket| ... } ⇒ OpenSSL::SSL::SSLSocket?

Establishes a SSL connection.

Examples:

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

  sock.each_line { |line| puts line }
end

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #ssl_socket.

Options Hash (**kwargs):

  • :bind_host (String)

    The local host to bind to.

  • :bind_port (Integer)

    The local port to bind to.

  • :version (1, 1.1, 1.2, 1.3, Symbol, nil)

    The SSL version to use.

  • :verify (Symbol, Boolean)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :fail_if_no_peer_cert
    • :client_once
  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil)

    The RSA key to use for the SSL context.

  • :key_file (String)

    The path to the SSL .key file.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil)

    The X509 certificate to use for the SSL context.

  • :cert_file (String)

    The path to the SSL .crt file.

  • :ca_bundle (String)

    Path to the CA certificate file or directory.

Yields:

  • (ssl_socket)

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

Yield Parameters:

  • ssl_socket (OpenSSL::SSL::SSLSocket)

    The new SSL Socket.

Returns:

  • (OpenSSL::SSL::SSLSocket, nil)

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

See Also:



193
194
195
# File 'lib/ronin/support/network/ssl/mixin.rb', line 193

def ssl_connect(host,port,**kwargs,&block)
  SSL.connect(host,port,**kwargs,&block)
end

#ssl_connect_and_send(data, host, port, **kwargs) {|ssl_socket| ... } ⇒ Object

Creates a new SSL connection and sends the given data.

Parameters:

  • data (String)

    The data to send through the connection.

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #ssl_connect.

Options Hash (**kwargs):

  • :bind_host (String)

    The local host to bind to.

  • :bind_port (Integer)

    The local port to bind to.

  • :version (1, 1.1, 1.2, 1.3, Symbol, nil)

    The SSL version to use.

  • :verify (Symbol, Boolean)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :fail_if_no_peer_cert
    • :client_once
  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil)

    The RSA key to use for the SSL context.

  • :key_file (String)

    The path to the SSL .key file.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil)

    The X509 certificate to use for the SSL context.

  • :cert_file (String)

    The path to the SSL .crt file.

  • :ca_bundle (String)

    Path to the CA certificate file or directory.

Yields:

  • (ssl_socket)

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

Yield Parameters:

  • ssl_socket (OpenSSL::SSL::SSLSocket)

    The newly created SSL Socket.

See Also:

Since:

  • 0.6.0



226
227
228
# File 'lib/ronin/support/network/ssl/mixin.rb', line 226

def ssl_connect_and_send(data,host,port,**kwargs,&block)
  SSL.connect_and_send(data,host,port,**kwargs,&block)
end

#ssl_context(**kwargs) ⇒ OpenSSL::SSL::SSLContext

Creates a new SSL Context.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :version (1, 1.1, 1.2, 1.3, Symbol, nil)

    The SSL version to use.

  • :verify (Symbol, Boolean)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :fail_if_no_peer_cert
    • :client_once
  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil)

    The RSA key to use for the SSL context.

  • :key_file (String)

    The path to the SSL .key file.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil)

    The X509 certificate to use for the SSL context.

  • :cert_file (String)

    The path to the SSL .crt file.

  • :ca_bundle (String)

    Path to the CA certificate file or directory.

Returns:

  • (OpenSSL::SSL::SSLContext)

    The newly created SSL Context.

See Also:

Since:

  • 0.6.0



76
77
78
# File 'lib/ronin/support/network/ssl/mixin.rb', line 76

def ssl_context(**kwargs)
  SSL.context(**kwargs)
end

#ssl_open?(host, port, **kwargs) ⇒ Boolean?

Tests whether a remote SSLed TCP port is open.

Examples:

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

Using a timeout:

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

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #ssl_connect.

Options Hash (**kwargs):

  • :bind_host (String)

    The local host to bind to.

  • :bind_port (Integer)

    The local port to bind to.

  • :version (1, 1.1, 1.2, 1.3, Symbol, nil)

    The SSL version to use.

  • :verify (Symbol, Boolean)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :fail_if_no_peer_cert
    • :client_once
  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil)

    The RSA key to use for the SSL context.

  • :key_file (String)

    The path to the SSL .key file.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil)

    The X509 certificate to use for the SSL context.

  • :cert_file (String)

    The path to the SSL .crt file.

  • :ca_bundle (String)

    Path to the CA certificate file or directory.

  • :timeout (Integer) — default: 5

    The maximum time to attempt connecting.

Returns:

  • (Boolean, nil)

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

See Also:

Since:

  • 0.6.0



149
150
151
# File 'lib/ronin/support/network/ssl/mixin.rb', line 149

def ssl_open?(host,port,**kwargs)
  SSL.open?(host,port,**kwargs)
end

#ssl_send(data, host, port, **kwargs) ⇒ true

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

Examples:

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

Parameters:

  • data (String)

    The data to send through the connection.

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #ssl_connect.

Options Hash (**kwargs):

  • :bind_host (String)

    The local host to bind to.

  • :bind_port (Integer)

    The local port to bind to.

  • :version (1, 1.1, 1.2, 1.3, Symbol, nil)

    The SSL version to use.

  • :verify (Symbol, Boolean)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :fail_if_no_peer_cert
    • :client_once
  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil)

    The RSA key to use for the SSL context.

  • :key_file (String)

    The path to the SSL .key file.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil)

    The X509 certificate to use for the SSL context.

  • :cert_file (String)

    The path to the SSL .crt file.

  • :ca_bundle (String)

    Path to the CA certificate file or directory.

Returns:

  • (true)

    The data was successfully sent.

See Also:

Since:

  • 0.6.0



323
324
325
# File 'lib/ronin/support/network/ssl/mixin.rb', line 323

def ssl_send(data,host,port,**kwargs)
  SSL.send(data,host,port,**kwargs)
end

#ssl_server(**kwargs) {|server| ... } ⇒ OpenSSL::SSL::SSLServer

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

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #ssl_context.

Options Hash (**kwargs):

  • :port (Integer) — default: 0

    The local port to listen on.

  • :host (String, nil)

    The host to bind to.

  • :backlog (Integer) — default: 5

    The maximum backlog of pending connections.

  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil) — default: SSL.key

    The RSA key to use for the SSL context.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil) — default: SSL.cert

    The X509 certificate to use for the SSL context.

  • :version (1, 1.1, 1.2, 1.3, Symbol, nil)

    The SSL version to use.

  • :verify (Symbol, Boolean)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :fail_if_no_peer_cert
    • :client_once
  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil)

    The RSA key to use for the SSL context.

  • :key_file (String)

    The path to the SSL .key file.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil)

    The X509 certificate to use for the SSL context.

  • :cert_file (String)

    The path to the SSL .crt file.

  • :ca_bundle (String)

    Path to the CA certificate file or directory.

Yields:

  • (server)

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

Yield Parameters:

  • server (OpenSSL::SSL::SSLServer)

    The newly created SSL server.

Returns:

  • (OpenSSL::SSL::SSLServer)

    The newly created SSL server.

Since:

  • 1.1.0



397
398
399
# File 'lib/ronin/support/network/ssl/mixin.rb', line 397

def ssl_server(**kwargs,&block)
  SSL.server(**kwargs,&block)
end

#ssl_server_loop(**kwargs) {|client| ... } ⇒ nil

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

Examples:

# $ 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
ssl_server_loop(port: 1337, cert: 'ssl.crt', key: 'ssl.key') do |sock|
  sock.puts 'lol'
end

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #ssl_context.

Options Hash (**kwargs):

  • :port (Integer) — default: 0

    The local port to listen on.

  • :host (String, nil)

    The host to bind to.

  • :backlog (Integer) — default: 5

    The maximum backlog of pending connections.

  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil) — default: SSL.key

    The RSA key to use for the SSL context.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil) — default: SSL.cert

    The X509 certificate to use for the SSL context.

  • :version (1, 1.1, 1.2, 1.3, Symbol, nil)

    The SSL version to use.

  • :verify (Symbol, Boolean)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :fail_if_no_peer_cert
    • :client_once
  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil)

    The RSA key to use for the SSL context.

  • :key_file (String)

    The path to the SSL .key file.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil)

    The X509 certificate to use for the SSL context.

  • :cert_file (String)

    The path to the SSL .crt file.

  • :ca_bundle (String)

    Path to the CA certificate file or directory.

Yields:

  • (client)

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

Yield Parameters:

  • client (OpenSSL::SSL::SSLSocket)

    A newly connected client.

Returns:

  • (nil)

See Also:

Since:

  • 0.6.0



461
462
463
# File 'lib/ronin/support/network/ssl/mixin.rb', line 461

def ssl_server_loop(**kwargs,&block)
  SSL.server_loop(**kwargs,&block)
end

#ssl_server_session(**kwargs) {|server| ... } ⇒ OpenSSL::SSL::SSLServer

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

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #ssl_context.

Options Hash (**kwargs):

  • :port (Integer) — default: 0

    The local port to listen on.

  • :host (String, nil)

    The host to bind to.

  • :backlog (Integer) — default: 5

    The maximum backlog of pending connections.

  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil) — default: SSL.key

    The RSA key to use for the SSL context.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil) — default: SSL.cert

    The X509 certificate to use for the SSL context.

  • :version (1, 1.1, 1.2, 1.3, Symbol, nil)

    The SSL version to use.

  • :verify (Symbol, Boolean)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :fail_if_no_peer_cert
    • :client_once
  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil)

    The RSA key to use for the SSL context.

  • :key_file (String)

    The path to the SSL .key file.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil)

    The X509 certificate to use for the SSL context.

  • :cert_file (String)

    The path to the SSL .crt file.

  • :ca_bundle (String)

    Path to the CA certificate file or directory.

Yields:

  • (server)

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

Yield Parameters:

  • server (OpenSSL::SSL::SSLServer)

    The newly created SSL server.

Returns:

  • (OpenSSL::SSL::SSLServer)

    The newly created SSL server.

Since:

  • 1.1.0



424
425
426
# File 'lib/ronin/support/network/ssl/mixin.rb', line 424

def ssl_server_session(**kwargs,&block)
  SSL.server_session(**kwargs,&block)
end

#ssl_server_socket(socket, **kwargs) ⇒ OpenSSL::SSL::SSLSocket

Accepts an SSL session from an existing TCP socket.

Parameters:

  • socket (TCPSocket)

    The existing TCP socket.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #ssl_socket.

Options Hash (**kwargs):

  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil) — default: SSL.key

    The RSA key to use for the SSL context.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil) — default: SSL.cert

    The X509 certificate to use for the SSL context.

  • :version (1, 1.1, 1.2, 1.3, Symbol, nil)

    The SSL version to use.

  • :verify (Symbol, Boolean)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :fail_if_no_peer_cert
    • :client_once
  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil)

    The RSA key to use for the SSL context.

  • :key_file (String)

    The path to the SSL .key file.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil)

    The X509 certificate to use for the SSL context.

  • :cert_file (String)

    The path to the SSL .crt file.

  • :ca_bundle (String)

    Path to the CA certificate file or directory.

Returns:

  • (OpenSSL::SSL::SSLSocket)

    The new SSL Socket.

See Also:

Since:

  • 0.6.0



358
359
360
# File 'lib/ronin/support/network/ssl/mixin.rb', line 358

def ssl_server_socket(socket,**kwargs)
  SSL.server_socket(socket,**kwargs)
end

#ssl_socket(socket, **kwargs) ⇒ OpenSSL::SSL::SSLSocket

Initiates an SSL session with an existing TCP socket.

Parameters:

  • socket (TCPSocket)

    The existing TCP socket.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #ssl_context.

Options Hash (**kwargs):

  • :version (1, 1.1, 1.2, 1.3, Symbol, nil)

    The SSL version to use.

  • :verify (Symbol, Boolean)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :fail_if_no_peer_cert
    • :client_once
  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil)

    The RSA key to use for the SSL context.

  • :key_file (String)

    The path to the SSL .key file.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil)

    The X509 certificate to use for the SSL context.

  • :cert_file (String)

    The path to the SSL .crt file.

  • :ca_bundle (String)

    Path to the CA certificate file or directory.

Returns:

  • (OpenSSL::SSL::SSLSocket)

    The new SSL Socket.

See Also:

Since:

  • 0.6.0



100
101
102
# File 'lib/ronin/support/network/ssl/mixin.rb', line 100

def ssl_socket(socket,**kwargs)
  SSL.socket(socket,**kwargs)
end