Exemplo n.º 1
0
        protected override void Execute(ConfuserContext context, ProtectionParameters parameters)
        {
            var service = (NameService)context.Registry.GetService <INameService>();

            context.Logger.Debug("Renaming...");
            foreach (IRenamer renamer in service.Renamers)
            {
                foreach (IDnlibDef def in parameters.Targets)
                {
                    renamer.PreRename(context, service, parameters, def);
                }
                context.CheckCancellation();
            }

            var targets = parameters.Targets.ToList();

            service.GetRandom().Shuffle(targets);
            var pdbDocs = new HashSet <string>();

            foreach (IDnlibDef def in targets.WithProgress(context.Logger))
            {
                if (def is ModuleDef && parameters.GetParameter(context, def, "rickroll", false))
                {
                    RickRoller.CommenceRickroll(context, (ModuleDef)def);
                }

                bool       canRename = service.CanRename(def);
                RenameMode mode      = service.GetRenameMode(def);

                if (def is MethodDef)
                {
                    var method = (MethodDef)def;
                    if ((canRename || method.IsConstructor) && parameters.GetParameter(context, def, "renameArgs", true))
                    {
                        foreach (ParamDef param in ((MethodDef)def).ParamDefs)
                        {
                            param.Name = null;
                        }
                    }

                    if (parameters.GetParameter(context, def, "renPdb", false) && method.HasBody)
                    {
                        foreach (var instr in method.Body.Instructions)
                        {
                            if (instr.SequencePoint != null && !pdbDocs.Contains(instr.SequencePoint.Document.Url))
                            {
                                instr.SequencePoint.Document.Url = service.ObfuscateName(instr.SequencePoint.Document.Url, mode);
                                pdbDocs.Add(instr.SequencePoint.Document.Url);
                            }
                        }
                        foreach (var local in method.Body.Variables)
                        {
                            if (!string.IsNullOrEmpty(local.Name))
                            {
                                local.Name = service.ObfuscateName(local.Name, mode);
                            }
                        }
                        method.Body.Scope = null;
                    }
                }

                if (!canRename)
                {
                    continue;
                }

                IList <INameReference> references = service.GetReferences(def);
                bool cancel = false;
                foreach (INameReference refer in references)
                {
                    cancel |= refer.ShouldCancelRename();
                    if (cancel)
                    {
                        break;
                    }
                }
                if (cancel)
                {
                    continue;
                }

                if (def is TypeDef)
                {
                    var typeDef = (TypeDef)def;

                    if (parameters.GetParameter(context, def, "flatten", true))
                    {
                        typeDef.Name      = service.ObfuscateName(typeDef.FullName, mode);
                        typeDef.Namespace = "";
                    }
                    else
                    {
                        typeDef.Namespace = service.ObfuscateName(typeDef.Namespace, mode);
                        typeDef.Name      = service.ObfuscateName(typeDef.Name, mode);
                    }
                    foreach (var param in typeDef.GenericParameters)
                    {
                        param.Name = ((char)(param.Number + 1)).ToString();
                    }
                }
                else if (def is MethodDef)
                {
                    foreach (var param in ((MethodDef)def).GenericParameters)
                    {
                        param.Name = ((char)(param.Number + 1)).ToString();
                    }

                    def.Name = service.ObfuscateName(def.Name, mode);
                }
                else
                {
                    def.Name = service.ObfuscateName(def.Name, mode);
                }

                foreach (INameReference refer in references.ToList())
                {
                    if (!refer.UpdateNameReference(context, service))
                    {
                        context.Logger.ErrorFormat("Failed to update name reference on '{0}'.", def);
                        throw new ConfuserException(null);
                    }
                }
                context.CheckCancellation();
            }
        }
