Module: Ronin::Network::Mixins::TCP

Includes:
Mixin
Defined in:
lib/ronin/network/mixins/tcp.rb

Overview

Adds TCP convenience methods and connection parameters to a class.

Defines the following parameters:

  • host (String) - TCP host.
  • port (Integer) - TCP port.
  • local_host (String) - TCP local host.
  • local_port (Integer) - TCP local port.
  • server_host (String) - TCP server host.
  • server_port (Integer) - TCP server port.

Instance Method Summary collapse

Methods included from Mixin

included

Instance Method Details

#tcp_banner {|banner| ... } ⇒ String (protected)

Connects to the host and port specified by the host and port parameters, reads the banner then closes the connection.

Examples:

tcp_banner
# => "220 mx.google.com ESMTP c20sm3096959rvf.1"

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.



175
176
177
178
179
# File 'lib/ronin/network/mixins/tcp.rb', line 175

def tcp_banner(&block)
  print_debug "Grabbing banner from #{self.host}:#{self.port}"

  return ::Net.tcp_banner(self.host,self.port,self.local_host,self.local_port,&block)
end

#tcp_connect {|socket| ... } ⇒ TCPSocket (protected)

Opens a TCP connection to the host and port specified by the host and port parameters. If the local_host and local_port parameters are set, they will be used for the local host and port of the TCP connection.

Examples:

tcp_connect # => TCPSocket
tcp_connect do |sock|
  sock.write("GET /\n\n")
  puts sock.readlines
  sock.close
end

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.



101
102
103
104
105
# File 'lib/ronin/network/mixins/tcp.rb', line 101

def tcp_connect(&block)
  print_info "Connecting to #{self.host}:#{self.port} ..."

  return ::Net.tcp_connect(self.host,self.port,self.local_host,self.local_port,&block)
end

#tcp_connect_and_send(data) {|socket| ... } ⇒ TCPSocket (protected)

Connects to the host and port specified by the host and port parameters, then sends the given data.

Parameters:

  • data (String)

    The data to send through the connection.

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.



125
126
127
128
129
130
# File 'lib/ronin/network/mixins/tcp.rb', line 125

def tcp_connect_and_send(data,&block)
  print_info "Connecting to #{self.host}:#{self.port} ..."
  print_debug "Sending data: #{data.inspect}"

  return ::Net.tcp_connect_and_send(data,self.host,self.port,self.local_host,self.local_port,&block)
end

#tcp_send(data) ⇒ true (protected)

Connects to the host and port specified by the host and port parameters, sends the given data and then disconnects.

Examples:

buffer = "GET /" + ('A' * 4096) + "\n\r"
Net.tcp_send(buffer)
# => true

Returns:

  • (true)

    The data was successfully sent.



195
196
197
198
199
200
201
202
203
# File 'lib/ronin/network/mixins/tcp.rb', line 195

def tcp_send(data)
  print_info "Connecting to #{self.host}:#{self.port} ..."
  print_debug "Sending data: #{data.inspect}"

  ::Net.tcp_send(data,self.host,self.port,self.local_host,self.local_port)

  print_info "Disconnected from #{self.host}:#{self.port}"
  return true
end

#tcp_server {|server| ... } ⇒ TCPServer (protected)

Creates a new TCPServer object listening on the server_host and server_port parameters.

Examples:

tcp_server

Yields:

  • (server)

    The given block will be passed the newly created server.

Yield Parameters:

  • server (TCPServer)

    The newly created server.

Returns:

  • (TCPServer)

    The newly created server.



223
224
225
226
227
228
229
230
231
# File 'lib/ronin/network/mixins/tcp.rb', line 223

def tcp_server(&block)
  if self.server_host
    print_info "Listening on #{self.server_host}:#{self.server_port} ..."
  else
    print_info "Listening on #{self.server_port} ..."
  end

  return ::Net.tcp_server(self.server_port,self.server_host,&block)
end

#tcp_server_session {|server| ... } ⇒ nil (protected)

Creates a new temporary TCPServer object listening on the server_host and server_port parameters.

Examples:

tcp_server_session do |server|
  client1 = server.accept
  client2 = server.accept

  client2.write(server.read_line)

  client1.close
  client2.close
end

Yields:

  • (server)

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

Yield Parameters:

  • server (TCPServer)

    The newly created server.

Returns:

  • (nil)


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/ronin/network/mixins/tcp.rb', line 259

def tcp_server_session(&block)
  if self.server_host
    print_info "Listening on #{self.server_host}:#{self.server_port} ..."
  else
    print_info "Listening on #{self.server_port} ..."
  end

  ::Net.tcp_server_session(&block)

  if self.server_host
    print_info "Closed #{self.server_host}:#{self.server_port}"
  else
    print_info "Closed #{self.server_port}"
  end

  return nil
end

#tcp_session {|socket| ... } ⇒ nil (protected)

Creates a TCP session to the host and port specified by the host and port parameters.

Yields:

  • (socket)

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

Yield Parameters:

  • socket (TCPsocket)

    The newly created TCPSocket object.

Returns:

  • (nil)


147
148
149
150
151
152
153
154
# File 'lib/ronin/network/mixins/tcp.rb', line 147

def tcp_session(&block)
  print_info "Connecting to #{self.host}:#{self.port} ..."

  Net.tcp_session(self.host,self.port,self.local_host,self.local_port,&block)

  print_info "Disconnected from #{self.host}:#{self.port}"
  return nil
end

#tcp_single_server {|client| ... } ⇒ nil (protected)

Creates a new temporary TCPServer object listening on server_host and server_port parameters. The TCPServer will accepting one client, pass the newly connected client to a given block, disconnects the client and stops listening.

Examples:

tcp_single_server do |client|
  client.puts 'lol'
end

Yields:

  • (client)

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

Yield Parameters:

  • client (TCPSocket)

    The newly connected client.

Returns:

  • (nil)


301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/ronin/network/mixins/tcp.rb', line 301

def tcp_single_server(&block)
  if self.server_host
    print_info "Listening on #{self.server_host}:#{self.server_port} ..."
  else
    print_info "Listening on #{self.server_port} ..."
  end

  ::Net.tcp_single_server do |client|
    client_addr = client.peeraddr
    client_host = (client_addr[2] || client_addr[3])
    client_port = client_addr[1]

    print_info "Client connected #{client_host}:#{client_port}"

    yield client if block_given?

    print_info "Disconnecting client #{client_host}:#{client_port}"
  end

  if self.server_host
    print_info "Closed #{self.server_host}:#{self.server_port}"
  else
    print_info "Closed #{self.server_port}"
  end

  return nil
end