Class: Ronin::UI::CLI::Commands::Net::Proxy

Inherits:
Ronin::UI::CLI::Command show all
Defined in:
lib/ronin/ui/cli/commands/net/proxy.rb

Overview

Starts a TCP/UDP intercept proxy.

Usage

ronin net:proxy [options]

Options

-v, --[no-]verbose               Enable verbose output.
-q, --[no-]quiet                 Disable verbose output.
    --[no-]silent                Silence all output.
    --[no-]color                 Enables color output.
                                 Default: true
-t, --[no-]tcp                   TCP Proxy.
                                 Default: true
-u, --[no-]udp                   UDP Proxy.
-x, --[no-]hexdump               Enable hexdump output.
-H, --host [HOST]                Host to listen on.
                                 Default: "0.0.0.0"
-p, --port [PORT]                Port to listen on.
-s, --server [HOST[:PORT]]       Server to forward connections to.
-r, --rewrite [/REGEXP/:STRING] Rewrite rules.
    --rewrite-client [/REGEXP/:STRING]
                                 Client rewrite rules.
    --rewrite-server [/REGEXP/:STRING]
                                 Server rewrite rules.
-i, --ignore [/REGEXP/ [...]]    Ignore rules.
    --ignore-client [/REGEXP/ [...]]
                                 Client ignore rules.
    --ignore-server [/REGEXP/ [...]]
                                 Server ignore rules.
-C, --close [/REGEXP/ [...]]     Close rules.
    --close-client [/REGEXP/ [...]]
                                 Client close rules.
    --close-server [/REGEXP/ [...]]
                                 Server close rules.
-R, --reset [/REGEXP/ [...]]     Reset rules.
    --reset-client [/REGEXP/ [...]]
                                 Client reset rules.
    --reset-server [/REGEXP/ [...]]
                                 Server reset rules.

Since:

  • 1.5.0

Instance Method Summary collapse

Methods inherited from Ronin::UI::CLI::Command

argument, arguments, arguments?, #cleanup, command_name, each_argument, each_option, examples, #initialize, option, #option_parser, options, options?, run, #run, start, #start, summary, usage

Methods included from Printing

#indent, #initialize, #print_array, #print_hash, #print_section, #print_title, #puts

Constructor Details

This class inherits a constructor from Ronin::UI::CLI::Command

Instance Method Details

#address(connection) ⇒ String (protected)

Returns the address for the connection.

Parameters:

  • connection ((UDPSocket,(host, port)), TCPSocket, UDPSocket)

    The connection.

Returns:

  • (String)

    The address of the connection.

Since:

  • 1.5.0



355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/ronin/ui/cli/commands/net/proxy.rb', line 355

def address(connection)
  case connection
  when Array
    socket, (host, port) = connection

    "#{host}:#{port}"
  when TCPSocket, UDPSocket
    addrinfo = connection.peeraddr

    "#{addrinfo[3]}:#{addrinfo[1]}"
  end
end

#executeObject

Executes the proxy command.

Since:

  • 1.5.0



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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
328
329
# File 'lib/ronin/ui/cli/commands/net/proxy.rb', line 195

