Path: blob/master/modules/post/windows/gather/checkvm.rb
19535 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::Process7include Msf::Post::Windows::Registry8include Msf::Auxiliary::Report910def initialize(info = {})11super(12update_info(13info,14'Name' => 'Windows Gather Virtual Environment Detection',15'Description' => %q{16This module attempts to determine whether the system is running17inside of a virtual environment and if so, which one. This18module supports detection of Hyper-V, VMWare, VirtualBox, Xen, QEMU,19and Parallels.20},21'License' => MSF_LICENSE,22'Author' => [23'Carlos Perez <carlos_perez[at]darkoperator.com>',24'Aaron Soto <aaron_soto[at]rapid7.com>'25],26'Platform' => [ 'win' ],27'SessionTypes' => %w[meterpreter powershell shell],28'References' => [29['URL', 'https://handlers.sans.org/tliston/ThwartingVMDetection_Liston_Skoudis.pdf'],30['URL', 'https://www.heise.de/security/downloads/07/1/1/8/3/5/5/9/vmde.pdf'],31['URL', 'https://evasions.checkpoint.com/techniques/registry.html']32],33'Notes' => {34'Stability' => [CRASH_SAFE],35'Reliability' => [],36'SideEffects' => []37}38)39)40end4142# enumerates through a list of VM signature processes and compares them to43# the processes running, returns true upon a match.44def processes_exist?(vm_processes)45vm_processes.each do |x|46@processes.each do |p|47return true if p['name'].casecmp?(x)48end49end50false51end5253# loops over a list of services that are known to be signatures of vm's and54# compares them to the list of running services.55def services_exist?(vm_services)56vm_services.each do |srvc|57return true if service_exists?(srvc)58end59false60end6162def service_exists?(service)63@services.include?(service)64end6566# registers relevant keys and stores them in a hash67def register_keys(key_list)68@keys = {}69key_list.each do |k|70srvals = get_srval(k)71srvals = [] if srvals.nil?72@keys.store(k, srvals)73end74@keys75end7677# checks the values of the keys and compares them to vm_k78def key_present?(vm_k)79@keys.each_value do |v|80return true if v.include?(vm_k)81end82false83end8485def get_srval(key)86srvals = registry_enumkeys(key)87srvals = [] if srvals.nil?88srvals89end9091# returns true if regval matches a regex92def regval_match?(key, val, rgx)93return true if get_regval_str(key, val) =~ rgx9495false96end9798# returns true if regval is eql to a string99def regval_eql?(key, val, str)100get_regval_str(key, val) == str101end102103def get_regval_str(key, valname)104ret = registry_getvaldata(key, valname)105if ret.is_a?(Array)106ret = ret.join107end108ret109end110111def parallels?112@system_bios_version = get_regval_str('HKLM\\HARDWARE\\DESCRIPTION\\System', 'SystemBiosVersion')113114@video_bios_version = get_regval_str('HKLM\\HARDWARE\\DESCRIPTION\\System', 'VideoBiosVersion')115116if @system_bios_version =~ /parallels/i || @video_bios_version =~ /parallels/i117return true118end119120false121end122123def hyperv?124physical_host = get_regval_str('HKLM\\SOFTWARE\\Microsoft\\Virtual Machine\\Guest\\Parameters', 'PhysicalHostNameFullyQualified')125126if physical_host127report_note(128host: session,129type: 'host.physicalHost',130data: { physicalHost: physical_host },131update: :unique_data132)133134print_good("This is a Hyper-V Virtual Machine running on physical host #{physical_host}")135return true136end137138sfmsvals = registry_enumkeys('HKLM\\SOFTWARE\\Microsoft')139if sfmsvals140%w[Hyper-V VirtualMachine].each do |vm|141return true if sfmsvals.include?(vm)142end143end144145if @system_bios_version =~ /vrtual/i || @system_bios_version == 'Hyper-V'146return true147end148149keys = %w[HKLM\\HARDWARE\\ACPI\\FADT HKLM\\HARDWARE\\ACPI\\RSDT HKLM\\HARDWARE\\ACPI\\DSDT]150151register_keys(keys)152153return true if key_present?('VRTUAL')154155hyperv_services = %w[vmicexchange]156157return true if services_exist?(hyperv_services)158159false160end161162def vmware?163vmware_services = %w[164vmdebug vmmouse VMTools VMMEMCTL tpautoconnsvc165tpvcgateway vmware wmci vmx86166]167168return true if services_exist?(vmware_services)169170@system_manufacturer = get_regval_str('HKLM\\HARDWARE\\DESCRIPTION\\System\\BIOS',171'SystemManufacturer')172173return true if @system_manufacturer =~ /vmware/i174175@scsi_port_1 = get_regval_str('HKLM\\HARDWARE\\DEVICEMAP\\Scsi\\Scsi Port 1\\Scsi Bus 0\\Target Id 0\\Logical Unit Id 0',176'Identifier')177178return true if @scsi_port_1 =~ /vmware/i179180return true if regval_match?(181'HKLM\\SYSTEM\\ControlSet001\\Control\\Class\\{4D36E968-E325-11CE-BFC1-08002BE10318}\\0000',182'DriverDesc',183/cl_vmx_svga|VMWare/i184)185186vmwareprocs = [187'vmtoolsd.exe',188'vmwareservice.exe',189'vmwaretray.exe',190'vmwareuser.exe'191]192193return true if processes_exist?(vmwareprocs)194195false196end197198def virtualbox?199vboxprocs = [200'vboxservice.exe',201'vboxtray.exe'202]203204vbox_srvcs = %w[VBoxMouse VBoxGuest VBoxService VBoxSF VBoxVideo]205206if services_exist?(vbox_srvcs) || processes_exist?(vboxprocs)207return true208end209210return true if key_present?('VBOX__')211212for i in 0..2 do213return true if regval_match?(214"HKLM\\HARDWARE\\DEVICEMAP\\Scsi\\Scsi Port #{i}0\\Scsi Bus 0\\Target215Id 0\\Logical Unit Id 0",216'Identifier',217/vbox/i218)219end220221return true if @system_bios_version =~ /vbox/i || @video_bios_version =~ /virtualbox/i222223@system_product_name = get_regval_str('HKLM\\HARDWARE\\DESCRIPTION\\System\\BIOS', 'SystemProductName')224225return true if @system_product_name =~ /virtualbox/i226227false228end229230def xen?231xenprocs = [232'xenservice.exe'233]234235xen_srvcs = %w[xenevtchn xennet xennet6 xensvc xenvdb]236237if processes_exist?(xenprocs) || services_exist?(xen_srvcs)238return true239end240241return true if key_present?('Xen')242243return true if @system_product_name =~ /xen/i244245false246end247248def qemu?249if @system_bios_version =~ /qemu/i || @video_bios_version =~ /qemu/i250return true251end252253if @scsi_port_0 =~ /qemu|virtio/i || @system_manufacturer =~ /qemu/i254return true255end256257return true if regval_match?(258'HKLM\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0',259'ProcessorNameString',260/qemu/i261)262263return true if key_present?('BOCHS_')264265false266end267268def report_vm(hypervisor)269print_good("This is a #{hypervisor} Virtual Machine")270report_note(271host: session,272type: 'host.hypervisor',273data: { hypervisor: hypervisor },274update: :unique_data275)276report_virtualization(hypervisor)277end278279def run280print_status('Checking if the target is a Virtual Machine ...')281@processes = get_processes282@processes = [] if @processes.nil?283284@services = registry_enumkeys('HKLM\\SYSTEM\\ControlSet001\\Services')285@services = [] if @services.nil?286287if parallels?288report_vm('Parallels')289elsif hyperv?290report_vm('Hyper-V')291elsif vmware?292report_vm('VMware')293elsif virtualbox?294report_vm('VirtualBox')295elsif xen?296report_vm('Xen')297elsif qemu?298report_vm('Qemu/KVM')299else300print_status('The target appears to be a Physical Machine')301end302end303end304305306