Beispiel #1
0
        void ApplySettings(IDnlibDef def, IEnumerable <ProtectionSettingsInfo> infos)
        {
            var settings = new ProtectionSettings();

            ProtectionSettingsInfo?last = null;
            var parser = new ObfAttrParser(protections);

            foreach (var info in infos)
            {
                if (info.Exclude)
                {
                    if (info.ApplyToMember)
                    {
                        settings.Clear();
                    }
                    continue;
                }

                last = info;

                if (info.ApplyToMember)
                {
                    parser.ParseProtectionString(settings, info.Settings);
                }
            }
            if (last != null && !last.Value.ApplyToMember)
            {
                parser.ParseProtectionString(settings, last.Value.Settings);
            }

            ProtectionParameters.SetParameters(context, def, settings);
        }
        protected override void Execute(ConfuserContext context, ProtectionParameters parameters)
        {
            var service = context.Registry.GetService <ITypeService>();


            var objectFactory = new TypeDefUser("factory", context.CurrentModule.GlobalType);

            CreateFactories(objectFactory, service, context, service.Factories);
            context.CurrentModule.Types.Add(objectFactory);

            ProtectionParameters.SetParameters(context, objectFactory, new ProtectionSettings());

            //Apply new generic signatures for targets
            foreach (ScannedItem item in service.Targets)
            {
                SetGenericsForItem(item);
            }

            //Create a new Main method so the original can bennifit from typescambling
            RerouteEntrypoint(service, context); //If this is removed, either remove the metrypoint from the scanned items or add a check in the Analize phase

            //Modify calls/references so that they work with new signatures
            foreach (var method in parameters.Targets.WithProgress(context.Logger).OfType <MethodDef>())
            {
                if (!method.HasBody)
                {
                    return;
                }
                service.RewriteMethodInstructions(method);
            }
        }
        private void CreateFactories(TypeDefUser factpryParentClass, ITypeService service, ConfuserContext context, IFactory[] factories)
        {
            foreach (IFactory factory in factories)
            {
                factory.CreateFactories(service, context.CurrentModule);

                foreach (var generatedFactoryMethod in factory.FactoryMethods)
                {
                    factpryParentClass.Methods.Add(generatedFactoryMethod);
                    ProtectionParameters.SetParameters(context, generatedFactoryMethod, new ProtectionSettings());
                }
            }
        }
Beispiel #4
0
        public void SetParam(IDnlibDef def, string name, string value)
        {
            ProtectionSettings param = ProtectionParameters.GetParameters(this.context, def);

            if (param == null)
            {
                ProtectionParameters.SetParameters(this.context, def, param = new ProtectionSettings());
            }
            if (!param.TryGetValue(this.analyze.Parent, out Dictionary <string, string> nameParam))
            {
                param[this.analyze.Parent] = nameParam = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            }
            nameParam[name] = value;
        }
        private void RerouteEntrypoint(ITypeService service, ConfuserContext context)
        {
            var originalEntry = context.CurrentModule.EntryPoint;

            if (originalEntry != null)
            {
                originalEntry.Name = "_start";

                var param = originalEntry.Parameters.FirstOrDefault()?.Type;


                var newEntry = new MethodDefUser("Main",
                                                 originalEntry.MethodSig,
                                                 originalEntry.ImplAttributes, originalEntry.Attributes);

                IMethod callSig      = originalEntry;
                var     scannedEntry = service.GetScannedItem(originalEntry);
                if (scannedEntry != null)
                {
                    callSig = new MethodSpecUser(originalEntry, new GenericInstMethodSig(scannedEntry.GenericCallTypes.ToArray()));
                }


                newEntry.Body = new dnlib.DotNet.Emit.CilBody(false, new Instruction[] {
                    Instruction.Create(param == null ? OpCodes.Nop : OpCodes.Ldarg_0),
                    Instruction.Create(OpCodes.Call, callSig),
                    Instruction.Create(OpCodes.Ret),
                }, new ExceptionHandler[0], new LocalList());

                originalEntry.DeclaringType.Methods.Add(newEntry);
                context.CurrentModule.EntryPoint = newEntry;
                ProtectionParameters.SetParameters(context, newEntry, ProtectionParameters.GetParameters(context, originalEntry));

                service.RewriteMethodInstructions(newEntry);
            }
        }
