Class: Browser

Inherits:
Object
  • Object
show all
Extended by:
Actions
Includes:
Options
Defined in:
lib/common/browser.rb,
lib/common/browser/actions.rb,
lib/common/browser/options.rb

Defined Under Namespace

Modules: Actions, Options

Constant Summary collapse

OPTIONS =
[
  :basic_auth,
  :cache_ttl,
  :max_threads,
  :user_agent,
  :proxy,
  :proxy_auth,
  :request_timeout,
  :connect_timeout,
  :cookie
]
@@instance =
nil

Instance Attribute Summary collapse

Attributes included from Options

#basic_auth, #cache_ttl, #connect_timeout, #proxy, #proxy_auth, #request_timeout, #user_agent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Actions

get, get_and_follow_location, head, post

Methods included from Options

#max_threads, #max_threads=

Constructor Details

#initialize(options = {}) ⇒ Browser


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/common/browser.rb', line 32

def initialize(options = {})
  @cache_dir   = options[:cache_dir] || CACHE_DIR + '/browser'

  # sets browser defaults
  browser_defaults
  # load config file
  conf = options[:config_file]
  load_config(conf) if conf
  # overrides defaults with user supplied values (overwrite values from config)
  override_config(options)

  unless @hydra
    @hydra = Typhoeus::Hydra.new(max_concurrency: self.max_threads)
  end

  @cache = TyphoeusCache.new(@cache_dir)
  @cache.clean

  Typhoeus::Config.cache = @cache
end

Instance Attribute Details

#cache_dirObject (readonly)

Returns the value of attribute cache_dir


25
26
27
# File 'lib/common/browser.rb', line 25

def cache_dir
  @cache_dir
end

Returns the value of attribute cookie


27
28
29
# File 'lib/common/browser.rb', line 27

def cookie
  @cookie
end

#hydraObject (readonly)

Returns the value of attribute hydra


25
26
27
# File 'lib/common/browser.rb', line 25

def hydra
  @hydra
end

#refererObject

Returns the value of attribute referer


27
28
29
# File 'lib/common/browser.rb', line 27

def referer
  @referer
end

Class Method Details

.instance(options = {}) ⇒ Browser


58
59
60
61
62
63
# File 'lib/common/browser.rb', line 58

def self.instance(options = {})
  unless @@instance
    @@instance = new(options)
  end
  @@instance
end

.resetObject


65
66
67
# File 'lib/common/browser.rb', line 65

def self.reset
  @@instance = nil
end

Instance Method Details

#browser_defaultsObject

sets browser default values


72
73
74
75
76
77
78
79
80
81
# File 'lib/common/browser.rb', line 72

def browser_defaults
  @max_threads = 20
  # 10 minutes, at this time the cache is cleaned before each scan. If this value is set to 0, the cache will be disabled
  @cache_ttl = 600
  # 2s
  @request_timeout = 2000
  # 1s
  @connect_timeout = 1000
  @user_agent = "WPScan v#{WPSCAN_VERSION} (http://wpscan.org)"
end

#forge_request(url, params = {}) ⇒ Typhoeus::Request


111
112
113
# File 'lib/common/browser.rb', line 111

def forge_request(url, params = {})
  Typhoeus::Request.new(url, merge_request_params(params))
end

#load_config(config_file = nil) ⇒ void

This method returns an undefined value.

If an option was set but is not in the new config_file it's value is kept


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/common/browser.rb', line 90

def load_config(config_file = nil)

  if File.symlink?(config_file)
    raise '[ERROR] Config file is a symlink.'
  else
    data = JSON.parse(File.read(config_file))
  end

  OPTIONS.each do |option|
    option_name = option.to_s
    unless data[option_name].nil?
      self.send(:"#{option_name}=", data[option_name])
    end
  end

end

#merge_request_params(params = {}) ⇒ Hash


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
# File 'lib/common/browser.rb', line 118

def merge_request_params(params = {})
  params = Browser.append_params_header_field(
    params,
    'User-Agent',
    @user_agent
  )

  if @proxy
    params = params.merge(proxy: @proxy)

    if @proxy_auth
      params = params.merge(proxyauth: @proxy_auth)
    end
  end

  if @basic_auth
    params = Browser.append_params_header_field(
      params,
      'Authorization',
      @basic_auth
    )
  end

  params.merge!(referer: referer)
  params.merge!(timeout: @request_timeout) if @request_timeout
  params.merge!(connecttimeout: @connect_timeout) if @connect_timeout

  # Used to enable the cache system if :cache_ttl > 0
  params.merge!(cache_ttl: @cache_ttl) unless params.has_key?(:cache_ttl)

  # Prevent infinite self redirection
  params.merge!(maxredirs: 3) unless params.has_key?(:maxredirs)

  # Disable SSL-Certificate checks
  params.merge!(ssl_verifypeer: false)
  params.merge!(ssl_verifyhost: 0)

  params.merge!(cookiejar: @cache_dir + '/cookie-jar')
  params.merge!(cookiefile: @cache_dir + '/cookie-jar')
  params.merge!(cookie: @cookie) if @cookie

  params
end