2

我已经成功地从另一个进程加载到内存中的非托管模块中读取了 PE 标头。我现在想做的是读取这个模块的导出名称。基本上,这就是我目前所拥有的(我省略了大部分 PE 解析代码,因为我已经知道它可以工作):

扩展

public static IntPtr Increment(this IntPtr ptr, int amount)
{
    return new IntPtr(ptr.ToInt64() + amount);
}

public static T ToStruct<T>(this byte[] data)
{
    GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
    T result = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
    handle.Free();
    return result;
}

public static byte[] ReadBytes(this Process process, IntPtr baseAddress, int size)
{
    int bytesRead;
    byte[] bytes = new byte[size];
    Native.ReadProcessMemory(process.Handle, baseAddress, bytes, size, out bytesRead);
    return bytes;
}

public static T ReadStruct<T>(this Process process, IntPtr baseAddress)
{
    byte[] bytes = ReadBytes(process, baseAddress, Marshal.SizeOf(typeof(T)));
    return bytes.ToStruct<T>();
}

public static string ReadString(this Process process, IntPtr baseAddress, int size)
{
    byte[] bytes = ReadBytes(process, baseAddress, size);
    return Encoding.ASCII.GetString(bytes);
}

获取出口()

Native.IMAGE_DATA_DIRECTORY dataDirectory =
    NtHeaders.OptionalHeader.DataDirectory[Native.IMAGE_DIRECTORY_ENTRY_EXPORT];

if (dataDirectory.VirtualAddress > 0 && dataDirectory.Size > 0)
{
    Native.IMAGE_EXPORT_DIRECTORY exportDirectory =
        _process.ReadStruct<Native.IMAGE_EXPORT_DIRECTORY>(
            _baseAddress.Increment((int)dataDirectory.VirtualAddress));

    IntPtr namesAddress = _baseAddress.Increment((int)exportDirectory.AddressOfNames);
    IntPtr nameOrdinalsAddress = _baseAddress.Increment((int)exportDirectory.AddressOfNameOrdinals);
    IntPtr functionsAddress = _baseAddress.Increment((int)exportDirectory.AddressOfFunctions);

    for (int i = 0; i < exportDirectory.NumberOfFunctions; i++)
    {
        Console.WriteLine(_process.ReadString(namesAddress.Increment(i * 4), 64));
    }
}

当我运行它时,我得到的只是一个双问号模式,然后是完全随机的字符。我知道标题被正确读取,因为签名是正确的。问题必须在于我迭代函数列表的方式。

4

1 回答 1

2

此链接上的代码似乎表明名称和序数形成了一对匹配的数组,计数到 NumberOfNames,并且函数是分开的。因此,您的循环可能会迭代错误的次数,但这并不能解释为什么您从一开始就看到错误的字符串。

对于仅打印名称,我通过如下所示的循环获得了成功。我认为调用ImageRvaToVa可能是您获得正确字符串所需的?但是,我不知道该功能是否会起作用,除非您通过调用实际加载了图像MapAndLoad——这是文档所要求的,并且在我使用的一些快速实验中,映射似乎不起作用LoadLibrary

这是 pInvoke 声明:

    [DllImport("DbgHelp.dll", CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurity]
    public static extern IntPtr ImageRvaToVa(
        IntPtr NtHeaders,
        IntPtr Base,
        uint Rva,
        IntPtr LastRvaSection);

这是我的主循环:

               LOADED_IMAGE loadedImage = ...; // populated with MapAndLoad
               IMAGE_EXPORT_DIRECTORY* pIID = ...; // populated with ImageDirectoryEntryToData

                uint* pFuncNames = (uint*)
                    ImageRvaToVa(
                        loadedImage.FileHeader,
                        loadedImage.MappedAddress, 
                        pIID->AddressOfNames, 
                        IntPtr.Zero);

                for (uint i = 0; i < pIID->NumberOfNames; i++ )
                {
                    uint funcNameRVA = pFuncNames[i];
                    if (funcNameRVA != 0)
                    {
                        char* funcName =
                                (char*) (ImageRvaToVa(loadedImage.FileHeader,
                                   loadedImage.MappedAddress, 
                                   funcNameRVA, 
                                   IntPtr.Zero));
                        var name = Marshal.PtrToStringAnsi((IntPtr) funcName);
                        Console.WriteLine("   funcName: {0}", name);
                    }
                }
于 2010-12-31T22:08:00.623 回答