Jump to content
Tuts 4 You

Harmony Injector Help


Ahmad_k

Recommended Posts

I was trying Harmony it looks very promising as it is very useful for me. My problem is i can't inject the created dll into my assembly to do some test using MegaDumper. I got (Failed to get Base of kernel32)

What i did is just intercept the calculate function inside another form application.

Is there any simple application for injecting dll ?

 

DLL Code to inject

using Harmony;
using System;
using System.Reflection;
using Harmony_test;
using System.Windows.Forms;

namespace ClassLibrary1
{
    public class Class1
    {
        static void Main(string[] args)
        {
            var harmony = HarmonyInstance.Create("com.test.test");
            harmony.PatchAll(Assembly.GetExecutingAssembly());
            
        }
    }

    [HarmonyPatch(typeof(Form1))]
    [HarmonyPatch("calculate")]
    [HarmonyPatch(new Type[] { typeof(int), typeof(int) })]
    class Patch
    {
        static void Prefix(int num1, int num2)
        {
            MessageBox.Show("MOD is loaded"); 
        }
    }
}

 

Test App

        private void button1_Click(object sender, EventArgs e)
        {
            calculate(int.Parse(textBox1.Text),int.Parse(textBox2.Text));
        }

        private void calculate (int num1, int num2)
        {
            textBox3.Text = (num1 + num2).ToString();
        }

 

Link to comment

For Harmony You need to load Target executable to the current domain in other words you need to create application loader.

The Step:

1. Create new WinForms (loader)

- Add reference to 0Harmony.dll and Target.exe

- Add button, name it btnOpenApp with click handler

private void btnOpenApp_Click(object sender, EventArgs e)
{
    AssemblyName assemblyName = AssemblyName.GetAssemblyName(@"c:\path\to\Target.exe");
    var assembly = Assembly.Load(assemblyName);
    var methodBase = assembly.ManifestModule.ResolveMethod(assembly.EntryPoint.MetadataToken);

    // do the patch
    Harmony.Patch();
    
    // Open the Target
    new Thread(() =>
    {
        // assume method entry point is static and doesn't have parameter
        methodBase.Invoke(null, null);
    }).Start();
}

2. Create class Harmony.cs

using Harmony;
using System;
using System.Reflection;
using System.Windows.Forms;

namespace YourWinformsNameSpace
{
    internal static class Harmony
    {
        public static void Patch()
        {
            HarmonyInstance h = HarmonyInstance.Create("test.patch.by.ewwink");
            h.PatchAll(Assembly.GetExecutingAssembly());
        }

        [HarmonyPatch(typeof(Target.FormClass), "calculate")]
        [HarmonyPatch(new Type[] { typeof(int), typeof(int) })]
        public class Patchcalculate
        {
            static void Prefix(int num1, ref int num2)
            {
                MessageBox.Show(string.Format("Second param {0} will be patched to 7", num2));
                num2 = 7;
            }
        }
    }
}

The above will patch second parameter for calculate method to 7.

make sure target Framework and CPU is match.

  • Like 2
Link to comment

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...