lib/pwn/plugins/transparent_browser.rb in pwn-0.4.667 vs lib/pwn/plugins/transparent_browser.rb in pwn-0.4.668

- old
+ new

@@ -7,10 +7,21 @@ require 'socksify' require 'openssl' require 'em/pure_ruby' require 'faye/websocket' +# Monkey Patch Watir +module Watir + # Browser Class to allow tor_obj from PWN::Plugins::Tor.start + # to populate attr_accessor :tor_obj + # This was done this way soley to maintain backwards compatibility + # with how browser_obj is returned. + class Browser + attr_accessor :tor_obj + end +end + module PWN module Plugins # This plugin rocks. Chrome, Firefox, PhantomJS, IE, REST Client, # all from the comfort of one plugin. Proxy support (e.g. Burp # Suite Professional) is completely available for all browsers @@ -21,20 +32,25 @@ @@logger = PWN::Plugins::PWNLogger.create # Supported Method Parameters:: # browser_obj1 = PWN::Plugins::TransparentBrowser.open( # browser_type: :firefox|:chrome|:headless|:rest|:websocket, - # proxy: 'optional - scheme://proxy_host:port', - # with_tor: 'optional - boolean (defaults to false)' + # proxy: 'optional - scheme://proxy_host:port || :tor', # with_devtools: 'optional - boolean (defaults to false)' # ) public_class_method def self.open(opts = {}) this_browser = nil browser_type = opts[:browser_type] proxy = opts[:proxy].to_s unless opts[:proxy].nil? - opts[:with_tor] ? (with_tor = true) : (with_tor = false) + + tor_obj = nil + if opts[:proxy] == :tor + tor_obj = PWN::Plugins::Tor.start if opts[:proxy] == :tor + proxy = "socks5://#{tor_obj[:ip]}:#{tor_obj[:port]}" + end + opts[:with_devtools] ? (with_devtools = true) : (with_devtools = false) # Let's crank up the default timeout from 30 seconds to 15 min for slow sites Watir.default_timeout = 900 @@ -75,14 +91,14 @@ # caps[:acceptInsecureCerts] = true if proxy this_profile['network.proxy.type'] = 1 this_profile['network.proxy.allow_hijacking_localhost'] = true - if with_tor + if tor_obj this_profile['network.proxy.socks_version'] = 5 - this_profile['network.proxy.socks'] = URI(proxy).host - this_profile['network.proxy.socks_port'] = URI(proxy).port + this_profile['network.proxy.socks'] = tor_obj[:ip] + this_profile['network.proxy.socks_port'] = tor_obj[:port] else this_profile['network.proxy.ftp'] = URI(proxy).host this_profile['network.proxy.ftp_port'] = URI(proxy).port this_profile['network.proxy.http'] = URI(proxy).host this_profile['network.proxy.http_port'] = URI(proxy).port @@ -108,11 +124,11 @@ switches = [] switches.push('--start-maximized') switches.push('--disable-notifications') if proxy - switches.push("--host-resolver-rules='MAP * 0.0.0.0 , EXCLUDE #{URI(proxy).host}'") if with_tor + switches.push("--host-resolver-rules='MAP * 0.0.0.0 , EXCLUDE #{tor_obj[:ip]}'") if tor_obj switches.push("--proxy-server=#{proxy}") end if with_devtools switches.push('--auto-open-devtools-for-tabs') @@ -165,14 +181,14 @@ # caps[:acceptInsecureCerts] = true if proxy this_profile['network.proxy.type'] = 1 this_profile['network.proxy.allow_hijacking_localhost'] = true - if with_tor + if tor_obj this_profile['network.proxy.socks_version'] = 5 - this_profile['network.proxy.socks'] = URI(proxy).host - this_profile['network.proxy.socks_port'] = URI(proxy).port + this_profile['network.proxy.socks'] = tor_obj[:ip] + this_profile['network.proxy.socks_port'] = tor_obj[:port] else this_profile['network.proxy.ftp'] = URI(proxy).host this_profile['network.proxy.ftp_port'] = URI(proxy).port this_profile['network.proxy.http'] = URI(proxy).host this_profile['network.proxy.http_port'] = URI(proxy).port @@ -181,11 +197,10 @@ end end options = Selenium::WebDriver::Firefox::Options.new(args: ['-headless'], accept_insecure_certs: true) options.profile = this_profile - # driver = Selenium::WebDriver.for(:firefox, capabilities: options) driver = Selenium::WebDriver.for(:firefox, options: options) this_browser = Watir::Browser.new(driver) when :headless_chrome this_profile = Selenium::WebDriver::Chrome::Profile.new @@ -196,40 +211,39 @@ switches.push('--headless') switches.push('--start-maximized') switches.push('--disable-notifications') if proxy - switches.push("--host-resolver-rules='MAP * 0.0.0.0 , EXCLUDE #{URI(proxy).host}'") if with_tor + switches.push("--host-resolver-rules='MAP * 0.0.0.0 , EXCLUDE #{tor_obj[:ip]}'") if tor_obj switches.push("--proxy-server=#{proxy}") end options = Selenium::WebDriver::Chrome::Options.new( args: switches, accept_insecure_certs: true ) options.profile = this_profile - # driver = Selenium::WebDriver.for(:chrome, capabilities: options) driver = Selenium::WebDriver.for(:chrome, options: options) this_browser = Watir::Browser.new(driver) when :rest this_browser = RestClient if proxy - if with_tor - TCPSocket.socks_server = URI(proxy).host - TCPSocket.socks_port = URI(proxy).port + if tor_obj + TCPSocket.socks_server = tor_obj[:ip] + TCPSocket.socks_port = tor_obj[:port] else this_browser.proxy = proxy end end when :websocket if proxy - if with_tor - TCPSocket.socks_server = URI(proxy).host - TCPSocket.socks_port = URI(proxy).port + if tor_obj + TCPSocket.socks_server = tor_obj[:ip] + TCPSocket.socks_port = tor_obj[:port] end proxy_opts = { origin: proxy } tls_opts = { verify_peer: false } this_browser = Faye::WebSocket::Client.new( '', @@ -245,10 +259,11 @@ else puts 'Error: browser_type only supports :firefox, :chrome, :headless, :rest, or :websocket' return nil end + this_browser.tor_obj = tor_obj if tor_obj this_browser rescue StandardError => e raise e end @@ -298,10 +313,15 @@ # ) public_class_method def self.close(opts = {}) this_browser_obj = opts[:browser_obj] + if this_browser_obj.respond_to?('tor_obj') + tor_obj = this_browser_obj.tor_obj + PWN::Plugins::Tor.stop(tor_obj: tor_obj) + end + unless this_browser_obj.to_s.include?('RestClient') # Close the browser unless this_browser_obj.nil? (thus the &) this_browser_obj&.close end nil @@ -321,11 +341,10 @@ public_class_method def self.help puts "USAGE: browser_obj1 = #{self}.open( browser_type: :firefox|:chrome|:headless_chrome|:headless_firefox|:rest|:websocket, - proxy: 'optional scheme://proxy_host:port', - with_tor: 'optional boolean (defaults to false)', + proxy: 'optional scheme://proxy_host:port || :tor', with_devtools: 'optional - boolean (defaults to false)' ) puts browser_obj1.public_methods ********************************************************