Пример #1
0
 protected override void Execute(ConfuserContext context, ProtectionParameters parameters)
 {
     foreach (ModuleDef moduleDef in parameters.Targets.OfType <ModuleDef>())
     {
         ModuleDefMD moduleDefMD = (ModuleDefMD)moduleDef;
         if (moduleDefMD.FullName.Contains(".exe"))
         {
             int num = context.Modules.Count - 1;
             for (int i = 0; i <= num; i++)
             {
                 for (int j = 0; j <= 50; j++)
                 {
                     Random rnd = new Random();
                     new TypeDefUser(NameService.RandomNameStatic(), moduleDefMD.CorLibTypes.Object.TypeDefOrRef).Attributes = TypeAttributes.Public;
                     TypeDef item = new TypeDefUser(NameService.RandomNameStatic(), NameService.RandomNameStatic(), moduleDefMD.CorLibTypes.Object.TypeDefOrRef)
                     {
                         Attributes = TypeAttributes.Public
                     };
                     TypeDef item2 = new TypeDefUser(NameService.RandomNameStatic(), NameService.RandomNameStatic(), moduleDefMD.CorLibTypes.Object.TypeDefOrRef)
                     {
                         Attributes = TypeAttributes.Public
                     };
                     moduleDefMD.Types.Add(item);
                     moduleDefMD.Types.Add(item2);
                 }
                 Random rnd1 = new Random();
             }
         }
         else
         {
             context.Logger.Log("WARN: Junk cannot be used on a dll");
         }
     }
 }
Пример #2
0
        private string NewName(string name)
        {
            string newName;

            if (!nameMap.TryGetValue(name, out newName))
            {
                nameMap[name] = newName = NameService.RandomNameStatic();
            }
            return(newName);
        } //you need to change this method, i use NameService so i don't have to do the renamer method again
Пример #3
0
        public override void ProcessCall(RPContext ctx, int instrIndex)
        {
            Instruction instruction = ctx.Body.Instructions[instrIndex];
            IMethod     operand     = (IMethod)instruction.Operand;

            if (!operand.DeclaringType.ResolveTypeDefThrow().IsValueType&& (operand.ResolveThrow().IsPublic || operand.ResolveThrow().IsAssembly))
            {
                MethodDef def;
                Tuple <Code, TypeDef, IMethod> key = Tuple.Create <Code, TypeDef, IMethod>(instruction.OpCode.Code, ctx.Method.DeclaringType, operand);
                if (!this.proxies.TryGetValue(key, out def))
                {
                    MethodSig methodSig = RPMode.CreateProxySignature(ctx, operand, instruction.OpCode.Code == Code.Newobj);
                    def = new MethodDefUser(NameService.RandomNameStatic(), methodSig)
                    {
                        Attributes     = MethodAttributes.CompilerControlled | MethodAttributes.Static,
                        ImplAttributes = MethodImplAttributes.IL
                    };
                    ctx.Method.DeclaringType.Methods.Add(def);
                    if ((instruction.OpCode.Code == Code.Call) && operand.ResolveThrow().IsVirtual)
                    {
                        def.IsStatic      = false;
                        methodSig.HasThis = true;
                        methodSig.Params.RemoveAt(0);
                    }
                    ctx.Marker.Mark(def, ctx.Protection);

                    /*ctx.Name.Analyze(def);
                     * ctx.Name.SetCanRename(def, false);*/
                    def.Body = new CilBody();
                    for (int i = 0; i < def.Parameters.Count; i++)
                    {
                        def.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg, def.Parameters[i]));
                    }
                    def.Body.Instructions.Add(Instruction.Create(instruction.OpCode, operand));
                    def.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
                    this.proxies[key] = def;
                }
                instruction.OpCode = OpCodes.Call;
                if (ctx.Method.DeclaringType.HasGenericParameters)
                {
                    GenericVar[] genArgs = new GenericVar[ctx.Method.DeclaringType.GenericParameters.Count];
                    for (int j = 0; j < genArgs.Length; j++)
                    {
                        genArgs[j] = new GenericVar(j);
                    }
                    instruction.Operand = new MemberRefUser(ctx.Module, def.Name, def.MethodSig, new GenericInstSig((ClassOrValueTypeSig)ctx.Method.DeclaringType.ToTypeSig(), genArgs).ToTypeDefOrRef());
                }
                else
                {
                    instruction.Operand = def;
                }
            }
        }
