Class: BetterCap::ProxyOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/bettercap/options/proxy_options.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProxyOptions

Returns a new instance of ProxyOptions.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bettercap/options/proxy_options.rb', line 56

def initialize
  @http_ports = [ 80 ]
  @https_ports = [ 443 ]
  @proxy = false
  @proxy_https = false
  @proxy_port = 8080
  @proxy_https_port = 8083
  @proxy_pem_file = nil
  @proxy_module = nil
  @sslstrip = true

  @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

Instance Attribute Details

#custom_https_proxyObject

Custom HTTPS transparent proxy address.



50
51
52
# File 'lib/bettercap/options/proxy_options.rb', line 50

def custom_https_proxy
  @custom_https_proxy
end

#custom_https_proxy_portObject

Custom HTTPS transparent proxy port.



52
53
54
# File 'lib/bettercap/options/proxy_options.rb', line 52

def custom_https_proxy_port
  @custom_https_proxy_port
end

#custom_proxyObject

Custom HTTP transparent proxy address.



46
47
48
# File 'lib/bettercap/options/proxy_options.rb', line 46

def custom_proxy
  @custom_proxy
end

#custom_proxy_portObject

Custom HTTP transparent proxy port.



48
49
50
# File 'lib/bettercap/options/proxy_options.rb', line 48

def custom_proxy_port
  @custom_proxy_port
end

#custom_redirectionsObject

Custom list of redirections.



54
55
56
# File 'lib/bettercap/options/proxy_options.rb', line 54

def custom_redirections
  @custom_redirections
end

#http_portsObject

List of HTTP ports, [ 80 ] by default.



24
25
26
# File 'lib/bettercap/options/proxy_options.rb', line 24

def http_ports
  @http_ports
end

#https_portsObject

List of HTTPS ports, [ 443 ] by default.



28
29
30
# File 'lib/bettercap/options/proxy_options.rb', line 28

def https_ports
  @https_ports
end

#proxyObject

If true, HTTP transparent proxy will be enabled.



18
19
20
# File 'lib/bettercap/options/proxy_options.rb', line 18

def proxy
  @proxy
end

#proxy_httpsObject

If true, HTTPS transparent proxy will be enabled.



20
21
22
# File 'lib/bettercap/options/proxy_options.rb', line 20

def proxy_https
  @proxy_https
end

#proxy_https_portObject

HTTPS proxy port.



26
27
28
# File 'lib/bettercap/options/proxy_options.rb', line 26

def proxy_https_port
  @proxy_https_port
end

#proxy_moduleObject

File name of the transparent proxy module to load.



32
33
34
# File 'lib/bettercap/options/proxy_options.rb', line 32

def proxy_module
  @proxy_module
end

#proxy_pem_fileObject

File name of the PEM certificate to use for the HTTPS proxy.



30
31
32
# File 'lib/bettercap/options/proxy_options.rb', line 30

def proxy_pem_file
  @proxy_pem_file
end

#proxy_portObject

HTTP proxy port.



22
23
24
# File 'lib/bettercap/options/proxy_options.rb', line 22

def proxy_port
  @proxy_port
end

#sslstripObject

If true, sslstrip is enabled.



34
35
36
# File 'lib/bettercap/options/proxy_options.rb', line 34

def sslstrip
  @sslstrip
end

#tcp_proxyObject

If true, TCP proxy will be enabled.



36
37
38
# File 'lib/bettercap/options/proxy_options.rb', line 36

def tcp_proxy
  @tcp_proxy
end

#tcp_proxy_moduleObject

TCP proxy module to load.



44
45
46
# File 'lib/bettercap/options/proxy_options.rb', line 44

def tcp_proxy_module
  @tcp_proxy_module
end

#tcp_proxy_portObject

TCP proxy local port.



38
39
40
# File 'lib/bettercap/options/proxy_options.rb', line 38

def tcp_proxy_port
  @tcp_proxy_port
end

#tcp_proxy_upstream_addressObject

TCP proxy upstream server address.



40
41
42
# File 'lib/bettercap/options/proxy_options.rb', line 40

def tcp_proxy_upstream_address
  @tcp_proxy_upstream_address
end

#tcp_proxy_upstream_portObject

TCP proxy upstream server port.



42
43
44
# File 'lib/bettercap/options/proxy_options.rb', line 42

def tcp_proxy_upstream_port
  @tcp_proxy_upstream_port
end

Class Method Details

.parse_ports(value) ⇒ Object

Parse a comma separated list of ports and return an array containing only valid ports, raise BetterCap::Error if that array is empty.

Raises:



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/bettercap/options/proxy_options.rb', line 208

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

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


253
254
255
# File 'lib/bettercap/options/proxy_options.rb', line 253

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

#has_proxy_module?Boolean

Return true if a proxy module was specified, otherwise false.

Returns:

  • (Boolean)


245
246
247
# File 'lib/bettercap/options/proxy_options.rb', line 245

def has_proxy_module?
  !@proxy_module.nil?
end

#parse!(ctx, opts) ⇒ Object



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
109
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/bettercap/options/proxy_options.rb', line 82

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)
    Proxy::TCP::Module.load( @tcp_proxy_module )
  end

  opts.on( '--tcp-proxy-port PORT', "Set local TCP proxy port, default to #{@tcp_proxy_port.to_s.yellow} ." ) do |v|
    @tcp_proxy      = true
    @tcp_proxy_port = v.to_i
  end

  opts.on( '--tcp-proxy-upstream-address ADDRESS', 'Set TCP proxy upstream server address.' ) do |v|
    raise BetterCap::Error, 'Invalid TCP proxy upstream server address specified.' unless Network::Validator.is_ip?(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|
    @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|
    @proxy = true
    @proxy_port = v.to_i
  end

  opts.on( '--no-sslstrip', 'Disable SSLStrip.' ) do
    @sslstrip = false
  end

  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)
  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 = self.parse_ports( v )
  end

  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|
    @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 = self.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|
    @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|
    @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

#parse_custom_proxy!(value, https = false) ⇒ Object

Setter for the #custom_proxy or #custom_https_proxy attribute, will raise a BetterCap::Error if value is not a valid IP address.

Raises:



222
223
224
225
226
227
228
229
# File 'lib/bettercap/options/proxy_options.rb', line 222

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_redirection!(rule) ⇒ Object

Parse a custom redirection rule.



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/bettercap/options/proxy_options.rb', line 232

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

#sslstrip?Boolean

Returns:

  • (Boolean)


249
250
251
# File 'lib/bettercap/options/proxy_options.rb', line 249

def sslstrip?
  @proxy and @sslstrip
end

#validate!(ctx) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/bettercap/options/proxy_options.rb', line 187

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 @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