Path: blob/master/modules/post/linux/gather/checkvm.rb
19812 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::Linux::Priv8include Msf::Post::Linux::System9include Msf::Post::Process1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'Linux Gather Virtual Environment Detection',16'Description' => %q{17This module attempts to determine whether the system is running18inside of a virtual environment and if so, which one. This19module supports detection of Hyper-V, VMWare, VirtualBox, Xen,20Bhyve and QEMU/KVM.21},22'License' => MSF_LICENSE,23'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],24'Platform' => [ 'linux' ],25'SessionTypes' => [ 'shell', 'meterpreter' ],26'Notes' => {27'Stability' => [CRASH_SAFE],28'SideEffects' => [],29'Reliability' => []30}31)32)33end3435# Run Method for when run command is issued36def run37print_status('Gathering System info ....')38vm = nil39dmi_info = nil4041if is_root?42dmi_info = cmd_exec('/usr/sbin/dmidecode')43end4445# Check DMi Info46if dmi_info47case dmi_info48when /microsoft corporation/i49vm = 'MS Hyper-V'50when /vmware/i51vm = 'VMware'52when /virtualbox/i53vm = 'VirtualBox'54when /qemu/i55vm = 'Qemu/KVM'56when /domu/i57vm = 'Xen'58end59end6061# Check kernel modules62if !vm63loaded_modules = read_file('/proc/modules')64if !loaded_modules65loaded_modules = cmd_exec('/sbin/lsmod').to_s66end6768case loaded_modules.gsub("\n", ' ')69when /vboxsf|vboxguest|vboxvideo|vboxvideo_drv|vboxdrv/i70vm = 'VirtualBox'71when /vmw_ballon|vmxnet|vmw/i72vm = 'VMware'73when /xen-vbd|xen-vnif|xen_netfront|xen_blkfront/74vm = 'Xen'75when /virtio_pci|virtio_net|virtio_blk|virtio_console|virtio_scsi|virtio_balloon|virtio_input|virtio-gpu|virtio-rng|virtio_dma_buf|virtio_mmio|virtio_pmem|virtio_snd/76vm = 'Qemu/KVM'77when /hv_vmbus|hv_blkvsc|hv_netvsc|hv_utils|hv_storvsc|hv_boot|hv_balloon|hyperv_keyboard|hid_hyperv|hyperv_fb/78vm = 'MS Hyper-V'79end80end8182# Check SCSI Driver83if !vm84proc_scsi = read_file('/proc/scsi/scsi')85if proc_scsi86case proc_scsi.gsub("\n", ' ')87when /vmware/i88vm = 'VMware'89when /vbox/i90vm = 'VirtualBox'91end92end93end9495# Check IDE Devices96if !vm97case cmd_exec('cat /proc/ide/hd*/model')98when /vbox/i99vm = 'VirtualBox'100when /vmware/i101vm = 'VMware'102when /qemu/i103vm = 'Qemu/KVM'104when /virtual [vc]d/i105vm = 'Hyper-V/Virtual PC'106end107end108109# identity Xen block Device Root110if !vm111proc_mounts = read_file('/proc/mounts')112if proc_mounts113case proc_mounts114when %r{/dev/xvd.* / }115vm = 'Xen'116end117end118end119120# Check system vendor121if !vm122sys_vendor = read_file('/sys/class/dmi/id/sys_vendor')123if sys_vendor124case sys_vendor.gsub("\n", ' ')125when /qemu/i126vm = 'Qemu'127when /vmware/i128vm = 'VMWare'129when /xen/i130vm = 'Xen'131when /microsoft/i132vm = 'Hyper-V'133end134end135end136137# Check using lspci138if !vm139case get_sysinfo[:distro]140when /oracle|centos|suse|redhat|mandrake|slackware|fedora/i141lspci_data = cmd_exec('/sbin/lspci')142when /debian|ubuntu/143lspci_data = cmd_exec('/usr/bin/lspci')144else145lspci_data = cmd_exec('lspci')146end147148case lspci_data.to_s.gsub("\n", ' ')149when /vmware/i150vm = 'VMware'151when /virtualbox/i152vm = 'VirtualBox'153end154end155156# Check Product Name157if !vm158product_name = read_file('/sys/class/dmi/id/product_name')159if product_name160case product_name.gsub("\n", ' ')161when /bhyve/i162vm = 'Bhyve'163when /qemu/i164vm = 'Qemu'165when /vmware/i166vm = 'VMware'167when /virtualbox/i168vm = 'VirtualBox'169when /xen/i170vm = 'Xen'171when /KVM/i172vm = 'KVM'173when /oracle/i174vm = 'Oracle Corporation'175end176end177end178179# Check BIOS Name180if !vm181bios_vendor = read_file('/sys/devices/virtual/dmi/id/bios_vendor')182if bios_vendor183case bios_vendor.gsub("\n", ' ')184when /^xen/i185vm = 'Xen'186when /innotek GmbH/i187vm = 'VirtualBox'188end189end190end191192# Check cpuinfo193if !vm194cpuinfo = read_file('/proc/cpuinfo')195if cpuinfo196case cpuinfo.gsub("\n", ' ')197when /qemu virtual cpu|emulated by qemu|KVM processor/i198vm = 'Qemu/KVM'199end200end201end202203# Check Xen devices204if !vm205xen_capabilities = read_file('/sys/hypervisor/uuid')206if xen_capabilities && !xen_capabilities.include?('00000000-0000-0000-0000-000000000000')207vm = 'Xen'208end209end210if !vm211xen_type = read_file('/sys/hypervisor/type')212if xen_type && xen_type == ('xen')213vm = 'Xen'214end215end216217# Check device tree218if !vm219compatible = read_file('/proc/device-tree/compatible')220if compatible && compatible.include?('qemu')221vm = 'Qemu/KVM'222end223end224if !vm225compatible = read_file('/proc/device-tree/hypervisor/compatible')226if compatible227case compatible228when /linux,kvm/i229vm = 'Qemu/KVM'230when /xen/i231vm = 'Xen'232when /vmware/i233vm = 'VMware'234end235end236end237238# Check Processes239if !vm240get_processes do |process|241case process['name']242when /hv_vss_daemon|hv_kvp_daemon|hv_fcopy_daemon/i243vm = 'MS Hyper-V'244end245end246end247248# Check dmesg Output249if !vm250dmesg = cmd_exec('dmesg')251case dmesg252when /vboxbios|vboxcput|vboxfacp|vboxxsdt|vbox cd-rom|vbox harddisk/i253vm = 'VirtualBox'254when /vmware virtual ide|vmware pvscsi|vmware virtual platform/i255vm = 'VMware'256when /xen_mem|xen-vbd/i257vm = 'Xen'258when /qemu virtual cpu version/i259vm = 'Qemu/KVM'260when %r{/dev/vmnet}261vm = 'VMware'262end263end264265if vm266print_good("This appears to be a '#{vm}' virtual machine")267report_virtualization(vm)268else269print_status('This does not appear to be a virtual machine')270end271end272end273274275