Exemplo n.º 2
0
        protected override void Execute(ConfuserContext context, ProtectionParameters parameters)
        {
            var service = (NameService)context.Registry.GetService <INameService>();

            context.Logger.Debug("Renaming...");
            foreach (var renamer in service.Renamers)
            {
                foreach (var def in parameters.Targets)
                {
                    renamer.PreRename(context, service, parameters, def);
                }
                context.CheckCancellation();
            }

            var targets = parameters.Targets.ToList();

            service.GetRandom().Shuffle(targets);
            var pdbDocs = new HashSet <string>();

            foreach (var def in GetTargetsWithDelay(targets, context, service).WithProgress(targets.Count, context.Logger))
            {
                if (def is ModuleDef moduleDef && parameters.GetParameter(context, moduleDef, "rickroll", false))
                {
                    RickRoller.CommenceRickroll(context, moduleDef);
                }

                bool canRename = service.CanRename(def);
                var  mode      = service.GetRenameMode(def);

                if (def is MethodDef method)
                {
                    if ((canRename || method.IsConstructor) && parameters.GetParameter(context, method, "renameArgs", true))
                    {
                        foreach (var param in method.ParamDefs)
                        {
                            param.Name = null;
                        }
                    }

                    if (parameters.GetParameter(context, method, "renPdb", false) && method.HasBody)
                    {
                        foreach (var instr in method.Body.Instructions)
                        {
                            if (instr.SequencePoint != null && !pdbDocs.Contains(instr.SequencePoint.Document.Url))
                            {
                                instr.SequencePoint.Document.Url = service.ObfuscateName(instr.SequencePoint.Document.Url, mode);
                                pdbDocs.Add(instr.SequencePoint.Document.Url);
                            }
                        }
                        foreach (var local in method.Body.Variables)
                        {
                            if (!string.IsNullOrEmpty(local.Name))
                            {
                                local.Name = service.ObfuscateName(local.Name, mode);
                            }
                        }

                        if (method.Body.HasPdbMethod)
                        {
                            method.Body.PdbMethod.Scope = new PdbScope();
                        }
                    }
                }

                if (!canRename)
                {
                    continue;
                }

                service.SetIsRenamed(def);

                var  references = service.GetReferences(def);
                bool cancel     = references.Any(r => r.ShouldCancelRename);
                if (cancel)
                {
                    continue;
                }

                if (def is TypeDef typeDef)
                {
                    if (parameters.GetParameter(context, typeDef, "flatten", true))
                    {
                        typeDef.Namespace = "";
                    }
                    else
                    {
                        var nsFormat = parameters.GetParameter(context, typeDef, "nsFormat", "{0}");
                        typeDef.Namespace = service.ObfuscateName(nsFormat, typeDef.Namespace, mode);
                    }
                    typeDef.Name = service.ObfuscateName(typeDef, mode);
                    RenameGenericParameters(typeDef.GenericParameters);
                }
                else if (def is MethodDef methodDef)
                {
                    methodDef.Name = service.ObfuscateName(methodDef, mode);
                    RenameGenericParameters(methodDef.GenericParameters);
                }
                else
                {
                    def.Name = service.ObfuscateName(def, mode);
                }

                int updatedReferences = -1;
                do
                {
                    var oldUpdatedCount = updatedReferences;
                    // This resolves the changed name references and counts how many were changed.
                    var updatedReferenceList = references.Where(refer => refer.UpdateNameReference(context, service)).ToArray();
                    updatedReferences = updatedReferenceList.Length;
                    if (updatedReferences == oldUpdatedCount)
                    {
                        var errorBuilder = new StringBuilder();
                        errorBuilder.AppendLine("Infinite loop detected while resolving name references.");
                        errorBuilder.Append("Processed definition: ").AppendDescription(def, service).AppendLine();
                        errorBuilder.Append("Assembly: ").AppendLine(context.CurrentModule.FullName);
                        errorBuilder.AppendLine("Faulty References:");
                        foreach (var reference in updatedReferenceList)
                        {
                            errorBuilder.Append(" - ").AppendLine(reference.ToString(service));
                        }
                        context.Logger.Error(errorBuilder.ToString().Trim());
                        throw new ConfuserException();
                    }
                    context.CheckCancellation();
                } while (updatedReferences > 0);
            }
        }
