Module: Ronin::Support::Network::TCP::Mixin

Included in:
Mixin
Defined in:
lib/ronin/support/network/tcp/mixin.rb

Overview

Provides helper methods for using the TCP protocol.

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

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

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

Examples:

tcp_accept(port: 1337) do |client|
  client.puts 'lol'
end

Parameters:

Options Hash (**kwargs):

  • :backlog (Integer) — default: 1

    The maximum backlog of pending connections.

  • :port (Integer, nil) — default: 0

    The local port to bind to.

  • :host (String, nil)

    The host to bind to.

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 (TCPSocket)

    The newly connected client.

Returns:

  • (nil)

See Also:

Since:

  • 0.5.0



393
394
395
# File 'lib/ronin/support/network/tcp/mixin.rb', line 393

def tcp_accept(**kwargs,&block)
  TCP.accept(**kwargs,&block)
end

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

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

Examples:

tcp_banner('pop.gmail.com',25)
# => "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 #tcp_connect.

Options Hash (**kwargs):

  • :bind_host (String)

    The local host to bind to.

  • :bind_port (Integer)

    The local port to bind to.

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:

  • 1.0.0



194
195
196
# File 'lib/ronin/support/network/tcp/mixin.rb', line 194

def tcp_banner(host,port,**kwargs,&block)
  TCP.banner(host,port,**kwargs,&block)
end

#tcp_connect(host, port, **kwargs) {|socket| ... } ⇒ TCPSocket?

Creates a new TCP socket connected to a given host and port.

Examples:

tcp_connect('www.example.com',80)
# => #<TCPSocket:fd 5, AF_INET, 192.168.122.165, 40364>
tcp_connect('www.wired.com',80) do |socket|
  socket.write("GET /\n\n")

  puts socket.readlines
end

Parameters:

Options Hash (**kwargs):

  • :bind_host (String, nil)

    The local host to bind to.

  • :bind_port (Integer, nil)

    The local port to bind to.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket. Once the block returns the socket will be closed.

Yield Parameters:

  • socket (TCPsocket)

    The newly created TCP socket.

Returns:

  • (TCPSocket, nil)

    The newly created TCPSocket object. If a block is given a nil will be returned.

See Also:

Since:

  • 1.0.0



117
118
119
# File 'lib/ronin/support/network/tcp/mixin.rb', line 117

def tcp_connect(host,port,**kwargs,&block)
  TCP.connect(host,port,**kwargs,&block)
end

#tcp_connect_and_send(data, host, port, **kwargs) {|socket| ... } ⇒ TCPSocket

Creates a new TCPSocket object, connected to a given host and port. The given data will then be written to the newly created TCPSocket.

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 #tcp_connect.

Options Hash (**kwargs):

  • :bind_host (String, nil)

    The local host to bind to.

  • :bind_port (Integer, nil)

    The local port to bind to.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket.

Yield Parameters:

  • socket (TCPSocket)

    The newly created TCPSocket object.

Returns:

  • (TCPSocket)

    The newly created TCPSocket object.

Since:

  • 1.0.0



154
155
156
# File 'lib/ronin/support/network/tcp/mixin.rb', line 154

def tcp_connect_and_send(data,host,port,**kwargs,&block)
  TCP.connect_and_send(data,host,port,**kwargs,&block)
end

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

Tests whether a remote TCP port is open.

Examples:

tcp_open?('example.com',80)
# => true

Using a timeout:

tcp_open?('example.com',1111, 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 #tcp_connect.

Options Hash (**kwargs):

  • bind_host (String, nil)

    The local host to bind to.

  • bind_port (Integer, nil)

    The local port to bind to.

  • :timeout (Integer) — default: 5

    The maximum time to attempt connecting.

Returns:

  • (Boolean, nil)

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

See Also:

Since:

  • 0.5.0



68
69
70
# File 'lib/ronin/support/network/tcp/mixin.rb', line 68

def tcp_open?(host,port,**kwargs)
  TCP.open?(host,port,**kwargs)
end

#tcp_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"
tcp_send(buffer,'victim.com',80)
# => 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 #tcp_connect.

Options Hash (**kwargs):

  • :bind_host (String)

    The local host to bind to.

  • :bind_port (Integer)

    The local port to bind to.

Returns:

  • (true)

    The data was successfully sent.

See Also:

Since:

  • 1.0.0



232
233
234
# File 'lib/ronin/support/network/tcp/mixin.rb', line 232

def tcp_send(data,host,port,**kwargs)
  TCP.send(data,host,port,**kwargs)
end

#tcp_server(**kwargs) {|server| ... } ⇒ TCPServer

Creates a new TCPServer listening on a given host and port.

Examples:

tcp_server(port: 1337)

Parameters:

Options Hash (**kwargs):

  • :port (Integer, nil) — 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.

Yields:

  • (server)

    The block which will be called after the server has been created.

Yield Parameters:

  • server (TCPServer)

    The newly created TCP server.

Returns:

  • (TCPServer)

    The new TCP server.

See Also:

Since:

  • 1.0.0



269
270
271
# File 'lib/ronin/support/network/tcp/mixin.rb', line 269

def tcp_server(**kwargs,&block)
  TCP.server(**kwargs,&block)
end

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

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

Examples:

tcp_server_loop(port: 1337) do |client|
  client.puts 'lol'
end

Parameters:

Options Hash (**kwargs):

  • :port (Integer, nil) — default: 0

    The local port to bind to.

  • :host (String, nil)

    The host to bind to.

  • :backlog (Integer) — default: 5

    The maximum backlog of pending connections.

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 (TCPSocket)

    A newly connected client.

Returns:

  • (nil)

See Also:

Since:

  • 0.5.0



352
353
354
# File 'lib/ronin/support/network/tcp/mixin.rb', line 352

def tcp_server_loop(**kwargs,&block)
  TCP.server_loop(**kwargs,&block)
end

#tcp_server_session(**kwargs) {|server| ... } ⇒ nil

Creates a new temporary TCPServer listening on a host and port.

Examples:

tcp_server_session(port: 1337) do |server|
  client1 = server.accept
  client2 = server.accept

  client2.write(server.read_line)

  client1.close
  client2.close
end

Parameters:

Options Hash (**kwargs):

  • :port (Integer, nil) — default: 0

    The local port to bind to.

  • :host (String, nil)

    The host to bind to.

  • :backlog (Integer) — default: 5

    The maximum backlog of pending connections.

Yields:

  • (server)

    The block which will be called after the server has been created. After the block has finished, the server will be closed.

Yield Parameters:

  • server (TCPServer)

    The newly created TCP server.

Returns:

  • (nil)

See Also:

Since:

  • 1.0.0



312
313
314
# File 'lib/ronin/support/network/tcp/mixin.rb', line 312

def tcp_server_session(**kwargs,&block)
  TCP.server_session(**kwargs,&block)
end