Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I inject a file into an EXE at runtime and reference the file during program operation?

I'd like a user to download an exe from my website, where (synchronously upon download) an XML file is injected into this application. This XML file contains a public key, and a signature.

How do I inject the file prior to downloading and reference it later during execution?

Ideally I won't be using a shell to inject the file, rather a native .NET api.

like image 783
makerofthings7 Avatar asked Mar 17 '11 14:03

makerofthings7


1 Answers

You could that easily with Mono.Cecil, you'd just have to write something like:

var module = ModuleDefinition.ReadModule ("Application.exe");

module.Resources.Add (
    new EmbeddedResource (
        "signature.xml",
        ManifestResourceAttributes.Private, 
        File.ReadAllBytes ("signature.xml")));

module.Write ("Application.exe",
    new WriterParameters {
        StrongNameKeyPair = new StrongNameKeyPair ("keypair.snk")
});

To inject the signature.xml resource from the signature.xml file, and sign back your assembly with your keypair.snk that you used to sign Application.exe.

And at runtime, you'd just have to use:

var stream = Assembly.GetExecutingAssembly ()
    .GetManifestResourceStream ("signature.xml");

To retrieve the resource.

like image 145
Jb Evain Avatar answered Sep 29 '22 10:09

Jb Evain