private void LoadExports(LOADED_IMAGE loadedImage) { var hMod = (void *)loadedImage.MappedAddress; if (hMod != null) { Log("Got handle"); uint size; var pExportDir = (IMAGE_EXPORT_DIRECTORY *)ImageDirectoryEntryToData( (void *)loadedImage.MappedAddress, false, IMAGE_DIRECTORY_ENTRY_EXPORT, out size); if (pExportDir != null) { Log("Got Image Export Descriptor"); var pFuncNames = (uint *)RvaToVa(loadedImage, pExportDir->AddressOfNames); for (uint i = 0; i < pExportDir->NumberOfNames; i++) { uint funcNameRva = pFuncNames[i]; if (funcNameRva != 0) { var funcName = (char *)RvaToVa(loadedImage, funcNameRva); var name = Marshal.PtrToStringAnsi((IntPtr)funcName); Log(" funcName: {0}", name); _exports.Add(name); } } } } }
private void LoadImports(LOADED_IMAGE loadedImage) { var hMod = (void *)loadedImage.MappedAddress; if (hMod != null) { Console.WriteLine("Got handle"); uint size; var pImportDir = (IMAGE_IMPORT_DESCRIPTOR *) ImageDirectoryEntryToData(hMod, false, IMAGE_DIRECTORY_ENTRY_IMPORT, out size); if (pImportDir != null) { Log("Got Image Import Descriptor"); while (pImportDir->OriginalFirstThunk != 0) { try { var szName = (char *)RvaToVa(loadedImage, pImportDir->Name); string name = Marshal.PtrToStringAnsi((IntPtr)szName); var pr = new Tuple <string, List <string> >(name, new List <string>()); _imports.Add(pr); var pThunkOrg = (THUNK_DATA *)RvaToVa(loadedImage, pImportDir->OriginalFirstThunk); while (pThunkOrg->AddressOfData != IntPtr.Zero) { uint ord; if ((pThunkOrg->Ordinal & 0x80000000) > 0) { ord = pThunkOrg->Ordinal & 0xffff; Log("imports ({0}).Ordinal{1} - Address: {2}", name, ord, pThunkOrg->Function); } else { var pImageByName = (IMAGE_IMPORT_BY_NAME *)RvaToVa(loadedImage, pThunkOrg->AddressOfData); if ( !IsBadReadPtr(pImageByName, (uint)sizeof(IMAGE_IMPORT_BY_NAME))) { ord = pImageByName->Hint; var szImportName = pImageByName->Name; string sImportName = Marshal.PtrToStringAnsi((IntPtr)szImportName); Log("imports ({0}).{1}@{2} - Address: {3}", name, sImportName, ord, pThunkOrg->Function); pr.Item2.Add(sImportName); } else { Log("Bad ReadPtr Detected or EOF on Imports"); break; } } pThunkOrg++; } } catch (AccessViolationException e) { Log("An Access violation occured\n" + "this seems to suggest the end of the imports section\n"); Log(e.ToString()); } pImportDir++; } } } }
private static IntPtr RvaToVa(LOADED_IMAGE loadedImage, IntPtr rva) { return(RvaToVa(loadedImage, (uint)(rva.ToInt32()))); }
private static IntPtr RvaToVa(LOADED_IMAGE loadedImage, uint rva) { return(ImageRvaToVa(loadedImage.FileHeader, loadedImage.MappedAddress, rva, IntPtr.Zero)); }
public static extern bool MapAndLoad(string imageName, string dllPath, out LOADED_IMAGE loadedImage, bool dotDll, bool readOnly);