1

I am trying to change a part in a third party library, it has a bug, however, there are no fixes and source code is not available as well.

At first, I tried to modify IL Opcodes directly by disassembling an assembly, but it has some kind of protection, so after recompiling it I was not able to use it even that I had removed Strong Name verification from it.

I have tried to change IL instructions in runtime, however

        InjectionHelper.Initialize();
        InjectionHelper.WaitForIntializationCompletion();
        Type type = obj.GetType();
        var methods = type.GetMethods();
       // MethodInfo targetMethod = type.GetMethod("MethodName",  BindingFlags.NonPublic | BindingFlags.Instance);

        var methodInfo = type
            .GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Single(
                m =>
                    m.ReturnType == typeof(string) &&
                    m.GetParameters().Length == 1);

        RuntimeHelpers.PrepareMethod(methodInfo.MethodHandle);
        byte[] ilOpCodes = methodInfo.GetMethodBody()?.GetILAsByteArray();
        for (int i = 0; i < ilOpCodes.Length; i++)
        {
            if (ilOpCodes[i] == OpCodes.Ldc_I4_1.Value)
            {
                ilOpCodes[i] = (byte) OpCodes.Ldc_I4_0.Value;
                break;
            }
        }
        InjectionHelper.UpdateILCodes(methodInfo,ilOpCodes);

But unfortunately got an exception.

Unhandled Exception: System.Exception: UpdateILCodes() failed, please check the initialization is failed or uncompleted.

Is there any mistake I made? I used this article as a reference https://www.codeproject.com/Articles/463508/NET-CLR-Injection-Modify-IL-Code-during-Run-time So what is the best way to make it usable?

0

1 Answer 1

5

In the past I've had issues editing the IL code during run time, especially if the method had already been compiled by the JIT Compiler, and the most full proof way I found was to patch it during run time was to edit the x86/x86_64(or whatever architecture you're running) generated by the JIT Compiler which is located at the pointer given by

MethodInfo.MethodHandle.GetFunctionPointer()

but you need to make sure the target function is already compiled by either calling it or using

RuntimeHelpers.PrepareMethod(MethodHandle)

From there you can either utilize unsafe code blocks or Marshal to copy your own x86 bytecode (for example a jump to the function you want to replace with)

I do however recommend you to use, or at least to take a look at, Harmony which is a library utilized by multiple games to support mods.

As for the reason why you're unable to utilize the library after removing the strong name it might be due to the fact that the application utilizing the library is 'linked' by the strong name and without it the load fails.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.