1

我的项目需要检查 .c 和 .dll 文件。它结合这些信息来确定它应该调用什么,然后调用它。

我需要检查 dll 以找出哪个 dll 具有哪个功能。到目前为止,我已经将 dll 映射到内存而不初始化它。现在我需要将标题映射到某些东西,以便我可以读出其中包含可调用名称的部分。

我怎样才能做到这一点?这是到目前为止的代码:

    [DllImport("kernel32.dll")]
    static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags);

    public static string[] GetFKTNames(string dll)
    {
        IntPtr lib = LoadLibraryEx(dll, IntPtr.Zero, LoadLibraryFlags.DONT_RESOLVE_DLL_REFERENCES);

        //INDICATES WHAT I WANT TO DO, BUT DOES NOT WORk
        //Header header = GetHeader(lib);
        //Unload(lib);
        //return header.Names;
}

编辑#2:

我取得了一点进步,今天放弃了……德国有 4 天的空闲时间……

我不完全确定这种编组是否正确 - 我无法测试它。我很想读一本关于这个主题的书——所以如果你知道一本解释标题如何工作以及有哪些不同标题的好书,请发表评论。

    private static List<string> ListDLLFunctions(string sADllName)
    {
        List<string> names = new List<string>();
        IntPtr LoadedImage = LoadLibraryEx(sADllName, IntPtr.Zero, LoadLibraryFlags.DONT_RESOLVE_DLL_REFERENCES);

        IMAGE_NT_HEADERS header = (IMAGE_NT_HEADERS) Marshal.PtrToStructure(libPtr, typeof(IMAGE_NT_HEADERS));

        //    ImageExportDirectory = (_IMAGE_EXPORT_DIRECTORY*)
        //        ImageDirectoryEntryToData(LoadedImage.MappedAddress,
        //        false, IMAGE_DIRECTORY_ENTRY_EXPORT, &cDirSize);
        //    if (ImageExportDirectory != NULL)
        //    {
        //        dNameRVAs = (DWORD *)ImageRvaToVa(LoadedImage.FileHeader, 
        //            LoadedImage.MappedAddress,
        //        ImageExportDirectory->AddressOfNames, NULL);
        //        for(size_t i = 0; i < ImageExportDirectory->NumberOfNames; i++)
        //        {
        //            sName = (char *)ImageRvaToVa(LoadedImage.FileHeader, 
        //                    LoadedImage.MappedAddress,
        //                   dNameRVAs[i], NULL);
        //         slListOfDllFunctions.push_back(sName);
        //        }
        //    }
        FreeLibrary(LoadedImage);
        return names;
    }

    static void Main(string[] args)
    {
        List<string> names = ListDLLFunctions("KERNEL32.DLL");
    }
4

3 回答 3

1

没有可以为您完成的“一次呼叫”功能。您必须加载 dll 并在导出表中查找名称。

有一个帖子,其中包含一些代码可以用 c++ 完成- 也许您可以尝试将其移植到 c#

于 2012-04-05T11:02:12.873 回答
0

不是来自代码,而是来自我通常使用的控制台

DumpBin实用程序查看 Dll 的导出。

我认为您可以使用该工具Process.Start(..)并解析其输出以获得所需的信息。

希望这可以帮助。

于 2012-04-05T10:30:02.777 回答
0

我为我的用例解决了这个问题:

我将库加载到内存中,将其复制到字节数组中,然后使用 PE 信息对其进行调查。

这个主题有几个有用的资源对我有很大帮助:

回想起来,我知道如何使用其他方法来获取我的信息。我写的课程非常适合学习 PE 和探索一些 dll,如果你想了解 PE 的工作原理,我建议重写它。

这是课程:

public class DLLHelper
{
    private byte[] dllInMemory;
    private UInt32 PESizeOfImage;
    private UInt32 VA_PE;

