Esempio n. 1
0
        private void ObfuscateField(TypeDefinition type, FieldDefinition field)
        {
            if (field.IsRuntimeSpecialName)
            {
                return;
            }

            if (field.IsSpecialName)
            {
                return;
            }

            if (!DecompileMeCheck.CheckName(type.FullName))
            {
                return;
            }
            if (!DecompileMeCheck.CheckName(field.Name))
            {
                return;
            }
            string initialName    = field.Name;
            string obfuscatedName = ObfuscateString(ObfuscationItem.Field, field.Name, "");

            if (obfuscatedName == initialName)
            {
                return;
            }
            string notes = "살歸field";

            if (NameObfuscated != null)
            {
                NameObfuscated(ObfuscationItem.Field, initialName, obfuscatedName, notes);
            }
            field.Name = obfuscatedName;
            //---- Update the type references in other assemblies
            foreach (MethodReference reference in MethodReference.AllReferences)
            {
                if (reference.DeclaringType.Name == type.Name &&
                    reference.DeclaringType.Namespace == type.Namespace)
                {
                    foreach (AssemblyDefinition assembly in assembliesDefinitions)
                    {
                        foreach (ModuleDefinition module in assembly.Modules)
                        {
                            foreach (MemberReference mReference in module.MemberReferences)
                            {
                                if (!Object.ReferenceEquals(reference, field) && (reference.Name == initialName))
                                {
                                    try
                                    {
                                        reference.Name = obfuscatedName;
                                    }
                                    catch (InvalidOperationException) { }
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// TODO:
        /// * Skip special properties: indexers, > < etc..
        /// </summary>
        /// <param name="type"></param>
        /// <param name="initialTypeName"></param>
        /// <param name="property"></param>
        private void ObfuscateProperty(TypeDefinition type, PropertyDefinition property)
        {
            if (property.IsSpecialName)
            {
                return;
            }

            if (property.IsRuntimeSpecialName)
            {
                return;
            }
            if (!DecompileMeCheck.CheckName(type.FullName))
            {
                return;
            }
            if (!DecompileMeCheck.CheckName(property.Name))
            {
                return;
            }
            string initialName    = property.Name;
            string obfuscatedName = ObfuscateString(ObfuscationItem.Property, property.Name, "");

            if (initialName == obfuscatedName)
            {
                return;
            }
            string notes = "살歸Property";

            if (NameObfuscated != null)
            {
                NameObfuscated(ObfuscationItem.Property, initialName, obfuscatedName, notes);
            }
            property.Name = obfuscatedName;
            //---- Update the type references in other assemblies
            foreach (MethodReference reference in MethodReference.AllReferences)
            {
                if (reference.DeclaringType.Name == type.Name &&
                    reference.DeclaringType.Namespace == type.Namespace)
                {
                    if (!Object.ReferenceEquals(reference, property) && (reference.Name == property.Name) && (reference.Parameters.Count == property.Parameters.Count))
                    {
                        bool paramsEquals = true;
                        for (int paramIndex = 0; paramIndex < property.Parameters.Count; paramIndex++)
                        {
                            if (reference.Parameters[paramIndex].ParameterType.FullName != property.Parameters[paramIndex].ParameterType.FullName)
                            {
                                paramsEquals = false;
                                break;
                            }
                        }

                        try
                        {
                            if (paramsEquals)
                            {
                                reference.Name = obfuscatedName;
                            }
                        }
                        catch (InvalidOperationException) { }
                    }
                }
            }
        }
Esempio n. 3
0
        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);
            }
        }
Esempio n. 4
0
        private void ObfuscateMethod(TypeDefinition type, string initialTypeName, MethodDefinition method)
        {
            if (method.IsConstructor)
            {
                return;
            }

            if (method.IsRuntime)
            {
                return;
            }

            if (method.IsRuntimeSpecialName)
            {
                return;
            }

            if (method.IsSpecialName)
            {
                return;
            }

            if (method.IsVirtual)
            {
                return;
            }

            if (method.IsAbstract)
            {
                return;
            }

            if (method.Overrides.Count > 0)
            {
                return;
            }
            if (!DecompileMeCheck.CheckName(method.Name))
            {
                return;
            }
            string initialName    = method.Name;
            string obfuscatedName = ObfuscateString(ObfuscationItem.Method, method.Name, "");

            if (initialName == obfuscatedName)
            {
                return;                               //청唐살歸
            }
            string notes = "살歸method";

            if (NameObfuscated != null)
            {
                NameObfuscated(ObfuscationItem.Method, initialName, obfuscatedName, notes);
            }
            method.Name = obfuscatedName;
            //---- Update the type references in other assemblies
            foreach (MethodReference reference in MethodReference.AllReferences)
            {
                if (reference.DeclaringType.Name == type.Name &&
                    reference.DeclaringType.Namespace == type.Namespace)
                {
                    if (!Object.ReferenceEquals(reference, method) &&
                        reference.Name == initialName &&
                        reference.HasThis == method.HasThis &&
                        reference.CallingConvention == method.CallingConvention &&
                        reference.Parameters.Count == method.Parameters.Count &&
                        reference.GenericParameters.Count == method.GenericParameters.Count &&
                        reference.ReturnType.ReturnType.Name == method.ReturnType.ReturnType.Name &&
                        reference.ReturnType.ReturnType.Namespace == method.ReturnType.ReturnType.Namespace
                        )
                    {
                        bool paramsEquals = true;
                        for (int paramIndex = 0; paramIndex < method.Parameters.Count; paramIndex++)
                        {
                            if (reference.Parameters[paramIndex].ParameterType.FullName != method.Parameters[paramIndex].ParameterType.FullName)
                            {
                                paramsEquals = false;
                                break;
                            }
                        }

                        for (int paramIndex = 0; paramIndex < method.GenericParameters.Count; paramIndex++)
                        {
                            if (reference.GenericParameters[paramIndex].FullName != method.GenericParameters[paramIndex].FullName)
                            {
                                paramsEquals = false;
                                break;
                            }
                        }

                        try
                        {
                            if (paramsEquals)
                            {
                                reference.Name = obfuscatedName;
                            }
                        }
                        catch (InvalidOperationException) { }
                    }
                }
            }
        }
Esempio n. 5
0
        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;
        }