2013-07-14 12:30:54 +00:00
|
|
|
##
|
2013-10-15 18:50:46 +00:00
|
|
|
# This module requires Metasploit: http//metasploit.com/download
|
|
|
|
# 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)
|
|
|
|
if prefetch_file.empty? or prefetch_file.nil?
|
|
|
|
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-08-30 21:28:54 +00:00
|
|
|
pf_filename = prefetch_file[name_offset..name_offset+60]
|
|
|
|
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-08-30 21:28:54 +00:00
|
|
|
run_count = prefetch_file[runcount_offset..runcount_offset+4].unpack('L*')[0].to_s
|
2013-11-17 20:10:55 +00:00
|
|
|
|
|
|
|
# Then the filepath hash
|
2013-08-30 21:28:54 +00:00
|
|
|
path_hash = prefetch_file[hash_offset..hash_offset+4].unpack('h8')[0].reverse.upcase.to_s
|
2013-11-17 20:10:55 +00:00
|
|
|
|
|
|
|
# Last we get the latest execution time
|
2013-11-17 20:26:30 +00:00
|
|
|
filetime_a = prefetch_file[filetime_offset..(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.
|
|
|
|
# First we'll use specific offsets for finding out the location
|
|
|
|
# and length of the filepath.
|
|
|
|
filepath = []
|
|
|
|
fpath_offset = prefetch_file[0x64..0x68].unpack('h4')[0].reverse.to_i(16)
|
|
|
|
fpath_length = prefetch_file[0x68..0x6C].unpack('h4')[0].reverse.to_i(16)
|
|
|
|
filepath_data = prefetch_file[fpath_offset..(fpath_offset+fpath_length)]
|
|
|
|
if not filepath_data.nil? or not filepath_data.empty?
|
|
|
|
r_filename = name.gsub(/\0/, '')
|
|
|
|
fpath_data_array = filepath_data.split("\x00\x00\x00")
|
|
|
|
fpath_data_array.each do |path|
|
|
|
|
fpath_full_file = path.split("\\")
|
|
|
|
fpath_file = fpath_full_file.last
|
|
|
|
if not fpath_file.nil?
|
|
|
|
fpath_fname = fpath_file.gsub(/\0/, '')
|
|
|
|
if r_filename == fpath_fname
|
|
|
|
fpath_path = path.gsub(/\0/, '')
|
|
|
|
if not fpath_path.empty?
|
|
|
|
filepath = fpath_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-08-30 21:28:54 +00:00
|
|
|
end
|
|
|
|
end
|
2013-11-17 20:10:55 +00:00
|
|
|
return [last_exec, path_hash, run_count, name, filepath]
|
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)
|
|
|
|
sys_root = expand_path("%SYSTEMROOT%")
|
|
|
|
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
|