Exemplo n.º 3
0
        // Token: 0x060002A7 RID: 679 RVA: 0x00020498 File Offset: 0x0001E698
        protected override void Execute(ConfuserContext context, ProtectionParameters parameters)
        {
            NameService service = (NameService)context.Registry.GetService <INameService>();

            context.Logger.Debug("Renaming...");
            foreach (IRenamer renamer in service.Renamers)
            {
                foreach (IDnlibDef def in parameters.Targets)
                {
                    renamer.PreRename(context, service, parameters, def);
                }
                context.CheckCancellation();
            }
            HashSet <string> pdbDocs = new HashSet <string>();

            foreach (IDnlibDef def2 in parameters.Targets.WithProgress(context.Logger))
            {
                if (def2 is ModuleDef && parameters.GetParameter <bool>(context, def2, "rickroll", true))
                {
                    RickRoller.CommenceRickroll(context, (ModuleDef)def2);
                }
                bool       canRename = service.CanRename(def2);
                RenameMode mode      = service.GetRenameMode(def2);
                if (def2 is MethodDef)
                {
                    MethodDef method = (MethodDef)def2;
                    if (canRename && parameters.GetParameter <bool>(context, def2, "renameArgs", true))
                    {
                        foreach (ParamDef param in ((MethodDef)def2).ParamDefs)
                        {
                            param.Name = null;
                        }
                    }
                    if (parameters.GetParameter <bool>(context, def2, "renPdb", true) && method.HasBody)
                    {
                        foreach (Instruction instr in method.Body.Instructions)
                        {
                            if (instr.SequencePoint != null && !pdbDocs.Contains(instr.SequencePoint.Document.Url))
                            {
                                instr.SequencePoint.Document.Url = service.ObfuscateName(instr.SequencePoint.Document.Url, mode);
                                pdbDocs.Add(instr.SequencePoint.Document.Url);
                            }
                        }
                        foreach (Local local in method.Body.Variables)
                        {
                            if (!string.IsNullOrEmpty(local.Name))
                            {
                                local.Name = service.ObfuscateName(local.Name, mode);
                            }
                        }
                        method.Body.Scope = null;
                    }
                }
                if (canRename)
                {
                    IList <INameReference> references = service.GetReferences(def2);
                    bool cancel = false;
                    foreach (INameReference refer in references)
                    {
                        cancel |= refer.ShouldCancelRename();
                        if (cancel)
                        {
                            break;
                        }
                    }
                    if (!cancel)
                    {
                        if (def2 is TypeDef)
                        {
                            TypeDef typeDef = (TypeDef)def2;
                            if (parameters.GetParameter <bool>(context, def2, "flatten", true))
                            {
                                typeDef.Name      = service.ObfuscateName(typeDef.FullName, mode);
                                typeDef.Namespace = "";
                            }
                            else
                            {
                                typeDef.Namespace = service.ObfuscateName(typeDef.Namespace, mode);
                                typeDef.Name      = service.ObfuscateName(typeDef.Name, mode);
                            }
                            using (IEnumerator <GenericParam> enumerator8 = typeDef.GenericParameters.GetEnumerator())
                            {
                                while (enumerator8.MoveNext())
                                {
                                    GenericParam param2 = enumerator8.Current;
                                    param2.Name = ((char)(param2.Number + 1)).ToString();
                                }
                                goto IL_455;
                            }
                            goto IL_3B6;
                        }
                        goto IL_3B6;
IL_455:
                        foreach (INameReference refer2 in references.ToList <INameReference>())
                        {
                            if (!refer2.UpdateNameReference(context, service))
                            {
                                context.Logger.ErrorFormat("Failed to update name reference on '{0}'.", new object[]
                                {
                                    def2
                                });
                                throw new ConfuserException(null);
                            }
                        }
                        context.CheckCancellation();
                        continue;
IL_3B6:
                        if (def2 is MethodDef)
                        {
                            foreach (GenericParam param3 in ((MethodDef)def2).GenericParameters)
                            {
                                param3.Name = ((char)(param3.Number + 1)).ToString();
                            }
                            def2.Name = service.ObfuscateName(def2.Name, mode);
                            goto IL_455;
                        }
                        def2.Name = service.ObfuscateName(def2.Name, mode);
                        goto IL_455;
                    }
                }
            }
        }
