今天自己写了一个dll 注入工具。再次封装了dll注入方法。在此提供给大家。代码复制即可插入程序中使用。参数为:进程id,dll文件路径。成功返回true,反之false
BOOL Inject(DWORD dwprocesId, char *pszDllFileName)
{
BOOL bRet=FALSE;
do{
DWORD dwSize = strlen(pszDllFileName) + 1;
//获取进程句柄
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwprocesId);
if (hProcess == NULL)
{
cout << "open process failed" << endl;
break;
}
LPVOID dllAdress = VirtualAllocEx(hProcess, NULL, dwSize, MEM_COMMIT, PAGE_READWRITE);
if (dllAdress == NULL)
{
cout << "VirtuallALLocEx failed" << endl;
break;
}
if(!WriteProcessMemory(hProcess, dllAdress, pszDllFileName, dwSize, NULL))
{
cout << "WriteProcesMemory failed" << endl;
break;
}
FARPROC farproc = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
if (farproc == NULL)
{
cout << "GetProcAddress failed" << endl;
break;
}
HANDLE hRemoteTread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)farproc, dllAdress, 0, NULL);
if (hRemoteTread == NULL)
{
cout << "hRemoteTread failed" << endl;
break;
}
bRet=true;
CloseHandle(hProcess);
}while(FALSE);
return bRet;
}