Я хочу подключить функцию WriteFile
. Целевой процесс notepad.exe. Но это не сработает. Код.Я хочу подключить win32API, но он не работает
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<TlHelp32.h>
DEBUG_EVENT de;
FARPROC g_pfTargetAPIAddress; //save the address of WriteFile function
BYTE g_ReadAPIBuffer; //save the original content of the first byte of WriteFile function
BYTE g_WriteAPIBuffer=0xCC; //change the first byte of WriteFile function to 0xCC , so that it can throw a breakpoint exception
HANDLE g_hTargetProcessHandle; //the handle of the target process (notepad.exe)
HANDLE g_hTargetThreadHandle; //the main thread of the target process
DWORD g_dwTargetProcessId;
DWORD g_dwTargetThreadId;
DWORD GetTargetProcess() //get the id of the target process (notepad.exe)
{
PROCESSENTRY32 pe32;
pe32.dwSize=sizeof(pe32);
HANDLE handle=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(handle==INVALID_HANDLE_VALUE)
{
printf("get tool help handle error,the error code is:%u\n",GetLastError());
system("PAUSE");
return NULL;
}
BOOL bMore=::Process32First(handle,&pe32);
while(bMore)
{
printf("the process name is:%ws\n",pe32.szExeFile);
printf("the process id is %d\n",pe32.th32ProcessID);
if(wcscmp(pe32.szExeFile,L"notepad.exe")==0)
{
return pe32.th32ParentProcessID;
}
bMore=::Process32Next(handle,&pe32);
}
return NULL;
}
BOOL OnProcessCreateDebug(LPDEBUG_EVENT de)
{
printf("now begin OnProcessCreateDebug\n");
g_hTargetProcessHandle=de->u.CreateProcessInfo.hProcess;
g_hTargetThreadHandle=de->u.CreateProcessInfo.hThread;
g_dwTargetThreadId=de->dwThreadId;
HMODULE hModule=::LoadLibrary(L"kernel32.dll");
if(hModule==INVALID_HANDLE_VALUE)
{
printf("get the api handle error ,the error code is :%u\n",GetLastError());
system("PAUSE");
return FALSE;
}
char cTargetAPIName[]="WriteFile";
g_pfTargetAPIAddress=::GetProcAddress(hModule,(LPCSTR)cTargetAPIName);
if(g_pfTargetAPIAddress==NULL)
{
printf("the getprocaddress in onprocesscreatedebug failes,the error code is:%u",GetLastError());
system("PAUSE");
return FALSE;
}
BOOL bReadResult=::ReadProcessMemory(de->u.CreateProcessInfo.hProcess,g_pfTargetAPIAddress,&g_ReadAPIBuffer,sizeof(BYTE),NULL);
if(!bReadResult)
{
printf("read the first byte of target api fails,the error code is :%u\n",GetLastError());
system("PAUSE");
return FALSE;
}
BOOL bWriteResult=::WriteProcessMemory(de->u.CreateProcessInfo.hProcess,g_pfTargetAPIAddress,&g_WriteAPIBuffer,sizeof(BYTE),NULL);
if(!bWriteResult)
{
printf("write the first byte of target api fails,the error code is :%u\n",GetLastError());
system("PAUSE");
return FALSE;
}
printf("now finish OnProcessCreateDebugEvent\n");
return TRUE;
}
BOOL OnExceptionDebugEvent(LPDEBUG_EVENT de)
{
printf("now go into the OnExceptionDebugEvent just now\n");
PEXCEPTION_RECORD per=&de->u.Exception.ExceptionRecord;
if(per->ExceptionCode==EXCEPTION_BREAKPOINT)
{
printf("now it is in the first if\n");
if(per->ExceptionAddress==g_pfTargetAPIAddress)
{
printf("now has entered the OnExceptionDebugEvent\n");
if(!WriteProcessMemory(g_hTargetProcessHandle,g_pfTargetAPIAddress,&g_WriteAPIBuffer,sizeof(BYTE),NULL))
{
printf("onexecption write the first byte of target api fails,the error code is :%u\n",GetLastError());
system("PAUSE");
return FALSE;
}
CONTEXT con;
con.ContextFlags=CONTEXT_CONTROL;
::GetThreadContext(g_hTargetThreadHandle,&con);
DWORD dwWriteFileBufferAddress;
if(!::ReadProcessMemory(g_hTargetProcessHandle,(LPCVOID)(con.Esp+0x8),&dwWriteFileBufferAddress,sizeof(DWORD),NULL))
{
printf("read the the lpBaseAddress of target api fails,the error code is :%u\n",GetLastError());
system("PAUSE");
return FALSE;
}
printf("now after esp+8\n");
DWORD dwSizeOfBuffer;
if(!::ReadProcessMemory(g_hTargetProcessHandle,(LPCVOID)(con.Esp+0x10),&dwSizeOfBuffer,sizeof(DWORD),NULL))
{
printf("read the lpBuffer of target api fails,the error code is :%u\n",GetLastError());
system("PAUSE");
return FALSE;
}
LPBYTE lpBuffer=(LPBYTE)malloc(dwSizeOfBuffer+1);
LPBYTE lppointer=lpBuffer;
for(int i=0;i<=dwSizeOfBuffer;i++)
{
*lppointer=0x1;
lppointer++;
}
*lppointer=0x0;
if(!::WriteProcessMemory(g_hTargetProcessHandle,(LPVOID)dwWriteFileBufferAddress,lpBuffer,dwSizeOfBuffer+1,NULL))
{
printf("overridewrite the buffer of target api fails,the error code is :%u\n",GetLastError());
system("PAUSE");
return FALSE;
}
free(lpBuffer);
con.Eip=(DWORD)g_pfTargetAPIAddress;
SetThreadContext(g_hTargetThreadHandle,&con);
ContinueDebugEvent(g_dwTargetProcessId,g_dwTargetThreadId,DBG_CONTINUE);
printf("now after the ContinueDebugEvent\n");
Sleep(0);
BYTE byte=0xcc;
if(!::WriteProcessMemory(g_hTargetProcessHandle,g_pfTargetAPIAddress,&byte,sizeof(BYTE),NULL))
{
printf("rewrite the first byte of target api fails,the error code is :%u\n",GetLastError());
system("PAUSE");
return FALSE;
}
}
else
{
printf("not enter the second if\n");
}
}
else printf("not enter the first if\n");
return TRUE;
}
BOOL HookAPI()
{
while(WaitForDebugEvent(&de,INFINITE))
{
if(de.dwDebugEventCode==CREATE_PROCESS_DEBUG_EVENT)
{
if(!OnProcessCreateDebug(&de)) return FALSE;
}
else if(de.dwDebugEventCode==EXCEPTION_DEBUG_EVENT)
{
printf("now it is going to call the OnExceptionDebugEvent\n");
if(!OnExceptionDebugEvent(&de)) return FALSE;
}
else if(de.dwDebugEventCode==EXIT_PROCESS_DEBUG_EVENT)
{
break;
}
::ContinueDebugEvent(de.dwProcessId,de.dwThreadId,DBG_CONTINUE);
}
return TRUE;
}
int main()
{
g_dwTargetProcessId=GetTargetProcess();
if(g_dwTargetProcessId==NULL)
{
printf("the value of dProcessId is null\n");
system("PAUSE");
return -1;
}
BOOL b=DebugActiveProcess(g_dwTargetProcessId);
if(!b)
{
printf("DebugActiveProcess fails,the last error is : %u\n",GetLastError());
system("PAUSE");
return -1;
}
HookAPI();
return 0;
}
Я хочу, чтобы подключить функцию WriteFile
, и она меняет первые байты WriteFile
функции в 0xCC
, но она не работает. Я могу сохранить файл как обычно, используя notepad.exe. Я думаю, что 0xCC
не может выбрасывать исключение точки останова. Кто-нибудь скажет мне, где проблема?
Похоже, вы уверены, что 'notepad.exe' является 32-битным, но в наши дни это редко бывает так. Взаимодействие API в 64-битных программах несколько отличается. – Havenard
AFAIK 'notepad.exe' отображает файл, который вы редактируете, в память, поэтому он, вероятно, часто не использует' WriteFile'. –
@Havenard Я просто изучаю и эту программу я запускаю только на 32-битной Windows Xp sp3 – freedomwings