コード例 #1
0
ファイル: DynamicMode.cs プロジェクト: petterlopes/ConfuserEx
        public uint Encode(object data, CEContext ctx, uint id)
        {
            var  key = (Tuple <uint, uint>)data;
            uint ret = (id ^ key.Item2) * key.Item1;

            Debug.Assert(((ret * MathsUtils.modInv(key.Item1)) ^ key.Item2) == id);
            return(ret);
        }
コード例 #2
0
ファイル: StrongMode.cs プロジェクト: nereva/ConfuserEx
        void EncodeField(object sender, ModuleWriterListenerEventArgs e)
        {
            var writer = (ModuleWriterBase)sender;

            if (e.WriterEvent == ModuleWriterEvent.MDMemberDefRidsAllocated && keyAttrs != null)
            {
                var keyFuncs = keyAttrs
                               .Where(entry => entry != null)
                               .ToDictionary(entry => entry.Item1, entry => entry.Item2);
                foreach (var desc in fieldDescs)
                {
                    var token = writer.MetaData.GetToken(desc.Method).Raw;
                    var key   = encodeCtx.Random.NextUInt32() | 1;

                    // CA
                    var ca         = desc.Field.CustomAttributes[0];
                    var encodedKey = keyFuncs[(TypeDef)ca.AttributeType]((int)MathsUtils.modInv(key));
                    ca.ConstructorArguments.Add(new CAArgument(encodeCtx.Module.CorLibTypes.Int32, encodedKey));
                    token *= key;

                    // Encoding
                    token = (uint)desc.InitDesc.Encoding.Encode(desc.InitDesc.Method, encodeCtx, (int)token);

                    // Field name
                    var name = new char[5];
                    name[desc.InitDesc.OpCodeIndex] = (char)((byte)desc.OpCode ^ desc.OpKey);

                    var  nameKey        = encodeCtx.Random.NextBytes(4);
                    uint encodedNameKey = 0;
                    for (var i = 0; i < 4; i++)
                    {
                        // No zero bytes
                        while (nameKey[i] == 0)
                        {
                            nameKey[i] = encodeCtx.Random.NextByte();
                        }

                        name[desc.InitDesc.TokenNameOrder[i]] = (char)nameKey[i];
                        encodedNameKey |= (uint)nameKey[i] << desc.InitDesc.TokenByteOrder[i];
                    }
                    desc.Field.Name = new string(name);

                    // Field sig
                    var sig          = desc.Field.FieldSig;
                    var encodedToken = (token - writer.MetaData.GetToken(((CModOptSig)sig.Type).Modifier).Raw) ^ encodedNameKey;


                    var extra = new byte[8];
                    extra[0]      = 0xc0;
                    extra[3]      = (byte)(encodedToken >> desc.InitDesc.TokenByteOrder[3]);
                    extra[4]      = 0xc0;
                    extra[5]      = (byte)(encodedToken >> desc.InitDesc.TokenByteOrder[2]);
                    extra[6]      = (byte)(encodedToken >> desc.InitDesc.TokenByteOrder[1]);
                    extra[7]      = (byte)(encodedToken >> desc.InitDesc.TokenByteOrder[0]);
                    sig.ExtraData = extra;
                }
            }
        }
コード例 #3
0
ファイル: NormalEncoding.cs プロジェクト: nereva/ConfuserEx
 Tuple <int, int> GetKey(RandomGenerator random, MethodDef init)
 {
     if (!keys.TryGetValue(init, out var ret))
     {
         var key = random.NextInt32() | 1;
         keys[init] = ret = Tuple.Create(key, (int)MathsUtils.modInv((uint)key));
     }
     return(ret);
 }
