Skip to content

Commit 60eebdd

Browse files
committed
Merge branch 'dev' of https://github.com/kernelwernel/VMAware into dev
2 parents 290203a + 7e4f0e6 commit 60eebdd

2 files changed

Lines changed: 66 additions & 45 deletions

File tree

src/cli.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include <iostream>
2626
#include <vector>
2727
#include <cstdint>
28-
#include <bit>
2928

3029
#if (defined(__GNUC__) || defined(__linux__))
3130
#include <unistd.h>
@@ -168,6 +167,7 @@ R"(Usage:
168167
--mit ignore the GPL techniques and run only the MIT-supported ones
169168
--enums display the technique enum name used by the lib
170169
)";
170+
171171
std::exit(0);
172172
}
173173

@@ -279,6 +279,7 @@ Qihoo 360 Sandbox
279279
nsjail
280280
Hypervisor-Phantom
281281
)";
282+
282283
std::exit(0);
283284
}
284285

@@ -720,7 +721,7 @@ std::string vm_description(const std::string& vm_brand) {
720721
RtlInitUnicodeString(&name, L"\\??\\C:\\Program Files\\KernelLogger");
721722

722723
HANDLE hFile;
723-
IO_STATUS_BLOCK iosb = { 0 };
724+
IO_STATUS_BLOCK iosb = { { 0 } };
724725
OBJECT_ATTRIBUTES attrs{};
725726
InitializeObjectAttributes(&attrs, &name, 0, NULL, NULL);
726727

src/vmaware.hpp

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2940,8 +2940,13 @@ struct VM {
29402940
#endif
29412941

29422942
#elif (WINDOWS)
2943+
// Clang/GCC on x64 emits a full 10-byte SIDT (16-bit limit + 64-bit base), on 32-bit it still only writes 6 bytes
2944+
#if defined(_M_X64) || defined(__x86_64__)
2945+
unsigned char m[10] = { 0 };
2946+
#else
29432947
unsigned char m[6] = { 0 };
2944-
u32 idt = 0;
2948+
#endif
2949+
u32 idt = 0;
29452950

29462951
__try {
29472952
#if (CLANG || GCC)
@@ -2967,13 +2972,16 @@ struct VM {
29672972
__except (EXCEPTION_EXECUTE_HANDLER) {
29682973
return false; // umip
29692974
}
2975+
2976+
// Extract 32-bit base from bytes [2..5]
29702977
idt = *((unsigned long*)&m[2]);
29712978

29722979
if ((idt >> 24) == 0xE8) {
29732980
return core::add(brands::VPC);
29742981
}
29752982

2976-
return (m[5] > 0xD0); // top‐most byte of the 64‑bit base
2983+
// On x64, m[5] is the top byte of the 64-bit base; on x86 it's high byte of 32-bit base
2984+
return (m[5] > 0xD0);
29772985
#endif
29782986
}
29792987

@@ -4445,7 +4453,7 @@ struct VM {
44454453
* @implements VM::VPC_INVALID
44464454
*/
44474455
[[nodiscard]] static bool vpc_invalid() {
4448-
#if (WINDOWS && x86_32)
4456+
#if (WINDOWS && x86_32 && !CLANG)
44494457
bool rc = false;
44504458

44514459
auto IsInsideVPC_exceptionFilter = [](PEXCEPTION_POINTERS ep) -> DWORD {
@@ -4502,38 +4510,50 @@ struct VM {
45024510
*/
45034511
[[nodiscard]] static bool sgdt() {
45044512
#if (WINDOWS)
4513+
#if defined(_M_X64) || defined(__x86_64__)
4514+
unsigned char gdtr[10] = { 0 };
4515+
#else
45054516
unsigned char gdtr[6] = { 0 };
4517+
#endif
45064518
unsigned int gdt = 0;
45074519

45084520
__try {
4509-
#if (CLANG || GCC)
4521+
#if (CLANG || GCC)
45104522
__asm__ volatile("sgdt %0" : "=m"(gdtr));
4511-
#elif (MSVC && x86_32)
4523+
#elif (MSVC && x86_32)
45124524
__asm {
45134525
sgdt gdtr
45144526
}
4515-
#elif (MSVC)
4527+
#elif (MSVC)
45164528
#pragma pack(push, 1)
4517-
struct { unsigned short limit; unsigned long long base; } _gdtr = {};
4529+
struct {
4530+
unsigned short limit;
4531+
unsigned long long base;
4532+
} _gdtr = {};
45184533
#pragma pack(pop)
4534+
45194535
_sgdt(&_gdtr);
45204536
std::memcpy(gdtr, &_gdtr, sizeof(gdtr));
4521-
#else
4537+
#else
45224538
return false;
4523-
#endif
4539+
#endif
45244540
}
45254541
__except (EXCEPTION_EXECUTE_HANDLER) {
45264542
return false; // umip
45274543
}
45284544

4545+
// 32-bit base from bytes [2..5]
45294546
std::memcpy(&gdt, &gdtr[2], sizeof(gdt));
45304547

4531-
if (gdtr[5] > 0xD0) { // top‐most byte of the 64‑bit base
4548+
// On x64, gdtr[5] is the top byte of the 64-bit base; on x86 it's high byte of 32-bit base
4549+
if (gdtr[5] > 0xD0) {
45324550
debug("SGDT: top-most byte signature detected");
45334551
return true;
45344552
}
4553+
4554+
// 0xFF signature in the high byte of the 32-bit base
45354555
return ((gdt >> 24) == 0xFF);
4536-
#else
4556+
#else
45374557
return false;
45384558
#endif
45394559
}
@@ -4712,7 +4732,7 @@ struct VM {
47124732
* @implements VM::VMWARE_BACKDOOR
47134733
*/
47144734
[[nodiscard]] static bool vmware_backdoor() {
4715-
#if (WINDOWS && x86_32)
4735+
#if (WINDOWS && x86_32 && !CLANG)
47164736
u32 a = 0;
47174737
u32 b = 0;
47184738

@@ -4721,49 +4741,49 @@ struct VM {
47214741
bool is_vm = false;
47224742

47234743
for (u8 i = 0; i < ioports.size(); ++i) {
4724-
ioport = ioports[i];
4725-
for (u8 cmd = 0; cmd < 0x2c; ++cmd) {
4726-
__try {
4727-
__asm {
4728-
push eax
4729-
push ebx
4730-
push ecx
4731-
push edx
4732-
4733-
mov eax, 'VMXh'
4734-
movzx ecx, cmd
4735-
mov dx, ioport
4736-
in eax, dx // <- key point is here
4737-
4738-
mov a, ebx
4739-
mov b, ecx
4740-
4741-
pop edx
4742-
pop ecx
4743-
pop ebx
4744-
pop eax
4745-
}
4744+
ioport = ioports[i];
4745+
for (u8 cmd = 0; cmd < 0x2c; ++cmd) {
4746+
__try {
4747+
__asm {
4748+
push eax
4749+
push ebx
4750+
push ecx
4751+
push edx
4752+
4753+
mov eax, 'VMXh'
4754+
movzx ecx, cmd
4755+
mov dx, ioport
4756+
in eax, dx // <- key point is here
4757+
4758+
mov a, ebx
4759+
mov b, ecx
4760+
4761+
pop edx
4762+
pop ecx
4763+
pop ebx
4764+
pop eax
4765+
}
47464766

4747-
is_vm = true;
4748-
break;
4767+
is_vm = true;
4768+
break;
4769+
}
4770+
__except (EXCEPTION_EXECUTE_HANDLER) {}
47494771
}
4750-
__except (EXCEPTION_EXECUTE_HANDLER) {}
47514772
}
4752-
}
47534773

4754-
if (is_vm) {
4755-
switch (b) {
4774+
if (is_vm) {
4775+
switch (b) {
47564776
case 1: return core::add(brands::VMWARE_EXPRESS);
47574777
case 2: return core::add(brands::VMWARE_ESX);
47584778
case 3: return core::add(brands::VMWARE_GSX);
47594779
case 4: return core::add(brands::VMWARE_WORKSTATION);
47604780
default: return core::add(brands::VMWARE);
4781+
}
47614782
}
4762-
}
47634783

4764-
return false;
4784+
return false;
47654785
#else
4766-
return false;
4786+
return false;
47674787
#endif
47684788
}
47694789

0 commit comments

Comments
 (0)