#include "stdafx.h"
#include <windows.h>
BOOL WINAPI MyTerminateProcess(HANDLE hProcess,UINT uExitCode);
bool ApiHook(char*,PROC,PROC);
DWORD oldaddr;
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
oldaddr=(DWORD)TerminateProcess;
ApiHook("Kernel32.dll",(PROC)oldaddr,(PROC)MyTerminateProcess);
return TRUE;
}
BOOL WINAPI MyTerminateProcess(HANDLE hProcess,UINT uExitcode)
{
MessageBox(NULL,"这次你结束不了了哇,给哥哥猖獗","毛毛虫",MB_OK);
return IDOK;
}
bool WINAPI ApiHook(char *dllname,PROC oldapi,PROC newapi)
{
HINSTANCE hInstance=GetModuleHandle(NULL);
BYTE *baseaddr=(BYTE*)hInstance;
IMAGE_DOS_HEADER *dos_header=(IMAGE_DOS_HEADER*)baseaddr;
IMAGE_NT_HEADERS *nt_headers=(IMAGE_NT_HEADERS*)(baseaddr+dos_header->e_lfanew);
IMAGE_IMPORT_DESCRIPTOR *import_descriptor=(IMAGE_IMPORT_DESCRIPTOR*)(baseaddr+nt_headers->OptionalHeader.DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES].VirtualAddress);
char nowdllname[MAX_PATH]={0};
IMAGE_THUNK_DATA *thunk_data;
while(import_descriptor->OriginalFirstThunk!=0)
{
strcpy(nowdllname,(char*)(baseaddr+import_descriptor->Name));
if (strcmp(strupr(nowdllname),strupr(dllname))==0)
{
thunk_data=(IMAGE_THUNK_DATA*)(baseaddr+import_descriptor->FirstThunk);
DWORD *thisaddr;
while(thunk_data->u1.Function)
{
thisaddr=(DWORD*)&(thunk_data->u1.Function);
if ((DWORD)thisaddr==(DWORD)oldapi)
{
DWORD protect,numwrite;
if(VirtualProtectEx(GetCurrentProcess(),(void*)(thisaddr),sizeof(DWORD),PAGE_EXECUTE_READWRITE,&protect))
{
if(WriteProcessMemory(GetCurrentProcess(),(void*)(thisaddr),(void*)newapi,sizeof(DWORD),&numwrite))
{
VirtualProtectEx(GetCurrentProcess(),(void*)(thisaddr),sizeof(DWORD),protect,&numwrite);
return true;
}
else
return false;
}
else
return false;
}
thunk_data++;
}
}
import_descriptor++;
}
return false;
}
小弟在学习Hookapi时写的一个dll文件,但是始终不能被加载,望各位大侠,看官
指点一二
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)