コード例 #1
0
ファイル: RPMode.cs プロジェクト: naderr1ua/DarksProtector
        protected static TypeDef GetDelegateType(RPContext ctx, MethodSig sig)
        {
            TypeDef def;

            if (!ctx.Delegates.TryGetValue(sig, out def))
            {
                def = new TypeDefUser(ctx.Name.RandomName(), ctx.Name.RandomName(), ctx.Module.CorLibTypes.GetTypeRef("System", "MulticastDelegate"))
                {
                    Attributes = TypeAttributes.AnsiClass | TypeAttributes.Sealed
                };
                MethodDefUser item = new MethodDefUser(".ctor", MethodSig.CreateInstance(ctx.Module.CorLibTypes.Void, ctx.Module.CorLibTypes.Object, ctx.Module.CorLibTypes.IntPtr))
                {
                    Attributes     = MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName,
                    ImplAttributes = MethodImplAttributes.CodeTypeMask
                };
                def.Methods.Add(item);
                MethodDefUser user2 = new MethodDefUser("Invoke", sig.Clone())
                {
                    MethodSig      = { HasThis = true },
                    Attributes     = MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual,
                    ImplAttributes = MethodImplAttributes.CodeTypeMask
                };
                def.Methods.Add(user2);
                ctx.Module.Types.Add(def);
                foreach (IDnlibDef def2 in def.FindDefinitions())
                {
                    ctx.Marker.Mark(def2, ctx.Protection);
                    ctx.Name.SetCanRename(def2, false);
                }
                ctx.Delegates[sig] = def;
            }
            return(def);
        }
コード例 #2
0
        protected TypeDef CreateDelegateType(MethodSig sig)
        {
            TypeDef ret = new TypeDefUser("AsStrongAsFuck" + Runtime.GetRandomName(), Runtime.GetRandomName() + Runtime.GetChineseString(20), Module.CorLibTypes.GetTypeRef("System", "MulticastDelegate"));

            ret.Attributes = TypeAttributes.Public | TypeAttributes.Sealed;

            var ctor = new MethodDefUser(".ctor", MethodSig.CreateInstance(Module.CorLibTypes.Void, Module.CorLibTypes.Object, Module.CorLibTypes.IntPtr));

            ctor.Attributes     = MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
            ctor.ImplAttributes = MethodImplAttributes.Runtime;
            ret.Methods.Add(ctor);

            var clone = sig.Clone();

            if (clone.HasThis && clone.ExplicitThis)
            {
                if (clone.Params.Count > 0)
                {
                    clone.Params.RemoveAt(0);
                }
            }


            var invoke = new MethodDefUser("Invoke", clone);

            invoke.MethodSig.HasThis = true;
            invoke.Attributes        = MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.NewSlot;
            invoke.ImplAttributes    = MethodImplAttributes.Runtime;
            ret.Methods.Add(invoke);
            Module.Types.Add(ret);

            return(ret);
        }
コード例 #3
0
        MethodSig ResolveMethod(MethodSig sig, GenericArguments genericArgs)
        {
            var newSig = sig.Clone();

            for (int i = 0; i < newSig.Params.Count; i++)
            {
                newSig.Params[i] = genericArgs.ResolveType(newSig.Params[i]);
            }

            if (newSig.ParamsAfterSentinel != null)
            {
                for (int i = 0; i < newSig.ParamsAfterSentinel.Count; i++)
                {
                    newSig.ParamsAfterSentinel[i] = genericArgs.ResolveType(newSig.ParamsAfterSentinel[i]);
                }
            }

            newSig.RetType = genericArgs.ResolveType(newSig.RetType);
            return(newSig);
        }
