VMã®æ¤ç¥ã«ã¤ãã¦
ãã«ã¦ã§ã¢ã解æããéã«ã¯VMã§è§£æãè¡ããã¨ãå¤ãã®ã§ãVMãæ¤ç¥ãã¦ã¢ã³ãããããã³ã°ãããã«ã¦ã§ã¢ãåå¨ãããããããç¨ããææ³ã«ã¤ãã¦ã¾ã¨ããã
1. TSCãç¨ãã
ãTSC(IA32_TIME_STAMP_COUNTER_MSR: TSC_MSR)ã¨ã¯ãCPUã¯ããã¯ãã¨ã«å ç®ããã64bitã®ã¿ã¤ã ã¹ã¿ã³ãã«ã¦ã³ã¿ã§ãããããã RDTSCï¼Read Time Stamp Counterï¼å½ä»¤ã使ã£ã¦èªã¿åºããã¨ã§ãCPUã¯ããã¯ã¨åãå解è½ãæã¤ç²¾åº¦ã®ã¿ã¤ãã使ããã¨ãã§ããã
ããã®ã¿ã¤ããç¨ãã¦ãVMã¨ãã¹ããã·ã³ã§å®è¡ããæã®æéå·®ã確èªãããã¨ã§VMãæ¤ç¥ã§ããã以ä¸ã®ã³ã¼ãã¯winä¸ã®gccã§åä½ããã
# include <stdio.h> int main(void) { unsigned int time1 = 0; unsigned int time2 = 0; __asm__( "RDTSC\n\t" "MOV %0, %%EAX\n\t" "RDTSC\n\t" "MOV %1, %%EAX\n\t" : "=&r" (time1) : "r" (time2) ); if ((time2 - time1) > 100) { printf("%s", "VM detected"); _exit(-1); } printf("%s", "VM not present"); return 0; }
2. ã¬ã¸ã¹ããªãè¦ã
ãWindows ã¬ã¸ã¹ããªã®HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Disk\Enum 0ã®å¤ãè¦ãã¨ããã£ã¹ã¯ã®ååã¨IDãåããããããVMwareãVBOXãªã©ã®å¤ãå«ãã§ãããããVMãæ¤ç¥ã§ããã
ãHKLM\SYSTEM\CurrentControlSet\Services\Disk\Enum\0ã®å¤ã«ãSCSI\Disk&Ven_VMware_&Prod_VMware_Virtual_S&Rev_1.0\4XXXXXXããIDE\DiskVBOX_HARDDISK___________________________1.0_____\4XXXXXXãå«ãã°ãVMä¸ã§åãã¦ããã以ä¸ã®ã³ã¼ãã¯python2.7.8ã§åä½ããã
# coding: UTF-8 import _winreg handle = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Services\\Disk\\Enum' ) try: reg_val = _winreg.QueryValueEx(handle, '0')[0] if "VMware" in reg_val: print "Vmware Detected" elif "VBOX" in reg_val: print "Virtualbox Detected" finally: _winreg.CloseKey(handle)
3. CPUã³ã¢æ°ã®ç¢ºèª
ãVMã®ããã©ã«ãã§ã®CPUã³ã¢æ°ã¯1ã¤ã«ãªã£ã¦ãããä»ã©ããCPUã³ã¢æ°ã1ã¤ã®ç©çãã·ã³ãªãã¦ãªãã®ã§CPUã³ã¢æ°ã確èªãããã¨ã§VMãæ¤ç¥ã§ããã
# coding: UTF-8 import multiprocessing if multiprocessing.cpu_count() == 1: print 'maybe VM' else: print 'real machine?'
è¨å®ããVMã®ã³ã¢æ°ã¯å¤ããããã®ã§ããã®ã¢ã³ãããããã³ã°ã¯ç°¡åã«åé¿ã§ããã
4. ãã£ã¹ã¯å®¹éã®ç¢ºèª
ãVMã®ãã£ã¹ã¯å®¹éã¯ç©çãã·ã³ã«æ¯ã¹ãããªããã¨ãå¤ãã®ã§ããã£ã¹ã¯å®¹éã確èªãããã¨ã§VMãæ¤ç¥ã§ããã以ä¸ã®ã³ã¼ãã¯100GB以ä¸ã®å ´åã¯VMã¨ãã¦ããã
# coding: UTF-8 import os def GetDiskSpaceForWin(drive): from ctypes import c_ulonglong, windll, byref free_bytes_available = c_ulonglong() total_number_of_bytes = c_ulonglong() total_number_of_free_bytes = c_ulonglong() windll.kernel32.GetDiskFreeSpaceExA( drive, byref(free_bytes_available), byref(total_number_of_bytes), byref(total_number_of_free_bytes) ) total_number_of_gigabytes = total_number_of_bytes.value / (1024 ** 3) return total_number_of_gigabytes if __name__ == '__main__': disk_space = 0 if os.name == 'nt': disk_space = GetDiskSpaceForWin('C:') elif os.name == 'posix': statvfs = os.statvfs('/') disk_space = statvfs.f_frsize * statvfs.f_blocks / (1024 ** 3) if disk_space < 100: print 'maybe VM' else: print 'real machine?'
ãã£ã¹ã¯ã®å®¹éä¸ãå²ãå½ã¦ãããä¸éãããã®ã§ãVMã®è¨å®ãå¤æ´ããã ãã§ã¯æ¤ç¥ãéããã®ã¯å³ããã
5. VMwareããã¯ãã¢I/Oãã¼ããç¨ãã
ãVMwareã«ã¯ã²ã¹ãOSã¨ãã¹ãOSã¨ã®éã§æ å ±ãããã¨ãããããã«åå¨ãã VMwareããã¯ãã¢I/Oãã¼ãã¨ããä»çµã¿ãããã®ã§ããã®åå¨ã確èªãããã¨ã§ãVMãæ¤ç¥ã§ããã
ãmacä¸ã®VirtualBoxã使ã£ã¦ãããããç°å¢ããªãã®ã§å®è£ ã¯çç¥ãããã¢ãã©ã¤ã¸ã³ã°â¢ãã«ã¦ã§ã¢ã®3.6ç« ã«è©³ããè¼ã£ã¦ããã®ã§è¦ã¦æ¬²ããã
6. IDTãLDTã¢ãã¬ã¹ã®ç¢ºèª
ãCPU1ã³ã¢ã«ã¤ããIDT(Interrupt Descriptor Table)ã¨ããå²ãè¾¼ã¿ãä¾å¤çºçæã«å¼ã³åºãé¢æ°ã®ãã¼ãã«ãå²ãå½ã¦ãããããã®ã¢ãã¬ã¹ã¯ç©çãã·ã³ã¨ä»®æ³ãã·ã³ã§éãã®ã§ãVMã®æ¤ç¥ã«ã¤ãããããã®ææ³ã¯ã·ã³ã°ã«ããã»ããµãç¨ãããã¦ããã°æå¹ã ãããã«ãããã»ããµåãé²ãã ç¾å¨ã§ã¯ãIDTã¯ã³ã¢ãã¨ã«åå¨ããã®ã§ããã®ææ³ã使ãã®ã¯ãããããã
ãLDT(Local Descriptor Table)ã¨ã¯ãç¹æ¨©å¤æ´ã®ããã®ã¡ã¢ãªã¼ãã¼ãã«ã§ãããåã»ã°ã¡ã³ãã®ãã¼ã¹ã¢ãã¬ã¹ãã¢ã¯ã»ã¹æ¨©ããµã¤ãºãªã©ãããã®ã»ã°ã¡ã³ãã»ã¬ã¯ã¿ã®å¤ãç©çãã·ã³ã¨ä»®æ³ãã·ã³ã§éãã®ã§ãVMã®æ¤ç¥ã«ã¤ããããç©çãã·ã³ã®å ´åã¯0ããä»®æ³ãã·ã³ã®å ´åã¯ãã以å¤ã®å¤ãå ¥ã£ã¦ããã
ããããã¢ãã©ã¤ã¸ã³ã°â¢ãã«ã¦ã§ã¢ã®3.6ç« ã«è©³ããè¼ã£ã¦ããã®ã§è¦ã¦æ¬²ããã