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:

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

- (String) address(connection) (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



346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/ronin/ui/cli/commands/net/proxy.rb', line 346

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

- (Object) execute

Executes the proxy command.

Since:

  • 1.5.0



185
186
187
188
189
190
191
192
193
194
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
# File 'lib/ronin/ui/cli/commands/net/proxy.rb', line 185

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

  if tcp?
    @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



391
392
393
394
395
396
397
# File 'lib/ronin/ui/cli/commands/net/proxy.rb', line 391

def print_data(data)
  if hexdump?
    @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



368
369
370
# File 'lib/ronin/ui/cli/commands/net/proxy.rb', line 368

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



381
382
383
# File 'lib/ronin/ui/cli/commands/net/proxy.rb', line 381

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

- (Network::TCP::Proxy, Network::UDP::Proxy) proxy_class (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



329
330
331
332
333
334
335
# File 'lib/ronin/ui/cli/commands/net/proxy.rb', line 329

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

- (Object) setup

Sets up the proxy command.

Since:

  • 1.5.0



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ronin/ui/cli/commands/net/proxy.rb', line 164

def setup
  super

  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