Evasions: CPU
Contents
CPU detection methods used
1. Check vendor ID string via CPUID instruction
2. Check if being run in Hypervisor via CPUID instruction
3. Check for global tables location: IDT/GDT/LDT
4. Using exotic instructions to fool virtual emulators
5. Detecting environment via execution of illegal instructions (VirtualPC only)
6. Detecting environment via IN instruction - backdoor port (VMware only)
Signature recommendations
Countermeasures
Credits
CPU detection methods used
Techniques in this group use specific processor instructions to either get particular information about CPU — or execute predefined instruction sequence which behaves differently in usual host OS and in virtual environment.
1. Check vendor ID string via CPUID instruction
The CPUID instruction is an instruction that returns processor identification and feature information to EBX, ECX, EDX. The information received to these registers can be used to identify a vendor.
Code sample
Detections table
Check vendor ID string via CPUID instruction - returned in parts in EBX, ECX, EDX: | ||
Detect | EAX as argument to CPUID | String |
---|---|---|
FreeBSD HV | 0x40000000 | bhyve bhyve |
Hyper-V | 0x40000000 | Microsoft Hv |
KVM | 0x40000000 | KVMKVMKVM |
Parallels | 0x40000000 | prl hyperv |
VirtualBox | 0x40000000 | VBoxVBoxVBox |
VirtualPC | 0x40000000 | Microsoft Hv |
VMware | 0x40000000 | VMwareVMware |
Xen | 0x40000000 | XenVMMXenVMM |
2. Check if being run in Hypervisor via CPUID instruction
An other way to detect if the program is being run in hypervisor is using the CPUID instruction in an other way.
Instead of setting EAX (the argument to CPUID) to be 0x40000000, EAX is set to 1.
When EAX is set to 1, the 31st bit in ECX (CPUID’s returned value) is set, it indicates that the program is being run in Hypervisor.
Code sample (function GetAdaptersAddresses)
Detections table
Check if being run in Hypervisor (via CPUID) | ||
Detect | EAX as argument to CPUID | Check of return value |
---|---|---|
Hypervisor | 1 | 31st bit in ECX - set if run in Hypervisor |
3. Check for global tables location: IDT/GDT/LDT
This technique doesn’t work on latest VMware releases (all Windows releases affected). However, it is described here for the sake of completeness.
This trick involves looking at the pointers to critical operating system tables that are typically relocated on a virtual machine. It’s what called “Red Pill” and was first introduced by Joanna Rutkowska.
There is one Local Descriptor Table Register (LDTR), one Global Descriptor Table Register (GDTR), and one Interrupt Descriptor Table Register (IDTR) per CPU. They have to be moved to a different location when a guest operating system is running to avoid conflicts with the host.
On real machines the IDT, for example, is located lower in memory than it is on guest (i.e., virtual) machines.
Code sample
Credits for this code sample: al-khaser project
4. Using exotic instructions to fool virtual emulators
This technique is described by this link (slide #37).
MMX instructions may be used as random instructions by malware. Sometimes such subsets of CPU instruction are not supported by emulators and thus exception is thrown instead of performing analysis.
Example:
5. Detecting environment via execution of illegal instructions (VirtualPC only)
The malware executes illegal instructions, which should generate exception on the real CPU but are executed normally - or in some different way - in virtual environment.
Information about CPU exceptions is provided by this link.
Code sample (variant 1, generating #ud exception)
It should be emphasized that there are more than 1,000 combinations of
bytes that may be used by malware in order to detect VirtualPC enviroment.
Code sample (variant 2, executing illegal STI instruction)
Code sample (variant 3, resetting VirtualPC)
6. Detecting environment via IN instruction - backdoor port (VMware only)
This article explains why backdoor port communication is used in VMware in the first place.
Code sample (variant 1)
Code sample (variant 2)
Signature recommendations
No signature recommendations are provided for this evasion group as it’s hard to track such a code being executed.
Countermeasures
Patch hypervisor. If it proves impossible — due to license issues or something else — patch VM config. Usually undocumented options help.
- vs CPUID instruction: refer to this article for the example of such a patch
- vs IN instruction (VMware backdoor): take a look at these config changes
Credits
Credits go to open-source project from where code samples were taken and to independent researcher who shared his findings:
- al-khaser project on github
- @waleedassar
Though Check Point tool InviZzzible has them all implemented, due to modular structure of the code it would require more space to show a code sample from this tool for the same purposes. That’s why we’ve decided to use other great open-source projects for examples throughout the encyclopedia.