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;
- using Mono.Collections.Generic;
- namespace Tutorial3Code
- {
- 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)
- {
- // Insert a random typedefinition that we've made
- mod.Types.Add(createType());
- // Create the new method and store it as a variable
- MethodDefinition newMtd = createMethod(mod);
- // Lets add the new method to the module entrypoint declaring type
- mod.EntryPoint.DeclaringType.Methods.Add(newMtd);
- // Iterate through the entrypoint's instruction
- for (int i = 0; i < mod.EntryPoint.Body.Instructions.Count; i++)
- {
- // Assign an instruction to the current iteration
- Instruction inst = mod.EntryPoint.Body.Instructions[i];
- // The opcode Ret (return) symbolizes the end of a method in most cases, sometimes its used to return a value in a function as a way of escaping the method
- if (inst.OpCode == OpCodes.Ret)
- {
- // Assign the ILProcessor, which is a handy tool for instruction manipulation
- ILProcessor ilp = mod.EntryPoint.Body.GetILProcessor();
- // Insert an instruction before the end of the method which calls our special method
- ilp.InsertBefore(inst, Instruction.Create(OpCodes.Call, newMtd));
- // We don't need to iterate through the instructions anymore
- break;
- }
- }
- }
- // Write the file to disk
- asm.Write(@"C:\Tut\messageboxInjection.exe");
- }
- static MethodDefinition createMethod(ModuleDefinition mod)
- {
- // Defines a new method, it isn't added to anything yet so no one knows it exists. It has a type of void and is static and public.
- MethodDefinition newMethod = new MethodDefinition("Test", MethodAttributes.Private | MethodAttributes.Static, mod.Import(typeof(void)));
- // Defines a new Collection of Instructions, we use collections over lists because it is recommended and Mono.Cecil.Collections.Generic is there for use!
- Collection<Instruction> newInsts = new Collection<Instruction>();
- // Add a ldstr to the stack
- newInsts.Add(Instruction.Create(OpCodes.Ldstr, "Hello There!"));
- // Call the writeline method of Console, output will be as following, "Console.WriteLine("Hello There");"
- newInsts.Add(Instruction.Create(OpCodes.Call, mod.Import(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }))));
- // Because our program didn't have a "ReadLine()" at the end it didn't hold its place, lets add one so we can read what it says
- newInsts.Add(Instruction.Create(OpCodes.Call, mod.Import(typeof(Console).GetMethod("ReadLine", Type.EmptyTypes))));
- // Pops the stack because the method return type is a void
- newInsts.Add(Instruction.Create(OpCodes.Pop));
- // Returns nothing
- newInsts.Add(Instruction.Create(OpCodes.Ret));
- foreach (Instruction inst in newInsts)
- {
- // Add that array to the methods Instructions
- newMethod.Body.Instructions.Add(inst);
- }
- return newMethod;
- }
- static TypeDefinition createType()
- {
- TypeDefinition newType = new TypeDefinition("MyNamespace", "MyTypeName", TypeAttributes.Class);
- return newType;
- }
- static void Log(string text)
- {
- Console.WriteLine(text);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement