Beispiel #1
0
        //Inspired by EOFAntiTamper
        private static void ModifyModule(ModuleDef module, bool callOnly)
        {
            var loaderType = callOnly ? typeof(CallLoader) : typeof(Loader);
            //Declare module to inject
            var injectModule = ModuleDefMD.Load(loaderType.Module);
            var global       = module.GlobalType.FindOrCreateStaticConstructor();
            //Declare CallLoader as a TypeDef using it's Metadata token
            var injectType = injectModule.ResolveTypeDef(MDToken.ToRID(loaderType.MetadataToken));

            //Use ConfuserEx InjectHelper class to inject Loader class into our target, under <Module>
            var members = InjectHelper.Inject(injectType, module.GlobalType, module);

            if (callOnly)
            {
                Console.WriteLine("Injecting Origami loader into {0}", module.GlobalType.Name);
                //Find the Initialize() Method in Loader
                var init = (MethodDef)members.Single(method => method.Name == "Initialize");
                //Add Instruction to call the init method
                global.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, init));
            }
            else
            {
                Console.WriteLine("Creating Origami entry point for stub {0}", module.GlobalType.Name);
                //Find the Initialize() Method in Loader
                var init = (MethodDef)members.Single(method => method.Name == "Initialize");
                //Add Instruction to call the init method
                global.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, init));

                var entryPoint = members.OfType <MethodDef>().Single(method => method.Name == "Main");
                //Set EntryPoint to Main method defined in the Loader class
                module.EntryPoint = entryPoint;

                //Add STAThreadAttribute
                var attrType = module.CorLibTypes.GetTypeRef("System", "STAThreadAttribute");
                var ctorSig  = MethodSig.CreateInstance(module.CorLibTypes.Void);
                entryPoint.CustomAttributes.Add(new CustomAttribute(
                                                    new MemberRefUser(module, ".ctor", ctorSig, attrType)));
            }

            //Remove.ctor method because otherwise it will
            //lead to Global constructor error( e.g[MD]: Error: Global item( field, method ) must be Static. [token: 0x06000002] / [MD]: Error: Global constructor. [token: 0x06000002] )
            foreach (var md in module.GlobalType.Methods)
            {
                if (md.Name == ".ctor")
                {
                    module.GlobalType.Remove(md);
                    break;
                }
            }
        }
Beispiel #2
0
        //Inspired by EOFAntiTamper
        private static void ModifyModule(ModuleDef module, bool callOnly)
        {
            var loaderType = typeof(Loader);
            //Declare module to inject
            var injectModule = ModuleDefMD.Load(loaderType.Module);
            //Get global constructor or create one if it does not already exist
            var global = module.GlobalType.FindOrCreateStaticConstructor();
            //Declare CallLoader as a TypeDef using it's Metadata token
            var injectType = injectModule.ResolveTypeDef(MDToken.ToRID(loaderType.MetadataToken));

            //Use ConfuserEx InjectHelper class to inject Loader class into our target, under <Module>
            var members = InjectHelper.Inject(injectType, module.GlobalType, module);


            Console.WriteLine("Creating EntryPoint for stub {0}", module.GlobalType.Name);
            //Resolve method for the EntryPoint
            var entryPoint = members.OfType <MethodDef>().Single(method => method.Name == "Main");

            //Set EntryPoint to Main method defined in the Loader class
            module.EntryPoint = entryPoint;

            //Add STAThreadAttribute
            var attrType = module.CorLibTypes.GetTypeRef("System", "STAThreadAttribute");
            var ctorSig  = MethodSig.CreateInstance(module.CorLibTypes.Void);

            entryPoint.CustomAttributes.Add(new CustomAttribute(
                                                new MemberRefUser(module, ".ctor", ctorSig, attrType)));

            //Remove.ctor method because otherwise it will
            //lead to Global constructor error( e.g[MD]: Error: Global item( field, method ) must be Static. [token: 0x06000002] / [MD]: Error: Global constructor. [token: 0x06000002] )
            foreach (var md in module.GlobalType.Methods)
            {
                if (md.Name != ".ctor")
                {
                    continue;
                }
                module.GlobalType.Remove(md);
                break;
            }
        }