学逆向论坛

找回密码
立即注册

只需一步,快速开始

发新帖

2万

积分

41

好友

1157

主题
发表于 2020-10-18 15:59:10 | 查看: 5713| 回复: 1
通过CPUID
原理:通过检查功能号1的CPUID返回的ecx的最高位是否为1,如果为1,则在虚拟环境下运行。真机的最高位不为1.
  • 检测处理器是否支持 cpuid 指令(忽略这一条)
    现在的CPU都支持 cpuid 指令,没必要去检测是否支持,除非在很早的的机器上运行才有必要(那好像也要80486的机器吧)。
    在 eflags.ID 标志位是 Processor Feature Identification 位,即最高位,通过修改这个标志位的值,以此来检测是否支持 cpuid 指令。

    编写驱动检测虚拟机

    编写驱动检测虚拟机
  • 要使用[size=13.475px]CPUID指令,输入[size=13.475px]eax表示功能号(类似中断的用法)
    输出[size=13.475px]eax,ebx,ecx,edx
    在EDX和ECX中返回的功能标志表明着该CPU都支持那些功能
    ECX返回值定义如下(资料来自Intel):
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    bit   Name     Description
    ---------------------------------------------------------
      00   SSE3     Streaming SIMD Extensions 3
      01            Reserved
      02   DTES64   64-Bit Debug Store
      03   MONITOR  MONITOR/MWAIT
      04   DS-CPL   CPL Qualified Debug Store
      05   VMX      Virtual Machine Extensions
      06   SMX      Safer Mode Extensions
      07   EST      Enhanced Intel SpeedStep® Technology
      08   TM2      Thermal Monitor 2
      09   SSSE3    Supplemental Streaming SIMD Extensions 3
      10   CNXT-ID  L1 Context ID
    12:11           Reserved
      13   CX16     CMPXCHG16B
      14   xTPR     xTPR Update Control
      15   PDCM     Perfmon and Debug Capability
    17:16           Reserved
      18   DCA      Direct Cache Access
      19   SSE4.1   Streaming SIMD Extensions 4.1
      20   SSE4.2   Streaming SIMD Extensions 4.2
      21   x2APIC   Extended xAPIC Support
      22   MOVBE    MOVBE Instruction
      23   POPCNT   POPCNT Instruction
    25:24           Reserved
      26   XSAVE    XSAVE/XSTOR States
      27   OSXSAVE
    31:28           Reserved

    下面是使用CPUID检测的核心汇编代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    .CODE
    getcpuid PROC
      xor eax,eax
      mov eax,1h
      cpuid
      mov a,eax
      mov b,ebx
      mov c_var,ecx
      mov d,edx
      ret
    getcpuid ENDP
    END

    通过利用[size=13.475px]IoInitializeTimer配合[size=13.475px]IoStartTimer的定时器来循环判定。
遍历模块
我们说到的第一种CPUID的方法可能通过修改VMX的配置等方法绕过。
还有一种方式是遍历当前系统中所有的sys。我们要查找的是vmmouse.sys,vmrawdsk.sys,vmusbmouse.sys中的任意一个。
核心是利用NtQuerySystemInformation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <ntddk.h>
#include <windef.h>


typedef struct _SYSTEM_MODULE_INFORMATION_ENTRY
{
    HANDLE Section;
    PVOID MappedBase;
    PVOID Base;
    ULONG Size;
    ULONG Flags;
    USHORT LoadOrderIndex;
    USHORT InitOrderIndex;
    USHORT LoadCount;
    USHORT PathLength;
    CHAR ImageName[256];
} SYSTEM_MODULE_INFORMATION, *PSYSTEM_MODULE_INFORMATION;


typedef NTSTATUS(*NTQUERYSYSTEMINFORMATION) (
    IN ULONG SystemInformationClass,
    OUT PVOID SystemInformation,
    IN ULONG_PTR SystemInformationLength,
    OUT PULONG_PTR ReturnLength OPTIONAL
    );

VOID getcpuid();
BOOLEAN CheckDriverModule();

UNICODE_STRING symLinkName = { 0 };
PDEVICE_OBJECT pDevice;
PETHREAD pThreadObject = NULL;
BOOLEAN boom = FALSE;

//eax为CPU型号
//若为虚拟机,则ecx最高位为1;如果是物理机,则最高位为0.
//code in cpuid.asm
DWORD a = 0, b = 0, c_var = 0, d = 0;


VOID Unload() {//PDRIVER_OBJECT DriverObject
    if (pThreadObject) {
        boom = TRUE;
        KeWaitForSingleObject(pThreadObject, Executive, KernelMode, FALSE, NULL);
    }
    if (pDevice) {
        IoStopTimer(pDevice);
        IoDeleteSymbolicLink(&symLinkName);
        IoDeleteDevice(pDevice);
        pDevice = NULL;
    }
    DbgPrint("Unload --------");
}

VOID TimerRoutine(PDEVICE_OBJECT DeviceObject, PVOID context) {
    DbgPrint("In TimerRoutine\n");
    //Begin normal check
    getcpuid();
    c_var = c_var >> 31;
    if (c_var){
        DbgPrint("Running in the virtual machine 2. Find VMWare by CPUID!!!!\n");
    }
}

