Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to use valuetype in dnlib in order to insert Console.ReadKey()

Featured Replies

Posted

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));
                    }

                }
            }
instructions.Insert(8, new Instruction(OpCodes.Call, mod.Import(typeof(System.ConsoleKeyInfo).GetMethod("ReadKey")));
  • 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

12 hours ago, BreakShoot said:

Throws  Instruction operand is null

can you send the file please ? the one that throw such error 

15 hours ago, XenocodeRCE said:

instructions.Insert(8, new Instruction(OpCodes.Call, mod.Import(typeof(System.ConsoleKeyInfo).GetMethod("ReadKey")));

LOL! :lol::D"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).
 

  • Author
8 minutes ago, CodeCracker said:

LOL! :lol::D"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.

1 hour ago, CodeCracker said:

LOL! :lol::D"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

 

Also interested in this, I've been trying the same thing and getting crashes and the errors he is getting

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

 

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.