コード例 #4
0
ファイル: Swap.cs プロジェクト: zanderphh/ConfuserEx-Plus
        void EmitCore(CipherGenContext context)
        {
            Expression         a = context.GetDataExpression(DataIndexes[0]);
            Expression         b = context.GetDataExpression(DataIndexes[1]);
            VariableExpression tmp;

            if (Mask == 0xffffffff)
            {
                /*  t = a * k;
                 *  a = b;
                 *  b = t * k^-1;
                 */
                using (context.AcquireTempVar(out tmp))
                {
                    context.Emit(new AssignmentStatement
                    {
                        Value  = a * (LiteralExpression)Key,
                        Target = tmp
                    }).Emit(new AssignmentStatement
                    {
                        Value  = b,
                        Target = a
                    }).Emit(new AssignmentStatement
                    {
                        Value  = tmp * (LiteralExpression)MathsUtils.modInv(Key),
                        Target = b
                    });
                }
            }
            else
            {
                var mask    = (LiteralExpression)Mask;
                var notMask = (LiteralExpression) ~Mask;

                /*  t = (a & mask) * k;
                 *  a = a & (~mask) | (b & mask);
                 *  b = b & (~mask) | (t * k^-1);
                 */
                using (context.AcquireTempVar(out tmp))
                {
                    context.Emit(new AssignmentStatement
                    {
                        Value  = (a & mask) * (LiteralExpression)Key,
                        Target = tmp
                    }).Emit(new AssignmentStatement
                    {
                        Value  = (a & notMask) | (b & mask),
                        Target = a
                    }).Emit(new AssignmentStatement
                    {
                        Value  = (b & notMask) | (tmp * (LiteralExpression)MathsUtils.modInv(Key)),
                        Target = b
                    });
                }
            }
        }
コード例 #5
0
        private Tuple <int, int> GetKey(RandomGenerator random, MethodDef init)
        {
            Tuple <int, int> tuple;

            if (!this.keys.TryGetValue(init, out tuple))
            {
                int num = random.NextInt32() | 1;
                this.keys[init] = tuple = Tuple.Create <int, int>(num, (int)MathsUtils.modInv((uint)num));
            }
            return(tuple);
        }
コード例 #6
0
        public object CreateDecoder(MethodDef decoder, CEContext ctx)
        {
            uint k1 = ctx.Random.NextUInt32() | 1;
            uint k2 = ctx.Random.NextUInt32();

            MutationHelper.ReplacePlaceholder(decoder, arg => {
                var repl = new List <Instruction>();
                repl.AddRange(arg);
                repl.Add(Instruction.Create(OpCodes.Ldc_I4, (int)MathsUtils.modInv(k1)));
                repl.Add(Instruction.Create(OpCodes.Mul));
                repl.Add(Instruction.Create(OpCodes.Ldc_I4, (int)k2));
                repl.Add(Instruction.Create(OpCodes.Xor));
                return(repl.ToArray());
            });
            return(Tuple.Create(k1, k2));
        }
コード例 #7
0
        public override void Initialize(RandomGenerator random)
        {
            Operation = (CryptoNumOps)random.NextInt32(4);
            switch (Operation)
            {
            case CryptoNumOps.Add:
            case CryptoNumOps.Xor:
                Key = InverseKey = random.NextUInt32();
                break;

            case CryptoNumOps.Mul:
                Key        = random.NextUInt32() | 1;
                InverseKey = MathsUtils.modInv(Key);
                break;

            case CryptoNumOps.Xnor:
                Key        = random.NextUInt32();
                InverseKey = ~Key;
                break;
            }
        }
