Path: blob/master/modules/post/windows/gather/enum_devices.rb
19534 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::Windows::Registry78def initialize(info = {})9super(10update_info(11info,12'Name' => 'Windows Gather Hardware Enumeration',13'Description' => %q{14Enumerate PCI hardware information from the registry. Please note this script15will run through registry subkeys such as: 'PCI', 'ACPI', 'ACPI_HAL', 'FDC', 'HID',16'HTREE', 'IDE', 'ISAPNP', 'LEGACY'', LPTENUM', 'PCIIDE', 'SCSI', 'STORAGE', 'SW',17and 'USB'; it will take time to finish. It is recommended to run this module as a18background job.19},20'License' => MSF_LICENSE,21'Author' => [ 'Brandon Perry <bperry.volatile[at]gmail.com>' ],22'Platform' => [ 'win' ],23'SessionTypes' => [ 'meterpreter' ],24'Notes' => {25'Stability' => [CRASH_SAFE],26'SideEffects' => [],27'Reliability' => []28}29)30)31end3233def list34tbl = Rex::Text::Table.new(35'Header' => 'Device Information',36'Indent' => 1,37'Columns' =>38[39'Device Description',40'Driver Version',41'Class',42'Manufacturer',43'Extra',44]45)4647keys = [48'HKLM\\SYSTEM\\ControlSet001\\Enum\\PCI\\',49'HKLM\\SYSTEM\\ControlSet001\\Enum\\ACPI\\',50'HKLM\\SYSTEM\\ControlSet001\\Enum\\ACPI_HAL\\',51'HKLM\\SYSTEM\\ControlSet001\\Enum\\FDC\\',52'HKLM\\SYSTEM\\ControlSet001\\Enum\\HID\\',53'HKLM\\SYSTEM\\ControlSet001\\Enum\\HTREE\\',54'HKLM\\SYSTEM\\ControlSet001\\Enum\\IDE\\',55'HKLM\\SYSTEM\\ControlSet001\\Enum\\ISAPNP\\',56'HKLM\\SYSTEM\\ControlSet001\\Enum\\LEGACY\\',57'HKLM\\SYSTEM\\ControlSet001\\Enum\\LPTENUM\\',58'HKLM\\SYSTEM\\ControlSet001\\Enum\\PCIIDE\\',59'HKLM\\SYSTEM\\ControlSet001\\Enum\\Root\\',60'HKLM\\SYSTEM\\ControlSet001\\Enum\\SCSI\\',61'HKLM\\SYSTEM\\ControlSet001\\Enum\\STORAGE\\',62'HKLM\\SYSTEM\\ControlSet001\\Enum\\SW\\',63'HKLM\\SYSTEM\\ControlSet001\\Enum\\USB\\',64]6566keys.each do |key|67devices = registry_enumkeys(key)6869t = []7071while !devices.nil? && !devices.empty?721.upto(3) do73t << framework.threads.spawn("Module(#{refname})", false, devices.shift) do |device|74next if device.nil?7576vprint_status("Enumerating #{device}")7778infos = registry_enumkeys(key + '\\' + device)79next if infos.nil?8081infos.each do |info|82next if info.nil?8384info_key = key + '\\' + device + '\\' + info8586desc = registry_getvaldata(info_key, 'DeviceDesc')87mfg = registry_getvaldata(info_key, 'Mfg')88device_class = registry_getvaldata(info_key, 'Class')89driver_guid = registry_getvaldata(info_key, 'Driver')90extra = ''9192if key =~ (/USB/) || key =~ (/LPTENUM/)93extra = registry_getvaldata(info_key, 'LocationInformation')94end9596if key =~ (/SCSI/) || key =~ (/\\IDE/) || key =~ (/ACPI\\/)97extra = registry_getvaldata(info_key, 'FriendlyName')98end99100desc = desc.split(';')[1] if desc =~ /^@/101mfg = mfg.split(';')[1] if mfg =~ /^@/102103desc = '' if desc.nil?104mfg = '' if mfg.nil?105device_class = '' if device_class.nil?106driver_guid = '' if driver_guid.nil?107extra = '' if extra.nil?108109next if desc.empty? && mfg.empty?110111driver_version = ''112113if (!driver_guid.nil? || !driver_guid.empty?) && (driver_guid =~ /\\/)114k = 'HKLM\\SYSTEM\\CurrentControlSet\\Control\\Class\\' + driver_guid115d = registry_getvaldata(k, 'DriverVersion')116driver_version << d if !d.nil?117end118119done = false120121tbl.rows.each do |row|122next unless (row[0] == desc) &&123(row[1] == driver_version) &&124(row[2] == device_class) &&125(row[3] == mfg) &&126(row[4] == extra)127128done = true129break130end131132tbl << [desc, driver_version, device_class, mfg, extra] if !done133end134end135t.map(&:join)136end137end138end139140results = tbl.to_s141vprint_line("\n" + results)142143path = store_loot('host.hardware', 'text/plain', session, results, 'hardware.txt', 'Host Hardware')144print_good("Results saved in: #{path}")145end146147def run148print_status("Enumerating hardware on #{sysinfo['Computer']}")149list150rescue StandardError => e151if e.to_s =~ /execution expired/i152print_error('Sorry, execution expired. Module could not finish running.')153else154print_error("An unexpected error has occurred: #{e}:\n#{e.backtrace}")155end156end157end158159160