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

后端 未结 5 1243
时光取名叫无心
时光取名叫无心 2021-02-06 11:22

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

5条回答
  •  梦毁少年i
    2021-02-06 11:31

    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.

提交回复
热议问题