远程线程注入

假设我们有个进程A, 我们想使用进程B在进程A内注入一个线程。可以使用CreateRemoteThread 这个windows API。可以API有如下签名

HANDLE CreateRemoteThread(
  [in]  HANDLE                 hProcess,
  [in]  LPSECURITY_ATTRIBUTES  lpThreadAttributes,
  [in]  SIZE_T                 dwStackSize,
  [in]  LPTHREAD_START_ROUTINE lpStartAddress,
  [in]  LPVOID                 lpParameter,
  [in]  DWORD                  dwCreationFlags,
  [out] LPDWORD                lpThreadId
);

特别注意第4个参数lpStartAddress是传一个函数的地址,用这个函数在进程A内创建一个进程。这个函数地址是进程A中的函数, 我今天看了"Game hacking - Developing Autonomous Bots for Online Games", 里面向当前进程注入了一个线程,所以传的函数指针是本地函数的地址,类似这样:

// thread_injection_target.cc
void PrintText() {
  // Do something.
}

int main() {
  HANDLE h  = GetCurrentProcess();
  CreateRemoteThread(h, NULL, 0, (LPVOID)&PrintText, NULL, 0, NULL, NULL);
}

这种只对注入当前进程有效,注入其他进程就不能使用这种方式了。先看一个最简单的远程进程注入的方式。可以这样写一个注入的target:

// read_memory.cc
#include <iostream>
#include <windows.h>

void PrintHello() { std::cout << "Hello!"; }

void PrintGoodbye() { std::cout << "PrintGoodbye!"; }

int main() {
  std::cout << "Address of PrintHello is:    " << (void *)&PrintHello
            << std::endl;
  std::cout << "Address of PrintGoodbye is:  " << (void *)&PrintGoodbye
            << std::endl;
  std::cout << "Current pid is:              " << GetCurrentProcessId()
            << std::endl;
  while (true) {
    getchar();
  }
}

这样我们可以在控制台看到进程pid和函数地址。当然你也可以去process explorer看pid,去OD看看函数地址(假如你不更改代码,函数地址一般不改变, 可以在vscode里面断点看下地址)。
在这里插入图片描述
有了上面这些信息,我们注入线程的方式也很简单:

#include "base/process_util.h"
#include <windows.h>

constexpr int kProcessID = 15116;
constexpr int kFunctionAddress = 0x401410;

int main() {
  HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, false, kProcessID);
  // NOTICE:
  // Here CreateRemoteThread accepts a function address which is in target
  // process. So it is no use to define a local function void a() {} and pass
  // LPTHREAD_START_ROUTINE(&a) to this method.
  HANDLE thread = CreateRemoteThread(process, NULL, 0,
                                     LPTHREAD_START_ROUTINE(kFunctionAddress),
                                     NULL, 0, NULL);
  WaitForSingleObject(thread, INFINITE);
  CloseHandle(thread);
}

每运行一次,可以看到控制台打印一个hello出来:

在这里插入图片描述

下篇 利用远程线程注入DLL

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值