Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using dnlib.DotNet;
- using dnlib.DotNet.Emit;
- class Program
- {
- // returns FieldDef of injected byte[]
- static FieldDef InjectArray(ModuleDefMD mod, byte[] injectedData, string injectedName)
- {
- // we'll have to import lots of new stuff into our module
- Importer importer = new Importer(mod);
- // add class with layout
- ITypeDefOrRef valueTypeRef = importer.Import(typeof(System.ValueType));
- TypeDef classWithLayout = new TypeDefUser("dummyClass", valueTypeRef);
- classWithLayout.Attributes |= TypeAttributes.Sealed | TypeAttributes.ExplicitLayout;
- classWithLayout.ClassLayout = new ClassLayoutUser(1, (uint)injectedData.Length);
- mod.Types.Add(classWithLayout);
- // add field with proper InitialValue
- FieldDef fieldWithRVA = new FieldDefUser("dummyField", new FieldSig(classWithLayout.ToTypeSig()), FieldAttributes.Static | FieldAttributes.Assembly | FieldAttributes.HasFieldRVA);
- fieldWithRVA.InitialValue = injectedData;
- mod.GlobalType.Fields.Add(fieldWithRVA);
- // add byte[] field
- ITypeDefOrRef byteArrayRef = importer.Import(typeof(System.Byte[]));
- FieldDef fieldInjectedArray = new FieldDefUser(injectedName, new FieldSig(byteArrayRef.ToTypeSig()), FieldAttributes.Static | FieldAttributes.Public);
- mod.GlobalType.Fields.Add(fieldInjectedArray);
- // and finally add code to global .cctor to initialize array.
- /*
- ldc.i4 XXXsizeofarrayXXX
- newarr [mscorlib]System.Byte
- dup
- ldtoken field valuetype className fieldName
- call void [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
- stsfld uint8[] bla
- */
- ITypeDefOrRef systemByte = importer.Import(typeof(System.Byte));
- ITypeDefOrRef runtimeHelpers = importer.Import(typeof(System.Runtime.CompilerServices.RuntimeHelpers));
- IMethod initArray = importer.Import(typeof(System.Runtime.CompilerServices.RuntimeHelpers).GetMethod("InitializeArray", new Type[] { typeof(System.Array), typeof(System.RuntimeFieldHandle) }));
- MethodDef cctor = mod.GlobalType.FindOrCreateStaticConstructor();
- IList<Instruction> instrs = cctor.Body.Instructions;
- instrs.Insert(0, new Instruction(OpCodes.Ldc_I4, injectedData.Length));
- instrs.Insert(1, new Instruction(OpCodes.Newarr, systemByte));
- instrs.Insert(2, new Instruction(OpCodes.Dup));
- instrs.Insert(3, new Instruction(OpCodes.Ldtoken, fieldWithRVA));
- instrs.Insert(4, new Instruction(OpCodes.Call, initArray));
- instrs.Insert(5, new Instruction(OpCodes.Stsfld, fieldInjectedArray));
- return fieldInjectedArray;
- }
- static void Main(string[] args)
- {
- ModuleDefMD mod = ModuleDefMD.Load(args[0]);
- FieldDef x = InjectArray(mod, new byte[] { 1, 2, 3, 4, 5, 6 }, "hello world!");
- mod.Write(Path.ChangeExtension(args[0], "patched" + Path.GetExtension(args[0])));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement