lib/pwn/plugins/transparent_browser.rb in pwn-0.5.192 vs lib/pwn/plugins/transparent_browser.rb in pwn-0.5.193

- old
+ new

@@ -40,11 +40,10 @@ # Supported Method Parameters:: # browser_obj1 = PWN::Plugins::TransparentBrowser.open( # browser_type: 'optional - :firefox|:chrome|:headless|:rest|:websocket (defaults to :chrome)', # proxy: 'optional - scheme://proxy_host:port || tor (defaults to nil)', # devtools: 'optional - boolean (defaults to true)', - # url: 'optional - URL to navigate to after opening browser (Defaults to about:about#RANDID)' # ) public_class_method def self.open(opts = {}) browser_type = opts[:browser_type] ||= :chrome proxy = opts[:proxy].to_s unless opts[:proxy].nil? @@ -61,12 +60,10 @@ devtools_supported = %i[chrome headless_chrome firefox headless_firefox headless] devtools = opts[:devtools] ||= false devtools = true if devtools_supported.include?(browser_type) && devtools - url = opts[:url] ||= "about:about##{SecureRandom.hex(8)}" - # Let's crank up the default timeout from 30 seconds to 15 min for slow sites Watir.default_timeout = 900 args = [] args.push('--start-maximized') @@ -128,11 +125,11 @@ this_profile['network.proxy.ssl'] = URI(proxy).host this_profile['network.proxy.ssl_port'] = URI(proxy).port end end - args.push('--devtools') if devtools + # args.push('--devtools') if devtools options = Selenium::WebDriver::Firefox::Options.new( args: args, accept_insecure_certs: true ) @@ -150,11 +147,11 @@ args.push("--host-resolver-rules='MAP * 0.0.0.0 , EXCLUDE #{tor_obj[:ip]}'") if tor_obj args.push("--proxy-server=#{proxy}") end if devtools - args.push('--auto-open-devtools-for-tabs') + # args.push('--auto-open-devtools-for-tabs') args.push('--disable-hang-monitor') end options = Selenium::WebDriver::Chrome::Options.new( args: args, @@ -256,11 +253,10 @@ TCPSocket.socks_port = tor_obj[:port] else browser_obj[:browser].proxy = proxy end end - puts browser_obj[:browser].get(url).body if url when :websocket if proxy if tor_obj TCPSocket.socks_server = tor_obj[:ip] @@ -283,14 +279,10 @@ puts 'Error: browser_type only supports :firefox, :chrome, :headless, :headless_chrome, :headless_firefox, :rest, :websocket' return nil end if devtools_supported.include?(browser_type) - rand_tab = SecureRandom.hex(8) - browser_obj[:browser].goto(url) - browser_obj[:browser].execute_script("document.title = '#{rand_tab}'") - if devtools driver = browser_obj[:browser].driver browser_obj[:devtools] = driver.devtools browser_obj[:devtools].send_cmd('DOM.enable') @@ -308,12 +300,12 @@ end browser_obj[:bidi] = driver.bidi jmp_devtools_panel(browser_obj: browser_obj, panel: :elements) - browser_obj[:browser].send_keys(:escape) end + new_tab(browser_obj: browser_obj, first_tab: true) end browser_obj rescue StandardError => e raise e @@ -477,36 +469,33 @@ end # Supported Method Parameters:: # tab = PWN::Plugins::TransparentBrowser.new_tab( # browser_obj: 'required - browser_obj returned from #open method)', - # url: 'optional - URL to navigate to after opening new tab (Defaults to nil)' + # first_tab: 'optional - boolean to indicate if this is the first tab (Defaults to false)' # ) public_class_method def self.new_tab(opts = {}) browser_obj = opts[:browser_obj] verify_devtools_browser(browser_obj: browser_obj) - url = opts[:url] + first_tab = opts[:first_tab] ||= false - firefox_types = %i[firefox headless_firefox] browser = browser_obj[:browser] browser_type = browser_obj[:type] devtools = browser_obj[:devtools] - browser.execute_script('window.open()') - jmp_tab(browser_obj: browser_obj, keyword: 'about:blank') - rand_tab = SecureRandom.hex(8) - if url.nil? - browser.goto("about:about##{rand_tab}") - browser.execute_script("document.title = '#{rand_tab}'") + unless first_tab + browser.execute_script('window.open()') + jmp_tab(browser_obj: browser_obj, keyword: 'about:blank') end - # Open the DevTools for Firefox, Chrome opens them automatically - browser.send_keys(:f12) if firefox_types.include?(browser_type) - # Open Console drawer if DevTools are open - browser.send_keys(:escape) unless devtools.nil? - browser.goto(url) unless url.nil? + rand_tab = SecureRandom.hex(8) + url = "about:about##{rand_tab}" + browser.goto(url) + browser.execute_script("document.title = '#{rand_tab}'") + toggle_devtools(browser_obj: browser_obj) if devtools + { title: browser.title, url: browser.url, state: :active } rescue StandardError => e raise e end @@ -658,12 +647,18 @@ public_class_method def self.toggle_devtools(opts = {}) browser_obj = opts[:browser_obj] verify_devtools_browser(browser_obj: browser_obj) + total_tabs = list_tabs(browser_obj: browser_obj).length + browser = browser_obj[:browser] browser.send_keys(:f12) + if total_tabs < 2 + sleep 1 + browser.send_keys(:escape) + end rescue StandardError => e raise e end # Supported Method Parameters:: @@ -695,21 +690,28 @@ hotkey.push('c') if firefox_types.include?(browser_type) when :console hotkey.push('j') if chrome_types.include?(browser_type) hotkey.push('k') if firefox_types.include?(browser_type) when :debugger, :sources - hotkey.push('z') if firefox_types.include?(browser_type) + if firefox_types.include?(browser_type) + # If we're in the console, we need to switch to the inspector first + jmp_devtools_panel(browser_obj: browser_obj, panel: :inspector) + sleep 1 + hotkey.push('z') if firefox_types.include?(browser_type) + end when :network hotkey.push('e') if firefox_types.include?(browser_type) else raise 'ERROR: panel parameter must be :elements|:inspector|:console|:debugger|:sources|:network' end # Have to call twice for Chrome, otherwise devtools stays closed - browser.body.click + browser_obj[:browser].send_keys(:escape) + # browser.body.click! browser.send_keys(hotkey) browser.send_keys(hotkey) if chrome_types.include?(browser_type) + browser.send_keys(:escape) rescue StandardError => e raise e end # Supported Method Parameters:: @@ -865,10 +867,10 @@ keyword: 'required - keyword in title or url used to switch tabs' ) tab = #{self}.new_tab( browser_obj: 'required - browser_obj returned from #open method)', - url: 'optional - URL to navigate to after opening new tab (Defaults to nil)' + first_tab: 'optional - boolean to indicate if this is the first tab (Defaults to false)' ) tab = #{self}.close_tab( browser_obj: 'required - browser_obj returned from #open method)', keyword: 'required - keyword in title or url used to close tabs'