public override void Finalize(RPContext ctx)
        {
            foreach (KeyValuePair <Tuple <Code, IMethod, IRPEncoding>, Tuple <FieldDef, MethodDef> > pair in this.fields)
            {
                byte           num;
                InitMethodDesc initMethod = this.GetInitMethod(ctx, pair.Key.Item3);
                do
                {
                    num = ctx.Random.NextByte();
                }while (num == ((byte)pair.Key.Item1));
                MethodDef def2 = pair.Value.Item1.DeclaringType.FindOrCreateStaticConstructor();
                def2.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, (IMethod)initMethod.Method));
                def2.Body.Instructions.Insert(0, Instruction.CreateLdcI4(num));
                def2.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Ldtoken, pair.Value.Item1));
                FieldDesc item = new FieldDesc {
                    Field    = pair.Value.Item1,
                    OpCode   = pair.Key.Item1,
                    Method   = pair.Key.Item2,
                    OpKey    = num,
                    InitDesc = initMethod
                };
                this.fieldDescs.Add(item);
            }
            foreach (TypeDef def3 in ctx.Delegates.Values)
            {
                MethodDef member = def3.FindOrCreateStaticConstructor();
                ctx.Marker.Mark(member, ctx.Protection);
                ctx.Name.SetCanRename(member, false);
            }
            MetaDataOptions metaDataOptions = ctx.Context.CurrentModuleWriterOptions.MetaDataOptions;

            metaDataOptions.Flags |= MetaDataFlags.PreserveExtraSignatureData;
            ctx.Context.CurrentModuleWriterListener.OnWriterEvent += new EventHandler <ModuleWriterListenerEventArgs>(this.EncodeField);
            this.encodeCtx = ctx;
        }
        InitMethodDesc GetInitMethod(RPContext ctx, IRPEncoding encoding)
        {
            InitMethodDesc[] initDescs;
            if (!inits.TryGetValue(encoding, out initDescs))
            {
                inits[encoding] = initDescs = new InitMethodDesc[ctx.InitCount];
            }

            int index = ctx.Random.NextInt32(initDescs.Length);

            if (initDescs[index] == null)
            {
                TypeDef   rtType         = ctx.Context.Registry.GetService <IRuntimeService>().GetRuntimeType("Confuser.Runtime.RefProxyStrong");
                MethodDef injectedMethod = InjectHelper.Inject(rtType.FindMethod("Initialize"), ctx.Module);
                ctx.Module.GlobalType.Methods.Add(injectedMethod);

                injectedMethod.Access = MethodAttributes.PrivateScope;
                injectedMethod.Name   = ctx.Name.RandomName();
                ctx.Name.SetCanRename(injectedMethod, false);
                ctx.Marker.Mark(injectedMethod, ctx.Protection);

                var desc = new InitMethodDesc {
                    Method = injectedMethod
                };

                // Field name has five bytes, each bytes has different order & meaning
                int[] order = Enumerable.Range(0, 5).ToArray();
                ctx.Random.Shuffle(order);
                desc.OpCodeIndex = order[4];

                desc.TokenNameOrder = new int[4];
                Array.Copy(order, 0, desc.TokenNameOrder, 0, 4);
                desc.TokenByteOrder = Enumerable.Range(0, 4).Select(x => x * 8).ToArray();
                ctx.Random.Shuffle(desc.TokenByteOrder);

                var keyInjection = new int[9];
                Array.Copy(desc.TokenNameOrder, 0, keyInjection, 0, 4);
                Array.Copy(desc.TokenByteOrder, 0, keyInjection, 4, 4);
                keyInjection[8] = desc.OpCodeIndex;
                MutationHelper.InjectKeys(injectedMethod, Enumerable.Range(0, 9).ToArray(), keyInjection);

                // Encoding
                MutationHelper.ReplacePlaceholder(injectedMethod, arg => { return(encoding.EmitDecode(injectedMethod, ctx, arg)); });
                desc.Encoding = encoding;

                initDescs[index] = desc;
            }
            return(initDescs[index]);
        }
        public override void Finalize(RPContext ctx)
        {
            foreach (var field in fields)
            {
                InitMethodDesc init = GetInitMethod(ctx, field.Key.Item3);
                byte           opKey;
                do
                {
                    // No zero bytes
                    opKey = ctx.Random.NextByte();
                } while (opKey == (byte)field.Key.Item1);

                TypeDef delegateType = field.Value.Item1.DeclaringType;

                MethodDef cctor = delegateType.FindOrCreateStaticConstructor();
                cctor.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, init.Method));
                cctor.Body.Instructions.Insert(0, Instruction.CreateLdcI4(opKey));
                cctor.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Ldtoken, field.Value.Item1));

                fieldDescs.Add(new FieldDesc
                {
                    Field    = field.Value.Item1,
                    OpCode   = field.Key.Item1,
                    Method   = field.Key.Item2,
                    OpKey    = opKey,
                    InitDesc = init
                });
            }

            foreach (TypeDef delegateType in ctx.Delegates.Values)
            {
                MethodDef cctor = delegateType.FindOrCreateStaticConstructor();
                ctx.Marker.Mark(cctor, ctx.Protection);
                ctx.Name.SetCanRename(cctor, false);
            }

            ctx.Context.CurrentModuleWriterOptions.MetaDataOptions.Flags |= MetaDataFlags.PreserveExtraSignatureData;
            ctx.Context.CurrentModuleWriterListener.OnWriterEvent        += EncodeField;
            encodeCtx = ctx;
        }
        private InitMethodDesc GetInitMethod(RPContext ctx, IRPEncoding encoding)
        {
            InitMethodDesc[] descArray;
            if (!this.inits.TryGetValue(encoding, out descArray))
            {
                this.inits[encoding] = descArray = new InitMethodDesc[ctx.InitCount];
            }
            int index = ctx.Random.NextInt32(descArray.Length);

            if (descArray[index] == null)
            {
                TypeDef   runtimeType    = ctx.Context.Registry.GetService <IRuntimeService>().GetRuntimeType("Confuser.Runtime.RefProxyStrong");
                MethodDef injectedMethod = InjectHelper.Inject(runtimeType.FindMethod("Initialize"), ctx.Module);
                ctx.Module.GlobalType.Methods.Add(injectedMethod);
                injectedMethod.Access = MethodAttributes.CompilerControlled;
                injectedMethod.Name   = ctx.Name.RandomName();
                ctx.Name.SetCanRename(injectedMethod, false);
                ctx.Marker.Mark(injectedMethod, ctx.Protection);
                InitMethodDesc desc = new InitMethodDesc {
                    Method = injectedMethod
                };
                int[] list = Enumerable.Range(0, 5).ToArray <int>();
                ctx.Random.Shuffle <int>(list);
                desc.OpCodeIndex    = list[4];
                desc.TokenNameOrder = new int[4];
                Array.Copy(list, 0, desc.TokenNameOrder, 0, 4);
                desc.TokenByteOrder = (from x in Enumerable.Range(0, 4) select x * 8).ToArray <int>();
                ctx.Random.Shuffle <int>(desc.TokenByteOrder);
                int[] destinationArray = new int[9];
                Array.Copy(desc.TokenNameOrder, 0, destinationArray, 0, 4);
                Array.Copy(desc.TokenByteOrder, 0, destinationArray, 4, 4);
                destinationArray[8] = desc.OpCodeIndex;
                MutationHelper.InjectKeys(injectedMethod, Enumerable.Range(0, 9).ToArray <int>(), destinationArray);
                MutationHelper.ReplacePlaceholder(injectedMethod, arg => encoding.EmitDecode(injectedMethod, ctx, arg));
                desc.Encoding    = encoding;
                descArray[index] = desc;
            }
            return(descArray[index]);
        }
