Exemple #1
0
        public void TestValidOpCodeMap()
        {
            var random = new Random();

            var mod  = new ModuleDefUser("test");
            var type = new TypeDefUser("Constants");

            for (var i = 0; i < 119; i++)
            {
                type.Fields.Add(new FieldDefUser("randomName" + random.Next(), new FieldSig(mod.CorLibTypes.Byte)));
            }

            mod.Types.Add(type);

            var ctor = type.FindOrCreateStaticConstructor();
            var body = new CilBody();

            for (var i = 1; i < 119; i++)
            {
                body.Instructions.Insert(0, Instruction.Create(OpCodes.Stfld, type.Fields[i]));
                body.Instructions.Insert(0, Instruction.Create(OpCodes.Ldc_I4, random.Next(0, 0xFF)));
                body.Instructions.Insert(0, Instruction.Create(OpCodes.Ldnull));
            }

            body.Instructions.Add(Instruction.Create(OpCodes.Ldnull));
            body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4, 112));
            body.Instructions.Add(Instruction.Create(OpCodes.Stfld, type.Fields[0]));
            body.Instructions.Add(Instruction.Create(OpCodes.Ret));
            ctor.Body = body;

            var ms = new MemoryStream();

            mod.Write(ms);

            var ctx = new RhydonContext {
                Module = ModuleDefMD.Load(ms), Logger = new DummyLogger()
            };

            OpCodeMap.Parse(ctx);
            Assert.IsNotNull(ctx.Constants);
            Assert.IsTrue(ctx.Constants.REG_R0 == 112);

            ms.Close();
        }
