lundi 26 juillet 2021

How to load exe with reflection if exe does not have a manifest?

I have a dump of an executable file (Runtime: v2.0.50727). It works well without any mistakes. I could load it to DnSpy to debug or to ILSpy. Both of them tells that all references successfully resolved.

However, I can't load it using this code:

try
{
    var second_module = System.Reflection.Assembly.LoadFrom("myprog.bin");
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
}

It gives me the following error:

System.BadImageFormatException: Could not load file or assembly 'file:///myprog.bin' or one of its dependencies. The module was expected to contain an assembly manifest.
File name: 'file:///myprog.bin'
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at myproggg.Program.Main(String[] args) in c:\Sources\My\myproggg\Program.cs:line 11

=== Pre-bind state information ===
LOG: Where-ref bind. Location = myprog.bin
LOG: Appbase = file:///c:/Sources/My/myproggg/bin/x86/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: c:\Sources\My\myproggg\bin\x86\Debug\myproggg.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Attempting download of new URL file:///c:/Sources/My/MPCExtractor/examples/MP/myprog.bin.
ERR: Failed to complete setup of assembly (hr = 0x80131018). Probing terminated.

I tried to do these steps:

  1. Changing compile option from "Any CPU" to "x86". No difference.

  2. Changing framework to 2.0 and to 4.5. No matter what framework I use, it anyway asks me about manifest.

Nothing of the above helped me.

What can I do more to load this dumped executable?





Aucun commentaire:

Enregistrer un commentaire