Beispiel #6
0
        protected override void Execute(ConfuserContext context, ProtectionParameters parameters)
        {
            context.Logger.Log("Initializing DarksVM");

            foreach (ModuleDefMD module in context.Modules)
            {
                context.Logger.LogFormat("Protecting '{0}' with DarksVM...", module.Name);
            }


            var random   = context.Registry.GetService <IRandomService>();
            var refProxy = context.Registry.GetService <IReferenceProxyService>();
            var nameSrv  = context.Registry.GetService <INameService>();
            var seed     = random.GetRandomGenerator(Parent.FullId).NextInt32();

            string    rtName = null;
            bool      dbg = false, stackwalk = false;
            ModuleDef merge = null;

            foreach (var module in context.Modules)
            {
                if (rtName == null)
                {
                    rtName = parameters.GetParameter <string>(context, module, "rtName");
                }
                if (dbg == false)
                {
                    dbg = parameters.GetParameter <bool>(context, module, "dbgInfo");
                }
                if (stackwalk == false)
                {
                    stackwalk = parameters.GetParameter <bool>(context, module, "stackwalk");
                }

                merge  = module;
                rtName = "Virtualization";
            }
            rtName = rtName ?? "KoiVM.Runtime--test";

            ModuleDefMD rtModule;
            var         resStream = typeof(Virtualizer).Assembly.GetManifestResourceStream("KoiVM.Runtime.dll");

            if (resStream != null)
            {
                rtModule = ModuleDefMD.Load(resStream, context.Resolver.DefaultModuleContext);
            }
            else
            {
                var rtPath = Path.Combine(koiDir, "KoiVM.Runtime.dll");
                rtModule = ModuleDefMD.Load(rtPath, context.Resolver.DefaultModuleContext);
            }
            rtModule.Assembly.Name = rtName;
            rtModule.Name          = rtName + ".dll";
            var vr = new Virtualizer(seed, context.Project.Debug);

            vr.ExportDbgInfo = dbg;
            vr.DoStackWalk   = stackwalk;
            vr.Initialize(rtModule);

            context.Annotations.Set(context, Fish.VirtualizerKey, vr);
            context.Annotations.Set(context, Fish.MergeKey, merge);

            if (merge != null)
            {
                var types = new List <TypeDef>(vr.RuntimeModule.GetTypes());
                types.Remove(vr.RuntimeModule.GlobalType);
                vr.CommitRuntime(merge);
                foreach (var type in types)
                {
                    foreach (var def in type.FindDefinitions())
                    {
                        if (def is TypeDef && def != type) // nested type
                        {
                            continue;
                        }
                        nameSrv.SetCanRename(def, false);
                        ProtectionParameters.SetParameters(context, def, new ProtectionSettings());
                    }
                }
            }
            else
            {
                vr.CommitRuntime(merge);
            }

            var ctor = typeof(InternalsVisibleToAttribute).GetConstructor(new[] { typeof(string) });

            foreach (ModuleDef module in context.Modules)
            {
                var methods = new HashSet <MethodDef>();
                foreach (var type in module.GetTypes())
                {
                    foreach (var method in type.Methods)
                    {
                        if (ProtectionParameters.GetParameters(context, method).ContainsKey(Parent))
                        {
                            methods.Add(method);
                        }
                    }
                }

                if (methods.Count > 0)
                {
                    var ca = new CustomAttribute((ICustomAttributeType)module.Import(ctor));
                    ca.ConstructorArguments.Add(new CAArgument(module.CorLibTypes.String, vr.RuntimeModule.Assembly.Name.String));
                    module.Assembly.CustomAttributes.Add(ca);
                }

                foreach (var entry in new Scanner(module, methods).Scan().WithProgress(context.Logger))
                {
                    if (entry.Item2)
                    {
                        context.Annotations.Set(entry.Item1, Fish.ExportKey, Fish.ExportKey);
                    }
                    else
                    {
                        refProxy.ExcludeTarget(context, entry.Item1);
                    }
                    context.CheckCancellation();
                }
            }
        }
Beispiel #7
0
        protected override void MarkMember(IDnlibDef member, ConfuserContext context)
        {
            ModuleDef module = ((IMemberRef)member).Module;

            ProtectionParameters.SetParameters(context, member, ProtectionParameters.GetParameters(context, module));
        }