コード例 #4
0
ファイル: RPMode.cs プロジェクト: nereva/ConfuserEx
        protected static TypeDef GetDelegateType(RPContext ctx, MethodSig sig)
        {
            if (ctx.Delegates.TryGetValue(sig, out var ret))
            {
                return(ret);
            }

            ret = new TypeDefUser(ctx.Name.ObfuscateName(ctx.Method.DeclaringType.Namespace, RenameMode.Unicode), ctx.Name.RandomName(), ctx.Module.CorLibTypes.GetTypeRef("System", "MulticastDelegate"))
            {
                Attributes = TypeAttributes.NotPublic | TypeAttributes.Sealed
            };

            var ctor = new MethodDefUser(".ctor", MethodSig.CreateInstance(ctx.Module.CorLibTypes.Void, ctx.Module.CorLibTypes.Object, ctx.Module.CorLibTypes.IntPtr))
            {
                Attributes     = MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName,
                ImplAttributes = MethodImplAttributes.Runtime
            };

            ret.Methods.Add(ctor);

            var invoke = new MethodDefUser("Invoke", sig.Clone());

            invoke.MethodSig.HasThis = true;
            invoke.Attributes        = MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.NewSlot;
            invoke.ImplAttributes    = MethodImplAttributes.Runtime;
            ret.Methods.Add(invoke);

            ctx.Module.Types.Add(ret);

            foreach (var def in ret.FindDefinitions())
            {
                ctx.Marker.Mark(def, ctx.Protection);
                ctx.Name.SetCanRename(def, false);
            }

            ctx.Delegates[sig] = ret;
            return(ret);
        }
コード例 #5
0
        public static TypeDef CreateDelegateType(MethodSig sig, ModuleDef target, MethodDef original)
        {
            TypeDef ret = new TypeDefUser("AsStrongAsFuck", Renamer.GetEndName(RenameMode.Base64, 3, 20), target.CorLibTypes.GetTypeRef("System", "MulticastDelegate"));

            ret.Attributes = original.DeclaringType.Attributes;

            var ctor = new MethodDefUser(".ctor", MethodSig.CreateInstance(target.CorLibTypes.Void, target.CorLibTypes.Object, target.CorLibTypes.IntPtr));

            ctor.Attributes     = MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
            ctor.ImplAttributes = MethodImplAttributes.Runtime;
            ret.Methods.Add(ctor);

            var clone = sig.Clone();

            var invoke = new MethodDefUser("Invoke", clone);

            invoke.MethodSig.HasThis = true;
            invoke.Attributes        = MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.NewSlot | MethodAttributes.Public;
            invoke.ImplAttributes    = MethodImplAttributes.Runtime;
            ret.Methods.Add(invoke);
            target.Types.Add(ret);

            return(ret);
        }