BOOLEAN CheckDriverModule() {
    BOOLEAN bRet = FALSE;
    NTQUERYSYSTEMINFORMATION m_NtQuerySystemInformation = NULL;
    UNICODE_STRING NtQuerySystemInformation_Name = { 0 };
    PSYSTEM_MODULE_INFORMATION ModuleEntry = NULL;
    ULONG_PTR RetLength = 0, BaseAddr = 0, EndAddr = 0;
    ULONG ModuleNums = 0, Index = 0;
    NTSTATUS status = STATUS_SUCCESS;
    PVOID Buffer = NULL;
    RtlInitUnicodeString(&NtQuerySystemInformation_Name, L"NtQuerySystemInformation");
    do
    {
        m_NtQuerySystemInformation = (NTQUERYSYSTEMINFORMATION)MmGetSystemRoutineAddress(&NtQuerySystemInformation_Name);
        if (m_NtQuerySystemInformation == NULL) {
            bRet = TRUE;
            break;
        }
        status = m_NtQuerySystemInformation(0xb, NULL, 0, &RetLength);
        if (status < 0 && status != STATUS_INFO_LENGTH_MISMATCH) {
            bRet = TRUE;
            break;
        }
        Buffer = ExAllocatePoolWithTag(PagedPool, RetLength, "ytz");
        if (Buffer == NULL) {
            bRet = TRUE;
            break;
        }
        RtlZeroMemory(Buffer, RetLength);
        status = m_NtQuerySystemInformation(0xb, Buffer, RetLength, &RetLength);
        if (status < 0) {
            bRet = TRUE;
            break;
        }
        ModuleNums = *(ULONG*)Buffer;
        ModuleEntry = (PSYSTEM_MODULE_INFORMATION)((ULONG_PTR)Buffer + 8);
        for (Index = 0; Index < ModuleNums; Index++) {
            if (strstr(ModuleEntry->ImageName, "vmmouse.sys") ||
                strstr(ModuleEntry->ImageName, "vmrawdsk.sys") ||
                strstr(ModuleEntry->ImageName, "vmusbmouse.sys")) {


                DbgPrint("The Module Name is %s\n", ModuleEntry->ImageName);
                bRet = TRUE;
                break;
            }
            ModuleEntry++;
        }
        break; //Amuse
    } while (TRUE);
    if (Buffer)
    {
        ExFreePool(Buffer);
        Buffer = NULL;
    }
    return bRet;

}

VOID CheckVmWare(PVOID context) {
    LARGE_INTEGER sleeptime = { 0 };
    sleeptime.QuadPart = -20000000;
    while (1)
    {
        if (boom)
            break;
        if (CheckDriverModule()) {
            DbgPrint("Running in the virtual machine 1. Find VMWare!!!!\n");
        }
        DbgPrint("Thread is working");
        KeDelayExecutionThread(KernelMode, 0, &sleeptime);
    }
    PsTerminateSystemThread(STATUS_SUCCESS);

}

NTSTATUS DriverEntry(PDRIVER_OBJECT driverObject, PUNICODE_STRING RegPath) {
    HANDLE hThread = NULL;
    NTSTATUS status = NULL;
    UNICODE_STRING DeviceName = { 0 };
    driverObject->DriverUnload = Unload;

    status = PsCreateSystemThread(&hThread, 0, NULL, NULL, NULL, CheckVmWare, NULL);
    if (!NT_SUCCESS(status)) {
        DbgPrint("Create Thread Failed!\n");
        return STATUS_UNSUCCESSFUL;
    }

    status = ObReferenceObjectByHandle(hThread, THREAD_ALL_ACCESS, *PsThreadType, KernelMode, &pThreadObject, NULL);
    if (!NT_SUCCESS(status))
    {
        DbgPrint("Cannot reference");
        ObDereferenceObject(pThreadObject);
    }

    RtlInitUnicodeString(&DeviceName, L"\\Device\\MyDevices");
    status = IoCreateDevice(driverObject, 0, &DeviceName, FILE_DEVICE_UNKNOWN, 0, TRUE, &pDevice);
    if (!NT_SUCCESS(status)) {
        DbgPrint("Create Device Failed!");
        return STATUS_UNSUCCESSFUL;
    }
    RtlInitUnicodeString(&symLinkName, "\\?\\SybLinkName");
    status = IoCreateSymbolicLink(&symLinkName, &DeviceName);
    if (!NT_SUCCESS(status)) {
        DbgPrint("Create SymLink Failed!");
        //return STATUS_UNSUCCESSFUL;
    }
    IoInitializeTimer(pDevice, (PIO_TIMER_ROUTINE)TimerRoutine, NULL);
    IoStartTimer(pDevice);

    ZwClose(hThread);
    return STATUS_SUCCESS;
}

注:在X64下使用上文的汇编代码时,需要修改下asm的属性

编写驱动检测虚拟机

编写驱动检测虚拟机

效果

编写驱动检测虚拟机

编写驱动检测虚拟机
温馨提示:
1.如果您喜欢这篇帖子,请给作者点赞评分,点赞会增加帖子的热度,评分会给作者加学币。(评分不会扣掉您的积分,系统每天都会重置您的评分额度)。
2.回复帖子不仅是对作者的认可,还可以获得学币奖励,请尊重他人的劳动成果,拒绝做伸手党!
3.发广告、灌水回复等违规行为一经发现直接禁言,如果本帖内容涉嫌违规,请点击论坛底部的举报反馈按钮,也可以在【投诉建议】板块发帖举报。
论坛交流群:672619046
发表于 2020-10-19 20:37:23
感谢分享,我会认真学习的!

小黑屋|手机版|站务邮箱|学逆向论坛 ( 粤ICP备2021023307号 )|网站地图

GMT+8, 2024-3-29 14:43 , Processed in 0.100816 second(s), 43 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表