Posted August 31, 20177 yr I am using the dnlib and I want to insert Console.ReadKey() at the end of the Console.WriteLines in another executable. Note I am extremely new to dnlib and wish to learn more about it. The code below partly works. Though the issue is. It produces the MSIL: IL_0028: call void [mscorlib]System.Console::ReadKey() When I want it to produce IL_015a: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey() Code ModuleDefMD mod = ModuleDefMD.Load(args[0]); ModuleDef mode = new ModuleDefUser(args[0]); Importer importer = new Importer(mod); AssemblyDef asmr = mod.Assembly; foreach (TypeDef type in mod.GetTypes()) { foreach (MethodDef Method in type.Methods) { if (Method.Name == "Main") { TypeRef consoleRef = new TypeRefUser(mod, "System", "Console", mod.CorLibTypes.AssemblyRef); MemberRef ReadAll = new MemberRefUser(mod, "ReadKey", MethodSig.CreateStatic(mod.CorLibTypes.Void), consoleRef); var method = Method; var instructions = method.Body.Instructions; instructions.Insert(8, new Instruction(OpCodes.Call, ReadAll)); } } }
September 1, 20177 yr instructions.Insert(8, new Instruction(OpCodes.Call, mod.Import(typeof(System.ConsoleKeyInfo).GetMethod("ReadKey")));
September 1, 20177 yr Author 1 hour ago, XenocodeRCE said: instructions.Insert(8, new Instruction(OpCodes.Call, mod.Import(typeof(System.ConsoleKeyInfo).GetMethod("ReadKey"))); Throws Instruction operand is null
September 1, 20177 yr 12 hours ago, BreakShoot said: Throws Instruction operand is null can you send the file please ? the one that throw such error
September 1, 20177 yr 15 hours ago, XenocodeRCE said: instructions.Insert(8, new Instruction(OpCodes.Call, mod.Import(typeof(System.ConsoleKeyInfo).GetMethod("ReadKey"))); LOL! "ReadKey" is from System.Console not from System.ConsoleKeyInfo. Proper code:instructions.Insert(8, new Instruction(OpCodes.Call, mod.Import(typeof(System.Console).GetMethod("ReadKey"))); XenocodeRCE: the (source) code is the one who give error (I presume).
September 1, 20177 yr Author 8 minutes ago, CodeCracker said: LOL! "ReadKey" is from System.Console not from System.ConsoleKeyInfo. Proper code:instructions.Insert(8, new Instruction(OpCodes.Call, mod.Import(typeof(System.Console).GetMethod("ReadKey"))); XenocodeRCE: the (source) code is the one who give error (I presume). Thanks for helping! I will test this when I return home. Hopefully will work. XenocodeRCE: The target file is a file that contains a few Console.WriteLine("random string"); in Main method. You drag it in, and boom the output file doesn't immediately close when opening (ie, ReadKey). Anyways the program using dnlib was crashing on mod.Write. And when I ignored errors and compiled anyways; DnSpy failed to decompile it.
September 1, 20177 yr 1 hour ago, CodeCracker said: LOL! "ReadKey" is from System.Console not from System.ConsoleKeyInfo. Proper code:instructions.Insert(8, new Instruction(OpCodes.Call, mod.Import(typeof(System.Console).GetMethod("ReadKey"))); XenocodeRCE: the (source) code is the one who give error (I presume). I misread his first message then because look : http://i.imgur.com/WXaxSsS.png
September 6, 20177 yr Also interested in this, I've been trying the same thing and getting crashes and the errors he is getting
September 6, 20177 yr When you don't know how something is done, just take dnlib's source and search for the appropriate keyword. In this case - search for "valuetype". Fixed @BreakShoot's code: ModuleDefMD mod = ModuleDefMD.Load(args[0]); Importer importer = new Importer(mod); AssemblyDef asmr = mod.Assembly; foreach (TypeDef type in mod.GetTypes()) { foreach (MethodDef Method in type.Methods) { if (Method.Name == "Main") { TypeRef cki = new TypeRefUser(mod, "System", "ConsoleKeyInfo", mod.CorLibTypes.AssemblyRef); TypeRef consoleRef = new TypeRefUser(mod, "System", "Console", mod.CorLibTypes.AssemblyRef); MemberRef ReadAll = new MemberRefUser(mod, "ReadKey", MethodSig.CreateStatic(new ValueTypeSig(cki)), consoleRef); var method = Method; var instructions = method.Body.Instructions; instructions.Insert(instructions.Count - 1, new Instruction(OpCodes.Call, ReadAll)); instructions.Insert(instructions.Count - 1, new Instruction(OpCodes.Pop)); } } } // Save the assembly to a file on disk mod.Write(args[1]); End result: IL_000c: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey() IL_0011: pop
September 6, 20177 yr I think you must add AssemblyResolver. var moduleContext = new ModuleContext(); var asmResolver = new AssemblyResolver(moduleContext, true); var resolver = new Resolver(asmResolver); moduleContext.AssemblyResolver = asmResolver; moduleContext.Resolver = resolver; ModuleDefMD moduleDefMd = ModuleDefMD.Load(@"C:\File.exe", moduleContext); foreach (TypeDef typeDef in moduleDefMd.GetTypes()) { }
Create an account or sign in to comment