proxy_options.rb 11.1 KB
Newer Older
1 2 3 4 5 6 7
# encoding: UTF-8
=begin

BETTERCAP

Author : Simone 'evilsocket' Margaritelli
Email  : evilsocket@gmail.com
Sophie Brun's avatar
Sophie Brun committed
8
Blog   : https://www.evilsocket.net/
9 10 11 12 13 14 15 16 17 18 19 20

This project is released under the GPL 3 license.

=end

module BetterCap

class ProxyOptions
  # If true, HTTP transparent proxy will be enabled.
  attr_accessor :proxy
  # If true, HTTPS transparent proxy will be enabled.
  attr_accessor :proxy_https
21 22
  # If set, only this address will be redirected to the HTTP(S) proxiy.
  attr_accessor :proxy_upstream_address
23 24 25 26 27 28 29 30 31 32 33 34 35 36
  # HTTP proxy port.
  attr_accessor :proxy_port
  # List of HTTP ports, [ 80 ] by default.
  attr_accessor :http_ports
  # HTTPS proxy port.
  attr_accessor :proxy_https_port
  # List of HTTPS ports, [ 443 ] by default.
  attr_accessor :https_ports
  # File name of the PEM certificate to use for the HTTPS proxy.
  attr_accessor :proxy_pem_file
  # File name of the transparent proxy module to load.
  attr_accessor :proxy_module
  # If true, sslstrip is enabled.
  attr_accessor :sslstrip
37 38
  # If true, direct connections to the IP of this machine will be allowed.
  attr_accessor :allow_local_connections
39 40
  # If true, log HTTP responses too.
  attr_accessor :log_response
Sophie Brun's avatar
Sophie Brun committed
41 42
  # If true, suppress HTTP requests logs.
  attr_accessor :no_http_logs
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  # If true, TCP proxy will be enabled.
  attr_accessor :tcp_proxy
  # TCP proxy local port.
  attr_accessor :tcp_proxy_port
  # TCP proxy upstream server address.
  attr_accessor :tcp_proxy_upstream_address
  # TCP proxy upstream server port.
  attr_accessor :tcp_proxy_upstream_port
  # TCP proxy module to load.
  attr_accessor :tcp_proxy_module
  # Custom HTTP transparent proxy address.
  attr_accessor :custom_proxy
  # Custom HTTP transparent proxy port.
  attr_accessor :custom_proxy_port
  # Custom HTTPS transparent proxy address.
  attr_accessor :custom_https_proxy
  # Custom HTTPS transparent proxy port.
  attr_accessor :custom_https_proxy_port
  # Custom list of redirections.
  attr_accessor :custom_redirections

  def initialize
    @http_ports = [ 80 ]
    @https_ports = [ 443 ]
    @proxy = false
    @proxy_https = false
69
    @proxy_upstream_address = nil
70 71 72 73 74
    @proxy_port = 8080
    @proxy_https_port = 8083
    @proxy_pem_file = nil
    @proxy_module = nil
    @sslstrip = true
75
    @allow_local_connections = false
76
    @log_response = false
Sophie Brun's avatar
Sophie Brun committed
77
    @no_http_logs = false
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

    @tcp_proxy = false
    @tcp_proxy_port = 2222
    @tcp_proxy_upstream_address = nil
    @tcp_proxy_upstream_port = nil
    @tcp_proxy_module = nil

    @custom_proxy = nil
    @custom_proxy_port = 8080

    @custom_https_proxy = nil
    @custom_https_proxy_port = 8083

    @custom_redirections = []
  end

  def parse!( ctx, opts )
    opts.separator ""
    opts.separator "PROXYING:".bold
    opts.separator ""

    opts.separator ""
    opts.separator "  TCP:"
    opts.separator ""

    opts.on( '--tcp-proxy', 'Enable TCP proxy ( requires other --tcp-proxy-* options to be specified ).' ) do
      @tcp_proxy = true
    end

    opts.on( '--tcp-proxy-module MODULE', "Ruby TCP proxy module to load." ) do |v|
      @tcp_proxy_module = File.expand_path(v)
Sophie Brun's avatar
Sophie Brun committed
109
      Proxy::TCP::Module.load( @tcp_proxy_module, opts )
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    end

    opts.on( '--tcp-proxy-port PORT', "Set local TCP proxy port, default to #{@tcp_proxy_port.to_s.yellow} ." ) do |v|
      raise BetterCap::Error, "Invalid port '#{v}' specified." unless Network::Validator.is_valid_port?(v)
      @tcp_proxy      = true
      @tcp_proxy_port = v.to_i
    end

    opts.on( '--tcp-proxy-upstream ADDRESS:PORT', 'Set TCP proxy upstream server address and port.' ) do |v|
      if v =~ /^(.+):(\d+)$/
        address = $1
        port    = $2
      else
        raise BetterCap::Error, "Invalid address and port specified, the correct syntax is ADDRESS:PORT ( i.e. 192.168.1.2:22 )."
      end

      address, port = validate_address address, port

      @tcp_proxy                  = true
      @tcp_proxy_upstream_address = address
      @tcp_proxy_upstream_port    = port.to_i
    end

    opts.on( '--tcp-proxy-upstream-address ADDRESS', 'Set TCP proxy upstream server address.' ) do |v|
      v, _ = validate_address v

      @tcp_proxy                  = true
      @tcp_proxy_upstream_address = v
    end

    opts.on( '--tcp-proxy-upstream-port PORT', 'Set TCP proxy upstream server port.' ) do |v|
      raise BetterCap::Error, "Invalid port '#{v}' specified." unless Network::Validator.is_valid_port?(v)
      @tcp_proxy               = true
      @tcp_proxy_upstream_port = v.to_i
    end

    opts.separator "  HTTP:"
    opts.separator ""

    opts.on( '--proxy', "Enable HTTP proxy and redirects all HTTP requests to it, default to #{'false'.yellow}." ) do
      @proxy = true
    end

    opts.on( '--proxy-port PORT', "Set HTTP proxy port, default to #{@proxy_port.to_s.yellow}." ) do |v|
      raise BetterCap::Error, "Invalid port '#{v}' specified." unless Network::Validator.is_valid_port?(v)
      @proxy = true
      @proxy_port = v.to_i
    end

159 160 161 162 163
    opts.on( '--allow-local-connections', "Allow direct connections to the proxy instance, default to #{@allow_local_connections.to_s.yellow}." ) do |v|
      @proxy = true
      @allow_local_connections = true
    end

164 165 166 167 168
    opts.on( '--no-sslstrip', 'Disable SSLStrip.' ) do
      @proxy    = true
      @sslstrip = false
    end

169 170 171 172
    opts.on( '--log-http-response', 'Log HTTP responses.' ) do
      @log_response = true
    end

Sophie Brun's avatar
Sophie Brun committed
173 174 175 176
    opts.on( '--no-http-logs', 'Suppress HTTP requests and responses logs.' ) do
      @no_http_logs = true
    end

177 178 179 180 181 182 183 184 185
    opts.on( '--proxy-module MODULE', "Ruby proxy module to load, either a custom file or one of the following: #{Proxy::HTTP::Module.available.map{|x| x.yellow}.join(', ')}." ) do |v|
      Proxy::HTTP::Module.load(ctx, opts, v)
      @proxy = true
    end

    opts.on( '--http-ports PORT1,PORT2', "Comma separated list of HTTP ports to redirect to the proxy, default to #{@http_ports.map{|x| x.to_s.yellow }.join(', ')}." ) do |v|
      @http_ports = ProxyOptions.parse_ports( v )
    end

