Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Mono.Cecil;
- using Mono.Cecil.Cil;
- namespace Tutorial2Code
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Loads the assembly from the start arguments
- AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(args[0]);
- // Iterates through the assembly's modules, aspects of the program
- foreach (ModuleDefinition mod in asm.Modules)
- {
- // Iterates through the classes of the assembly
- foreach (TypeDefinition td in mod.Types)
- {
- // Writes the types name,
- DoType(td);
- }
- }
- asm.Write(@"C:\Tut\out.exe");
- Console.ReadLine();
- }
- static void DoType(TypeDefinition td)
- {
- foreach (TypeDefinition ntd in td.NestedTypes)
- {
- // Repeat if the class has nested classes.
- DoType(ntd);
- }
- foreach (MethodDefinition md in td.Methods)
- {
- // First check if it an important method, like a constructor or runtime special name which will crash our app if renamed
- if (!md.IsRuntimeSpecialName && !md.IsRuntime)
- {
- // Create a random name from a random number
- md.Name = new Random().Next(5, 50000).ToString();
- // Very Important, Make sure the method has a body!
- if (md.HasBody)
- {
- // Iterate through the methods instructions
- for (int i = 0; i < md.Body.Instructions.Count; i++)
- {
- // Make it easier to call the instruction by creating an instance of it
- Instruction inst = md.Body.Instructions[i];
- // Lets see what the instruction is about?
- Log(inst.ToString());
- // If the opcode is ldstr, load string to stack
- if (inst.OpCode == OpCodes.Ldstr)
- {
- // Change the value of the string to something funny!
- inst.Operand = "Hello World";
- }
- }
- }
- }
- }
- }
- static void Log(string text)
- {
- Console.WriteLine(text);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement