mardi 20 octobre 2020

C#: Replacing content of method via Reflection not always working

I'm trying to protect the IP in some assemblies against decompiling (I know that there is no absolute saftety).
What I've done so far is putting the dlls encrypted on disk, loading them via decryption in memory (done in C++) and loading them beforehand with Assembly.Load(byte[] rawData). That works so far but the loading of these assemblies has to be initiated (via COM Interop in this case).

So everybody who calls my method Initialize() from his own code can loop through all the assemblies in the current AppDomain via reflection and get the unencrypted bytes by reflection also (see https://stackoverflow.com/a/54372064/14487083).
To prevent that I replaced the content of the method Assembly.GetRawData() via reflection as described here: https://stackoverflow.com/a/36415711/14487083.
That also works and I was almost happy with my solution but there's at least one additional way to get the raw bytes of a loaded assembly with the help of a custom hash class (see https://stackoverflow.com/a/46209735/14487083).
My protection to change the content of the method Assembly.GetRawData() doesn't work in this case altough

new Hash(Assembly).GenerateHash(HashAlgorithm)

gets the data with a call to Assembly.GetRawData().

Any ideas why the solution works with

var pi = assembly.GetType().GetMethod("GetRawBytes", BindingFlags.Instance | BindingFlags.NonPublic);
byte[] assemblyBytes = (byte[]) pi.Invoke(assembly, null);

but not with

new Hash(Assembly).GenerateHash(HashAlgorithm)




Aucun commentaire:

Enregistrer un commentaire