Exemple #5
0
        private InitMethodDesc GetInitMethod(RPContext ctx, IRPEncoding encoding)
        {
            InitMethodDesc[] initDescs;
            if (!inits.TryGetValue(encoding, out initDescs))
                inits[encoding] = initDescs = new InitMethodDesc[ctx.InitCount];

            int index = ctx.Random.NextInt32(initDescs.Length);
            if (initDescs[index] == null) {
                TypeDef rtType = ctx.Context.Registry.GetService<IRuntimeService>().GetRuntimeType("Confuser.Runtime.RefProxyStrong");
                MethodDef injectedMethod = InjectHelper.Inject(rtType.FindMethod("Initialize"), ctx.Module);
                ctx.Module.GlobalType.Methods.Add(injectedMethod);

                injectedMethod.Access = MethodAttributes.PrivateScope;
                injectedMethod.Name = ctx.Name.RandomName();
                ctx.Name.SetCanRename(injectedMethod, false);
                ctx.Marker.Mark(injectedMethod);

                var desc = new InitMethodDesc { Method = injectedMethod };

                // Field name has five bytes, each bytes has different order & meaning
                int[] order = Enumerable.Range(0, 5).ToArray();
                ctx.Random.Shuffle(order);
                desc.OpCodeIndex = order[4];

                desc.TokenNameOrder = new int[4];
                Array.Copy(order, 0, desc.TokenNameOrder, 0, 4);
                desc.TokenByteOrder = Enumerable.Range(0, 4).Select(x => x * 8).ToArray();
                ctx.Random.Shuffle(desc.TokenByteOrder);

                var keyInjection = new int[9];
                Array.Copy(desc.TokenNameOrder, 0, keyInjection, 0, 4);
                Array.Copy(desc.TokenByteOrder, 0, keyInjection, 4, 4);
                keyInjection[8] = desc.OpCodeIndex;
                MutationHelper.InjectKeys(injectedMethod, Enumerable.Range(0, 9).ToArray(), keyInjection);

                // Encoding
                MutationHelper.ReplacePlaceholder(injectedMethod, arg => { return encoding.EmitDecode(injectedMethod, ctx, arg); });
                desc.Encoding = encoding;

                initDescs[index] = desc;
            }
            return initDescs[index];
        }