0

I've been trying to inject a type and method for a good day now lol. Um. Here is some of the code: I'm basically just trying to replace System.Convert.FromBase64String with Simpy.Simp but I can't seem to get it to work... (I used InjectHelper.cs from https://github.com/yck1509/ConfuserEx/blob/master/Confuser.Core/Helpers/InjectHelper.cs) and a modded dnlib from https://github.com/yck1509/dnlib/tree/532c767a9a4f6af51cd4eb5d1c0af750c8509c5d)

ModuleDefMD moduleDefMD = ModuleDefMD.Load(typeof(runtime).Module);
        TypeDef type = new TypeDefUser("Simpy");
        type.Attributes = TypeAttributes.AutoLayout | TypeAttributes.Class;
        moduleDef.Types.Add(type);
        TypeDef typeDef = moduleDefMD.ResolveTypeDef(MDToken.ToRID(typeof(Simpy).MetadataToken));
        //InjectHelper.Inject(typeDef, type, moduleDef);
        IEnumerable<IDnlibDef> source = InjectHelper.Inject(typeDef, type, moduleDef);
        init = (MethodDef)source.Single((IDnlibDef method) => method.Name == "Simp");
        foreach (TypeDef typeDef2 in moduleDef.Types)
        {
            foreach (MethodDef methodDef in typeDef2.Methods)
            {
                bool flag = methodDef.Body == null;
                if (!flag)
                {
                    methodDef.Body.SimplifyBranches();
                    for (int i = 0; i < methodDef.Body.Instructions.Count; i++)
                    {
                        bool flag2 = methodDef.Body.Instructions[i].OpCode == OpCodes.Ldstr;
                        if (flag2)
                        {
                            string s = methodDef.Body.Instructions[i].Operand.ToString();
                            string text = Convert.ToBase64String(Encoding.UTF8.GetBytes(s));
                            methodDef.Body.Instructions[i].OpCode = OpCodes.Nop;
                            methodDef.Body.Instructions.Insert(i + 1, new Instruction(OpCodes.Call, moduleDef.Import(typeof(Encoding).GetMethod("get_UTF8", new Type[0]))));
                            methodDef.Body.Instructions.Insert(i + 2, new Instruction(OpCodes.Ldstr, text));
                            methodDef.Body.Instructions.Insert(i + 3, new Instruction(OpCodes.Call, init));
                            methodDef.Body.Instructions.Insert(i + 4, new Instruction(OpCodes.Callvirt, moduleDef.Import(typeof(Encoding).GetMethod("GetString", new Type[]
                            {
                                typeof(byte[])
                            }))));
                            i += 4;
                        }
                    }
                }
            }
        }
  • Welcome to Stack Overflow! Please take the [tour] and read [ask]. _"Can't get it to work"_: what goes wrong? Also, please condense your code down to a [mre]. It's much easier to help when one doesn't have to pore through many lines of irrelevant code. – Pranav Hosangadi Sep 09 '20 at 18:35
  • Okay, thanks so much! – Yung Samzy Sep 09 '20 at 20:54
  • With the code I have up there, when I inject the code into a new assembly the assembly can't call the injected code because it's missing a parent. – Yung Samzy Sep 09 '20 at 20:56
  • I tried recording what happens here: [link](https://samzydev.xyz/downloads/err.mkv) – Yung Samzy Sep 09 '20 at 21:01
  • I found out why it was calling that, it was because I didn't add a reference to system.object lol. But now instead of converting correctly it doesn't lol – Yung Samzy Sep 09 '20 at 21:33

0 Answers0