Skip to content

Commit d08750b

Browse files
author
Requiem
committed
fix: x86_32 compatibility with the new methods
1 parent ce5a2bb commit d08750b

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

src/vmaware.hpp

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3821,18 +3821,13 @@ struct VM {
38213821
else {
38223822
// Windows machine running under Hyper-V type 1
38233823
// If we reach here, we do some sanity checks to ensure a hypervisor is not trying to spoof itself as Hyper-V, bypassing some detections
3824-
#if (x86_64)
3825-
u8 idtr_buffer[10] = { 0 };
3826-
#else
3827-
u8 idtr_buffer[6] = { 0 };
3828-
#endif
3824+
#if (x86_64)
3825+
u8 idtr_buffer[10] = { 0 };
38293826

38303827
// we know we're not using SEH here, it's on purpose, and doesn't matter the CPU core
38313828
#if (CLANG || GCC)
38323829
__asm__ volatile("sidt %0" : "=m"(idtr_buffer));
3833-
#elif (MSVC) && (x86_32)
3834-
__asm { sidt idtr_buffer }
3835-
#elif (MSVC) && (x86_64)
3830+
#elif (MSVC)
38363831
#pragma pack(push, 1)
38373832
struct {
38383833
USHORT Limit;
@@ -3846,7 +3841,7 @@ struct VM {
38463841
ULONG_PTR idt_base = 0;
38473842
memcpy(&idt_base, &idtr_buffer[2], sizeof(idt_base));
38483843

3849-
// if running under Hyper-V (doesnt matter the VTL/partition level), this value is hardcoded and intercepted/emulated at kernel level
3844+
// if running under Hyper-V in AMD64 (doesnt matter the VTL/partition level), this value is hardcoded and intercepted/emulated at kernel level
38503845
// specifically at KiPreprocessFault -> KiOpDecode -> KiOpLocateDecodeEntry (KiOp_SLDTSTRSMSW)
38513846
// this is intercepted by the kernel before handling execution to the hypervisor, so it's a decent safeguard against basic cpuid spoofing
38523847
if (idt_base == 0xfffff80000001000) {
@@ -3858,6 +3853,11 @@ struct VM {
38583853
debug("HYPER-X: Detected hypervisor trying to spoof itself as Hyper-V");
38593854
state = HYPERV_UNKNOWN; // doing this is enough to trigger a VM detection, we dont need to mark a 100% vm score as our techniques will do the job for us
38603855
}
3856+
#else
3857+
debug("HYPER-X: Detected Hyper-V host machine");
3858+
core::add(brand_enum::HYPERV_ROOT);
3859+
state = HYPERV_ARTIFACT_VM;
3860+
#endif
38613861
}
38623862
}
38633863

@@ -5020,10 +5020,17 @@ struct VM {
50205020
#else
50215021
auto is_smt_enabled = []() noexcept -> bool {
50225022
auto popcount = [](uint64_t v) noexcept -> int {
5023-
#if (GCC || CLANG)
5023+
#if (GCC) || (CLANG)
50245024
return __builtin_popcountll(v);
50255025
#elif (MSVC)
5026+
#if (x86_32)
5027+
return static_cast<int>(
5028+
__popcnt(static_cast<unsigned int>(v)) +
5029+
__popcnt(static_cast<unsigned int>(v >> 32))
5030+
);
5031+
#else
50265032
return static_cast<int>(__popcnt64(static_cast<unsigned long long>(v)));
5033+
#endif
50275034
#else
50285035
int c = 0;
50295036
while (v) { c += static_cast<int>(v & 1ull); v >>= 1; }
@@ -11838,25 +11845,25 @@ struct VM {
1183811845
tls_state = &state;
1183911846

1184011847
// lambda to capture exceptions
11841-
PVECTORED_EXCEPTION_HANDLER handler =
11842-
+[](PEXCEPTION_POINTERS ep) -> LONG {
11843-
if (!tls_state || !tls_state->in_asm)
11844-
return EXCEPTION_CONTINUE_SEARCH;
11848+
const PVOID vh = AddVectoredExceptionHandler(
11849+
1,
11850+
[](PEXCEPTION_POINTERS ep) -> LONG {
11851+
if (!tls_state || !tls_state->in_asm)
11852+
return EXCEPTION_CONTINUE_SEARCH;
1184511853

11846-
const DWORD code = ep->ExceptionRecord->ExceptionCode;
11847-
if (code == EXCEPTION_ILLEGAL_INSTRUCTION) {
11848-
tls_state->exception_seen = 1;
11849-
#if (x86_64)
11850-
ep->ContextRecord->Rip += 3;
11851-
#else
11852-
ep->ContextRecord->Eip += 3;
11853-
#endif
11854-
return EXCEPTION_CONTINUE_EXECUTION;
11854+
const DWORD code = ep->ExceptionRecord->ExceptionCode;
11855+
if (code == EXCEPTION_ILLEGAL_INSTRUCTION) {
11856+
tls_state->exception_seen = 1;
11857+
#if (x86_64)
11858+
ep->ContextRecord->Rip += 3;
11859+
#else
11860+
ep->ContextRecord->Eip += 3;
11861+
#endif
11862+
return EXCEPTION_CONTINUE_EXECUTION;
11863+
}
11864+
return EXCEPTION_CONTINUE_SEARCH;
1185511865
}
11856-
return EXCEPTION_CONTINUE_SEARCH;
11857-
};
11858-
11859-
const PVOID vh = AddVectoredExceptionHandler(1, handler);
11866+
);
1186011867
if (!vh) return false;
1186111868

1186211869
// xor rdpru ret

0 commit comments

Comments
 (0)