Ejemplo n.º 1
0
        /// <summary>
        /// Updates the member type references.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="memberReference">The member reference.</param>
        private static void UpdateMemberTypeReferences(ICloakContext context, MemberReference memberReference)
        {
            //Get the type reference for this
            TypeReference methodType = memberReference.DeclaringType;

            //Get the assembly for this
            if (methodType.Scope is AssemblyNameReference)
            {
                string assemblyName = ((AssemblyNameReference)methodType.Scope).FullName;
                //Check if this needs to be updated
                if (context.MappingGraph.IsAssemblyMappingDefined(assemblyName))
                {
                    AssemblyMapping assemblyMapping = context.MappingGraph.GetAssemblyMapping(assemblyName);
                    TypeMapping     t = assemblyMapping.GetTypeMapping(methodType);
                    if (t == null)
                    {
                        return; //No type defined
                    }
                    //Update the type name
                    if (!String.IsNullOrEmpty(t.ObfuscatedTypeName))
                    {
                        methodType.Name = t.ObfuscatedTypeName;
                    }

                    //We can't change method specifications....
                    if (memberReference is MethodSpecification)
                    {
                        MethodSpecification specification = (MethodSpecification)memberReference;
                        MethodReference     meth          = specification.GetElementMethod();
                        //Update the method name also if available
                        if (t.HasMethodMapping(meth))
                        {
                            meth.Name = t.GetObfuscatedMethodName(meth);
                        }
                    }
                    else if (memberReference is FieldReference)
                    {
                        FieldReference fr = (FieldReference)memberReference;
                        if (t.HasFieldMapping(fr))
                        {
                            memberReference.Name = t.GetObfuscatedFieldName(fr);
                        }
                    }
                    else if (memberReference is MethodReference) //Is this ever used?? Used to be just an else without if
                    {
                        MethodReference mr = (MethodReference)memberReference;
                        //Update the method name also if available
                        if (t.HasMethodMapping(mr))
                        {
                            memberReference.Name = t.GetObfuscatedMethodName(mr);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void RunTask()
        {
            //Go through the members and build up a mapping graph
            //If this is done then the members in the graph will be obfuscated, otherwise we'll
            //just obfuscate private members

            //Set up the mapping graph
            AssemblyMapping assemblyMapping = context.MappingGraph.AddAssembly(context.AssemblyDefinition);

            //Go through each module
            foreach (ModuleDefinition moduleDefinition in context.AssemblyDefinition.Modules)
            {
                //Go through each type
                foreach (TypeDefinition typeDefinition in moduleDefinition.GetAllTypes())
                {
                    //First of all - see if we've declared it already - if so get the existing reference
                    TypeMapping typeMapping = assemblyMapping.GetTypeMapping(typeDefinition);
                    if (typeMapping == null)
                    {
                        //We don't have it - get it
                        if (context.Settings.ObfuscateAllModifiers)
                        {
                            typeMapping = assemblyMapping.AddType(typeDefinition, context.NameManager.GenerateName(NamingTable.Type));
                        }
                        else
                        {
                            typeMapping = assemblyMapping.AddType(typeDefinition, null);
                        }
                    }

                    //Go through each method
                    foreach (MethodDefinition methodDefinition in typeDefinition.Methods)
                    {
                        MapMethod(assemblyMapping, typeDefinition, typeMapping, methodDefinition);
                    }

                    //Properties
                    foreach (PropertyDefinition propertyDefinition in typeDefinition.Properties)
                    {
                        MapProperty(assemblyMapping, typeDefinition, typeMapping, propertyDefinition);
                    }

                    //Fields
                    foreach (FieldDefinition fieldDefinition in typeDefinition.Fields)
                    {
                        MapField(typeMapping, fieldDefinition);
                    }
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Updates the type references.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="typeReference">The type reference.</param>
 private static void UpdateTypeReferences(ICloakContext context, TypeReference typeReference)
 {
     //Get the assembly for this
     if (typeReference.Scope is AssemblyNameReference)
     {
         string assemblyName = ((AssemblyNameReference)typeReference.Scope).FullName;
         //Check if this needs to be updated
         if (context.MappingGraph.IsAssemblyMappingDefined(assemblyName))
         {
             AssemblyMapping assemblyMapping = context.MappingGraph.GetAssemblyMapping(assemblyName);
             TypeMapping     t = assemblyMapping.GetTypeMapping(typeReference);
             if (t == null)
             {
                 return; //No type defined
             }
             //Update the type name
             if (!String.IsNullOrEmpty(t.ObfuscatedTypeName))
             {
                 typeReference.Name = t.ObfuscatedTypeName;
             }
         }
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Performs obfuscation on the specified assembly.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="definition">The assembly definition.</param>
        private static void Obfuscate(ICloakContext context, AssemblyDefinition definition)
        {
            //Get the assembly mapping information (if any)
            if (context.MappingGraph.IsAssemblyMappingDefined(definition))
            {
                //Get the mapping object
                AssemblyMapping assemblyMapping = context.MappingGraph.GetAssemblyMapping(definition);

                //Go through each module
                foreach (ModuleDefinition moduleDefinition in definition.Modules)
                {
                    //Go through each type
                    foreach (TypeDefinition typeDefinition in moduleDefinition.GetAllTypes())
                    {
                        //Get the type mapping
                        TypeMapping typeMapping = assemblyMapping.GetTypeMapping(typeDefinition);
                        if (typeMapping == null)
                        {
                            continue; //There is a problem....
                        }
                        //Rename if necessary
                        if (!String.IsNullOrEmpty(typeMapping.ObfuscatedTypeName))
                        {
                            typeDefinition.Name = typeMapping.ObfuscatedTypeName;
                        }

                        //Go through each method
                        foreach (MethodDefinition methodDefinition in typeDefinition.Methods)
                        {
                            //If this is an entry method then note it
                            //bool isEntry = false;
                            //if (definition.EntryPoint != null && definition.EntryPoint.Name == methodDefinition.Name)
                            //    isEntry = true;
                            if (typeMapping.HasMethodMapping(methodDefinition))
                            {
                                methodDefinition.Name = typeMapping.GetObfuscatedMethodName(methodDefinition);
                            }

                            //Dive into the method body
                            UpdateMethodReferences(context, methodDefinition);
                            //if (isEntry)
                            //    definition.EntryPoint = methodDefinition;
                        }

                        //Properties
                        foreach (PropertyDefinition propertyDefinition in typeDefinition.Properties)
                        {
                            if (typeMapping.HasPropertyMapping(propertyDefinition))
                            {
                                propertyDefinition.Name =
                                    typeMapping.GetObfuscatedPropertyName(propertyDefinition);
                            }

                            //Dive into the method body
                            if (propertyDefinition.GetMethod != null)
                            {
                                UpdateMethodReferences(context, propertyDefinition.GetMethod);
                            }
                            if (propertyDefinition.SetMethod != null)
                            {
                                UpdateMethodReferences(context, propertyDefinition.SetMethod);
                            }
                        }

                        //Fields
                        foreach (FieldDefinition fieldDefinition in typeDefinition.Fields)
                        {
                            if (typeMapping.HasFieldMapping(fieldDefinition))
                            {
                                fieldDefinition.Name = typeMapping.GetObfuscatedFieldName(fieldDefinition);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Processes the assembly - goes through each member and applies a mapping.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="definition">The assembly definition.</param>
        private static void ProcessAssembly(ICloakContext context, AssemblyDefinition definition)
        {
            //Store whether to obfuscate all members
            bool obfuscateAll = context.Settings.ObfuscateAllModifiers;

            //Set up the mapping graph
            AssemblyMapping assemblyMapping = context.MappingGraph.AddAssembly(definition);

            //Get a reference to the name manager
            NameManager nameManager = context.NameManager;

            //Go through each module
            foreach (ModuleDefinition moduleDefinition in definition.Modules)
            {
                //Go through each type
                foreach (TypeDefinition typeDefinition in moduleDefinition.GetAllTypes())
                {
                    //First of all - see if we've declared it already - if so get the existing reference
                    TypeMapping typeMapping = assemblyMapping.GetTypeMapping(typeDefinition);
                    if (typeMapping == null)
                    {
                        //We don't have it - get it
                        if (obfuscateAll)
                        {
                            typeMapping = assemblyMapping.AddType(typeDefinition,
                                                                  nameManager.GenerateName(NamingTable.Type));
                        }
                        else
                        {
                            typeMapping = assemblyMapping.AddType(typeDefinition, null);
                        }
                    }

                    //Go through each method
                    foreach (MethodDefinition methodDefinition in typeDefinition.Methods)
                    {
                        //First of all - check if we've obfuscated it already - if we have then don't bother
                        if (typeMapping.HasMethodBeenObfuscated(methodDefinition.Name))
                        {
                            continue;
                        }

                        //We won't do constructors - causes issues
                        if (methodDefinition.IsConstructor)
                        {
                            continue;
                        }

                        //We haven't - let's work out the obfuscated name
                        if (obfuscateAll)
                        {
                            //Take into account whether this is overriden, or an interface implementation
                            if (methodDefinition.IsVirtual)
                            {
                                //We handle this differently - rather than creating a new name each time we need to reuse any already generated names
                                //We do this by firstly finding the root interface or object
                                TypeDefinition baseType = FindBaseTypeDeclaration(typeDefinition, methodDefinition);
                                if (baseType != null)
                                {
                                    //Find it in the mappings
                                    TypeMapping baseTypeMapping = assemblyMapping.GetTypeMapping(baseType);
                                    if (baseTypeMapping != null)
                                    {
                                        //We found the type mapping - look up the name it uses for this method and use that
                                        if (baseTypeMapping.HasMethodMapping(methodDefinition))
                                        {
                                            typeMapping.AddMethodMapping(methodDefinition, baseTypeMapping.GetObfuscatedMethodName(methodDefinition));
                                        }
                                        else
                                        {
                                            //That's strange... we shouldn't get into here - but if we ever do then
                                            //we'll add the type mapping into both
                                            string obfuscatedName = nameManager.GenerateName(NamingTable.Method);
                                            typeMapping.AddMethodMapping(methodDefinition, obfuscatedName);
                                            baseTypeMapping.AddMethodMapping(methodDefinition, obfuscatedName);
                                        }
                                    }
                                    else
                                    {
                                        //Otherwise add it into our list manually
                                        //at the base level first off
                                        baseTypeMapping = assemblyMapping.AddType(baseType,
                                                                                  nameManager.GenerateName(NamingTable.Type));
                                        string obfuscatedName = nameManager.GenerateName(NamingTable.Method);
                                        baseTypeMapping.AddMethodMapping(methodDefinition, obfuscatedName);
                                        //Now at our implemented level
                                        typeMapping.AddMethodMapping(methodDefinition, obfuscatedName);
                                    }
                                }
                                else
                                {
                                    //We must be at the base already - add normally
                                    typeMapping.AddMethodMapping(methodDefinition,
                                                                 nameManager.GenerateName(NamingTable.Method));
                                }
                            }
                            else //Add normally
                            {
                                typeMapping.AddMethodMapping(methodDefinition,
                                                             nameManager.GenerateName(NamingTable.Method));
                            }
                        }
                        else if (methodDefinition.IsPrivate)
                        {
                            typeMapping.AddMethodMapping(methodDefinition, nameManager.GenerateName(NamingTable.Method));
                        }
                    }

                    //Properties
                    foreach (PropertyDefinition propertyDefinition in typeDefinition.Properties)
                    {
                        //First of all - check if we've obfuscated it already - if we have then don't bother
                        if (typeMapping.HasPropertyBeenObfuscated(propertyDefinition.Name))
                        {
                            continue;
                        }

                        //Go through the old fashioned way
                        if (obfuscateAll)
                        {
                            if ((propertyDefinition.GetMethod != null && propertyDefinition.GetMethod.IsVirtual) ||
                                (propertyDefinition.SetMethod != null && propertyDefinition.SetMethod.IsVirtual))
                            {
                                //We handle this differently - rather than creating a new name each time we need to reuse any already generated names
                                //We do this by firstly finding the root interface or object
                                TypeDefinition baseType = FindBaseTypeDeclaration(typeDefinition, propertyDefinition);
                                if (baseType != null)
                                {
                                    //Find it in the mappings
                                    TypeMapping baseTypeMapping = assemblyMapping.GetTypeMapping(baseType);
                                    if (baseTypeMapping != null)
                                    {
                                        //We found the type mapping - look up the name it uses for this property and use that
                                        if (baseTypeMapping.HasPropertyMapping(propertyDefinition))
                                        {
                                            typeMapping.AddPropertyMapping(propertyDefinition, baseTypeMapping.GetObfuscatedPropertyName(propertyDefinition));
                                        }
                                        else
                                        {
                                            //That's strange... we shouldn't get into here - but if we ever do then
                                            //we'll add the type mapping into both
                                            string obfuscatedName = nameManager.GenerateName(NamingTable.Property);
                                            typeMapping.AddPropertyMapping(propertyDefinition, obfuscatedName);
                                            baseTypeMapping.AddPropertyMapping(propertyDefinition, obfuscatedName);
                                        }
                                    }
                                    else
                                    {
                                        //Otherwise add it into our list manually
                                        //at the base level first off
                                        baseTypeMapping = assemblyMapping.AddType(baseType,
                                                                                  nameManager.GenerateName(NamingTable.Type));
                                        string obfuscatedName = nameManager.GenerateName(NamingTable.Property);
                                        baseTypeMapping.AddPropertyMapping(propertyDefinition, obfuscatedName);
                                        //Now at our implemented level
                                        typeMapping.AddPropertyMapping(propertyDefinition, obfuscatedName);
                                    }
                                }
                                else
                                {
                                    //We must be at the base already - add normally
                                    typeMapping.AddPropertyMapping(propertyDefinition,
                                                                   nameManager.GenerateName(NamingTable.Property));
                                }
                            }
                            else
                            {
                                typeMapping.AddPropertyMapping(propertyDefinition,
                                                               nameManager.GenerateName(NamingTable.Property));
                            }
                        }
                        else if (propertyDefinition.GetMethod != null && propertyDefinition.SetMethod != null)
                        {
                            //Both parts need to be private
                            if (propertyDefinition.GetMethod.IsPrivate && propertyDefinition.SetMethod.IsPrivate)
                            {
                                typeMapping.AddPropertyMapping(propertyDefinition, nameManager.GenerateName(NamingTable.Property));
                            }
                        }
                        else if (propertyDefinition.GetMethod != null)
                        {
                            //Only the get is present - make sure it is private
                            if (propertyDefinition.GetMethod.IsPrivate)
                            {
                                typeMapping.AddPropertyMapping(propertyDefinition, nameManager.GenerateName(NamingTable.Property));
                            }
                        }
                        else if (propertyDefinition.SetMethod != null)
                        {
                            //Only the set is present - make sure it is private
                            if (propertyDefinition.SetMethod.IsPrivate)
                            {
                                typeMapping.AddPropertyMapping(propertyDefinition, nameManager.GenerateName(NamingTable.Property));
                            }
                        }
                    }

                    //Fields
                    foreach (FieldDefinition fieldDefinition in typeDefinition.Fields)
                    {
                        //First of all - check if we've obfuscated it already - if we have then don't bother
                        if (typeMapping.HasFieldBeenObfuscated(fieldDefinition.Name))
                        {
                            continue;
                        }

                        if (obfuscateAll)
                        {
                            typeMapping.AddFieldMapping(fieldDefinition, nameManager.GenerateName(NamingTable.Field));
                        }
                        else if (fieldDefinition.IsPrivate)
                        {
                            //Rename if private
                            typeMapping.AddFieldMapping(fieldDefinition, nameManager.GenerateName(NamingTable.Field));
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private void MapMethod(AssemblyMapping assemblyMapping, TypeDefinition typeDefinition, TypeMapping typeMapping, MethodDefinition methodDefinition)
        {
            //First of all - check if we've obfuscated it already - if we have then don't bother
            if (typeMapping.HasMethodBeenObfuscated(methodDefinition.Name))
            {
                return;
            }

            //We won't do constructors - causes issues
            if (methodDefinition.IsConstructor)
            {
                return;
            }

            //We haven't - let's work out the obfuscated name
            if (context.Settings.ObfuscateAllModifiers)
            {
                //Take into account whether this is overriden, or an interface implementation
                if (methodDefinition.IsVirtual)
                {
                    //We handle this differently - rather than creating a new name each time we need to reuse any already generated names
                    //We do this by firstly finding the root interface or object
                    TypeDefinition baseType = FindBaseTypeDeclaration(typeDefinition, methodDefinition);
                    if (baseType != null)
                    {
                        //Find it in the mappings
                        TypeMapping baseTypeMapping = assemblyMapping.GetTypeMapping(baseType);
                        if (baseTypeMapping != null)
                        {
                            //We found the type mapping - look up the name it uses for this method and use that
                            if (baseTypeMapping.HasMethodMapping(methodDefinition))
                            {
                                typeMapping.AddMethodMapping(methodDefinition, baseTypeMapping.GetObfuscatedMethodName(methodDefinition));
                            }
                            else
                            {
                                //That's strange... we shouldn't get into here - but if we ever do then
                                //we'll add the type mapping into both
                                string obfuscatedName = context.NameManager.GenerateName(NamingTable.Method);
                                typeMapping.AddMethodMapping(methodDefinition, obfuscatedName);
                                baseTypeMapping.AddMethodMapping(methodDefinition, obfuscatedName);
                            }
                        }
                        else
                        {
                            //Otherwise add it into our list manually
                            //at the base level first off
                            baseTypeMapping = assemblyMapping.AddType(baseType,
                                                                      context.NameManager.GenerateName(NamingTable.Type));
                            string obfuscatedName = context.NameManager.GenerateName(NamingTable.Method);
                            baseTypeMapping.AddMethodMapping(methodDefinition, obfuscatedName);
                            //Now at our implemented level
                            typeMapping.AddMethodMapping(methodDefinition, obfuscatedName);
                        }
                    }
                    else
                    {
                        //We must be at the base already - add normally
                        typeMapping.AddMethodMapping(methodDefinition,
                                                     context.NameManager.GenerateName(NamingTable.Method));
                    }
                }
                else //Add normally
                {
                    typeMapping.AddMethodMapping(methodDefinition,
                                                 context.NameManager.GenerateName(NamingTable.Method));
                }
            }
            else if (methodDefinition.IsPrivate)
            {
                typeMapping.AddMethodMapping(methodDefinition, context.NameManager.GenerateName(NamingTable.Method));
            }
        }
Ejemplo n.º 7
0
        private void MapProperty(AssemblyMapping assemblyMapping, TypeDefinition typeDefinition, TypeMapping typeMapping, PropertyDefinition propertyDefinition)
        {
            //First of all - check if we've obfuscated it already - if we have then don't bother
            if (typeMapping.HasPropertyBeenObfuscated(propertyDefinition.Name))
            {
                return;
            }

            //Go through the old fashioned way
            if (context.Settings.ObfuscateAllModifiers)
            {
                if ((propertyDefinition.GetMethod != null && propertyDefinition.GetMethod.IsVirtual) ||
                    (propertyDefinition.SetMethod != null && propertyDefinition.SetMethod.IsVirtual))
                {
                    //We handle this differently - rather than creating a new name each time we need to reuse any already generated names
                    //We do this by firstly finding the root interface or object
                    TypeDefinition baseType = FindBaseTypeDeclaration(typeDefinition, propertyDefinition);
                    if (baseType != null)
                    {
                        //Find it in the mappings
                        TypeMapping baseTypeMapping = assemblyMapping.GetTypeMapping(baseType);
                        if (baseTypeMapping != null)
                        {
                            //We found the type mapping - look up the name it uses for this property and use that
                            if (baseTypeMapping.HasPropertyMapping(propertyDefinition))
                            {
                                typeMapping.AddPropertyMapping(propertyDefinition, baseTypeMapping.GetObfuscatedPropertyName(propertyDefinition));
                            }
                            else
                            {
                                //That's strange... we shouldn't get into here - but if we ever do then
                                //we'll add the type mapping into both
                                string obfuscatedName = context.NameManager.GenerateName(NamingTable.Property);
                                typeMapping.AddPropertyMapping(propertyDefinition, obfuscatedName);
                                baseTypeMapping.AddPropertyMapping(propertyDefinition, obfuscatedName);
                            }
                        }
                        else
                        {
                            //Otherwise add it into our list manually
                            //at the base level first off
                            baseTypeMapping = assemblyMapping.AddType(baseType,
                                                                      context.NameManager.GenerateName(NamingTable.Type));
                            string obfuscatedName = context.NameManager.GenerateName(NamingTable.Property);
                            baseTypeMapping.AddPropertyMapping(propertyDefinition, obfuscatedName);
                            //Now at our implemented level
                            typeMapping.AddPropertyMapping(propertyDefinition, obfuscatedName);
                        }
                    }
                    else
                    {
                        //We must be at the base already - add normally
                        typeMapping.AddPropertyMapping(propertyDefinition,
                                                       context.NameManager.GenerateName(NamingTable.Property));
                    }
                }
                else
                {
                    typeMapping.AddPropertyMapping(propertyDefinition,
                                                   context.NameManager.GenerateName(NamingTable.Property));
                }
            }
            else if (propertyDefinition.GetMethod != null && propertyDefinition.SetMethod != null)
            {
                //Both parts need to be private
                if (propertyDefinition.GetMethod.IsPrivate && propertyDefinition.SetMethod.IsPrivate)
                {
                    typeMapping.AddPropertyMapping(propertyDefinition, context.NameManager.GenerateName(NamingTable.Property));
                }
            }
            else if (propertyDefinition.GetMethod != null)
            {
                //Only the get is present - make sure it is private
                if (propertyDefinition.GetMethod.IsPrivate)
                {
                    typeMapping.AddPropertyMapping(propertyDefinition, context.NameManager.GenerateName(NamingTable.Property));
                }
            }
            else if (propertyDefinition.SetMethod != null)
            {
                //Only the set is present - make sure it is private
                if (propertyDefinition.SetMethod.IsPrivate)
                {
                    typeMapping.AddPropertyMapping(propertyDefinition, context.NameManager.GenerateName(NamingTable.Property));
                }
            }
        }