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 Tutorial4Code
- {
- 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)
- {
- // Create a type definition
- TypeDefinition injected = createType(mod);
- // Finds or creates the modules main constructor, "<Module>.cctor".
- FindMethod(mod, injected);
- // Getting the method "Begin" from our injected type
- MethodDefinition inMtd = Extensions.FirstOrDefault<MethodDefinition>(injected.Methods, m => m.Name == "Begin");
- // Iterating through the instructions and replacing a string if its equal to "PADDING"
- for (int i = 0; i < inMtd.Body.Instructions.Count; i++)
- {
- Instruction inst = inMtd.Body.Instructions[i];
- if (inst.OpCode == OpCodes.Ldstr)
- {
- if ((inst.Operand as string) == "PADDING")
- {
- inst.Operand = "Hi there, I've been changed from my original value!";
- }
- }
- }
- // Insert the injected type
- mod.Types.Add(injected);
- }
- // Write the file to disk
- asm.Write(@"C:\Tut\importerInjector.exe");
- }
- static void FindMethod(ModuleDefinition mod, TypeDefinition importedType)
- {
- // Because the main constructor is never in a nested type we don't have to worry about looking there
- foreach (TypeDefinition td in mod.Types)
- {
- // If the type definitions name is equal to "<Module>"
- if (td.Name == "<Module>")
- {
- // We'll use a bool to see if the static constructor is found
- bool constructorFound = false;
- // Same as up at the start, we want the "Begin" method of the injected type.
- MethodDefinition meth = Extensions.FirstOrDefault<MethodDefinition>(importedType.Methods, m => m.Name == "Begin");
- // Searching for the module's static constructor
- foreach (MethodDefinition md in td.Methods)
- {
- if (md.Name == ".cctor")
- {
- // If its found it'll set the variable to true
- constructorFound = true;
- // Add the needed instructions to call our method to the static constructor
- AddInstructions(md, meth);
- }
- }
- if (!constructorFound)
- {
- // If the constructor is not found, add a static constructor
- MethodDefinition cctor = AddStaticConstructor(td);
- // Then we can add the instructions to call our injected method
- AddInstructions(cctor, meth);
- }
- }
- }
- }
- private static MethodDefinition AddStaticConstructor(TypeDefinition td)
- {
- ModuleDefinition mod = td.Module;
- // Static constructors must have the same attributes, otherwise it won't be recoginized
- MethodDefinition method = new MethodDefinition(".cctor", Mono.Cecil.MethodAttributes.Private | Mono.Cecil.MethodAttributes.Static | Mono.Cecil.MethodAttributes.HideBySig | Mono.Cecil.MethodAttributes.SpecialName | Mono.Cecil.MethodAttributes.RTSpecialName, mod.Import(typeof(void)));
- // Add the static constructor to "<Module>"
- td.Methods.Add(method);
- return method;
- }
- private static void AddInstructions(MethodDefinition cctor, MethodDefinition reference)
- {
- // You should know what is happening here, if not go and visit my other tutorials
- ModuleDefinition mod = cctor.DeclaringType.Module;
- ILProcessor ilp = cctor.Body.GetILProcessor();
- Instruction call = Instruction.Create(OpCodes.Call, reference);
- Instruction ret = Instruction.Create(OpCodes.Ret);
- ilp.Append(call);
- ilp.Append(ret);
- }
- static TypeDefinition createType(ModuleDefinition mod)
- {
- // Gets the current assembly as an assemblydefinition
- AssemblyDefinition currentAsm = AssemblyDefinition.ReadAssembly(typeof(Injection).Assembly.Location);
- // Gets the target type that we want to inject into the other assembly
- TypeDefinition targetType = Extensions.FirstOrDefault<TypeDefinition>(currentAsm.MainModule.Types, t => t.FullName == typeof(Injection).FullName);
- // Sets the namespace of it to "" meaning that it'll be placed in the root namespace
- targetType.Namespace = "";
- // Create a new importer
- CecilImporter importer = new CecilImporter(mod);
- // Import the type using CecilImporter
- return importer.CreateImportedType(targetType);
- }
- static void Log(string text)
- {
- Console.WriteLine(text);
- }
- }
- }
- == INJECTION CLASS ==
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Tutorial4Code
- {
- public static class Injection
- {
- public static void Begin()
- {
- System.Windows.Forms.MessageBox.Show("PADDING", "Hello World", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement