Mirror of metasploit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

enum_devices.rb 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ##
  2. # This module requires Metasploit: http://metasploit.com/download
  3. # Current source: https://github.com/rapid7/metasploit-framework
  4. ##
  5. require 'msf/core'
  6. require 'rex'
  7. class MetasploitModule < Msf::Post
  8. include Msf::Post::Windows::Registry
  9. def initialize(info={})
  10. super(update_info(info,
  11. 'Name' => 'Windows Gather Hardware Enumeration',
  12. 'Description' => %q{
  13. Enumerate PCI hardware information from the registry. Please note this script
  14. will run through registry subkeys such as: 'PCI', 'ACPI', 'ACPI_HAL', 'FDC', 'HID',
  15. 'HTREE', 'IDE', 'ISAPNP', 'LEGACY'', LPTENUM', 'PCIIDE', 'SCSI', 'STORAGE', 'SW',
  16. and 'USB'; it will take time to finish. It is recommended to run this module as a
  17. background job.
  18. },
  19. 'License' => MSF_LICENSE,
  20. 'Author' => [ 'Brandon Perry <bperry.volatile[at]gmail.com>' ],
  21. 'Platform' => [ 'win' ],
  22. 'SessionTypes' => [ 'meterpreter' ]
  23. ))
  24. end
  25. def list
  26. tbl = Rex::Text::Table.new(
  27. 'Header' => "Device Information",
  28. 'Indent' => 1,
  29. 'Columns' =>
  30. [
  31. "Device Description",
  32. "Driver Version",
  33. "Class",
  34. "Manufacturer",
  35. "Extra",
  36. ])
  37. keys = [
  38. "HKLM\\SYSTEM\\ControlSet001\\Enum\\PCI\\",
  39. "HKLM\\SYSTEM\\ControlSet001\\Enum\\ACPI\\",
  40. "HKLM\\SYSTEM\\ControlSet001\\Enum\\ACPI_HAL\\",
  41. "HKLM\\SYSTEM\\ControlSet001\\Enum\\FDC\\",
  42. "HKLM\\SYSTEM\\ControlSet001\\Enum\\HID\\",
  43. "HKLM\\SYSTEM\\ControlSet001\\Enum\\HTREE\\",
  44. "HKLM\\SYSTEM\\ControlSet001\\Enum\\IDE\\",
  45. "HKLM\\SYSTEM\\ControlSet001\\Enum\\ISAPNP\\",
  46. "HKLM\\SYSTEM\\ControlSet001\\Enum\\LEGACY\\",
  47. "HKLM\\SYSTEM\\ControlSet001\\Enum\\LPTENUM\\",
  48. "HKLM\\SYSTEM\\ControlSet001\\Enum\\PCIIDE\\",
  49. "HKLM\\SYSTEM\\ControlSet001\\Enum\\Root\\",
  50. "HKLM\\SYSTEM\\ControlSet001\\Enum\\SCSI\\",
  51. "HKLM\\SYSTEM\\ControlSet001\\Enum\\STORAGE\\",
  52. "HKLM\\SYSTEM\\ControlSet001\\Enum\\SW\\",
  53. "HKLM\\SYSTEM\\ControlSet001\\Enum\\USB\\",
  54. ]
  55. keys.each do |key|
  56. devices = registry_enumkeys(key)
  57. t = []
  58. while(not devices.nil? and not devices.empty?)
  59. 1.upto(3) do
  60. t << framework.threads.spawn("Module(#{self.refname})", false, devices.shift) do |device|
  61. next if device.nil?
  62. vprint_status("Enumerating #{device}")
  63. infos = registry_enumkeys(key + "\\" + device)
  64. next if infos.nil?
  65. infos.each do |info|
  66. next if info.nil?
  67. info_key = key + "\\" + device + "\\" + info
  68. desc = registry_getvaldata(info_key, "DeviceDesc")
  69. mfg = registry_getvaldata(info_key, "Mfg")
  70. device_class = registry_getvaldata(info_key, "Class")
  71. driver_guid = registry_getvaldata(info_key, "Driver")
  72. extra = ""
  73. if key =~ /USB/ or key =~ /LPTENUM/
  74. extra = registry_getvaldata(info_key, "LocationInformation")
  75. end
  76. if key =~ /SCSI/ or key =~ /\\IDE/ or key =~ /ACPI\\/
  77. extra = registry_getvaldata(info_key, "FriendlyName")
  78. end
  79. desc = desc.split(';')[1] if desc =~ /^@/
  80. mfg = mfg.split(';')[1] if mfg =~ /^@/
  81. desc = '' if desc.nil?
  82. mfg = '' if mfg.nil?
  83. device_class = '' if device_class.nil?
  84. driver_guid = '' if driver_guid.nil?
  85. extra = '' if extra.nil?
  86. next if desc.empty? and mfg.empty?
  87. driver_version = ""
  88. if not driver_guid.nil? or not driver_guid.empty?
  89. if driver_guid =~ /\\/
  90. k = "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Class\\" + driver_guid
  91. d = registry_getvaldata(k, "DriverVersion")
  92. driver_version << d if not d.nil?
  93. end
  94. end
  95. done = false
  96. tbl.rows.each do |row|
  97. if row[0] == desc and
  98. row[1] == driver_version and
  99. row[2] == device_class and
  100. row[3] == mfg and
  101. row[4] == extra
  102. done = true
  103. break
  104. end
  105. end
  106. tbl << [desc, driver_version, device_class, mfg, extra] if not done
  107. end
  108. end
  109. t.map {|x| x.join }
  110. end
  111. end
  112. end
  113. results = tbl.to_s
  114. vprint_line("\n" + results)
  115. path = store_loot("host.hardware", "text/plain", session, results, "hardware.txt", "Host Hardware")
  116. print_status("Results saved in: #{path}")
  117. end
  118. def run
  119. print_status("Enumerating hardware on #{sysinfo['Computer']}")
  120. begin
  121. list
  122. rescue ::Exception => e
  123. if e.to_s =~ /execution expired/i
  124. print_error("Sorry, execution expired. Module could not finish running.")
  125. else
  126. print_error("An unexpected error has occurred: #{e.to_s}:\n#{e.backtrace}")
  127. end
  128. end
  129. end
  130. end