05月09, 2018

远程线程注入方法 CreatRemoteThread

为向一个进程中注入一个想要运行的DLL,先要获得这个进程的线程ID,这样可以祝福到任何接收到HOOK信息的进程中。 还有另外一种方法,在SetWindowsHookEx中,使用LoadLibrary将DLL加载至当前进程(不是目标进程)的地址空间中。 然后使用GetProcAddress获取所需函数的地址。 然后再交给SetWindowsHookEx,构造一个钩子,沟渠一个会自动加载我们DLL的进程,这样整个DLL注入就OK了。

CreatRemoteThread过程:

  1. 使用VirtualAllocEx在目标进程的地址空间中创建一块我们DLL所在路径长度的内存空间。

    2. 使用WriteProcessMemory将DLL路径写入分配的内存。

    3. 一旦DLL路径写入内存中,再使用CreateRemoteThread(或者其他无正式说明的功能),它再调用LoadLibrary函数将DLL注入目标进程中。

如果目标进程不在当前会话中而是在一个不同的会话中,那么CreateRemoteThread将会失效。而这些无正式说明功能就不会。

在我们的代码中,我们使用CreateRemote 线程和两个无正式说明的函数NtCreateThreadEx 和RtlCreateUserThread。

Mimikatz 和 Metasploit。这两个都是使用RtlCreateUserThread来实现DLL注入的。

NtCreateThreadEx是一个系统调用,是用户空间应用和内核打交道的方法。

RtlCreateUserThread调用NtCreateThreadEx 。因此RtlCreateUserThread应该是NtCreateThreadEx 的封装。我们想调用RtlCreateUserThread是因为NtCreateThreadEx 的系统调用选项可以在Windows版本间改变。因此,RtlCreateUserThread更好用一些。 Mimikatz 和Meterpreter使用RtlCreateUserThread是由于这个选项更加安全

使用CreateRemoteThread

  1. 使用VirtualAllocEx在目标进程的地址空间中创建一块我们DLL所在路径长度的内存空间
//This dll path should be relative to the target process or an absolute path
char* dll = "inject.dll";
//We need a handle to the process we will be injecting into
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
//Create the space needed for the dll we are going to be injecting
LPVOID lpSpace = (LPVOID)VirtualAllocEx(hProcess, NULL, strlen(dll), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);

本文链接:https://harry.ren/post/creat-remote-thread.html

-- EOF --

Comments