Jump to content
Tuts 4 You

Trying to modify an assembly at runtime


MindSystem

Recommended Posts

Posted (edited)

Hi everybody,

that was a long time that i didn't try to code anything. So sorry if there're stupid mistakes in my code.

I try to have an assembly that modify itself at runtime

For example, all Nop opcode becomes Ret opcode (don't try to find logic :) )

 

So i inject a method in the .cctor of the assembly. Here's the code :

 

Assembly currentAssembly = Assembly.GetExecutingAssembly();
            Type type = currentAssembly.GetType();
            foreach(MethodInfo method in type.GetMethods())
            {
                RuntimeHelpers.PrepareMethod(method.MethodHandle);
                byte[] iLAsByteArray = method.GetMethodBody().GetILAsByteArray();
                for (int i = 0; i < iLAsByteArray.Length; i++)
                {
                    if ((short)iLAsByteArray == OpCodes.Endfinally.Value)
                    {
                        iLAsByteArray = (byte)OpCodes.Ldarg_0.Value;
                    }
                }
                UpdateILCodes(method, iLAsByteArray, -1);
            }

 

the code is a mess but as i said, i didn't touched a computer for a long time.

 

Thanks for you greatful help :)

Edited by Legend-Modz-V1
Posted
On 26/12/2016 at 9:10 PM, ilya01 said:

Seems, that are you using injection library. Try to watch source of exaples, how to use it.

But this library is unstable and It does not always work.

i looked on examples for a day but impossible to find solution ;/

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...