2

我正在“C/C++ - Win32API”环境中编写一个 dll。我有一些常量变量(都是 DWORD 值和 LPWSTR/LPSTR 字符串),我必须让用户修改它们。我正在寻找的是(希望)一种能够按照所述方式启用 SAFE 二进制修改的工具,该工具可以更新 PE 的所有必要表。

4

1 回答 1

3

您可以在单独的 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。

有了这些信息,就很容易构造一个新的数据块,打开 PE 文件并覆盖 0x2000 处的数据。

要确定代码的偏移量,您需要解析 PE 文件头和节头。这是相当简单的。或者,您可以在构建过程中从 dumpbin 输出中获取偏移量,并将其输入到编辑工具的构建中。

请注意,要在发布模式下进行测试,您需要实际使用,theData否则链接器会将其丢弃。另请注意,该部分仅具有read属性,因此它是真正的只读的。尝试写入它会导致访问冲突。

最后......这一切都很邋遢。除非你真的别无选择,否则我不会打扰。

于 2013-03-24T17:08:54.220 回答