Exemple #2
0
        // Token: 0x060000B0 RID: 176 RVA: 0x0000E128 File Offset: 0x0000C328
        public static void Execute(ModuleDef md)
        {
            TypeDef     globalType  = md.GlobalType;
            TypeDefUser typeDefUser = new TypeDefUser(globalType.Name);

            globalType.Name     = "BlinkVM";
            globalType.BaseType = md.CorLibTypes.GetTypeRef("System", "Object");
            md.Types.Insert(0, typeDefUser);
            MethodDef methodDef  = globalType.FindOrCreateStaticConstructor();
            MethodDef methodDef2 = typeDefUser.FindOrCreateStaticConstructor();

            methodDef.Name = "BlinkVM";
            methodDef.IsRuntimeSpecialName = false;
            methodDef.IsSpecialName        = false;
            methodDef.Access = MethodAttributes.PrivateScope;
            methodDef2.Body  = new CilBody(true, new List <Instruction>
            {
                Instruction.Create(OpCodes.Call, methodDef),
                Instruction.Create(OpCodes.Ret)
            }, new List <ExceptionHandler>(), new List <Local>());
            for (int i = 0; i < globalType.Methods.Count; i++)
            {
                MethodDef methodDef3 = globalType.Methods[i];
                bool      isNative   = methodDef3.IsNative;
                if (isNative)
                {
                    MethodDefUser methodDefUser = new MethodDefUser(methodDef3.Name, methodDef3.MethodSig.Clone());
                    methodDefUser.Attributes = (MethodAttributes.Private | MethodAttributes.FamANDAssem | MethodAttributes.Static);
                    methodDefUser.Body       = new CilBody();
                    methodDefUser.Body.Instructions.Add(new Instruction(OpCodes.Jmp, methodDef3));
                    methodDefUser.Body.Instructions.Add(new Instruction(OpCodes.Ret));
                    globalType.Methods[i] = methodDefUser;
                    typeDefUser.Methods.Add(methodDef3);
                }
            }
        }
        protected override void Execute(ConfuserContext context, ProtectionParameters parameters)
        {
            var vr = context.Annotations.Get <Virtualizer>(context, Fish.VirtualizerKey);

            var marker      = context.Registry.GetService <IMarkerService>();
            var refProxy    = context.Registry.GetService <IReferenceProxyService>();
            var antiTamper  = context.Registry.GetService <IAntiTamperService>();
            var compression = context.Registry.GetService <ICompressionService>();

            var methods = new HashSet <MethodDef>(parameters.Targets.OfType <MethodDef>());
            var refRepl = new Dictionary <IMemberRef, IMemberRef>();

            var oldType = context.CurrentModule.GlobalType;
            var newType = new TypeDefUser(oldType.Name);

            oldType.Namespace = "What_a_great_VM";
            oldType.Name      = "VM";
            oldType.BaseType  = context.CurrentModule.CorLibTypes.GetTypeRef("System", "Object");
            context.CurrentModule.Types.Insert(0, newType);

            var old_cctor = oldType.FindOrCreateStaticConstructor();
            var cctor     = newType.FindOrCreateStaticConstructor();

            old_cctor.Name = "Load";
            old_cctor.IsRuntimeSpecialName = false;
            old_cctor.IsSpecialName        = false;
            old_cctor.Access = MethodAttributes.PrivateScope;
            cctor.Body       = new CilBody(true, new List <Instruction>
            {
                Instruction.Create(OpCodes.Call, old_cctor),
                Instruction.Create(OpCodes.Ret)
            }, new List <ExceptionHandler>(), new List <Local>());

            marker.Mark(cctor, Parent);
            antiTamper.ExcludeMethod(context, cctor);

            for (var i = 0; i < oldType.Methods.Count; i++)
            {
                var nativeMethod = oldType.Methods[i];
                if (nativeMethod.IsNative)
                {
                    var methodStub = new MethodDefUser(nativeMethod.Name, nativeMethod.MethodSig.Clone());
                    methodStub.Attributes = MethodAttributes.Assembly | MethodAttributes.Static;
                    methodStub.Body       = new CilBody();
                    methodStub.Body.Instructions.Add(new Instruction(OpCodes.Jmp, nativeMethod));
                    methodStub.Body.Instructions.Add(new Instruction(OpCodes.Ret));

                    oldType.Methods[i] = methodStub;
                    newType.Methods.Add(nativeMethod);
                    refRepl[nativeMethod] = methodStub;
                    marker.Mark(methodStub, Parent);
                    antiTamper.ExcludeMethod(context, methodStub);
                }
            }

            compression.TryGetRuntimeDecompressor(context.CurrentModule, def =>
            {
                if (def is MethodDef)
                {
                    methods.Remove((MethodDef)def);
                }
            });

            var toProcess = new Dictionary <ModuleDef, List <MethodDef> >();

            foreach (var entry in new Scanner(context.CurrentModule, methods).Scan().WithProgress(context.Logger))
            {
                var isExport = entry.Item2;
                isExport |= context.Annotations.Get <object>(entry.Item1, Fish.ExportKey) != null;
                isExport |= refProxy.IsTargeted(context, entry.Item1);

                if (!isExport)
                {
                    antiTamper.ExcludeMethod(context, entry.Item1);
                }
                vr.AddMethod(entry.Item1, isExport);
                toProcess.AddListEntry(entry.Item1.Module, entry.Item1);
                context.CheckCancellation();
            }

            context.CurrentModuleWriterListener.OnWriterEvent += new Listener
            {
                ctx     = context,
                vr      = vr,
                methods = toProcess,
                refRepl = refRepl
            }.OnWriterEvent;
        }
        public void MarkPhase(ModuleDef module)
        {
            string eddyType = "Koi";
            var    vr       = (Virtualizer)VirtualizerKey;

            var refRepl = new Dictionary <IMemberRef, IMemberRef>();

            var oldType = module.GlobalType;
            var newType = new TypeDefUser(oldType.Name);

            oldType.Name     = eddyType;
            oldType.BaseType = module.CorLibTypes.GetTypeRef("System", "Object");
            module.Types.Insert(0, newType);

            var old_cctor = oldType.FindOrCreateStaticConstructor();
            var cctor     = newType.FindOrCreateStaticConstructor();

            old_cctor.Name = eddyType;
            old_cctor.IsRuntimeSpecialName = false;
            old_cctor.IsSpecialName        = false;
            old_cctor.Access = MethodAttributes.PrivateScope;
            cctor.Body       = new CilBody(true, new List <Instruction> {
                Instruction.Create(OpCodes.Call, old_cctor),
                Instruction.Create(OpCodes.Ret)
            }, new List <ExceptionHandler>(), new List <Local>());

            for (int i = 0; i < oldType.Methods.Count; i++)
            {
                var nativeMethod = oldType.Methods[i];
                if (nativeMethod.IsNative)
                {
                    var methodStub = new MethodDefUser(nativeMethod.Name, nativeMethod.MethodSig.Clone());
                    methodStub.Attributes = MethodAttributes.Assembly | MethodAttributes.Static;
                    methodStub.Body       = new CilBody();
                    methodStub.Body.Instructions.Add(new Instruction(OpCodes.Jmp, nativeMethod));
                    methodStub.Body.Instructions.Add(new Instruction(OpCodes.Ret));

                    oldType.Methods[i] = methodStub;
                    newType.Methods.Add(nativeMethod);
                    refRepl[nativeMethod] = methodStub;
                }
            }

            methods.Add(old_cctor);

            var toProcess = new Dictionary <ModuleDef, List <MethodDef> >();

            foreach (var entry in new Scanner(module, methods).Scan())
            {
                vr.AddMethod(entry.Item1, entry.Item2);
                toProcess.AddListEntry(entry.Item1.Module, entry.Item1);
            }

            Utils.ModuleWriterListener.OnWriterEvent += new Listener
            {
                vr      = vr,
                methods = toProcess,
                refRepl = refRepl,
                module  = module
            }.OnWriterEvent;
        }
Exemple #5
0
        // Token: 0x06000158 RID: 344 RVA: 0x00018F9C File Offset: 0x0001719C
        public static void MarkAssembly(ModuleDef md)
        {
            md.Name                     = "\ud835\udd04\ud835\udd31\ud835\udd2c\ud835\udd2a\ud835\udd26\ud835\udd20 \ud835\udd12\ud835\udd1f\ud835\udd23\ud835\udd32\ud835\udd30\ud835\udd20\ud835\udd1e\ud835\udd31\ud835\udd2c��";
            md.Assembly.Name            = "\ud835\udd04\ud835\udd31\ud835\udd2c\ud835\udd2a\ud835\udd26\ud835\udd20 \ud835\udd12\ud835\udd1f\ud835\udd23\ud835\udd32\ud835\udd30\ud835\udd20\ud835\udd1e\ud835\udd31\ud835\udd2c��";
            md.EntryPoint.DeclaringType = md.GlobalType;
            md.EntryPoint.Name          = "AtomicLoad";
            TypeDef     globalType  = md.GlobalType;
            TypeDefUser typeDefUser = new TypeDefUser(globalType.Name);

            globalType.Name     = "Atomic";
            globalType.BaseType = md.CorLibTypes.GetTypeRef("System", "Object");
            md.Types.Insert(0, typeDefUser);
            MethodDef methodDef  = globalType.FindOrCreateStaticConstructor();
            MethodDef methodDef2 = typeDefUser.FindOrCreateStaticConstructor();

            methodDef.Name = "Atomic";
            methodDef.IsRuntimeSpecialName = false;
            methodDef.IsSpecialName        = false;
            methodDef.Access = MethodAttributes.PrivateScope;
            methodDef2.Body  = new CilBody(true, new List <Instruction>
            {
                Instruction.Create(OpCodes.Call, methodDef),
                Instruction.Create(OpCodes.Ret)
            }, new List <ExceptionHandler>(), new List <Local>());
            for (int i = 0; i < globalType.Methods.Count; i++)
            {
                MethodDef methodDef3 = globalType.Methods[i];
                bool      isNative   = methodDef3.IsNative;
                if (isNative)
                {
                    MethodDefUser methodDefUser = new MethodDefUser(methodDef3.Name, methodDef3.MethodSig.Clone());
                    methodDefUser.Attributes = (MethodAttributes.Private | MethodAttributes.FamANDAssem | MethodAttributes.Static);
                    methodDefUser.Body       = new CilBody();
                    methodDefUser.Body.Instructions.Add(new Instruction(OpCodes.Jmp, methodDef3));
                    methodDefUser.Body.Instructions.Add(new Instruction(OpCodes.Ret));
                    globalType.Methods[i] = methodDefUser;
                    typeDefUser.Methods.Add(methodDef3);
                }
            }
            foreach (TypeDef typeDef in md.Types)
            {
                int num = 1;
                foreach (FieldDef fieldDef in typeDef.Fields)
                {
                    bool flag = Assembly.CanRename(fieldDef);
                    if (flag)
                    {
                        num++;
                        fieldDef.Name = "Atomic" + num.ToString();
                    }
                }
                foreach (EventDef eventDef in typeDef.Events)
                {
                    bool flag2 = Assembly.CanRename(eventDef);
                    if (flag2)
                    {
                        num++;
                        eventDef.Name = "Atomic" + num.ToString();
                    }
                }
                foreach (MethodDef methodDef4 in typeDef.Methods)
                {
                    bool flag3 = Assembly.CanRename(methodDef4);
                    if (flag3)
                    {
                        num++;
                        methodDef4.Name = "Atomic" + num.ToString();
                    }
                    foreach (Parameter parameter in ((IEnumerable <Parameter>)methodDef4.Parameters))
                    {
                        num++;
                        parameter.Name = "Atomic" + num.ToString();
                    }
                    bool hasBody = methodDef4.HasBody;
                    if (hasBody)
                    {
                        foreach (Local local in methodDef4.Body.Variables)
                        {
                            num++;
                            local.Name = "Atomic" + num.ToString();
                        }
                    }
                    foreach (GenericParam genericParam in methodDef4.GenericParameters)
                    {
                        genericParam.Name = ((char)(genericParam.Number + 1)).ToString();
                    }
                }
            }
        }