Advertisement
LaPanthere

Tutorial 2: Instruction and Method Manipulation

Jan 31st, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Mono.Cecil;
  6. using Mono.Cecil.Cil;
  7.  
  8. namespace Tutorial2Code
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             // Loads the assembly from the start arguments
  15.             AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(args[0]);
  16.  
  17.             // Iterates through the assembly's modules, aspects of the program
  18.             foreach (ModuleDefinition mod in asm.Modules)
  19.             {
  20.                 // Iterates through the classes of the assembly
  21.                 foreach (TypeDefinition td in mod.Types)
  22.                 {
  23.                     // Writes the types name,
  24.                     DoType(td);
  25.                 }
  26.             }
  27.             asm.Write(@"C:\Tut\out.exe");
  28.             Console.ReadLine();
  29.         }
  30.         static void DoType(TypeDefinition td)
  31.         {
  32.             foreach (TypeDefinition ntd in td.NestedTypes)
  33.             {
  34.                 // Repeat if the class has nested classes.
  35.                 DoType(ntd);
  36.             }
  37.             foreach (MethodDefinition md in td.Methods)
  38.             {
  39.                 // First check if it an important method, like a constructor or runtime special name which will crash our app if renamed
  40.                 if (!md.IsRuntimeSpecialName && !md.IsRuntime)
  41.                 {
  42.                     // Create a random name from a random number
  43.                     md.Name = new Random().Next(5, 50000).ToString();
  44.  
  45.                     // Very Important, Make sure the method has a body!
  46.                     if (md.HasBody)
  47.                     {
  48.                         // Iterate through the methods instructions
  49.                         for (int i = 0; i < md.Body.Instructions.Count; i++)
  50.                         {
  51.                             // Make it easier to call the instruction by creating an instance of it
  52.                             Instruction inst = md.Body.Instructions[i];
  53.  
  54.                             // Lets see what the instruction is about?
  55.                             Log(inst.ToString());
  56.  
  57.                             // If the opcode is ldstr, load string to stack
  58.                             if (inst.OpCode == OpCodes.Ldstr)
  59.                             {
  60.                                 // Change the value of the string to something funny!
  61.                                 inst.Operand = "Hello World";
  62.                             }
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.         static void Log(string text)
  69.         {
  70.             Console.WriteLine(text);
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement