Class: BetterCap::Options

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

Overview

Parse command line arguments, set options and initialize Context accordingly.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iface) ⇒ Options

Create a BetterCap::Options class instance using the specified network interface.


30
31
32
33
34
35
36
# File 'lib/bettercap/options/options.rb', line 30

def initialize( iface )
  @core    = CoreOptions.new iface
  @spoof   = SpoofOptions.new
  @sniff   = SniffOptions.new
  @proxies = ProxyOptions.new
  @servers = ServerOptions.new
end

Instance Attribute Details

#coreObject (readonly)

Core options


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

def core
  @core
end

#proxiesObject (readonly)

Proxies related options.


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

def proxies
  @proxies
end

#serversObject (readonly)

Misc servers related options.


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

def servers
  @servers
end

#sniffObject (readonly)

Sniffing related options.


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

def sniff
  @sniff
end

#spoofObject (readonly)

Spoofing related options.


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

def spoof
  @spoof
end

Class Method Details

.parse!Object

Initialize the BetterCap::Context, parse command line arguments and update program state accordingly. Will rise a BetterCap::Error if errors occurred.


41
42
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
69
70
71
# File 'lib/bettercap/options/options.rb', line 41

def self.parse!
  ctx = Context.get

  OptionParser.new do |opts|
    opts.version = BetterCap::VERSION
    opts.banner = "Usage: bettercap [options]"

    ctx.options.core.parse!( ctx, opts )
    ctx.options.spoof.parse!( ctx, opts )
    ctx.options.sniff.parse!( ctx, opts )
    ctx.options.proxies.parse!( ctx, opts )
    ctx.options.servers.parse!( ctx, opts )

  end.parse!

  # Initialize logging system.
  Logger.init( ctx )

  if ctx.options.core.check_updates
    UpdateChecker.check
    exit
  end

  # Validate options.
  ctx.options.validate!( ctx )
  # Load firewall instance, network interface informations and detect the
  # gateway address.
  ctx.update!

  ctx
end

Instance Method Details

#get_redirections(ifconfig) ⇒ Object

Create a list of BetterCap::Firewalls::Redirection objects which are needed given the specified command line arguments.


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
# File 'lib/bettercap/options/options.rb', line 96

def get_redirections ifconfig
  redirections = []

  if @servers.dnsd or @proxies.sslstrip?
    redirections << redir( ifconfig[:ip_saddr], 53, @servers.dnsd_port )
    redirections << redir( ifconfig[:ip_saddr], 53, @servers.dnsd_port, 'UDP' )
  end

  if @proxies.proxy
    @proxies.http_ports.each do |port|
      redirections << redir( ifconfig[:ip_saddr], port, @proxies.proxy_port )
    end
  end

  if @proxies.proxy_https
    @proxies.https_ports.each do |port|
      redirections << redir( ifconfig[:ip_saddr], port, @proxies.proxy_https_port )
    end
  end

  if @proxies.tcp_proxy
    redirections << redir_single( @proxies.tcp_proxy_upstream_address, ifconfig[:ip_saddr], @proxies.tcp_proxy_upstream_port, @proxies.tcp_proxy_port )
  end

  if @proxies.custom_proxy
    @proxies.http_ports.each do |port|
      redirections << redir( @proxies.custom_proxy, port, @proxies.custom_proxy_port )
    end
  end

  if @proxies.custom_https_proxy
    @proxies.https_ports.each do |port|
      redirections << redir( @proxies.custom_https_proxy, port, @proxies.custom_https_proxy_port )
    end
  end

  @proxies.custom_redirections.each do |r|
    redirections << redir( ifconfig[:ip_saddr], r[:from], r[:to], r[:proto] )
  end

  redirections
end

#need_gateway?Boolean

Returns:

  • (Boolean)

80
81
82
# File 'lib/bettercap/options/options.rb', line 80

def need_gateway?
  ( @core.should_discover_hosts? or @spoof.enabled? )
end

#redir(address, port, to, proto = 'TCP') ⇒ Object

Helper method to create a Firewalls::Redirection object.


85
86
87
# File 'lib/bettercap/options/options.rb', line 85

def redir( address, port, to, proto = 'TCP' )
  Firewalls::Redirection.new( @core.iface, proto, nil, port, address, to )
end

#redir_single(from, address, port, to, proto = 'TCP') ⇒ Object

Helper method to create a Firewalls::Redirection object for a single address ( from ).


90
91
92
# File 'lib/bettercap/options/options.rb', line 90

def redir_single( from, address, port, to, proto = 'TCP' )
  Firewalls::Redirection.new( @core.iface, proto, from, port, address, to )
end

#starting_messageObject

Print the starting status message.


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
# File 'lib/bettercap/options/options.rb', line 140

def starting_message
  on     = ''.green
  off    = ''.red
  status = {
    'spoofing'    => ( @spoof.enabled?  ? on : off ),
    'discovery'   => ( @core.discovery? ? on : off ),
    'sniffer'     => ( @sniff.enabled?  ? on : off ),
    'tcp-proxy'   => ( @proxies.tcp_proxy ? on : off ),
    'http-proxy'  => ( @proxies.proxy ? on : off ),
    'https-proxy' => ( @proxies.proxy_https ? on : off ),
    'sslstrip'    => ( @proxies.sslstrip? ? on : off ),
    'http-server' => ( @servers.httpd ? on : off ),
    'dns-server'  => ( @proxies.sslstrip? or @servers.dnsd ? on : off )
  }

  msg = "Starting [ "
  status.each do |k,v|
    msg += "#{k}:#{v} "
  end
  msg += "] ...\n\n"

  Logger.info msg

  Logger.warn "You are running an unstable/beta version of this software, please" \
              " update to a stable one if available." if BetterCap::VERSION =~ /[\d\.+]b/
end

#validate!(ctx) ⇒ Object


73
74
75
76
77
78
# File 'lib/bettercap/options/options.rb', line 73

def validate!( ctx )
  @core.validate!
  @proxies.validate!( ctx )
  # Print starting message.
  starting_message
end