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/linux/gather/checkvm.rb
Views: 11704
##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)27)28end2930# Run Method for when run command is issued31def run32print_status('Gathering System info ....')33vm = nil34dmi_info = nil3536if is_root?37dmi_info = cmd_exec('/usr/sbin/dmidecode')38end3940# Check DMi Info41if dmi_info42case dmi_info43when /microsoft corporation/i44vm = 'MS Hyper-V'45when /vmware/i46vm = 'VMware'47when /virtualbox/i48vm = 'VirtualBox'49when /qemu/i50vm = 'Qemu/KVM'51when /domu/i52vm = 'Xen'53end54end5556# Check kernel modules57if !vm58loaded_modules = read_file('/proc/modules')59if !loaded_modules60loaded_modules = cmd_exec('/sbin/lsmod').to_s61end6263case loaded_modules.gsub("\n", ' ')64when /vboxsf|vboxguest|vboxvideo|vboxvideo_drv|vboxdrv/i65vm = 'VirtualBox'66when /vmw_ballon|vmxnet|vmw/i67vm = 'VMware'68when /xen-vbd|xen-vnif|xen_netfront|xen_blkfront/69vm = 'Xen'70when /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/71vm = 'Qemu/KVM'72when /hv_vmbus|hv_blkvsc|hv_netvsc|hv_utils|hv_storvsc|hv_boot|hv_balloon|hyperv_keyboard|hid_hyperv|hyperv_fb/73vm = 'MS Hyper-V'74end75end7677# Check SCSI Driver78if !vm79proc_scsi = read_file('/proc/scsi/scsi')80if proc_scsi81case proc_scsi.gsub("\n", ' ')82when /vmware/i83vm = 'VMware'84when /vbox/i85vm = 'VirtualBox'86end87end88end8990# Check IDE Devices91if !vm92case cmd_exec('cat /proc/ide/hd*/model')93when /vbox/i94vm = 'VirtualBox'95when /vmware/i96vm = 'VMware'97when /qemu/i98vm = 'Qemu/KVM'99when /virtual [vc]d/i100vm = 'Hyper-V/Virtual PC'101end102end103104# identity Xen block Device Root105if !vm106proc_mounts = read_file('/proc/mounts')107if proc_mounts108case proc_mounts109when %r{/dev/xvd.* / }110vm = 'Xen'111end112end113end114115# Check system vendor116if !vm117sys_vendor = read_file('/sys/class/dmi/id/sys_vendor')118if sys_vendor119case sys_vendor.gsub("\n", ' ')120when /qemu/i121vm = 'Qemu'122when /vmware/i123vm = 'VMWare'124when /xen/i125vm = 'Xen'126when /microsoft/i127vm = 'Hyper-V'128end129end130end131132# Check using lspci133if !vm134case get_sysinfo[:distro]135when /oracle|centos|suse|redhat|mandrake|slackware|fedora/i136lspci_data = cmd_exec('/sbin/lspci')137when /debian|ubuntu/138lspci_data = cmd_exec('/usr/bin/lspci')139else140lspci_data = cmd_exec('lspci')141end142143case lspci_data.to_s.gsub("\n", ' ')144when /vmware/i145vm = 'VMware'146when /virtualbox/i147vm = 'VirtualBox'148end149end150151# Check Product Name152if !vm153product_name = read_file('/sys/class/dmi/id/product_name')154if product_name155case product_name.gsub("\n", ' ')156when /bhyve/i157vm = 'Bhyve'158when /qemu/i159vm = 'Qemu'160when /vmware/i161vm = 'VMware'162when /virtualbox/i163vm = 'VirtualBox'164when /xen/i165vm = 'Xen'166when /KVM/i167vm = 'KVM'168when /oracle/i169vm = 'Oracle Corporation'170end171end172end173174# Check BIOS Name175if !vm176bios_vendor = read_file('/sys/devices/virtual/dmi/id/bios_vendor')177if bios_vendor178case bios_vendor.gsub("\n", ' ')179when /^xen/i180vm = 'Xen'181when /innotek GmbH/i182vm = 'VirtualBox'183end184end185end186187# Check cpuinfo188if !vm189cpuinfo = read_file('/proc/cpuinfo')190if cpuinfo191case cpuinfo.gsub("\n", ' ')192when /qemu virtual cpu|emulated by qemu|KVM processor/i193vm = 'Qemu/KVM'194end195end196end197198# Check Xen devices199if !vm200xen_capabilities = read_file('/sys/hypervisor/uuid')201if xen_capabilities202if ! xen_capabilities.include? '00000000-0000-0000-0000-000000000000'203vm = 'Xen'204end205end206end207if !vm208xen_type = read_file('/sys/hypervisor/type')209if xen_type210if xen_type == 'xen'211vm = 'Xen'212end213end214end215216# Check device tree217if !vm218compatible = read_file('/proc/device-tree/compatible')219if compatible220if compatible.include? 'qemu'221vm = 'Qemu/KVM'222end223end224end225if !vm226compatible = read_file('/proc/device-tree/hypervisor/compatible')227if compatible228case compatible229when /linux,kvm/i230vm = 'Qemu/KVM'231when /xen/i232vm = 'Xen'233when /vmware/i234vm = 'VMware'235end236end237end238239# Check Processes240if !vm241get_processes do |process|242case process['name']243when /hv_vss_daemon|hv_kvp_daemon|hv_fcopy_daemon/i244vm = 'MS Hyper-V'245end246end247end248249# Check dmesg Output250if !vm251dmesg = cmd_exec('dmesg')252case dmesg253when /vboxbios|vboxcput|vboxfacp|vboxxsdt|vbox cd-rom|vbox harddisk/i254vm = 'VirtualBox'255when /vmware virtual ide|vmware pvscsi|vmware virtual platform/i256vm = 'VMware'257when /xen_mem|xen-vbd/i258vm = 'Xen'259when /qemu virtual cpu version/i260vm = 'Qemu/KVM'261when %r{/dev/vmnet}262vm = 'VMware'263end264end265266if vm267print_good("This appears to be a '#{vm}' virtual machine")268report_virtualization(vm)269else270print_status('This does not appear to be a virtual machine')271end272end273end274275276