vc 程序注入鉤子DLL通用模組開源
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
#include "stdafx.h"
#include <windows.h>
#include <string>
#include "stdio.h"
#include <iostream>
using namespace std;
#define DEF_BUF_SIZE 1024
// 用於儲存注入模組DLL的路徑全名
char szDllPath[DEF_BUF_SIZE] = {0} ;
// 使用遠端執行緒向指定ID的程序注入模組
BOOL InjectModuleToProcessById ( DWORD dwProcessId )
{
if ( dwProcessId == 0 )
return FALSE ;
// 開啟程序
HANDLE hProcess = OpenProcess ( PROCESS_ALL_ACCESS, FALSE, dwProcessId ) ;
if ( hProcess == NULL )
return FALSE ;
//申請存放檔名的空間
UINT nLen = (UINT)strlen ( szDllPath ) + 1;
LPVOID lpRemoteDllName = VirtualAllocEx ( hProcess, NULL, nLen, MEM_COMMIT, PAGE_READWRITE ) ;
if ( lpRemoteDllName == NULL )
{
printf ( "[ERROR]VirtualAllocEx(%d)/n", GetLastError() );
return FALSE ;
}
//把dll檔名寫入申請的空間
if ( WriteProcessMemory ( hProcess, lpRemoteDllName, szDllPath, nLen, NULL) == FALSE )
{
printf ( "[ERROR]WriteProcessMemory(%d)/n", GetLastError() );
return FALSE ;
}
//獲取動態連結庫函式地址
HMODULE hModule = GetModuleHandle ( L"kernel32.dll" ) ;
LPTHREAD_START_ROUTINE fnStartAddr = ( LPTHREAD_START_ROUTINE )GetProcAddress(hModule,"LoadLibraryA") ;
if ( (DWORD)fnStartAddr == 0 )
{
printf ( "[ERROR]GetProcAddress(%d)/n", GetLastError() );
return FALSE ;
}
//建立遠端執行緒
HANDLE hRemoteThread = CreateRemoteThread ( hProcess, NULL, 0,fnStartAddr, lpRemoteDllName, 0, NULL ) ;
if ( hRemoteThread == NULL )
{
printf ( "[ERROR]CreateRemoteThread(%d)/n", GetLastError() );
return FALSE ;
}
// 等待遠端執行緒結束
if ( WaitForSingleObject ( hRemoteThread, INFINITE ) != WAIT_OBJECT_0 )
{
printf ( "[ERROR]WaitForSingleObject(%d)/n", GetLastError() );
return FALSE ;
}
CloseHandle ( hRemoteThread ) ;
CloseHandle ( hModule ) ;
CloseHandle ( hProcess ) ;
return TRUE ;
}
int _tmain(int argc, _TCHAR* argv[])
{
// 取得當前工作目錄路徑
GetCurrentDirectoryA ( DEF_BUF_SIZE, szDllPath ) ;
// 生成注入模組DLL的路徑全名
strcat ( szDllPath, "//DLL.dll" ) ;
DWORD dwProcessId = 0 ;
// 接收使用者輸入的目標程序ID
while ( printf ( "請輸入目標程序ID:" ) && cin >> dwProcessId && dwProcessId > 0 )
{
BOOL bRet = InjectModuleToProcessById ( dwProcessId ) ;
printf ( bRet ? "注入成功!/n":"注入失敗!/n") ;
}
return 0;
}