    [DllImport("kernel32.dll")]
    static public extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags);

    [DllImport("kernel32.dll")]
    static public extern bool FreeLibrary(IntPtr hModule);

    public enum LoadLibraryFlags : uint
    {
        DONT_RESOLVE_DLL_REFERENCES = 0x00000001,
        LOAD_IGNORE_CODE_AUTHZ_LEVEL = 0x00000010,
        LOAD_LIBRARY_AS_DATAFILE = 0x00000002,
        LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = 0x00000040,
        LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x00000020,
        LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
    }


    public DLLHelper(string dllpath)
    {
        PESizeOfImage = GetDllSizeInMemory(dllpath);
        dllInMemory = GetDLLCopy(dllpath, PESizeOfImage);
        UInt32 VA_p_PE = 0x3C;
        VA_PE = Get4ByteFromLocation(VA_p_PE, dllInMemory);
    }

    private byte[] GetDLLCopy(string dllpath, uint PESizeOfImage)
    {
        IntPtr libPtr = LoadLibraryEx(dllpath, IntPtr.Zero, LoadLibraryFlags.DONT_RESOLVE_DLL_REFERENCES);
        byte[] dllInMemory = new byte[PESizeOfImage];
        Marshal.Copy(libPtr, dllInMemory, 0, (int)PESizeOfImage);
        FreeLibrary(libPtr);
        return dllInMemory;
    }

    private UInt32 GetDllSizeInMemory(string dllpath)
    {
        byte[] dllpreload = File.ReadAllBytes(dllpath);
        UInt32 pp_PE = 0x3C;
        UInt32 p_PE = Get4ByteFromLocation(pp_PE, dllpreload);
        UInt32 p_PEOPTIONALHEADER = p_PE + 0x18;
        UInt32 p_PESizeOfImage = p_PEOPTIONALHEADER + 0x38;
        return Get4ByteFromLocation(p_PESizeOfImage, dllpreload);
    }

    public void DumpToFile(String filename)
    {
        File.WriteAllBytes(filename, dllInMemory);
    }

    public string GetDLLName()
    {
        UInt32 VAExport = GetVAExport(VA_PE, dllInMemory);
        UInt32 VAName = GetVAName(VAExport, dllInMemory);
        String Name = GetString(VAName, dllInMemory);
        return Name;
    }

    public List<String> GetFunctionNames()
    {
        List<String> fkts = new List<String>();
        UInt32 VAExport = GetVAExport(VA_PE, dllInMemory);
        UInt32 VA_p_firstFKT = GetVA_p_firstFKT(VAExport, dllInMemory);
        UInt32 VA_p_lastFKT = GetVA_p_lastFKT(VAExport, dllInMemory);
        for (UInt32 VA_p_fkt = VA_p_firstFKT; VA_p_fkt <= VA_p_lastFKT; VA_p_fkt += sizeof(UInt32))
        {
            UInt32 VA_fkt = Get4ByteFromLocation(VA_p_fkt, dllInMemory);
            fkts.Add(GetString(VA_fkt, dllInMemory));
        }
        return fkts;
    }

    private UInt32 GetVA_p_lastFKT(UInt32 VAExport, byte[] dllInMemory)
    {
        UInt32 first = GetVA_p_firstFKT(VAExport, dllInMemory);
        UInt32 count = GetfktCount(VAExport, dllInMemory);
        UInt32 last = first + (count - 1) * sizeof(UInt32);
        return last;
    }

    private UInt32 GetfktCount(UInt32 VAExport, byte[] dllInMemory)
    {
        UInt32 RVA_Count = 0x14;
        UInt32 VA_Count = VAExport + RVA_Count;
        return Get4ByteFromLocation(VA_Count, dllInMemory);
    }

    private UInt32 GetVA_p_firstFKT(UInt32 VAExport, byte[] dllInMemory)
    {
        UInt32 RVA_p_FIRST = 0x20;
        UInt32 VA_p_FIRST = VAExport + RVA_p_FIRST;
        return Get4ByteFromLocation(VA_p_FIRST, dllInMemory);
    }

    private UInt32 GetVAName(UInt32 VAExport, byte[] dllInMemory)
    {
        UInt32 RVA_p_NAME = 0x0C;
        UInt32 VA_p_NAME = VAExport + RVA_p_NAME;
        return Get4ByteFromLocation(VA_p_NAME, dllInMemory);
    }

    private UInt32 GetVAExport(UInt32 VAPE, byte[] dllInMemory)
    {
        UInt32 RVA_p_EXPORT = 0x78;
        UInt32 VA_p_EXPORT = VAPE + RVA_p_EXPORT;
        return Get4ByteFromLocation(VA_p_EXPORT, dllInMemory);
    }

   string GetString(UInt32 location, byte[] dll)
    {
        int length = 0;
        while (dll[location + length] != 0x00)
        {
            length++;
        }
        if (location > int.MaxValue) throw new Exception("uncastable");
        return Encoding.UTF8.GetString(dll, (int)location, length);
    }

   private UInt32 Get4ByteFromLocation(UInt32 location, byte[] dll)
   {
       if (!(BitConverter.IsLittleEndian))
       {
           byte[] partial = GetByteSubset(4, location, dll);
           Array.Reverse(partial);
           return BitConverter.ToUInt32(partial, 0);
       }
       return BitConverter.ToUInt32(dll, (int)location);
   }

   private byte[] GetByteSubset(int size, UInt32 location, byte[] dll)
   {
       byte[] val = new byte[size];
       for (int i = 0; i < size; i++)
       {
           val[i] = dll[location + i];
       }
       return val;
   }
}
于 2012-04-13T15:54:22.910 回答