186 187 188 189 190
    opts.on( '--proxy-upstream-address ADDRESS', 'If set, only requests coming from this server address will be redirected to the HTTP/HTTPS proxies.' ) do |v|
      v, _ = validate_address v
      @proxy_upstream_address = v
    end

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 319 320 321 322 323 324 325 326
    opts.separator ""
    opts.separator "  HTTPS:"
    opts.separator ""

    opts.on( '--proxy-https', "Enable HTTPS proxy and redirects all HTTPS requests to it, default to #{'false'.yellow}." ) do
      @proxy_https = true
    end

    opts.on( '--proxy-https-port PORT', "Set HTTPS proxy port, default to #{@proxy_https_port.to_s.yellow}." ) do |v|
      raise BetterCap::Error, "Invalid port '#{v}' specified." unless Network::Validator.is_valid_port?(v)
      @proxy_https = true
      @proxy_https_port = v.to_i
    end

    opts.on( '--proxy-pem FILE', "Use a custom PEM CA certificate file for the HTTPS proxy, default to #{Proxy::HTTP::SSL::Authority::DEFAULT.yellow} ." ) do |v|
      @proxy_https = true
      @proxy_pem_file = File.expand_path v
    end

    opts.on( '--https-ports PORT1,PORT2', "Comma separated list of HTTPS ports to redirect to the proxy, default to #{@https_ports.map{|x| x.to_s.yellow }.join(', ')}." ) do |v|
      @https_ports = ProxyOptions.parse_ports( v )
    end

    opts.separator ""
    opts.separator "  CUSTOM:"
    opts.separator ""

    opts.on( '--custom-proxy ADDRESS', 'Use a custom HTTP upstream proxy instead of the builtin one.' ) do |v|
      parse_custom_proxy!(v)
    end

    opts.on( '--custom-proxy-port PORT', "Specify a port for the custom HTTP upstream proxy, default to #{@custom_proxy_port.to_s.yellow}." ) do |v|
      raise BetterCap::Error, "Invalid port '#{v}' specified." unless Network::Validator.is_valid_port?(v)
      @custom_proxy_port = v.to_i
    end

    opts.on( '--custom-https-proxy ADDRESS', 'Use a custom HTTPS upstream proxy instead of the builtin one.' ) do |v|
      parse_custom_proxy!( v, true )
    end

    opts.on( '--custom-https-proxy-port PORT', "Specify a port for the custom HTTPS upstream proxy, default to #{@custom_https_proxy_port.to_s.yellow}." ) do |v|
      raise BetterCap::Error, "Invalid port '#{v}' specified." unless Network::Validator.is_valid_port?(v)
      @custom_https_proxy_port = v.to_i
    end

    opts.on( '--custom-redirection RULE', "Apply a custom port redirection, the format of the rule is #{'PROTOCOL ORIGINAL_PORT NEW_PORT'.yellow}. For instance #{'TCP 21 2100'.yellow} will redirect all TCP traffic going to port 21, to port 2100." ) do |v|
      parse_redirection!( v )
    end
  end

  def validate!( ctx )
    if @tcp_proxy
      raise BetterCap::Error, "No TCP proxy port specified ( --tcp-proxy-port PORT )." if @tcp_proxy_port.nil?
      raise BetterCap::Error, "No TCP proxy upstream server address specified ( --tcp-proxy-upstream-address ADDRESS )." if @tcp_proxy_upstream_address.nil?
      raise BetterCap::Error, "No TCP proxy upstream server port specified ( --tcp-proxy-upstream-port PORT )." if @tcp_proxy_upstream_port.nil?
    end

    if @proxy and @sslstrip and ctx.options.servers.dnsd
      raise BetterCap::Error, "SSL Stripping and builtin DNS server are mutually exclusive features, " \
                              "either use the --no-sslstrip option or remove the --dns option."
    end

    if has_proxy_module? and ( !@proxy and !@proxy_https )
      raise BetterCap::Error, "A proxy module was specified but none of the HTTP or HTTPS proxies are " \
                              "enabled, specify --proxy or --proxy-https options."
    end

  end

  # Parse a comma separated list of ports and return an array containing only
  # valid ports, raise BetterCap::Error if that array is empty.
  def self.parse_ports(value)
    ports = []
    value.split(",").each do |v|
      v = v.strip.to_i
      if v > 0 and v <= 65535
        ports << v
      end
    end
    raise BetterCap::Error, 'Invalid ports specified.' if ports.empty?
    ports
  end

  # Setter for the #custom_proxy or #custom_https_proxy attribute, will raise a
  # BetterCap::Error if +value+ is not a valid IP address.
  def parse_custom_proxy!(value, https=false)
    raise BetterCap::Error, 'Invalid custom HTTP upstream proxy address specified.' unless Network::Validator.is_ip?(value)
    if https
      @custom_https_proxy = value
    else
      @custom_proxy = value
    end
  end

  # Parse a custom redirection rule.
  def parse_redirection!(rule)
    if rule =~ /^((TCP)|(UDP))\s+(\d+)\s+(\d+)$/i
      @custom_redirections << {
        :proto => $1.upcase,
        :from  => $4.to_i,
        :to    => $5.to_i
      }
    else
      raise BetterCap::Error, 'Invalid custom redirection rule specified.'
    end
  end

  # Return true if a proxy module was specified, otherwise false.
  def has_proxy_module?
    !@proxy_module.nil?
  end

  def sslstrip?
    @proxy and @sslstrip
  end

  def any?
    @proxy or @proxy_https or @tcp_proxy or @custom_proxy
  end

  def validate_address( address, port = nil )
    unless Network::Validator.is_ip?(address)
      begin
        address = IPSocket.getaddress address
      rescue SocketError
        raise BetterCap::Error, "Could not resolve '#{address}' to a valid ip address."
      end
    end

    raise BetterCap::Error, "Invalid port '#{port}' specified." unless port.nil? or Network::Validator.is_valid_port?(port)

    [ address, port ]
  end
end

end