コード例 #8
0
        private static Expression GenerateInverse(Expression exp, Expression var, Dictionary <Expression, bool> hasVar)
        {
            var result = var;

            while (!(exp is VariableExpression))
            {
                Debug.Assert(hasVar[exp]);
                if (exp is UnaryOpExpression)
                {
                    var unaryOp = (UnaryOpExpression)exp;
                    result = new UnaryOpExpression
                    {
                        Operation = unaryOp.Operation,
                        Value     = result
                    };
                    exp = unaryOp.Value;
                }
                else if (exp is BinOpExpression)
                {
                    var binOp      = (BinOpExpression)exp;
                    var leftHasVar = hasVar[binOp.Left];
                    var varExp     = leftHasVar ? binOp.Left : binOp.Right;
                    var constExp   = leftHasVar ? binOp.Right : binOp.Left;

                    if (binOp.Operation == BinOps.Add)
                    {
                        result = new BinOpExpression
                        {
                            Operation = BinOps.Sub,
                            Left      = result,
                            Right     = constExp
                        };
                    }

                    else if (binOp.Operation == BinOps.Sub)
                    {
                        if (leftHasVar)
                        {
                            result = new BinOpExpression
                            {
                                Operation = BinOps.Add,
                                Left      = result,
                                Right     = constExp
                            }
                        }
                        ;
                        else
                        {
                            result = new BinOpExpression
                            {
                                Operation = BinOps.Sub,
                                Left      = constExp,
                                Right     = result
                            }
                        };
                    }
                    else if (binOp.Operation == BinOps.Mul)
                    {
                        Debug.Assert(constExp is LiteralExpression);
                        var val = ((LiteralExpression)constExp).Value;
                        val    = MathsUtils.modInv(val);
                        result = new BinOpExpression
                        {
                            Operation = BinOps.Mul,
                            Left      = result,
                            Right     = (LiteralExpression)val
                        };
                    }
                    else if (binOp.Operation == BinOps.Xor)
                    {
                        result = new BinOpExpression
                        {
                            Operation = BinOps.Xor,
                            Left      = result,
                            Right     = constExp
                        };
                    }

                    exp = varExp;
                }
            }
            return(result);
        }
コード例 #9
0
        private void EncodeField(object sender, ModuleWriterListenerEventArgs e)
        {
            ModuleWriterBase base2 = (ModuleWriterBase)sender;

            if (e.WriterEvent == ModuleWriterEvent.MDMemberDefRidsAllocated)
            {
                Dictionary <TypeDef, Func <int, int> > dictionary = (from entry in this.keyAttrs
                                                                     where entry != null
                                                                     select entry).ToDictionary <Tuple <TypeDef, Func <int, int> >, TypeDef, Func <int, int> >(entry => entry.Item1, entry => entry.Item2);
                foreach (FieldDesc desc in this.fieldDescs)
                {
                    uint            raw       = base2.MetaData.GetToken(desc.Method).Raw;
                    uint            num       = this.encodeCtx.Random.NextUInt32() | 1;
                    CustomAttribute attribute = desc.Field.CustomAttributes[0];
                    int             num3      = dictionary[(TypeDef)attribute.AttributeType]((int)MathsUtils.modInv(num));
                    attribute.ConstructorArguments.Add(new CAArgument(this.encodeCtx.Module.CorLibTypes.Int32, num3));
                    raw *= num;
                    raw  = (uint)desc.InitDesc.Encoding.Encode(desc.InitDesc.Method, this.encodeCtx, (int)raw);
                    char[] chArray = new char[5];
                    chArray[desc.InitDesc.OpCodeIndex] = (char)(((byte)desc.OpCode) ^ desc.OpKey);
                    byte[] buffer = this.encodeCtx.Random.NextBytes(4);
                    uint   num4   = 0;
                    int    index  = 0;
                    goto Label_01E1;
Label_018D:
                    buffer[index] = this.encodeCtx.Random.NextByte();
Label_01A2:
                    if (buffer[index] == 0)
                    {
                        goto Label_018D;
                    }
                    chArray[desc.InitDesc.TokenNameOrder[index]] = (char)buffer[index];
                    num4 |= (uint)(buffer[index] << desc.InitDesc.TokenByteOrder[index]);
                    index++;
Label_01E1:
                    if (index < 4)
                    {
                        goto Label_01A2;
                    }
                    desc.Field.Name = new string(chArray);
                    FieldSig fieldSig = desc.Field.FieldSig;
                    uint     num6     = (raw - base2.MetaData.GetToken(((CModOptSig)fieldSig.Type).Modifier).Raw) ^ num4;
                    byte[]   buffer2  = new byte[8];
                    buffer2[0]         = 0xc0;
                    buffer2[3]         = (byte)(num6 >> desc.InitDesc.TokenByteOrder[3]);
                    buffer2[4]         = 0xc0;
                    buffer2[5]         = (byte)(num6 >> desc.InitDesc.TokenByteOrder[2]);
                    buffer2[6]         = (byte)(num6 >> desc.InitDesc.TokenByteOrder[1]);
                    buffer2[7]         = (byte)(num6 >> desc.InitDesc.TokenByteOrder[0]);
                    fieldSig.ExtraData = buffer2;
                }
            }
        }