def execute
  @proxy = proxy_class.new(
    :port   => @port,
    :host   => @host,
    :server => [@server_host, @server_port]
  )

  case @proxy
  when Network::TCP::Proxy
    @proxy.on_client_connect do |client|
      print_outgoing client, '[connecting]'
    end

    @proxy.on_client_disconnect do |client,server|
      print_outgoing client, '[disconnecting]'
    end

    @proxy.on_server_connect do |client,server|
      print_incoming client, '[connected]'
    end

    @proxy.on_server_disconnect do |client,server|
      print_incoming client, '[disconnected]'
    end
  end

  if @reset_client
    @reset_client.each do |pattern|
      @proxy.on_client_data do |client,server,data|
        @proxy.reset! if data =~ pattern
      end
    end
  end

  if @close_client
    @close_client.each do |pattern|
      @proxy.on_client_data do |client,server,data|
        @proxy.close! if data =~ pattern
      end
    end
  end

  if @ignore_client
    @ignore_client.each do |pattern|
      @proxy.on_client_data do |client,server,data|
        @proxy.ignore! if data =~ pattern
      end
    end
  end

  if @rewrite_client
    @rewrite_client.each do |pattern,replace|
      @proxy.on_client_data do |client,server,data|
        data.gsub!(pattern,replace)
      end
    end
  end

  if @reset_server
    @reset_server.each do |pattern|
      @proxy.on_server_data do |client,server,data|
        @proxy.reset! if data =~ pattern
      end
    end
  end

  if @close_server
    @close_server.each do |pattern|
      @proxy.on_server_data do |client,server,data|
        @proxy.close! if data =~ pattern
      end
    end
  end

  if @ignore_server
    @ignore_server.each do |pattern|
      @proxy.on_server_data do |client,server,data|
        @proxy.ignore! if data =~ pattern
      end
    end
  end

  if @rewrite_server
    @rewrite_server.each do |pattern,replace|
      @proxy.on_server_data do |client,server,data|
        data.gsub!(pattern,replace)
      end
    end
  end

  if @reset
    @reset.each do |pattern|
      @proxy.on_data do |client,server,data|
        @proxy.reset! if data =~ pattern
      end
    end
  end

  if @close
    @close.each do |pattern|
      @proxy.on_data do |client,server,data|
        @proxy.close! if data =~ pattern
      end
    end
  end

  if @ignore
    @ignore.each do |pattern|
      @proxy.on_data do |client,server,data|
        @proxy.ignore! if data =~ pattern
      end
    end
  end

  if @rewrite
    @rewrite.each do |pattern,replace|
      @proxy.on_data do |client,server,data|
        data.gsub!(pattern,replace)
      end
    end
  end

  @proxy.on_client_data do |client,server,data|
    print_outgoing client
    print_data data
  end

  @proxy.on_server_data do |client,server,data|
    print_incoming client
    print_data data
  end

  print_info "Listening on #{@host}:#{@port} ..."
  @proxy.start
end

Prints data from a message.

Parameters:

  • data (String)

    The data from a message.

Since:

  • 1.5.0



400
401
402
403
404
# File 'lib/ronin/ui/cli/commands/net/proxy.rb', line 400

def print_data(data)
  if hexdump? then @hexdumper.dump(data)
  else             puts data
  end
end

Prints a connection header for an incoming event.

Parameters:

  • client ((UDPSocket,(host, port)), TCPSocket, UDPSocket)

    The client.

  • event (String) (defaults to: nil)

    The optional name of the event.

Since:

  • 1.5.0



377
378
379
# File 'lib/ronin/ui/cli/commands/net/proxy.rb', line 377

def print_incoming(client,event=nil)
  print_info "#{address(client)} <- #{@proxy} #{event}"
end

Prints a connection header for an outgoing event.

Parameters:

  • client ((UDPSocket,(host, port)), TCPSocket, UDPSocket)

    The client.

  • type (String) (defaults to: nil)

    The optional name of the event.

Since:

  • 1.5.0



390
391
392
# File 'lib/ronin/ui/cli/commands/net/proxy.rb', line 390

def print_outgoing(client,type=nil)
  print_info "#{address(client)} -> #{@proxy} #{type}"
end

#proxy_classNetwork::TCP::Proxy, Network::UDP::Proxy (protected)

Determines the Proxy class based on the --tcp or --udp options.

Returns:

  • (Network::TCP::Proxy, Network::UDP::Proxy)

    The proxy class.

Since:

  • 1.5.0



340
341
342
343
344
# File 'lib/ronin/ui/cli/commands/net/proxy.rb', line 340

def proxy_class
  if    udp? then Network::UDP::Proxy
  elsif tcp? then Network::TCP::Proxy
  end
end

#setupObject

Sets up the proxy command.

Since:

  • 1.5.0



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ronin/ui/cli/commands/net/proxy.rb', line 169

def setup
  super

  unless (tcp? || udp?)
    print_error "Must specify --tcp or --udp"
    exit -1
  end

  unless server?
    print_error "Must specify the SERVER argument"
    exit -1
  end

  @server_host, @server_port = server.split(':',2)
  @server_port = if @server_port
                   @server_port.to_i
                 end

  if hexdump?
    @hexdumper = Hexdump::Dumper.new
  end
end