[toc]

课堂复习

  1. 异常的分发共有几轮?

    两轮,KiDispatchException 函数的最后一个参数 FirstChance 表示当前是第几次进行异常分发,另一个函数 RaiseException 的最后一个参数也表示当前是第几次进行分发

  2. 通过什么可以区分当前所处的是 R0 还是 R3?

    windows 下,代码被分为 R3 和 R0 权限,CS 寄存器的最低2位表示当前所处的是3环(用户)还是0环(内核),可通过 mov eax,cs test eax,1 来区分

  3. 异常产生方式有几种?

    • CPU 满足特定条件后内部主动产生的异常,类似 int 3(IDT)
    • 用户通过 RaiseException 构建 ExceptionRecord 主动抛出异常(KiDispatchException
  4. 编译器会为用户自定义的 __try__except 添加怎样的异常处理函数?

    同一个函数内,无论用户编写多少个 SEH,编译器只会安装一个 except_handler4

  5. 当用户模式下产生异常时,SEH 函数会在什么时候被调用?

    int 3 -> idt[3] -> _KiTrap03 -> CommonDispatchException -> KiDispatchException -> KeUserExceptionDispatcher(3) -> RtlDispatchException(3) -> RtlpExcuteHandlerForException(3) -> except_handler4 -> except_handler4_common

  6. 在 R0 中异常是如何被传递给3环调试器的?

    DbgkForwardException -> DbgkpSendApiMessage -> 3环调试器

  7. R0 和 R3 的 RtlDispatchException 有什么区别?

    • KiDispatchException(0) -> RtlDispatchException(0) -> SEH
    • KiUserExceptionDispatcher(3) -> RtlDispatchException (3) -> VEH SEH UEH (VCH)

反调试与反反调试

静态反调试

静态反调试一般在调试开始时阻拦调试者,调试者只需找到原因后可一次性突破

PEB

对于所有的用户层 PEB 静态反调试,可以在程序正式运行前先挂起用户程序,然后修改相应的字段为非调试状态,再继续执行

kernel32!IsDebuggerPresent() API 检测进程环境块(PEB)中的 BeingDebugged 标志,检查这个标志以确定进程是否正在被用户模式的调试器调试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <windows.h>
// 当程序被调试的时候, BeingDebugged 字段保存的是 1
bool CheckBeingDebugged()
{
__asm
{
; 通过 FS:[0x30] 获取到 PEB 结构体的地址
mov eax, dword ptr fs:[0x30]

; 通过 PEB 偏移为 0x02 的地方获取到 BeingDebugged
movzx eax, byte ptr [eax + 0x02]
}
}
int main()
{
if (CheckBeingDebugged())
printf("当前处于[被]调试状态\n");
else
printf("当前处于[非]调试状态\n");
system("pause");
return 0;
}

调用 IsDebuggerPresent 字段,间接读 BeingDebugged 字段

1
2
3
4
5
6
7
8
9
10
int main() 
{
// IsDebuggerPresent 就是在检查 PEB.BeingDebugged 字段
if (IsDebuggerPresent())
printf("当前处于[被]调试状态\n");
else
printf("当前处于[非]调试状态\n");
system("pause");
return 0;
}

通常程序未被调用时,PEB 另一个成员 NtGlobalFlag(偏移0x68)值为0,若进程被调试,通常值为0x70

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
bool CheckNtGlobalFlag()
{
int NtGlobalFlag = 0;
__asm
{
; 通过 TEB 偏移为 0x30 找到 PEB 结构
mov eax, dword ptr fs : [0x30]

; 通过 PEB 偏移为 0x68 的地方找到 NtGlobalFlag
mov eax, dword ptr[eax + 0x68]

; 将结果保存到变量,目的是方便比较
mov NtGlobalFlag, eax
}
return NtGlobalFlag == 0x70 ? true : false;
}
int main()
{
if (CheckNtGlobalFlag())
printf("当前处于[被]调试状态\n");
else
printf("当前处于[非]调试状态\n");

system("pause");
return 0;
}

_HEAP 结构不是一个公开的结构体,不同版本的 nt 内核可能对这个结构体有不同的实现,所以兼容性不够强

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
bool CheckProcessHeap()
{
int Flags = 0, ForceFlags = 0;
__asm
{
; 通过 fs : [0x30] 可以找到 PEB 的地址
mov eax, dword ptr fs:[0x30]

; 通过 PEB 偏移为 0x18 的位置找到 ProcessHeap(_HEAP)
mov eax, dword ptr [eax + 0x18]

; 通过 _HEAP 偏移为 0x400x44 的字段找到两个标志
mov ecx, dword ptr [eax + 0x40]
mov Flags, ecx
mov ecx, dword ptr [eax + 0x44]
mov ForceFlags, ecx
}
printf("%08X %08X\n", Flags, ForceFlags);
return Flags != 2 || ForceFlags != 0;
}
int main()
{
if (CheckProcessHeap())
printf("当前处于[被]调试状态\n");
else
printf("当前处于[非]调试状态\n");
system("pause");
return 0;
}

使用原始API

对于所有使用函数进行反调试的情况,都可以使用 ApiHook 来进行反反调试,但是要注意 NtQueryInformationProcess 功能非常多,在函数内应该过滤和调试相关的枚举值进行操作,不应该影响到其它的查询信息

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
#include <iostream>
#include <windows.h>
#include <winternl.h>
#pragma comment(lib,"ntdll.lib")
// 如果进程处于被调试状态,那么函数接收的就是 -1
bool CheckProcessDebugPort()
{
int nDebugPort = 0;
NtQueryInformationProcess(
GetCurrentProcess(), // 目标进程句柄
ProcessDebugPort, // 查询信息类型
&nDebugPort, // 输出查询信息
sizeof(nDebugPort), // 查询类型大小
NULL); // 实际返回数据大小
return nDebugPort == 0xFFFFFFFF ? true : false;
}
int main()
{
if (CheckProcessDebugPort())
printf("当前处于[被]调试状态\n");
else
printf("当前处于[非]调试状态\n");
system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 如果当前的程序被调试了,那么保存的就是非零值
bool CheckProcessDebugObjectHandle()
{
HANDLE hProcessDebugObjectHandle = 0;
NtQueryInformationProcess(
GetCurrentProcess(), // 目标进程句柄
(PROCESSINFOCLASS)0x1E, // 查询信息类型
&hProcessDebugObjectHandle, // 输出查询信息
sizeof(hProcessDebugObjectHandle), // 查询类型大小
NULL); // 实际返回大小
return hProcessDebugObjectHandle ? true : false;
}
int main()
{
if (CheckProcessDebugObjectHandle())
printf("当前处于[被]调试状态\n");
else
printf("当前处于[非]调试状态\n");
system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 如果被调试了就是0,非则就是其它值
bool CheckProcessDebugFlag()
{
BOOL bProcessDebugFlag = 0;
NtQueryInformationProcess(
GetCurrentProcess(), // 目标进程句柄
(PROCESSINFOCLASS)0x1F, // 查询信息类型
&bProcessDebugFlag, // 输出查询信息
sizeof(bProcessDebugFlag), // 查询类型大小
NULL); // 实际返回大小
return bProcessDebugFlag ? false : true;
}
int main()
{
if (CheckProcessDebugFlag())
printf("当前处于[被]调试状态\n");
else
printf("当前处于[非]调试状态\n");
system("pause");
return 0;
}

当一个普通的程序被双击打开时,实际是被资源管理器(Explorer)打开的,所以普通程序的父进程应该就是资源管理器,若检查到自己的父进程不是,就是被调试了

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
bool CheckParentProcess()
{
struct PROCESS_BASIC_INFORMATION {
ULONG ExitStatus; // 进程返回码
PPEB PebBaseAddress; // PEB地址
ULONG AffinityMask; // CPU亲和性掩码
LONG BasePriority; // 基本优先级
ULONG UniqueProcessId; // 本进程PID
ULONG InheritedFromUniqueProcessId; // 父进程PID
}stcProcInfo;
// 查询到进程相关的基本信息,需要提供一个结构体进行接收
NtQueryInformationProcess(GetCurrentProcess(), ProcessBasicInformation,
&stcProcInfo, sizeof(stcProcInfo), NULL);
DWORD ExplorerPID = 0;
//获取父进程ID
DWORD CurrentPID = stcProcInfo.InheritedFromUniqueProcessId;
// 以资源管理器的类名查询到资源管理所在的进程PID
GetWindowThreadProcessId(FindWindow(L"Progman", NULL), &ExplorerPID);
// 如果相同就说明没有被调试
return ExplorerPID == CurrentPID ? false : true;
}
int main()
{
if (CheckParentProcess())
printf("当前处于[被]调试状态\n");
else
printf("当前处于[非]调试状态\n");
system("pause");
return 0;
}

查询当前系统的调试情况: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
bool CheckSystemKernelDebuggerInformation() 
{
// 保存了和系统调试相关的属性
struct _SYSTEM_KERNEL_DEBUGGER_INFORMATION
{
BOOLEAN KernelDebuggerEnabled;
BOOLEAN KernelDebuggerNotPresent;
} DebuggerInfo = { 0 };
// 查询当前系统的调试情况
NtQuerySystemInformation(
(SYSTEM_INFORMATION_CLASS)0x23, // 查询信息类型
&DebuggerInfo, // 输出查询信息
sizeof(DebuggerInfo), // 查询类型大小
NULL); // 实际返回大小
return DebuggerInfo.KernelDebuggerEnabled;
}
int main()
{
if (CheckSystemKernelDebuggerInformation())
printf("当前处于[被]调试状态\n");
else
printf("当前处于[非]调试状态\n");
system("pause");
return 0;
}

动态反调试

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
#include <iostream>
#include <windows.h>
typedef enum THREAD_INFO_CLASS {
ThreadHideFromDebugger = 17
};
// 一个未公开的函数,需要手动的使用 GetProcAddress 获取
typedef NTSTATUS(NTAPI* ZW_SET_INFORMATION_THREAD)(
IN HANDLE ThreadHandle,
IN THREAD_INFO_CLASS ThreadInformaitonClass,
IN PVOID ThreadInformation,
IN ULONG ThreadInformationLength);
void ZSIT_DetachDebug()
{
ZW_SET_INFORMATION_THREAD Func = (ZW_SET_INFORMATION_THREAD)
// 加载了 ntdll 模块,因为函数保存在这个模块中
GetProcAddress(LoadLibrary(L"ntdll.dll"),
// 函数的名称
"ZwSetInformationThread");
// 对调试器隐藏当前的线程,原理上就是让 DbgkpSendApiMessage 函数
// 不向建立了调试关系的调试器发送调试信息。
Func(GetCurrentThread(), ThreadHideFromDebugger, NULL, NULL);
}
int main()
{
ZSIT_DetachDebug();
printf("runnning...\n");
system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
int main()
{
if (FindWindow(L"OllyDbg", NULL))
printf("存在调试器\n");
else
printf("没检测到调试器\n");

return 0;
}

OD插件

插件加载的原理

  1. 应用程序如何找到自己的插件:所有支持插件的应用程序都会存在一个插件路径,用户提供的就应该放置在这个路径底下,应用程序通过遍历文件的方式来确保能够找到插件
  2. 插件的存在形式:插件通常以 dll 文件形式存在,这些文件可以是任何后缀名结尾的,并不影响插件本身的功能
  3. 应用程序如何识别插件路径下的模块是否是插件:一个合格的插件,应该能提供相应的导出函数说明当前插件的名称,版本及能够支持的应用程序
  4. 插件如何提供功能:通过实现指定的导出函数可以提供相应的功能
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
#include <vector>
#include <string>
#include <iostream>
#include <windows.h>
using namespace std;

// 定义一个函数指针的别名
using f_query = bool (*)(int v, char name[20], char version[20]);
using f_run = void (*)();
// 用于保存插件信息的结构体
typedef struct _PLUGIN_INFO
{
char name[20];
char version[20];
HMODULE module;
} PLUGIN_INFO, *PPLUGIN_INFO;
// 定义容器用于保存所有的插件
vector<PLUGIN_INFO> plugins;

// 初始化函数
void init()
{
// 保存找到的文件的信息
WIN32_FIND_DATAA FileInfo = { 0 };

// 1. 遍历当前的插件目录,搜索所有的模块
HANDLE FindHandle = FindFirstFileA(".\\plugin\\*.plugin", &FileInfo);

// 2. 如果说第一个文件查找成功就继续查找下一个
if (FindHandle != INVALID_HANDLE_VALUE)
{
do {
// 保存当前插件的信息
PLUGIN_INFO plugin_info = { 0 };

// 模块文件的路径
string path = string(".\\plugin\\") + FileInfo.cFileName;

// 尝试使用 LoadLibrary 加载模块
plugin_info.module = LoadLibraryA(path.c_str());

// 判断一下这是不是一个有效的模块
if (plugin_info.module != NULL)
{
// 判断当前有没有导出指定函数
f_query f = (f_query)GetProcAddress(plugin_info.module, "query");

// 如果函数获取成功,并且函数返回true就表示这是一个有效的插件

if (f != nullptr && f(1, plugin_info.name, plugin_info.version))
{
printf("插件: [%s %s] 已经被加载了\n",
plugin_info.name, plugin_info.version);

plugins.push_back(plugin_info);
}
}

} while (FindNextFileA(FindHandle, &FileInfo));
}
}
// 运行期间的函数
void run()
{
// 可以遍历插件列表,调用其中约定好的函数
for (auto& p : plugins)
{
// 获取函数,这个函数可以不提供
f_run f = (f_run)GetProcAddress(p.module, "run");
if (f) f();
}
}
// 清理资源的函数
void release()
{
// 一般会在 release 调用插件的清理函数
for (auto& p : plugins)
{
// 获取函数,这个函数可以不提供
f_run f = (f_run)GetProcAddress(p.module, "release");
if (f) f();
}
}
// 结束进程前调用的函数
void my_exit()
{
// 程序结束前应该卸载插件
for (int i = 0; i < plugins.size(); ++i)
{
// 将插件进行卸载(从内存空间移除)
// 卸载插件必须卸载所有可能调用插件函数的之后
FreeLibrary(plugins[i].module);
}
}
int main()
{
// 通产在程序运行的时候加载插件
init();
run();
release();
my_exit();
return 0;
}

提供的插件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <windows.h>
#include <iostream>
// 插件导出一个约定好的函数,函数告诉了应用程序自己的名称版本等信息
extern "C" __declspec(dllexport) bool query(int v, char name[20], char version[20])
{
// 参数一: 传入的参数,告诉插件自己的版本信息
// 参数二: 传出的参数,告诉应用程序自己的名称
// 参数三: 传出的参数,告诉应用程序自己的版本
// 如果目标应用不是 1.0 版本,该插件就不支持
if (v != 1)
return false;
memcpy(name, "plugin2", 8);
memcpy(version, "2.0", 4);
return true;
}
extern "C" __declspec(dllexport) void run()
{
printf("这是插件提供的功能");
}