Module: Ronin::Network::TCP

Included in:
Net, Mixins::TCP, SSL, Support
Defined in:
lib/ronin/network/tcp/tcp.rb,
lib/ronin/network/tcp/proxy.rb

Overview

Provides helper methods for using the TCP protocol.

Defined Under Namespace

Classes: Proxy

Instance Method Summary (collapse)

Instance Method Details

- (nil) tcp_accept(port = nil, host = nil) {|client| ... }

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

Examples:

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

Parameters:

  • port (Integer) (defaults to: nil)

    The local port to bind to.

  • host (String) (defaults to: nil)

    (‘0.0.0.0’) 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



425
426
427
428
429
430
431
432
# File 'lib/ronin/network/tcp/tcp.rb', line 425

def tcp_accept(port=nil,host=nil)
  tcp_server_session(port,host,1) do |server|
    client = server.accept

    yield client if block_given?
    client.close
  end
end

- (String) tcp_banner(host, port, local_host = nil, local_port = nil) {|banner| ... }

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.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) 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.



230
231
232
233
234
235
236
237
238
239
# File 'lib/ronin/network/tcp/tcp.rb', line 230

def tcp_banner(host,port,local_host=nil,local_port=nil)
  banner = nil

  tcp_session(host,port,local_host,local_port) do |socket|
    banner = socket.readline.strip
  end

  yield banner if block_given?
  return banner
end

- (TCPSocket) tcp_connect(host, port, local_host = nil, local_port = nil) {|socket| ... }

Creates a new TCPSocket object connected to a given host and port.

Examples:

tcp_connect('www.hackety.org',80) # => TCPSocket
tcp_connect('www.wired.com',80) do |socket|
  socket.write("GET /\n\n")

  puts socket.readlines
  socket.close
end

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (‘0.0.0.0’) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

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

See Also:



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ronin/network/tcp/tcp.rb', line 118

def tcp_connect(host,port,local_host=nil,local_port=nil)
  host       = host.to_s
  port       = port.to_i
  local_host = (local_host || '0.0.0.0').to_s
  local_port = local_port.to_i

  socket = TCPSocket.new(host,port,local_host,local_port)

  yield socket if block_given?
  return socket
end

- (Object) tcp_connect_and_send(data, host, port, local_host = nil, local_port = nil) {|socket| ... }

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.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

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



157
158
159
160
161
162
163
# File 'lib/ronin/network/tcp/tcp.rb', line 157

def tcp_connect_and_send(data,host,port,local_host=nil,local_port=nil)
  socket = tcp_connect(host,port,local_host,local_port)
  socket.write(data)

  yield socket if block_given?
  return socket
end

- (Boolean?) tcp_open?(host, port, local_host = nil, local_port = nil, timeout = nil)

Tests whether a remote TCP port is open.

Examples:

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

Using a timeout:

tcp_open?('example.com',1111,nil,nil,5)
# => nil

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

  • timeout (Integer) (defaults to: nil)

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

Since:

  • 0.5.0



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ronin/network/tcp/tcp.rb', line 63

def tcp_open?(host,port,local_host=nil,local_port=nil,timeout=nil)
  timeout ||= 5

  begin
    Timeout.timeout(timeout) do
      tcp_session(host,port,local_host,local_port)
    end

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

- (true) tcp_send(data, host, port, local_host = nil, local_port = nil)

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.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

Returns:

  • (true)

    The data was successfully sent.



270
271
272
273
274
275
276
# File 'lib/ronin/network/tcp/tcp.rb', line 270

def tcp_send(data,host,port,local_host=nil,local_port=nil)
  tcp_session(host,port,local_host,local_port) do |socket|
    socket.write(data)
  end

  return true
end

- (TCPServer) tcp_server(port = nil, host = nil, backlog = 5) {|server| ... }

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

Examples:

tcp_server(1337)

Parameters:

  • port (Integer) (defaults to: nil)

    The local port to listen on.

  • host (String) (defaults to: nil)

    (‘0.0.0.0’) The host to bind to.

  • backlog (Integer) (defaults to: 5)

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



306
307
308
309
310
311
312
313
314
315
# File 'lib/ronin/network/tcp/tcp.rb', line 306

def tcp_server(port=nil,host=nil,backlog=5)
  port = port.to_i
  host = (host || '0.0.0.0').to_s

  server = TCPServer.new(host,port)
  server.listen(backlog)

  yield server if block_given?
  return server
end

- (nil) tcp_server_loop(port = nil, host = nil) {|client| ... }

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

Examples:

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

Parameters:

  • port (Integer) (defaults to: nil)

    The local port to bind to.

  • host (String) (defaults to: nil)

    (‘0.0.0.0’) The host to bind to.

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



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

def tcp_server_loop(port=nil,host=nil)
  tcp_server_session(port,host) do |server|
    loop do
      client = server.accept

      yield client if block_given?
      client.close
    end
  end
end

- (nil) tcp_server_session(port = nil, host = nil, backlog = 5) {|server| ... }

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

Examples:

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

  client2.write(server.read_line)

  client1.close
  client2.close
end

Parameters:

  • port (Integer) (defaults to: nil)

    The local port to bind to.

  • host (String) (defaults to: nil)

    (‘0.0.0.0’) The host to bind to.

  • backlog (Integer) (defaults to: 5)

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


351
352
353
354
355
# File 'lib/ronin/network/tcp/tcp.rb', line 351

def tcp_server_session(port=nil,host=nil,backlog=5,&block)
  server = tcp_server(port,host,backlog,&block)
  server.close()
  return nil
end

- (nil) tcp_session(host, port, local_host = nil, local_port = nil) {|socket| ... }

Creates a new temporary TCPSocket object, connected to the given host and port.

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket. After the block has returned, the socket will then be closed.

Yield Parameters:

  • socket (TCPsocket)

    The newly created TCPSocket object.

Returns:

  • (nil)


192
193
194
195
196
197
198
# File 'lib/ronin/network/tcp/tcp.rb', line 192

def tcp_session(host,port,local_host=nil,local_port=nil)
  socket = tcp_connect(host,port,local_host,local_port)

  yield socket if block_given?
  socket.close
  return nil
end

- (Object) tcp_single_server(port = nil, host = nil)

Deprecated.

Deprecated as of 0.5.0. Use #tcp_accept instead.



438
439
440
# File 'lib/ronin/network/tcp/tcp.rb', line 438

def tcp_single_server(port=nil,host=nil)
  tcp_accept(port,host)
end