64,641
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <cstdlib>
#include <Dbghelp.h>
#pragma comment(lib,"Dbghelp.lib")
using namespace std;
VOID ReplaceATEntryInOneMod(LPSTR pszCalleeModName,PROC pfnCurrent,PROC pfnNew,HMODULE hModCaller)
{
ULONG ulSize;
PIMAGE_IMPORT_DESCRIPTOR pImportDesc = NULL;
__try
{
pImportDesc = (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryEntryToDataEx(hModCaller,TRUE,IMAGE_DIRECTORY_ENTRY_IMPORT,&ulSize);
}
__finally
{
}
if(pImportDesc == NULL)
{
return ;
}
for(; pImportDesc->Name ; pImportDesc++ )
{
PSTR pszModName = (PSTR)((PBYTE)hModCaller + pImportDesc->Name);
if(lstrcmpiA(pszModName,pszCalleeModName) == 0)
{
PIMAGE_THUNK_DATA pThunk = (PIMAGE_THUNK_DATA)((PBYTE)hModCaller + pImportDesc->FirstThunk);
for(; pThunk->u1.Function ; pThunk ++ )
{
PROC* ppfn = (PROC*)&pThunk->u1.Function;
BOOL bFound = (*ppfn == pfnCurrent);
if(bFound)
{
if(!WriteProcessMemory(GetCurrentProcess(),ppfn,&pfnNew,sizeof(pfnNew),NULL) && (ERROR_NOACCESS == GetLastError()))
{
DWORD dwOldProter;
if(!VirtualProtect(ppfn,sizeof(pfnNew),PAGE_WRITECOPY,&dwOldProter))
{
WriteProcessMemory(GetCurrentProcess(),ppfn,&pfnNew,sizeof(pfnNew),NULL);
VirtualProtect(ppfn,sizeof(pfnNew),dwOldProter,&dwOldProter);
}
}
return ;
}
}
}
}
}
int MyMessageBox(HWND hWnd,LPCSTR lpText,LPCSTR lpCaption,UINT uType)
{
MessageBeep(0);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
PROC pfnOrig = GetProcAddress(GetModuleHandle(_T("User32")),"MessageBoxA");
cout << GetLastError() << endl;
HMODULE hModCaller = GetModuleHandle(_T("Test.exe"));
ReplaceATEntryInOneMod("user32.dll",pfnOrig,(PROC)MyMessageBox,hModCaller);
system("pause");
return 0;
}