主题
创建远程线程 - CreateRemoteThread
函数简介
在指定的窗口所在进程中创建一个线程,常用于进程注入、代码注入等高级系统编程场景。
接口名称
CreateRemoteThreadDLL调用
c
long CreateRemoteThread(long instance, long hwnd, long lpStartAddress, long lpParameter, int dwCreationFlags, long* lpThreadId);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| hwnd | 长整数型 | 窗口句柄或者进程ID。 |
| lpStartAddress | 长整数型 | 线程入口地址。 |
| lpParameter | 长整数型 | 线程参数。 |
| dwCreationFlags | 整数型 | 线程创建标志(Windows API),见下表。 |
| lpThreadId | 长整数型指针 | 返回线程ID的指针。 |
dwCreationFlags 创建标志
对应 Windows CreateRemoteThread 的 dwCreationFlags 参数,常用值如下(按位或组合):
| 值 | 十六进制 | 说明 |
|---|---|---|
| 0 | 0x00000000 | 创建后立即运行(默认) |
| 4 | 0x00000004 | CREATE_SUSPENDED:创建后挂起,需调用 ResumeThread 恢复 |
| 65536 | 0x00010000 | CREATE_NO_WINDOW:不创建控制台窗口 |
位运算组合示例
csharp
// 创建挂起状态的远程线程
int flags = 0x00000004; // CREATE_SUSPENDED
long threadId = 0;
long handle = ola.CreateRemoteThread(hwnd, entryAddr, param, flags, ref threadId);cpp
// 挂起 + 无窗口
int flags = 0x00000004 | 0x00010000;
long threadId = 0;
long handle = ola.CreateRemoteThread(hwnd, entryAddr, param, flags, &threadId);python
# 默认:创建后立即运行
flags = 0
thread_id = 0
handle = ola.CreateRemoteThread(hwnd, entry_addr, param, flags, thread_id)示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long handle = ola.CreateRemoteThread(hwnd, 0, 0, 0, "value");csharp
using OLAPlug;
var ola = new OLAPlugServer();
long handle = ola.CreateRemoteThread(hwnd, 0, 0, 0, "value");python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
handle = ola.CreateRemoteThread(hwnd, 0, 0, 0, "value")java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
long handle = ola.CreateRemoteThread(hwnd, 0, 0, 0, "value");go
import "github.com/ola/olaplug/olaplug"
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
handle := ola.CreateRemoteThread(hwnd, 0, 0, 0, "value")rust
use olaplug::OLAPlugServer;
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let handle = ola.create_remote_thread(&hwnd, 0, 0, 0, "value");cpp
var ola = com("OlaPlug.OlaSoft")
var handle = ola.CreateRemoteThread(hwnd, 0, 0, 0, "value")vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
handle = ola.CreateRemoteThread(hwnd, 0, 0, 0, "value")text
.局部变量 ola, OLAPlug
ola.创建 ()
handle = ola.CreateRemoteThread(hwnd, 0, 0, 0, "value")aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var handle = ola.CreateRemoteThread(hwnd, 0, 0, 0, "value");text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 handle = ola.CreateRemoteThread(hwnd, 0, 0, 0, "value")cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long handle = ola.CreateRemoteThread(hwnd, 0, 0, 0, "value");原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
CreateRemoteThread(instance, hwnd, 0, 0, 0, 0);csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateRemoteThread(long ola, long hwnd, long lpStartAddress, long lpParameter, int dwCreationFlags, long lpThreadId);
long instance = CreateCOLAPlugInterFace();
CreateRemoteThread(instance, hwnd, 0, 0, 0, 0);python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
ola.CreateRemoteThread(instance, hwnd, 0, 0, 0, 0)返回值
| 返回值 | 说明 |
|---|---|
非 0 | 成功,返回 线程句柄。 |
0 | 失败。 |
注意事项
| 项目 | 说明 |
|---|---|
| 权限 | 创建远程线程需要相应的进程权限。 |
| 线程入口地址必须在目标进程的地址空间内有效 | 线程入口地址必须在目标进程的地址空间内有效。 |
| 建议在不再需要线程句柄时调用 CloseHand | 建议在不再需要线程句柄时调用 CloseHandle 关闭。 |
| 创建挂起状态的线程可以使用 CREATE_SUS | 创建挂起状态的线程可以使用 CREATE_SUSPENDED 标志。 |
| 使用不当可能导致目标进程崩溃或系统不稳定 | 使用不当可能导致目标进程崩溃或系统不稳定。 |
