using namespace std;
BOOL
CreateRemoteDllInjectDll(DWORD dwProcessId, char
*
pdllname);
int
main() {
DWORD dwProcessId;
char DLLName[
20
];
/
/
需要输入的DLL文件名字
cout <<
"Pleace input DLLfile name :"
<< endl;
cin >> DLLName;
cout <<
"Pleace input PRocessID :"
<< endl;
cin >> dwProcessId;
CreateRemoteDllInjectDll(dwProcessId,DLLName);
return
0
;
}
/
*
*
1.
打开要注入的进程
2.
给进程分配虚拟内存 VirtualAllocEx
3.
给分配的内存写入要注入的DLL目录
4.
找到 kernel32.dll 模块的里面的LoadLibrary函数的地址
5.
用CreatRemoTethread给目标进程注入dll
6.
关闭目标进程的句柄
*
*
/
BOOL
CreateRemoteDllInjectDll(DWORD dwProcessId, char
*
pdllname) {
HANDLE hProcess
=
NULL;
LPVOID pDLLAddr
=
NULL;\
HMODULE hker
=
NULL;
FARPROC pFunProcAddr
=
NULL;
DWORD dwsize
=
0
;
hker
=
GetModuleHandleA(
"kernel32.dll"
);
/
/
得到kernel32.dll进程中的地址
if
( NULL
=
=
hker) {
puts(
"GetModuleHandle kernel32.dll is error"
);
return
false;
}
hProcess
=
OpenProcess(PROCESS_ALL_ACCESS,false,dwProcessId);
/
/
打开要注入的进程
if
( NULL
=
=
hProcess) {
/
/
检查打开进程是否成功
puts(
"OpenProcess is error"
);
return
false;
}
dwsize
=
strlen(pdllname)
+
1
;
/
/
DLL文件目录的长度
pDLLAddr
=
VirtualAllocEx(hProcess,NULL,dwsize,MEM_COMMIT,PAGE_READWRITE );
/
/
申请的内存空间,并且地址保存在pDLLAddr目录中
if
(pDLLAddr
=
=
NULL) {
/
/
检查
puts(
"VirtualAllocEx is error"
);
return
false;
}
if
(!WriteProcessMemory(hProcess,pDLLAddr,pdllname,dwsize,NULL)) {
/
/
把所需要的注入的dll文件目录字符注入给目标进程
puts(
"WriteProcessMemory is error"
);
return
false;
}
pFunProcAddr
=
GetProcAddress(hker,
"LoadLibraryA"
);
/
/
得到loadlibrarya函数的地址
if
(pFunProcAddr
=
=
NULL) {
puts(
"Get LoadLibraryA is error"
);
return
false;
}
HANDLE hRemotehandle
=
CreateRemoteThread(hProcess,NULL,
0
,(LPTHREAD_START_ROUTINE)(pFunProcAddr),pDLLAddr,
0
,NULL);
/
/
创造远程线程
if
(!hRemotehandle) {
puts(
" CreateRemoteThread is error"
);
return
false;
WaitForSingleObject(hRemotehandle, INFINITE);
CloseHandle(hRemotehandle);
/
/
关闭句柄
CloseHandle(hProcess);
return
0
;
}
}