コード例 #6
0
        protected MethodDef createProxy(ModuleDef moduleDef, MethodDef target)
        {
            MethodSig proxy_signature = createProxySig(moduleDef, target);

            TypeDefUser new_type = new TypeDefUser("DEFAULT_TYPE_DEF" + target.FullName);

            new_type.Attributes |= TypeAttributes.Public | TypeAttributes.AutoClass;

            // Creating delegate type.
            // TODO: Add caching system.
            // TODO: Add string parameter support to make string parameter "Obfuscated by Wisser Tg"

            TypeDefUser new_delegate = new TypeDefUser("Delegate_" + target.FullName, moduleDef.CorLibTypes.GetTypeRef("System", "MulticastDelegate"));

            new_delegate.Attributes = target.ResolveMethodDefThrow().DeclaringType.Attributes;
            {
                MethodDefUser ctor = new MethodDefUser(".ctor", MethodSig.CreateInstance(moduleDef.CorLibTypes.Void, moduleDef.CorLibTypes.Object, moduleDef.CorLibTypes.IntPtr));
                ctor.Attributes     = MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
                ctor.ImplAttributes = MethodImplAttributes.Runtime;

                MethodDefUser invoke = new MethodDefUser("Invoke", proxy_signature.Clone());
                invoke.MethodSig.HasThis = true;
                invoke.ImplAttributes    = MethodImplAttributes.Runtime;
                invoke.Attributes        = MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.NewSlot | MethodAttributes.Public;

                new_delegate.Methods.Add(invoke);
                new_delegate.Methods.Add(ctor);

                moduleDef.Types.Add(new_delegate);
            }

            FieldDefUser field = new FieldDefUser("Delegate_holder_", new FieldSig(new ClassSig(new_delegate)), FieldAttributes.Private | FieldAttributes.Static);

            {
                ModuleDefMD mscorlib = ModuleDefMD.Load(typeof(void).Assembly.Modules.First());
                MethodDef   cctor    = target.DeclaringType.FindOrCreateStaticConstructor();
                if (cctor.Body.Instructions.Last().OpCode == OpCodes.Ret)
                {
                    cctor.Body.Instructions.Remove(cctor.Body.Instructions.Last());
                }

                cctor.Body.Instructions.Add(OpCodes.Nop.ToInstruction());
                cctor.Body.Instructions.Add(OpCodes.Ldtoken.ToInstruction(new_delegate));
                cctor.Body.Instructions.Add(OpCodes.Call.ToInstruction(mscorlib.Find("System.Type", true).FindMethod("GetTypeFromHandle")));
                cctor.Body.Instructions.Add(OpCodes.Ldtoken.ToInstruction(moduleDef.Import(target.DeclaringType)));
                cctor.Body.Instructions.Add(OpCodes.Call.ToInstruction(mscorlib.Find("System.Type", true).FindMethod("GetTypeFromHandle")));
                cctor.Body.Instructions.Add(OpCodes.Callvirt.ToInstruction(mscorlib.Import(mscorlib.Find("System.Reflection.Module", true).FindMethod("get_Module"))));
                cctor.Body.Instructions.Add(OpCodes.Ldc_I4.ToInstruction((int)target.MDToken.Raw));
                cctor.Body.Instructions.Add(OpCodes.Callvirt.ToInstruction(mscorlib.Import(mscorlib.Find("System.Reflection.Module", true).FindMethod("ResolveMethod"))));
                cctor.Body.Instructions.Add(OpCodes.Isinst.ToInstruction(mscorlib.Import(mscorlib.Find("System.Reflection.MethodInfo", true))));
                var b = mscorlib.Find("System.Delegate", true).FindMethod("CreateDelegate",
                                                                          MethodSig.CreateStatic(mscorlib.Find("System.Delegate", true).ToTypeSig(), mscorlib.Find("System.Type", true).ToTypeSig(), mscorlib.Find("System.Reflection.MethodInfo", true).ToTypeSig()));
                cctor.Body.Instructions.Add(OpCodes.Call.ToInstruction(mscorlib.Find("System.Delegate", true).FindMethod("CreateDelegate",
                                                                                                                         MethodSig.CreateStatic(mscorlib.Find("System.Delegate", true).ToTypeSig(), mscorlib.Find("System.Type", true).ToTypeSig(), mscorlib.Find("System.Reflection.MethodInfo", true).ToTypeSig()))));
                cctor.Body.Instructions.Add(OpCodes.Castclass.ToInstruction(new_delegate));
                cctor.Body.Instructions.Add(OpCodes.Stsfld.ToInstruction(field));
                cctor.Body.Instructions.Add(OpCodes.Ret.ToInstruction());

                // TODO: Check for Ret instruction existance.
            }

            MethodDefUser proxy_methodDef = new MethodDefUser("Proxy_method__" + target.FullName, proxy_signature);

            proxy_methodDef.Attributes     = MethodAttributes.PrivateScope | MethodAttributes.Static;
            proxy_methodDef.ImplAttributes = MethodImplAttributes.Managed | MethodImplAttributes.IL;

            proxy_methodDef.Body = new CilBody();
            proxy_methodDef.Body.Instructions.Add(OpCodes.Ldsfld.ToInstruction(field));
            for (int i = 0; i < target.Parameters.Count; i++)
            {
                proxy_methodDef.Body.Instructions.Add(OpCodes.Ldarg.ToInstruction(proxy_methodDef.Parameters[i]));
            }
            proxy_methodDef.Body.Instructions.Add(OpCodes.Callvirt.ToInstruction(new_delegate.FindMethod("Invoke")));
            proxy_methodDef.Body.Instructions.Add(OpCodes.Ret.ToInstruction());

            target.DeclaringType.Methods.Add(proxy_methodDef);

            return(proxy_methodDef);
        }