Files
metasploit-gs/modules/post/linux/gather/checkvm.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

238 lines
5.8 KiB
Ruby
Raw Normal View History

##
2017-07-24 06:26:21 -07:00
# This module requires Metasploit: https://metasploit.com/download
2013-10-15 13:50:46 -05:00
# Current source: https://github.com/rapid7/metasploit-framework
##
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Post
2013-09-05 13:41:25 -05:00
include Msf::Post::File
include Msf::Post::Linux::Priv
include Msf::Post::Linux::System
2023-07-16 17:46:58 +02:00
include Msf::Post::Process
2013-09-05 13:41:25 -05:00
2021-04-20 21:23:24 +05:30
def initialize(info = {})
super(
update_info(
info,
'Name' => 'Linux Gather Virtual Environment Detection',
'Description' => %q{
2013-09-05 13:41:25 -05:00
This module attempts to determine whether the system is running
inside of a virtual environment and if so, which one. This
module supports detection of Hyper-V, VMWare, VirtualBox, Xen,
2021-04-20 21:23:24 +05:30
and QEMU/KVM.
},
'License' => MSF_LICENSE,
'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],
'Platform' => [ 'linux' ],
'SessionTypes' => [ 'shell', 'meterpreter' ]
)
)
2013-09-05 13:41:25 -05:00
end
# Run Method for when run command is issued
def run
2021-04-20 21:23:24 +05:30
print_status('Gathering System info ....')
2013-09-05 13:41:25 -05:00
vm = nil
dmi_info = nil
if is_root?
2021-04-20 21:23:24 +05:30
dmi_info = cmd_exec('/usr/sbin/dmidecode')
2013-09-05 13:41:25 -05:00
end
# Check DMi Info
if dmi_info
case dmi_info
when /microsoft corporation/i
2021-04-20 21:23:24 +05:30
vm = 'MS Hyper-V'
2013-09-05 13:41:25 -05:00
when /vmware/i
2021-04-20 21:23:24 +05:30
vm = 'VMware'
2013-09-05 13:41:25 -05:00
when /virtualbox/i
2021-04-20 21:23:24 +05:30
vm = 'VirtualBox'
2013-09-05 13:41:25 -05:00
when /qemu/i
2021-04-20 21:23:24 +05:30
vm = 'Qemu/KVM'
2013-09-05 13:41:25 -05:00
when /domu/i
2021-04-20 21:23:24 +05:30
vm = 'Xen'
2013-09-05 13:41:25 -05:00
end
end
# Check kernel modules
2021-04-20 21:23:24 +05:30
if !vm
loaded_modules = read_file('/proc/modules')
if !loaded_modules
loaded_modules = cmd_exec('/sbin/lsmod').to_s
end
case loaded_modules.gsub("\n", ' ')
when /vboxsf|vboxguest|vboxvideo|vboxvideo_drv|vboxdrv/i
2021-04-20 21:23:24 +05:30
vm = 'VirtualBox'
2013-09-05 13:41:25 -05:00
when /vmw_ballon|vmxnet|vmw/i
2021-04-20 21:23:24 +05:30
vm = 'VMware'
2021-05-23 10:23:56 -04:00
when /xen-vbd|xen-vnif|xen_netfront|xen_blkfront/
2021-04-20 21:23:24 +05:30
vm = 'Xen'
2023-07-16 18:29:36 +02:00
when /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/
2021-04-20 21:23:24 +05:30
vm = 'Qemu/KVM'
2023-07-16 17:23:33 +02:00
when /hv_vmbus|hv_blkvsc|hv_netvsc|hv_utils|hv_storvsc|hv_boot|hv_balloon|hyperv_keyboard|hid_hyperv|hyperv_fb/
2021-04-20 21:23:24 +05:30
vm = 'MS Hyper-V'
2013-09-05 13:41:25 -05:00
end
end
# Check SCSI Driver
2021-04-20 21:23:24 +05:30
if !vm
proc_scsi = read_file('/proc/scsi/scsi')
2021-04-20 18:44:38 +05:30
if proc_scsi
2021-04-20 21:23:24 +05:30
case proc_scsi.gsub("\n", ' ')
2021-04-20 09:40:11 +05:30
when /vmware/i
2021-04-20 21:23:24 +05:30
vm = 'VMware'
2021-04-20 09:40:11 +05:30
when /vbox/i
2021-04-20 21:23:24 +05:30
vm = 'VirtualBox'
2021-04-20 09:40:11 +05:30
end
2013-09-05 13:41:25 -05:00
end
end
# Check IDE Devices
2021-04-20 21:23:24 +05:30
if !vm
case cmd_exec('cat /proc/ide/hd*/model')
2013-09-05 13:41:25 -05:00
when /vbox/i
2021-04-20 21:23:24 +05:30
vm = 'VirtualBox'
2013-09-05 13:41:25 -05:00
when /vmware/i
2021-04-20 21:23:24 +05:30
vm = 'VMware'
2013-09-05 13:41:25 -05:00
when /qemu/i
2021-04-20 21:23:24 +05:30
vm = 'Qemu/KVM'
2013-09-05 13:41:25 -05:00
when /virtual [vc]d/i
2021-04-20 21:23:24 +05:30
vm = 'Hyper-V/Virtual PC'
2013-09-05 13:41:25 -05:00
end
end
2023-02-08 13:47:34 +00:00
# identity Xen block Device Root
2021-05-23 10:23:56 -04:00
if !vm
proc_mounts = read_file('/proc/mounts')
if proc_mounts
case proc_mounts
2023-02-08 13:47:34 +00:00
when %r{/dev/xvd.* / }
2021-05-23 10:23:56 -04:00
vm = 'Xen'
end
end
end
2023-02-08 13:47:34 +00:00
# Check system vendor
if !vm
sys_vendor = read_file('/sys/class/dmi/id/sys_vendor')
if sys_vendor
case sys_vendor.gsub("\n", ' ')
when /qemu/i
vm = 'Qemu'
when /vmware/i
vm = 'VMWare'
when /xen/i
vm = 'Xen'
2023-07-16 18:18:24 +02:00
when /microsoft/i
vm = 'Hyper-V'
end
end
end
2013-09-05 13:41:25 -05:00
# Check using lspci
2021-04-20 21:23:24 +05:30
if !vm
2013-09-05 13:41:25 -05:00
case get_sysinfo[:distro]
when /oracle|centos|suse|redhat|mandrake|slackware|fedora/i
2021-04-20 21:23:24 +05:30
lspci_data = cmd_exec('/sbin/lspci')
2013-09-05 13:41:25 -05:00
when /debian|ubuntu/
2021-04-20 21:23:24 +05:30
lspci_data = cmd_exec('/usr/bin/lspci')
2013-09-05 13:41:25 -05:00
else
2021-04-20 21:23:24 +05:30
lspci_data = cmd_exec('lspci')
2013-09-05 13:41:25 -05:00
end
2021-04-20 21:23:24 +05:30
case lspci_data.to_s.gsub("\n", ' ')
2013-09-05 13:41:25 -05:00
when /vmware/i
2021-04-20 21:23:24 +05:30
vm = 'VMware'
2013-09-05 13:41:25 -05:00
when /virtualbox/i
2021-04-20 21:23:24 +05:30
vm = 'VirtualBox'
2013-09-05 13:41:25 -05:00
end
end
2023-07-16 17:07:06 +02:00
# Check Product Name
2021-04-20 21:23:24 +05:30
if !vm
product_name = read_file('/sys/class/dmi/id/product_name')
if product_name
2021-04-20 21:23:24 +05:30
case product_name.gsub("\n", ' ')
2021-04-20 20:58:04 +05:30
when /vmware/i
2021-04-20 21:23:24 +05:30
vm = 'VMware'
2021-04-20 20:58:04 +05:30
when /virtualbox/i
2021-04-20 21:23:24 +05:30
vm = 'VirtualBox'
2021-04-20 20:58:04 +05:30
when /xen/i
2021-04-20 21:23:24 +05:30
vm = 'Xen'
2021-04-20 20:58:04 +05:30
when /KVM/i
2021-04-20 21:23:24 +05:30
vm = 'KVM'
2021-04-20 20:58:04 +05:30
when /oracle/i
2021-04-20 21:23:24 +05:30
vm = 'Oracle Corporation'
2021-04-20 20:58:04 +05:30
end
2013-09-05 13:41:25 -05:00
end
end
# Check BIOS Name
if !vm
bios_vendor = read_file('/sys/devices/virtual/dmi/id/bios_vendor')
if bios_vendor
case bios_vendor.gsub("\n", ' ')
when /^xen/i
vm = 'Xen'
end
2023-07-16 18:22:25 +02:00
end
end
# Check cpuinfo
if !vm
cpuinfo = read_file('/proc/cpuinfo')
if cpuinfo
case cpuinfo.gsub("\n", ' ')
when /qemu virtual cpu|emulated by qemu|KVM processor/i
vm = 'Qemu/KVM'
end
end
end
# Check Xen devices
if !vm
2023-08-22 12:36:48 +02:00
xen_capabilities = read_file('/sys/hypervisor/uuid')
if xen_capabilities
2023-08-22 12:36:48 +02:00
if ! xen_capabilities.include? '00000000-0000-0000-0000-000000000000'
vm = 'Xen'
end
end
end
2023-07-16 17:46:58 +02:00
# Check Processes
if !vm
get_processes do |process|
case process['name']
when /hv_vss_daemon|hv_kvp_daemon|hv_fcopy_daemon/i
vm = 'MS Hyper-V'
end
end
end
2013-09-05 13:41:25 -05:00
# Check dmesg Output
2021-04-20 21:23:24 +05:30
if !vm
dmesg = cmd_exec('dmesg')
2013-09-05 13:41:25 -05:00
case dmesg
when /vboxbios|vboxcput|vboxfacp|vboxxsdt|vbox cd-rom|vbox harddisk/i
2021-04-20 21:23:24 +05:30
vm = 'VirtualBox'
2013-09-05 13:41:25 -05:00
when /vmware virtual ide|vmware pvscsi|vmware virtual platform/i
2021-04-20 21:23:24 +05:30
vm = 'VMware'
2013-09-05 13:41:25 -05:00
when /xen_mem|xen-vbd/i
2021-04-20 21:23:24 +05:30
vm = 'Xen'
2013-09-05 13:41:25 -05:00
when /qemu virtual cpu version/i
2021-04-20 21:23:24 +05:30
vm = 'Qemu/KVM'
when %r{/dev/vmnet}
vm = 'VMware'
2013-09-05 13:41:25 -05:00
end
end
if vm
print_good("This appears to be a '#{vm}' virtual machine")
report_virtualization(vm)
2013-09-05 13:41:25 -05:00
else
2021-04-20 21:23:24 +05:30
print_status('This does not appear to be a virtual machine')
2013-09-05 13:41:25 -05:00
end
end
end