Class: Ronin::Web::CLI::Commands::Browser Private

Inherits:
Ronin::Web::CLI::Command show all
Includes:
CommandKit::Colors, BrowserOptions
Defined in:
lib/ronin/web/cli/commands/browser.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Screenshots one or more URLs.

Usage

ronin-web browser [options] [URL]

Options

-B, --browser NAME|PATH          The browser name or path to execute
-W, --width WIDTH                Sets the width of the browser viewport (Default: 1024)
-H, --height HEIGHT              Sets the height of the browser viewport (Default: 768)
    --headless                   Run the browser in headless mode
    --visible                    Open a visible browser
-x, --x INT                      Sets the position of the browser X coordinate
-y, --y INT                      Sets the position of the browser Y coordinate
    --inject-js JS               Injects JavaScript into every page
    --inject-js-file FILE        Injects a JavaScript file into every page
    --bypass-csp                 Enables bypassing CSP
    --print-urls                 Print all requested URLs
    --print-status               Print the status of all requested URLs
    --print-requests             Print all requests sent by the browser
    --print-responses            Print responses to all requests
    --print-traffic              Print requests and responses
    --print-headers              Print headers of requests/responses
    --print-body                 Print request/response bodies
    --shell                      Starts an interactive shell
    --js-shell                   Starts an interactive JavaScript shell
-h, --help                       Print help information

Arguments

[URL]                            The initial URL to visit

Since:

  • 2.0.0

Instance Method Summary collapse

Methods included from BrowserOptions

#browser, included

Constructor Details

#initialize(**kwargs) ⇒ Browser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the ronin-web browser command.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keywords for the command.

Since:

  • 2.0.0



142
143
144
145
146
147
148
# File 'lib/ronin/web/cli/commands/browser.rb', line 142

def initialize(**kwargs)
  super(**kwargs)

  @mode = if stdout.tty? then :visible
          else                :headless
          end
end

Instance Method Details

#browser_kwargsHash{Symbol => Object}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Additional keyword arguments for Ronin::Web::Browser.new.

Returns:

  • (Hash{Symbol => Object})

Since:

  • 2.0.0



255
256
257
258
259
260
261
262
263
264
# File 'lib/ronin/web/cli/commands/browser.rb', line 255

def browser_kwargs
  kwargs = super()

  case @mode
  when :headless then kwargs[:headless] = true
  when :visible  then kwargs[:visible]  = true
  end

  return kwargs
end

#close_browserObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Close the browser.

Since:

  • 2.0.0



246
247
248
# File 'lib/ronin/web/cli/commands/browser.rb', line 246

def close_browser
  browser.quit
end

#configure_browserObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Configures the browser and registers callbacks.

Since:

  • 2.0.0



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/ronin/web/cli/commands/browser.rb', line 177

def configure_browser
  if options[:x] || options[:y]
    browser.position = {
      left: options.fetch(:x,0),
      top:  options.fetch(:y,0)
    }
  end

  browser.bypass_csp = true if options[:bypass_csp]

  if options[:inject_js_file]
    browser.inject_js(File.read(options[:inject_js_file]))
  elsif options[:inject_js]
    browser.inject_js(options[:inject_js])
  end

  if options[:print_status]
    browser.every_response(&method(:print_url_status))
  elsif options[:print_cookies]
    browser.every_response(&method(:print_cookies))
  elsif options[:print_urls]
    browser.every_url(&method(:puts))
  elsif options[:print_traffic]
    browser.every_request(&method(:print_request))
    browser.every_response(&method(:print_response))
  else
    browser.every_request(&method(:print_request))   if options[:print_requests]
    browser.every_response(&method(:print_response)) if options[:print_responses]
  end
end

#open_browser(url = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Open the browser window.

Parameters:

  • url (String, nil) (defaults to: nil)

    The optional URL to visit.

Since:

  • 2.0.0



214
215
216
# File 'lib/ronin/web/cli/commands/browser.rb', line 214

def open_browser(url=nil)
  browser.goto(url) if url
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints the request or response body.

Parameters:

  • sigil (String)

    The "sigil" representing either a request (>) or a response (<).

  • body (String)

    the request or response body.

