我正在“C/C++ - Win32API”环境中编写一个 dll。 我有一些常量变量(都是 DWORD 值和 LPWSTR/LPSTR 字符串),我必须启用用户修改。 我正在寻找(希望)一个能够实现所描述的安全二进制修改的工具, 以更新 PE 的所有必要表的方式。
最佳答案
您可以在单独的 PE 部分中创建一个结构体:
// Create the section
#pragma section("myconst", read)
// Declare a struct to hold the constant data
typedef struct
{
DWORD a;
DWORD b;
char stringa[256];
char stringb[256];
} ConstData;
// Create an initialized instance of the struct in the new section
__declspec(allocate("myconst"))
const ConstData theData = {0xdeadbeef, 0xfeedface, "Hello", "dolly"};
编译代码。打开 Visual Studio 命令提示符,运行
dumpbin /all myexe.exe > dump.txt
notepad dump.txt
搜索 myconst
部分。您应该看到类似以下内容:
SECTION HEADER #4
myconst name
208 virtual size
4000 virtual address (00404000 to 00404207)
400 size of raw data
2000 file pointer to raw data (00002000 to 000023FF)
0 file pointer to relocation table
0 file pointer to line numbers
0 number of relocations
0 number of line numbers
40000040 flags
Initialized Data
Read Only
RAW DATA #4
00404000: EF BE AD DE CE FA ED FE 48 65 6C 6C 6F 00 00 00 ï¾ÞÎúíþHello...
00404010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00404020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
您可以看到代码中初始化的两个十六进制值和第一个字符串值。您还可以看到该数据在 PE 文件中的偏移量(“指向原始数据的文件指针”)是 0x2000。
有了这些信息,就可以很容易地构造一个新的数据 block 、打开 PE 文件并覆盖 0x2000 处的数据。
要确定代码的偏移量,您需要解析 PE 文件头和节头。这相当简单。或者,您可以在构建过程中获取转储箱输出的偏移量,并将其输入到编辑工具的构建中。
请注意,要在 Release模式下测试它,您需要实际使用theData
,否则链接器将丢弃它。另请注意,该部分只有 read
属性,因此它是真正的只读。尝试写入它会导致访问冲突。
最后...这一切都非常邋遢。除非你真的别无选择,否则我不会打扰。
关于c - 如何巧妙修改PE数据段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15599995/