【发布时间】:2020-12-14 14:01:17
【问题描述】:
我需要修补程序,所以我制作了一个可以注入的 dll,并在注入时修补指令的一个字节。代码非常简单,只是出于好奇,这次我想用 C 而不是 C++ 编写它。但是当我加载我的 dll 时,它是线程崩溃而不是写入该指针。如果您使用 WriteProcessMemory 执行此操作,则指针为 100% 并且可以工作,但由于某种原因,不能从 dll 直接从进程写入。目标可执行文件是 x86,dll 是用 gcc 编译的。编译参数如下所示: gcc -shared mypatch.c -o mypatch.dll
#include <Windows.h>
#include <stdio.h>
#include <TlHelp32.h>
void main()
{
AllocConsole(); // Made a console so I can try to read the data, I thought maybe I was getting the wrong address, but I didn't.
FILE* consoleFile;
freopen_s(&consoleFile, "CONIN$", "r", stdin);
freopen_s(&consoleFile, "CONOUT$", "w", stderr);
freopen_s(&consoleFile, "CONOUT$", "w", stdout);
DWORD relAddr = 0x123456; // My address
DWORD addr = (DWORD)GetModuleHandle(NULL) + relAddr; // Adress is relative to the main executable.
printf("%X", *(byte*)(addr)); // Can read the real byte
*(byte*)(addr) = 0x90; // Can't write, crash.
printf("%X", *(byte*)(addr)); // Can't see, crashed.
}
BOOL WINAPI DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
if(dwReason == DLL_PROCESS_ATTACH)
{
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)main, 0, 0, 0);
}
return 1;
}
错误信息:
Unhandled exception at 0x70D41355 in SuperCoolApp.exe: 0xC0000005: Access violation writing location 0x123456. occurred
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。