Since:

  • 2.0.0



350
351
352
353
354
355
# File 'lib/ronin/web/cli/commands/browser.rb', line 350

def print_body(sigil,body)
  puts sigil
  response.body.each_line do |line|
    puts "#{sigil} #{line}"
  end
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints the Set-Cookie header for each HTTP response.

Parameters:

  • response (Ferrum::Network::Response)

    A response from the browser.

Since:

  • 2.0.0



363
364
365
366
367
# File 'lib/ronin/web/cli/commands/browser.rb', line 363

def print_cookies(response)
  if (set_cookie = respones.headers['set-cookie'])
    puts set_cookie
  end
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints headers.

Parameters:

  • sigil (String)

    The "sigil" representing either a request (>) or a response (<).

  • headers (Hash{String => String})

    The header names and values.

Since:

  • 2.0.0



334
335
336
337
338
# File 'lib/ronin/web/cli/commands/browser.rb', line 334

def print_headers(sigil,headers)
  headers.each do |name,value|
    puts "#{sigil} #{colors.bright_white(name)}: #{value}"
  end
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints a request from the browser.

Parameters:

  • request (Ferrum::Network::InterceptedRequest)

Since:

  • 2.0.0



289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/ronin/web/cli/commands/browser.rb', line 289

def print_request(request)
  sigil = colors.bold(colors.bright_white('>'))

  puts "#{sigil} #{colors.bold(colors.bright_cyan(request.method))} #{colors.cyan(request.url)}"

  if options[:print_headers]
    print_headers(sigil,request.headers)
  end

  if options[:print_body] && (body = request.body)
    print_body(sigil,body)
  end
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints a response.

Parameters:

  • response (Ferrum::Network::Response)

    The respones object.

Since:

  • 2.0.0



309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/ronin/web/cli/commands/browser.rb', line 309

def print_response(response)
  sigil = colors.bold(colors.bright_white('<'))

  print "#{sigil} "
  print_url_status(response)

  if options[:print_headers]
    print_headers(sigil,response.headers)
  end

  if options[:print_body]
    print_body(sigil,response.body)
  end
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints the status and URL of a response.

Parameters:

  • response (Ferrum::Network::Response)

    The respones object.

Since:

  • 2.0.0



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/ronin/web/cli/commands/browser.rb', line 272

def print_url_status(response)
  if response.status < 300
    puts "#{colors.bright_green(response.status)} #{colors.green(response.url)}"
  elsif response.status < 400
    puts "#{colors.bright_yellow(response.status)} #{colors.yellow(response.url)}"
  elsif response.status < 500
    puts "#{colors.bright_red(response.status)} #{colors.red(response.url)}"
  else
    puts "#{colors.bold(colors.bright_red(response.status))} #{colors.bold(colors.red(response.url))}"
  end
end

#run(url = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the ronin-web browser command.

Parameters:

  • url (String, nil) (defaults to: nil)

    The optional URL to visit.

Since:

  • 2.0.0



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ronin/web/cli/commands/browser.rb', line 156

def run(url=nil)
  unless (url || options[:shell] || options[:js_shell])
    print_error "must specify a URL or --shell / --js-shell"
    exit(-1)
  end

  configure_browser
  open_browser(url)

  if options[:shell] || options[:js_shell]
    start_shell
  else
    wait_until_closed
  end

  close_browser
end

#start_shellObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Starts an interactive browser shell.

Since:

  • 2.0.0



221
222
223
224
225
226
227
228
# File 'lib/ronin/web/cli/commands/browser.rb', line 221

def start_shell
  # start the shell then immediately quit the browser once exited
  if options[:js_shell]
    JSShell.start(browser)
  elsif options[:shell]
    BrowserShell.start(browser)
  end
end

#wait_until_closedObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Waits until the browser is done or if the user exits the command.

Since:

  • 2.0.0



233
234
235
236
237
238
239
240
241
# File 'lib/ronin/web/cli/commands/browser.rb', line 233

def wait_until_closed
  if @mode == :visible
    # wait for the browser window to be closed
    browser.wait_until_closed
  else
    # wait until there's no network traffic
    browser.network.wait_for_idle { browser.quit }
  end
end