Пример #4
0
            protected override void Execute(ConfuserContext context, ProtectionParameters parameters)
            {
                foreach (var method in parameters.Targets.OfType <MethodDef>())
                {
                    if (!method.HasBody)
                    {
                        continue;
                    }

                    method.Body.SimplifyMacros(method.Parameters);
                    var instructions = method.Body.Instructions;
                    for (int i = 0; i < instructions.Count; i++)
                    {
                        if (instructions[i].Operand is Local local)
                        {
                            FieldDef def = null;
                            if (!convertedLocals.ContainsKey(local))
                            {
                                def = new FieldDefUser(NameService.RandomNameStatic(), new FieldSig(local.Type), FieldAttributes.Public | FieldAttributes.Static);
                                context.CurrentModule.GlobalType.Fields.Add(def);
                                convertedLocals.Add(local, def);
                            }
                            else
                            {
                                def = convertedLocals[local];
                            }

                            OpCode eq = null;
                            switch (instructions[i].OpCode.Code)
                            {
                            case Code.Ldloc:
                                eq = OpCodes.Ldsfld;
                                break;

                            case Code.Ldloca:
                                eq = OpCodes.Ldsflda;
                                break;

                            case Code.Stloc:
                                eq = OpCodes.Stsfld;
                                break;
                            }
                            instructions[i].OpCode  = eq;
                            instructions[i].Operand = def;
                        }
                    }
                    convertedLocals.ToList().ForEach(x => method.Body.Variables.Remove(x.Key));
                    convertedLocals = new Dictionary <Local, FieldDef>();
                }
            }
Пример #5
0
 private void HideInts(MethodDef methodDef)
 {
     if (canObfuscate(methodDef))
     {
         foreach (Instruction instruction in methodDef.Body.Instructions)
         {
             if (instruction.OpCode != OpCodes.Ldc_I4)
             {
                 continue;
             }
             MethodDef meth = new MethodDefUser(NameService.RandomNameStatic(), MethodSig.CreateStatic(methodDef.DeclaringType.Module.CorLibTypes.Int32), MethodImplAttributes.IL | MethodImplAttributes.Managed, MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig)
             {
                 Body = new CilBody()
             };
             meth.Body.Instructions.Add(new Instruction(OpCodes.Ldc_I4, instruction.GetLdcI4Value()));
             meth.Body.Instructions.Add(new Instruction(OpCodes.Ret));
             methodDef.DeclaringType.Methods.Add(meth);
             instruction.OpCode  = OpCodes.Call;
             instruction.Operand = meth;
         }
     }
 }
Пример #6
0
        public void InlineInteger(MethodDef method, int i)
        {
            if (method.DeclaringType.IsGlobalModuleType)
            {
                return;
            }
            if (!method.HasBody)
            {
                return;
            }
            var instr = method.Body.Instructions;

            if ((i - 1) > 0)
            {
                try
                {
                    if (instr[i - 1].OpCode == OpCodes.Callvirt)
                    {
                        if (instr[i + 1].OpCode == OpCodes.Call)
                        {
                            return;
                        }
                    }
                }
                catch { }
            }

            //if (instr[i + 4].IsBr())
            //{
            //    return;
            //}
            bool is_valid_inline = true;

            switch (random.Next(0, 2))
            {
            //true
            case 0:
                is_valid_inline = true;
                break;

            //false
            case 1:
                is_valid_inline = false;
                break;
            }

            Local new_local = new Local(method.Module.CorLibTypes.String);

            method.Body.Variables.Add(new_local);
            Local new_local2 = new Local(method.Module.CorLibTypes.Int32);

            method.Body.Variables.Add(new_local2);
            var value       = instr[i].GetLdcI4Value();
            var first_ldstr = NameService.RandomNameStatic();

            instr.Insert(i, Instruction.Create(OpCodes.Ldloc_S, new_local2));

            instr.Insert(i, Instruction.Create(OpCodes.Stloc_S, new_local2));
            if (is_valid_inline)
            {
                instr.Insert(i, Instruction.Create(OpCodes.Ldc_I4, value));
                instr.Insert(i, Instruction.Create(OpCodes.Ldc_I4, value + 1));
            }
            else
            {
                instr.Insert(i, Instruction.Create(OpCodes.Ldc_I4, value + 1));
                instr.Insert(i, Instruction.Create(OpCodes.Ldc_I4, value));
            }
            instr.Insert(i,
                         Instruction.Create(OpCodes.Call,
                                            method.Module.Import(typeof(System.String).GetMethod("op_Equality",
                                                                                                 new Type[] { typeof(string), typeof(string) }))));
            instr.Insert(i, Instruction.Create(OpCodes.Ldstr, first_ldstr));
            instr.Insert(i, Instruction.Create(OpCodes.Ldloc_S, new_local));
            instr.Insert(i, Instruction.Create(OpCodes.Stloc_S, new_local));
            if (is_valid_inline)
            {
                instr.Insert(i, Instruction.Create(OpCodes.Ldstr, first_ldstr));
            }
            else
            {
                instr.Insert(i,
                             Instruction.Create(OpCodes.Ldstr,
                                                NameService.RandomNameStatic()));
            }
            instr.Insert(i + 5, Instruction.Create(OpCodes.Brtrue_S, instr[i + 6]));
            instr.Insert(i + 7, Instruction.Create(OpCodes.Br_S, instr[i + 8]));

            instr.RemoveAt(i + 10);

            //if (ProxyMethodConst.Contains(method))
            //{
            //    var last = method.Body.Instructions.Count;
            //    if (!instr[last - 2].IsLdloc()) return;
            //    instr[last - 2].OpCode = OpCodes.Ldloc_S;
            //    instr[last - 2].Operand = new_local2;
            //}
        }