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

Defined in:
lib/ronin/support/network/tcp.rb,
lib/ronin/support/network/tcp/mixin.rb,
lib/ronin/support/network/tcp/proxy.rb

Overview

Since:

  • 1.0.0

Defined Under Namespace

Modules: Mixin Classes: Proxy

Class Method Summary collapse

Class Method Details

.accept(backlog: 1, **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:

  • backlog (Integer) (defaults to: 1)

    The maximum backlog of pending connections.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for server.

Options Hash (**kwargs):

  • :port (Integer, nil)

    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)

Since:

  • 0.5.0



433
434
435
436
437
438
439
440
# File 'lib/ronin/support/network/tcp.rb', line 433

def self.accept(backlog: 1, **kwargs)
  server_session(backlog: backlog, **kwargs) do |server|
    client = server.accept

    yield client if block_given?
    client.close
  end
end

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

Since:

  • 1.0.0



219
220
221
222
223
224
225
226
227
228
# File 'lib/ronin/support/network/tcp.rb', line 219

def self.banner(host,port,**kwargs)
  banner = nil

  connect(host,port,**kwargs) do |socket|
    banner = socket.readline.strip
  end

  yield banner if block_given?
  return banner
end

.connect(host, port, bind_host: nil, bind_port: nil) {|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:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • bind_host (String, nil) (defaults to: nil)

    The local host to bind to.

  • bind_port (Integer, nil) (defaults to: 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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ronin/support/network/tcp.rb', line 123

def self.connect(host,port, bind_host: nil, bind_port: nil)
  host = DNS::IDN.to_ascii(host)
  port = port.to_i

  socket = if bind_host || bind_port
             TCPSocket.new(host,port,bind_host.to_s,bind_port.to_i)
           else
             TCPSocket.new(host,port)
           end

  if block_given?
    begin
      yield socket
    ensure
      socket.close
    end
  else
    return socket
  end
end

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



177
178
179
180
181
182
183
# File 'lib/ronin/support/network/tcp.rb', line 177

def self.connect_and_send(data,host,port,**kwargs)
  socket = connect(host,port,**kwargs)
  socket.write(data)

  yield socket if block_given?
  return socket
end

.open?(host, port, timeout: 5, **kwargs) ⇒ Boolean?

Tests whether a remote TCP port is open.

Examples:

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.

  • timeout (Integer) (defaults to: 5)

    (5) The maximum time to attempt connecting.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for connect.

Options Hash (**kwargs):

  • bind_host (String, nil)

    The local host to bind to.

  • bind_port (Integer, nil)

    The local port to bind to.

Returns:

  • (Boolean, nil)

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

Since:

  • 0.5.0



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ronin/support/network/tcp.rb', line 69

def self.open?(host,port, timeout: 5, **kwargs)
  Timeout.timeout(timeout) do
    socket = connect(host,port,**kwargs)
    socket.close
  end

  return true
rescue Timeout::Error
  return nil
rescue SocketError, SystemCallError
  return false
end

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

Since:

  • 1.0.0



262
263
264
265
266
267
268
# File 'lib/ronin/support/network/tcp.rb', line 262

def self.send(data,host,port,**kwargs)
  connect(host,port,**kwargs) do |socket|
    socket.write(data)
  end

  return true
end

.server(port: 0, host: nil, backlog: 5) {|server| ... } ⇒ TCPServer

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

Examples:

server = TCP.server(port: 1337)

Parameters:

  • port (Integer, nil) (defaults to: 0)

    The local port to listen on.

  • host (String, nil) (defaults to: nil)

    The host to bind to.

  • backlog (Integer) (defaults to: 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



298
299
300
301
302
303
304
305
306
307
308
# File 'lib/ronin/support/network/tcp.rb', line 298

def self.server(port: 0, host: nil, backlog: 5)
  server = if host
             TCPServer.new(host.to_s,port.to_i)
           else
             TCPServer.new(port.to_i)
           end
  server.listen(backlog)

  yield server if block_given?
  return server
end

.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:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for server.

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)

Since:

  • 0.5.0



387
388
389
390
391
392
393
394
395
396
# File 'lib/ronin/support/network/tcp.rb', line 387

def self.server_loop(**kwargs)
  server_session(**kwargs) do |server|
    loop do
      client = server.accept

      yield client if block_given?
      client.close
    end
  end
end

.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:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for server.

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)

Since:

  • 1.0.0



347
348
349
350
351
# File 'lib/ronin/support/network/tcp.rb', line 347

def self.server_session(**kwargs,&block)
  server = server(**kwargs,&block)
  server.close
  return nil
end