Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/post/windows/gather/checkvm.rb
Views: 11655
##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)185186187vmwareprocs = [188'vmtoolsd.exe',189'vmwareservice.exe',190'vmwaretray.exe',191'vmwareuser.exe'192]193194return true if processes_exist?(vmwareprocs)195196false197end198199def virtualbox?200vboxprocs = [201'vboxservice.exe',202'vboxtray.exe'203]204205vbox_srvcs = %w[VBoxMouse VBoxGuest VBoxService VBoxSF VBoxVideo]206207if services_exist?(vbox_srvcs) || processes_exist?(vboxprocs)208return true209end210211return true if key_present?('VBOX__')212213for i in 0..2 do214return true if regval_match?(215"HKLM\\HARDWARE\\DEVICEMAP\\Scsi\\Scsi Port #{i}0\\Scsi Bus 0\\Target216Id 0\\Logical Unit Id 0",217'Identifier',218/vbox/i219)220end221222return true if @system_bios_version =~ /vbox/i || @video_bios_version =~ /virtualbox/i223224@system_product_name = get_regval_str('HKLM\\HARDWARE\\DESCRIPTION\\System\\BIOS', 'SystemProductName')225226return true if @system_product_name =~ /virtualbox/i227228false229end230231def xen?232xenprocs = [233'xenservice.exe'234]235236xen_srvcs = %w[xenevtchn xennet xennet6 xensvc xenvdb]237238if processes_exist?(xenprocs) || services_exist?(xen_srvcs)239return true240end241242return true if key_present?('Xen')243244return true if @system_product_name =~ /xen/i245246false247end248249def qemu?250if @system_bios_version =~ /qemu/i || @video_bios_version =~ /qemu/i251return true252end253254if @scsi_port_0 =~ /qemu|virtio/i || @system_manufacturer =~ /qemu/i255return true256end257258return true if regval_match?(259'HKLM\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0',260'ProcessorNameString',261/qemu/i262)263264return true if key_present?('BOCHS_')265266false267end268269def report_vm(hypervisor)270print_good("This is a #{hypervisor} Virtual Machine")271report_note(272host: session,273type: 'host.hypervisor',274data: { hypervisor: hypervisor },275update: :unique_data276)277report_virtualization(hypervisor)278end279280def run281print_status('Checking if the target is a Virtual Machine ...')282@processes = get_processes283@processes = [] if @processes.nil?284285@services = registry_enumkeys('HKLM\\SYSTEM\\ControlSet001\\Services')286@services = [] if @services.nil?287288if parallels?289report_vm('Parallels')290elsif hyperv?291report_vm('Hyper-V')292elsif vmware?293report_vm('VMware')294elsif virtualbox?295report_vm('VirtualBox')296elsif xen?297report_vm('Xen')298elsif qemu?299report_vm('Qemu/KVM')300else301print_status('The target appears to be a Physical Machine')302end303end304end305306307