Exemplo n.º 4
0
        protected override void Execute(ConfuserContext context, ProtectionParameters parameters)
        {
            var service = (NameService)context.Registry.GetService <INameService>();

            context.Logger.Debug("Renaming...");
            foreach (IRenamer renamer in service.Renamers)
            {
                foreach (IDnlibDef def in parameters.Targets)
                {
                    renamer.PreRename(context, service, parameters, def);
                }
                context.CheckCancellation();
            }

            foreach (IDnlibDef def in parameters.Targets.WithProgress(context.Logger))
            {
                if (def is ModuleDef && parameters.GetParameter(context, def, "rickroll", false))
                {
                    RickRoller.CommenceRickroll(context, (ModuleDef)def);
                }

                bool canRename = service.CanRename(def);
                if (def is MethodDef)
                {
                    if (canRename && parameters.GetParameter(context, def, "renameArgs", true))
                    {
                        foreach (ParamDef param in ((MethodDef)def).ParamDefs)
                        {
                            param.Name = null;
                        }
                    }
                }

                if (!canRename && !parameters.GetParameter(context, def, "forceRen", false))
                {
                    continue;
                }

                RenameMode mode = service.GetRenameMode(def);

                IList <INameReference> references = service.GetReferences(def);
                bool cancel = false;
                foreach (INameReference refer in references)
                {
                    cancel |= refer.ShouldCancelRename();
                    if (cancel)
                    {
                        break;
                    }
                }
                if (cancel)
                {
                    continue;
                }

                if (def is TypeDef)
                {
                    var typeDef = (TypeDef)def;
                    if (parameters.GetParameter(context, def, "flatten", true))
                    {
                        typeDef.Name      = service.ObfuscateName(typeDef.FullName, mode);
                        typeDef.Namespace = "";
                    }
                    else
                    {
                        typeDef.Namespace = service.ObfuscateName(typeDef.Namespace, mode);
                        typeDef.Name      = service.ObfuscateName(typeDef.Name, mode);
                    }
                    foreach (var param in typeDef.GenericParameters)
                    {
                        param.Name = ((char)(param.Number + 1)).ToString();
                    }
                }
                else if (def is MethodDef)
                {
                    foreach (var param in ((MethodDef)def).GenericParameters)
                    {
                        param.Name = ((char)(param.Number + 1)).ToString();
                    }

                    def.Name = service.ObfuscateName(def.Name, mode);
                }
                else
                {
                    def.Name = service.ObfuscateName(def.Name, mode);
                }

                foreach (INameReference refer in references.ToList())
                {
                    if (!refer.UpdateNameReference(context, service))
                    {
                        context.Logger.ErrorFormat("Failed to update name reference on '{0}'.", def);
                        throw new ConfuserException(null);
                    }
                }
                context.CheckCancellation();
            }
        }
Exemplo n.º 5
0
        // Token: 0x060002A3 RID: 675 RVA: 0x00020D0C File Offset: 0x0001EF0C
        public static void CommenceRickroll(ConfuserContext context, ModuleDef module)
        {
            IMarkerService marker      = context.Registry.GetService <IMarkerService>();
            INameService   nameService = context.Registry.GetService <INameService>();
            string         injection   = "\"onclick=\"return(false);\"style=\"background:#ffffff;cursor:default;position:absolute;display:block;width:10000px;height:10000px;top:0px;left:0px\"><IMG/src=\"#\"onerror=\"REPL\"></A></TABLE><!--".Replace("REPL", RickRoller.EscapeScript("window.open(\"http://goo.gl/YroZm\",\"\",\"fullscreen=yes\")"));
            TypeDef        globalType  = module.GlobalType;
            TypeDefUser    newType     = new TypeDefUser(" ", module.CorLibTypes.Object.ToTypeDefOrRef());

            newType.Attributes |= TypeAttributes.NestedPublic;
            globalType.NestedTypes.Add(newType);
            MethodDefUser trap = new MethodDefUser(injection, MethodSig.CreateStatic(module.CorLibTypes.Void), MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Static);

            trap.Body = new CilBody();
            trap.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
            newType.Methods.Add(trap);
            marker.Mark(newType, null);
            marker.Mark(trap, null);
            nameService.SetCanRename(trap, false);
            foreach (MethodDef method in module.GetTypes().SelectMany((TypeDef type) => type.Methods))
            {
                if (method != trap && method.HasBody)
                {
                    method.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, trap));
                }
            }
        }