|
//这里写你的-----DLL代码
// inject.cpp
#include "inject.h"
extern "C" __declspec(dllexport) void InjectedFunction() {
// 这里是你想要在目标进程执行的代码
MessageBox(NULL, L"DLL Injected!", L"Injected", MB_OK);
}
//这里是-----创建你的注入器程序
// injector.cpp
#include <Windows.h>
#include <TlHelp32.h>
#include <iostream>
DWORD FindProcessID(const wchar_t* processName) {
DWORD processID = 0;
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 structure;
structure.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(snapshot, &structure)) {
do {
if (!wcscmp(structure.szExeFile, processName)) {
processID = structure.th32ProcessID;
break;
}
} while (Process32Next(snapshot, &structure));
}
CloseHandle(snapshot);
return processID;
}
bool InjectDLL(DWORD processID, const wchar_t* dllPath) {
HANDLE processHandle = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE, FALSE, processID);
if (processHandle == NULL) {
return false;
}
LPVOID remoteDllName = VirtualAllocEx(processHandle, NULL, wcslen(dllPath) * sizeof(wchar_t), MEM_COMMIT, PAGE_READWRITE);
if (remoteDllName == NULL) {
CloseHandle(processHandle);
return false;
}
if (WriteProcessMemory(processHandle, remoteDllName, (LPVOID)dllPath, wcslen(dllPath) * sizeof(wchar_t), NULL) == 0) {
VirtualFreeEx(processHandle, remoteDllName, 0, MEM_RELEASE);
CloseHandle(processHandle);
return false;
}
HMODULE hModule = LoadLibrary(L"kernel32.dll");
LPTHREAD_START_ROUTINE threadFunc = (LPTHREAD_START_ROUTINE)GetProcAddress(hModule, "LoadLibraryW");
HANDLE hRemoteThread = CreateRemoteThread(processHandle, NULL, 0, threadFunc, remoteDllName, 0, NULL);
if (hRemoteThread == NULL) {
VirtualFreeEx(processHandle, remoteDllName, 0, MEM_RELEASE);
CloseHandle(processHandle);
return false;
}
WaitForSingleObject(hRemoteThread, INFINITE);
CloseHandle(hRemoteThread);
VirtualFreeEx(processHandle, remoteDllName, 0, MEM_RELEASE);
CloseHandle(processHandle);
return true;
}
int main() {
DWORD processID = FindProcessID(L"notepad.exe"); // 目标进程名称
if (processID == 0) {
std::cout << "Process not found." << std::endl;
return 1;
}
if (InjectDLL(processID, L"C:\\path\\to\\inject.dll")) {
std::cout << "DLL Injected Successfully." << std::endl;
} else {
std::cout << "D
|
|