如何判断当前环境是否在Vmware的虚拟机里面?
判断后的想法就是如果是在Vmware里,那么自动安装起Vmware Tools,应该怎么判断呢? 在注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet 有几个包含VMWare的项,搜索定位下,忘记具体哪个了 据说用cpuid比较可靠道听途说http://www.hx95.com/Article/Tech/201112/49785.html 要不根据硬盘的型号来判断 看各大虚拟机的硬盘型号都有自己软件的名字。 可以通过wmi获取主板BIOS信息对比。 Dim $VFlag = TrueDim $obj = ObjGet("winmgmts:\\localhost\root\CIMV2")
Dim $objItem = $obj.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", 0x10 + 0x20)
If IsObj($objItem) Then
For $Item In $objItem
If StringInStr($Item.Model, "VMWARE") Then
$VFlag = False
ExitLoop
EndIf
Next
If $VFlag Then
MsgBox(0, "", "NOT VMWARE")
Else
MsgBox(0, "", "IS VMWARE")
EndIf
Else
MsgBox(0, "error", "NO obj")
EndIf $a = _CheckVM()
If $a = "" Then
MsgBox(0, "我不在虚拟机里", "我不在虚拟机里!")
Else
MsgBox(0, "我在虚拟机里!", "我在虚拟机里,我的理由是" & @CRLF & @CRLF & $a)
EndIf
;检查是否运行于虚拟机环境。返回空值说明运行于真实环境,返回一个字符串,说明运行于虚拟机环境并给出解释。
;方法:检查虚拟机服务/进程,硬盘,BIOS 主板/芯片组
;发现2个以上即确定为虚拟机环境
Func _CheckVM()
$strComputer = '.'
$objWMIService = ObjGet('winmgmts:\\' & $strComputer & '\root\cimv2')
$vmhit_count = 0
$vmhit_details = ""
; 检查虚拟机管理程序
If ProcessExists("VBoxService.exe") Or ProcessExists("VBoxTray.exe") Or ProcessExists("VMwareTray.exe") Or ProcessExists("VMwareUser.exe") Then _AddVMHit($vmhit_count, $vmhit_details, "RUNNING SOFTWARE", "Found a Vbox or VMware guest OS service or tray process")
; 检查虚拟机设备
If Not IsObj($objWMIService) Then
MsgBox(0, "", "? WTF?")
Return ""
EndIf
; 检查虚拟机硬盘
$colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_DiskDrive', 'WQL', 0x10 + 0x20)
If IsObj($colItems) Then
For $objItem In $colItems
$vReturn = $objItem.Model
Select
Case StringInStr($vReturn, "VBOX HARDDISK")
_AddVMHit($vmhit_count, $vmhit_details, "DISKS", "Found device ""VBOX HARDDISK""")
Case StringInStr($vReturn, "QEMU HARDDISK")
_AddVMHit($vmhit_count, $vmhit_details, "DISKS", "Found device ""QEMU HARDDISK""")
Case StringInStr($vReturn, "VMWARE VIRTUAL IDE HARD DRIVE")
_AddVMHit($vmhit_count, $vmhit_details, "DISKS", "Found device ""VMWARE VIRTUAL IDE HARD DRIVE""")
Case StringInStr($vReturn, "VMWARE Virtual S SCSI Disk Device")
_AddVMHit($vmhit_count, $vmhit_details, "DISKS", "Found device ""VMWARE Virtual S SCSI Disk Device""")
EndSelect
Next
EndIf
; 检查虚拟机BIOS
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_BIOS", "WQL", 0x10 + 0x20)
If IsObj($colItems) Then
For $objItem In $colItems
Select
Case StringInStr($objItem.BIOSVersion(0), "Vbox")
_AddVMHit($vmhit_count, $vmhit_details, "BIOS", "Found Vbox BIOS version")
Case StringInStr($objItem.SMBIOSBIOSVersion, "virt")
_AddVMHit($vmhit_count, $vmhit_details, "BIOS", "Found Vbox BIOS version")
EndSelect
Next
EndIf
; 检查虚拟机主板/芯片组
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Baseboard", "WQL", 0x10 + 0x20)
If IsObj($colItems) Then
For $objItem In $colItems
Select
Case StringInStr($objItem.Name, "Base Board") And StringInStr($objItem.Product, "440BX Desktop Reference Platform")
_AddVMHit($vmhit_count, $vmhit_details, "MOTHERBOARD", "Found VMware-style motherboard, ""440BX Desktop Reference Platform"" / Name=""Base Board""")
EndSelect
Next
EndIf
If $vmhit_count >= 2 Then
Return $vmhit_details & @CRLF & @CRLF & "Hits in " & $vmhit_count & " of 4 hardware categories - probably a virtual machine."
Else
Return ""
EndIf
EndFunc ;==>_CheckVM
Func _AddVMHit(ByRef $vmhit_count, ByRef $vmhit_details, $this_hit_category, $this_hit_text)
If StringInStr($vmhit_details, "In CATEGORY:" & $this_hit_category & ":") Then
$vmhit_details &= " and " & $this_hit_text
Else
If $vmhit_details > "" Then $vmhit_details &= @CRLF
$vmhit_details &= "In CATEGORY:" & $this_hit_category & ": " & $this_hit_text
$vmhit_count += 1
EndIf
EndFunc ;==>_AddVMHit 也是老外写的,可以参考一下
Opt("MustDeclareVars", 1)
If _VCheck() Then
MsgBox(0, "", "True")
Else
MsgBox(0, "", "False")
EndIf
Func _VCheck()
Local $strComputer = ".", $sMake, $sModel, $sBIOSVersion, $bIsVM, $sVMPlatform
Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
If IsObj($colItems) Then
For $objItem In $colItems
;MsgBox(0,"","Name: " & $objItem.Name)
$sMake = $objItem.Manufacturer
$sModel = $objItem.Model
Next
EndIf
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_BIOS", "WQL", 0x10 + 0x20)
If IsObj($colItems) Then
For $objItem In $colItems
;MsgBox(0,"",$objItem.BIOSVersion(0))
$sBIOSVersion = $objItem.SMBIOSBIOSVersion
Next
EndIf
$bIsVM = False
$sVMPlatform = ""
MsgBox(0, "", "Manufacturer=" & $sMake)
MsgBox(0, "", "Model=" & $sModel)
MsgBox(0, "", "BIOSVersion=" & $sBIOSVersion)
If $sModel = "Virtual Machine" Then
; Microsoft virtualization technology detected, assign defaults
$sVMPlatform = "Hyper-V"
$bIsVM = True
; Try to determine more specific values
Switch $sBIOSVersion
Case "VRTUAL - 1000831"
$bIsVM = True
$sVMPlatform = "Hyper-V 2008 Beta or RC0"
Case "VRTUAL - 5000805", "BIOS Date: 05/05/08 20:35:56Ver: 08.00.02"
$bIsVM = True
$sVMPlatform = "Hyper-V 2008 RTM"
Case "VRTUAL - 3000919"
$bIsVM = True
$sVMPlatform = "Hyper-V 2008 R2"
Case "A M I- 2000622"
$bIsVM = True
$sVMPlatform = "VS2005R2SP1 or VPC2007"
Case "A M I- 9000520"
$bIsVM = True
$sVMPlatform = "VS2005R2"
Case "A M I- 9000816", "A M I- 6000901"
$bIsVM = True
$sVMPlatform = "Windows Virtual PC"
Case "A M I- 8000314"
$bIsVM = True
$sVMPlatform = "VS2005 or VPC2004"
EndSwitch
ElseIf $sModel = "VMware Virtual Platform" Then
; VMware detected
$sVMPlatform = "VMware"
$bIsVM = True
ElseIf $sModel = "VirtualBox" Then
; VirtualBox detected
$bIsVM = True
$sVMPlatform = "VirtualBox"
Else
EndIf
If $bIsVM Then
MsgBox(0, "", "IsVirtualMachine=True")
MsgBox(0, "", "VirtualMachinePlatform=" & $sVMPlatform)
Else
MsgBox(0, "", "IsVirtualMachine=False")
EndIf
Return $bIsVM
EndFunc ;==>_VCheck 强大的CIMV2 楼上这个很好,vpc vmware都可判断!
页:
[1]