Win32 API 枚举 dll 导出函数?

问题描述 投票:0回答:9

我发现了类似的问题,但没有找到我正在寻找的答案。所以这里是:

对于原生Win32 dll,是否有Win32 API来枚举其导出函数名称?

c++ windows winapi dll
9个回答
55
投票

dumpbin /exports
几乎就是您想要的,但那是一个开发人员工具,而不是 Win32 API。

强烈反对使用

LoadLibraryEx
DONT_RESOLVE_DLL_REFERENCES
,但恰好对于这种特殊情况很有用 – 它完成了将 DLL 映射到内存的繁重工作(但您实际上不需要或不想使用来自库),这使得您可以轻松阅读标头:
LoadLibraryEx
返回的模块句柄正好指向它。

#include <winnt.h>
HMODULE lib = LoadLibraryEx("library.dll", NULL, DONT_RESOLVE_DLL_REFERENCES);
assert(((PIMAGE_DOS_HEADER)lib)->e_magic == IMAGE_DOS_SIGNATURE);
PIMAGE_NT_HEADERS header = (PIMAGE_NT_HEADERS)((BYTE *)lib + ((PIMAGE_DOS_HEADER)lib)->e_lfanew);
assert(header->Signature == IMAGE_NT_SIGNATURE);
assert(header->OptionalHeader.NumberOfRvaAndSizes > 0);
PIMAGE_EXPORT_DIRECTORY exports = (PIMAGE_EXPORT_DIRECTORY)((BYTE *)lib + header->
    OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
assert(exports->AddressOfNames != 0);
BYTE** names = (BYTE**)((int)lib + exports->AddressOfNames);
for (int i = 0; i < exports->NumberOfNames; i++)
    printf("Export: %s\n", (BYTE *)lib + (int)names[i]);

完全未经测试,但我认为它或多或少是正确的。 (著名的遗言。)


12
投票

试试这个:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void EnumExportedFunctions (char *, void (*callback)(char*));
int Rva2Offset (unsigned int);

typedef struct {
    unsigned char Name[8];
    unsigned int VirtualSize;
    unsigned int VirtualAddress;
    unsigned int SizeOfRawData;
    unsigned int PointerToRawData;
    unsigned int PointerToRelocations;
    unsigned int PointerToLineNumbers;
    unsigned short NumberOfRelocations;
    unsigned short NumberOfLineNumbers;
    unsigned int Characteristics;
} sectionHeader;

sectionHeader *sections;
unsigned int NumberOfSections = 0;

int Rva2Offset (unsigned int rva) {
    int i = 0;

    for (i = 0; i < NumberOfSections; i++) {
        unsigned int x = sections[i].VirtualAddress + sections[i].SizeOfRawData;

        if (x >= rva) {
            return sections[i].PointerToRawData + (rva + sections[i].SizeOfRawData) - x;
        }
    }

    return -1;
}

void EnumExportedFunctions (char *szFilename, void (*callback)(char*)) {
    FILE *hFile = fopen (szFilename, "rb");

    if (hFile != NULL) {
        if (fgetc (hFile) == 'M' && fgetc (hFile) == 'Z') {
            unsigned int e_lfanew = 0;
            unsigned int NumberOfRvaAndSizes = 0;
            unsigned int ExportVirtualAddress = 0;
            unsigned int ExportSize = 0;
            int i = 0;

            fseek (hFile, 0x3C, SEEK_SET);
            fread (&e_lfanew, 4, 1, hFile);
            fseek (hFile, e_lfanew + 6, SEEK_SET);
            fread (&NumberOfSections, 2, 1, hFile);
            fseek (hFile, 108, SEEK_CUR);
            fread (&NumberOfRvaAndSizes, 4, 1, hFile);

            if (NumberOfRvaAndSizes == 16) {
                fread (&ExportVirtualAddress, 4, 1, hFile);
                fread (&ExportSize, 4, 1, hFile);

                if (ExportVirtualAddress > 0 && ExportSize > 0) {
                    fseek (hFile, 120, SEEK_CUR);

                    if (NumberOfSections > 0) {
                        sections = (sectionHeader *) malloc (NumberOfSections * sizeof (sectionHeader));

                        for (i = 0; i < NumberOfSections; i++) {
                            fread (sections[i].Name, 8, 1, hFile);
                            fread (&sections[i].VirtualSize, 4, 1, hFile);
                            fread (&sections[i].VirtualAddress, 4, 1, hFile);
                            fread (&sections[i].SizeOfRawData, 4, 1, hFile);
                            fread (&sections[i].PointerToRawData, 4, 1, hFile);
                            fread (&sections[i].PointerToRelocations, 4, 1, hFile);
                            fread (&sections[i].PointerToLineNumbers, 4, 1, hFile);
                            fread (&sections[i].NumberOfRelocations, 2, 1, hFile);
                            fread (&sections[i].NumberOfLineNumbers, 2, 1, hFile);
                            fread (&sections[i].Characteristics, 4, 1, hFile);
                        }

                        unsigned int NumberOfNames = 0;
                        unsigned int AddressOfNames = 0;

                        int offset = Rva2Offset (ExportVirtualAddress);
                        fseek (hFile, offset + 24, SEEK_SET);
                        fread (&NumberOfNames, 4, 1, hFile);

                        fseek (hFile, 4, SEEK_CUR);
                        fread (&AddressOfNames, 4, 1, hFile);

                        unsigned int namesOffset = Rva2Offset (AddressOfNames), pos = 0;
                        fseek (hFile, namesOffset, SEEK_SET);

                        for (i = 0; i < NumberOfNames; i++) {
                            unsigned int y = 0;
                            fread (&y, 4, 1, hFile);
                            pos = ftell (hFile);
                            fseek (hFile, Rva2Offset (y), SEEK_SET);

                            char c = fgetc (hFile);
                            int szNameLen = 0;

                            while (c != '\0') {
                                c = fgetc (hFile);
                                szNameLen++;
                            }

                            fseek (hFile, (-szNameLen)-1, SEEK_CUR);
                            char* szName = calloc (szNameLen + 1, 1);
                            fread (szName, szNameLen, 1, hFile);

                            callback (szName);

                            fseek (hFile, pos, SEEK_SET);
                        }
                    }
                }
            }
        }

        fclose (hFile);
    }
}

示例:

void mycallback (char* szName) {
    printf ("%s\n", szName);
}

int main () {
    EnumExportedFunctions ("C:\\Windows\\System32\\user32.dll", mycallback);
    return 0;
}

输出:

ActivateKeyboardLayout
AddClipboardFormatListener
AdjustWindowRect
AdjustWindowRectEx
AlignRects
AllowForegroundActivation
AllowSetForegroundWindow
AnimateWindow
AnyPopup
AppendMenuA
AppendMenuW
ArrangeIconicWindows
AttachThreadInput
BeginDeferWindowPos
BeginPaint
BlockInput
BringWindowToTop
BroadcastSystemMessage
BroadcastSystemMessageA
BroadcastSystemMessageExA
BroadcastSystemMessageExW
BroadcastSystemMessageW
BuildReasonArray
CalcMenuBar
.....etc

7
投票

转到 Microsoft 研究并获取 Detours Library。它的一个示例完全符合您的要求。整个库基本上使绕行/重新路由 win32 函数调用变得非常容易。这是非常酷的东西。

绕路

编辑: 另请注意,如果您只想查看导出表,您可以(至少在视觉工作室中)设置项目属性以打印导出/导入表。我不记得确切的选项,但应该很容易通过谷歌搜索。

**Edit2:**选项是Project Properties->Linker->Debugging->Generate MapFile->Yes(/MAP)


4
投票

虽然简单地说

LoadLibraryEx
DONT_RESOLVE_DLL_REFERENCES
可以大大简化这项任务,但你可以让它比他展示的更简单。您可以使用
SymEnumerateSymbols
为您列出符号,而不是自己查找和枚举 DLL 的导出目录。

虽然只比他的代码稍微简单一些(没有断言,他的代码只有六行),但这至少在理论上提供了一点额外的灵活性,以防微软有一天决定稍微改变可执行格式,和/或改变正是 HMODULE 所指的,所以他不再起作用(因为这些细节中的大多数都没有正式记录)。


1
投票

如果您不想麻烦地编写自己的代码,而宁愿使用已经存在的 DLL 用于此目的,我推荐 PE 文件格式 DLL。附带源代码,以便您可以根据需要进行修改。无需担心 GPL。

还提供一个 GUI 应用程序,展示如何使用 DLL。


0
投票

如果您只是想找到一种方法来找出 DLL 中导出了哪些函数,则可以使用 Microsoft 的 dependency walker (depends.exe)。不过,如果您确实需要以编程方式发现导出,这对您没有帮助。


0
投票

我可能是错的,说实话我没有仔细检查过,但我相信在与您的进程不同的架构下构建的模块上使用 ephemient 的代码可能会存在一些兼容性问题。 (再说一遍,我现在可能完全是胡言乱语)

github 上有一个名为 dll2def 的项目,它使用相同的技术(尽管它自行将文件加载到内存中),但似乎有一些检查来根据二进制架构查找导出。您最可能感兴趣的代码位于此文件


0
投票

距离提出这个问题已经过去了 12 年,但我想指出所提出的解决方案中的一些问题。

它们都不能解释序数(没有名称字符串的导出)。 序数索引之间的潜在差距使情况变得复杂。序数有一个起始基数(IMAGE_EXPORT_DIRECTORY 的“Base”字段),但不能保证序数是连续的。

不想花时间编写代码,但一种方法是按索引 0 迭代到 NumberOfFunctions。
然后在第二个(内部)循环中将 0 到 NumberOfNames 的索引匹配到 AddressOfNameOrdinals 数组中。
如果将函数索引与 AddressOfNameOrdinals 数组索引相匹配,则该索引就是您在 AddressOfNames 数组中的索引(必须解析的偏移量)。如果您没有获得匹配项(在 NumberOfNames 索引上),那么它是一个序数导出。
如果 AddressOfFunctions 条目中的函数索引为 0,那么它只是一个序数间隙,您可以跳到下一个索引。
要获取实际序数(用于以字符串形式打印),请将“Base”添加到 NumberOfFunctions 循环索引中。


0
投票

微软在 2021 年底专门写了一篇相关的文档文章。我自己测试了它,它的效果非常好。 https://learn.microsoft.com/en-us/windows/win32/debug/enumerate-symbols

© www.soinside.com 2019 - 2024. All rights reserved.