Exemple #1
0
        public static void AddNewNameSpace(string pOldName, string pNName)
        {
            NSLinker pChild = new NSLinker(pNName);

            for (int i = 0; i < _AllNS.Count; i++)
            {
                if (_AllNS[i].CurrentName == pOldName)
                {
                    pChild.ParentLinker = _AllNS[i];
                    break;
                }
            }
            if (pChild.ParentLinker == null)
            {
                NSLinker nparent = new NSLinker(pOldName);
                _AllNS.Add(nparent);//添加一个 np将是一个原始节点
                pChild.ParentLinker = nparent;
            }
            _AllNS.Add(pChild);
        }
Exemple #2
0
        /// <summary>
        /// 获取最初的名称空间的名字
        /// </summary>
        /// <param name="pLinker"></param>
        /// <returns></returns>
        public static string GetOriginalName(string pCurrentName)
        {
            NSLinker tLinker = null;

            for (int i = 0; i < _AllNS.Count; i++)
            {
                if (_AllNS[i].CurrentName == pCurrentName)
                {
                    tLinker = _AllNS[i];
                }
            }
            if (tLinker == null)
            {
                return(pCurrentName);
            }
            while (tLinker.ParentLinker != null)
            {
                tLinker = tLinker.ParentLinker;
            }
            return(tLinker.CurrentName);
        }
        private void ObfuscateNamespace(TypeDefinition type)
        {
            if (type.IsRuntimeSpecialName)
            {
                return;
            }

            if (type.IsSpecialName)
            {
                return;
            }

            if (type.Name.Contains("Resources"))
            {
                return;
            }

            if (!DecompileMeCheck.CheckName(type.FullName))
            {
                return;
            }

            if (_excludedTypesNames.Contains(type.FullName))
            {
                return;
            }

            //---- Obfuscate
            string initialFullName  = type.FullName;
            string initialNamespace = type.Namespace;

            type.Namespace = GetObfuscatedNamespace(type.Namespace);
            if (initialFullName != type.FullName)
            {
                NSLinker.AddNewNameSpace(initialFullName, type.FullName);
            }
            //---- Update the type references in other assemblies
            foreach (AssemblyDefinition assembly in assembliesDefinitions)
            {
                foreach (ModuleDefinition module in assembly.Modules)
                {
                    foreach (TypeReference typeReference in module.TypeReferences)
                    {
                        if (typeReference.Namespace == initialNamespace)
                        {
                            NSLinker.AddNewNameSpace(typeReference.FullName, type.FullName);
                            typeReference.Namespace = type.Namespace;
                        }
                    }
                }
            }

            //---- Resources
            Dictionary <string, string> newDictionary = new Dictionary <string, string>();

            foreach (string key in _resourcesMapping.Keys)
            {
                string resValue = _resourcesMapping[key];
                if (resValue.Contains("."))
                {
                    if (resValue.Substring(0, resValue.LastIndexOf('.')) == initialNamespace)
                    {
                        resValue = type.Namespace + resValue.Substring(resValue.LastIndexOf('.'));
                    }
                }

                newDictionary.Add(key, resValue);
            }

            _resourcesMapping = newDictionary;
        }
        private void ObfuscateType(TypeDefinition type)
        {
            if (type.IsRuntimeSpecialName)
            {
                return;
            }

            if (type.IsSpecialName)
            {
                return;
            }

            if (type.Name.Contains("Resources"))
            {
                return;
            }
            if (!DecompileMeCheck.CheckName(type.FullName))
            {
                return;
            }
            if (_excludedTypesNames.Contains(type.FullName))
            {
                return;
            }

            //---- Obfuscate
            string initialTypeName = type.FullName;
            string notes           = "살歸type";
            string obtypeName      = ObfuscateString(ObfuscationItem.Type, type.Name, notes);

            if (obtypeName == initialTypeName)
            {
                return;
            }
            if (NameObfuscated != null)
            {
                NameObfuscated(ObfuscationItem.Type, initialTypeName, type.Name, notes);
            }

            type.Name = obtypeName;
            //---- Update the type references in other assemblies
            foreach (AssemblyDefinition assembly in assembliesDefinitions)
            {
                foreach (ModuleDefinition module in assembly.Modules)
                {
                    foreach (TypeReference typeReference in module.TypeReferences)
                    {
                        if (typeReference.FullName == initialTypeName)
                        {
                            NSLinker.AddNewNameSpace(typeReference.FullName, type.FullName);
                            typeReference.Name = type.Name;
                        }
                    }
                }
            }
            //---- Prepare ressources names
            if (!initialTypeName.Contains("/"))
            {
                string veryoldName = NSLinker.GetOriginalName(initialTypeName);
                if (!_resourcesMapping.ContainsKey(veryoldName))
                {
                    // Save the obfuscation mapping
                    _resourcesMapping.Add(veryoldName, type.FullName);
                }
            }
            //---- Obfuscate methods
            foreach (MethodDefinition method in type.Methods)
            {
                ObfuscateMethod(type, initialTypeName, method);
            }

            //---- Obfuscate fields
            foreach (FieldDefinition field in type.Fields)
            {
                ObfuscateField(type, field);
            }

            //---- Obfuscate properties
            foreach (PropertyDefinition property in type.Properties)
            {
                ObfuscateProperty(type, property);
            }
        }
        private void AsyncStartObfuscation()
        {
            List <string> assembliesPaths       = new List <string>();
            List <bool>   assembliesToObfuscate = new List <bool>();

            NSLinker.InitNSLinker();
            SetProgress("Loading...", 0);

            //---- Create the Xml Document for mapping
            _xmlMapping     = new XmlDocument();
            _xmlMappingRoot = _xmlMapping.CreateElement("mappings");
            _xmlMapping.AppendChild(_xmlMappingRoot);

            //---- Load the assemblies
            foreach (string assemblyPath in _assembliesPaths.Keys)
            {
                // Full load the assembly
                AssemblyDefinition assembly = AssemblyFactory.GetAssembly(assemblyPath);
                foreach (ModuleDefinition module in assembly.Modules)
                {
                    module.FullLoad();
                }

                assembliesDefinitions.Add(assembly);
                assembliesPaths.Add(Path.GetFileName(assemblyPath));
                assembliesToObfuscate.Add(_assembliesPaths[assemblyPath]);
            }

            SetProgress("Obfuscate...", 0);

            //---- Obfuscate the assemblies
            int assemblyIndex = -1;

            foreach (AssemblyDefinition assembly in assembliesDefinitions)
            {
                assemblyIndex++;

                if (!assembliesToObfuscate[assemblyIndex])
                {
                    continue;
                }

                SetProgress("Obfuscate assembly: " + assembly.Name.Name, 0);

                //---- Obfuscate Namespaces
                if (_obfuscateNamespaces)
                {
                    foreach (TypeDefinition type in assembly.MainModule.Types)
                    {
                        ObfuscateNamespace(type);
                    }
                }
                //
                foreach (TypeDefinition type in assembly.MainModule.Types)
                {
                    ObfuscateType(type);
                }
                //---- Obfuscate Resources
                foreach (Resource resource in assembly.MainModule.Resources)
                {
                    ObfuscateResource(resource);
                }

                SetProgress("Obfuscate resource: " + assembly.Name.Name, 99);
            }

            SetProgress("Saving...", 0);

            //---- Save the modified assemblies
            assemblyIndex = -1;
            foreach (AssemblyDefinition assembly in assembliesDefinitions)
            {
                assemblyIndex++;

                //---- Create output directory if it doesn't exists
                if (Directory.Exists(_outputDirectory) == false)
                {
                    Directory.CreateDirectory(_outputDirectory);
                }

                //---- Delete previous file
                string outputFileName = Path.Combine(_outputDirectory, "" + assembliesPaths[assemblyIndex]);
                if (File.Exists(outputFileName))
                {
                    File.Delete(outputFileName);
                }

                //---- Save the modified assembly
                AssemblyFactory.SaveAssembly(assembly, outputFileName);
            }

            //---- Save mapping
            //_xmlMapping.Save(Path.Combine(_outputDirectory, "Mapping.xml"));

            SetProgress("Complete.90%", 90);

            if (AllComplated != null)
            {
                AllComplated(null, null);
            }
        }