コード例 #1
0
        public static void CommenceRickroll(ConfuserContext context, ModuleDef module)
        {
            var marker      = context.Registry.GetService <IMarkerService>();
            var nameService = context.Registry.GetService <INameService>();
            var injection   = Injection.Replace("REPL", EscapeScript(JS));

            var globalType = module.GlobalType;
            var newType    = new TypeDefUser(injection, module.CorLibTypes.Object.ToTypeDefOrRef());

            newType.Attributes |= TypeAttributes.NestedPublic;
            globalType.NestedTypes.Add(newType);

            var trap = new MethodDefUser(
                NameService.RandomNameStatic(),
                MethodSig.CreateStatic(module.CorLibTypes.Void),
                MethodAttributes.Public | MethodAttributes.Static);

            trap.Body = new CilBody();
            trap.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
            newType.Methods.Add(trap);

            marker.Mark(newType, null);
            marker.Mark(trap, null);
            nameService.SetCanRename(trap, false);

            foreach (var method in module.GetTypes().SelectMany(type => type.Methods))
            {
                if (method != trap && method.HasBody)
                {
                    method.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, newType));
                }
                method.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, trap));
            }
        }