private bool IsMethodContainsAttribute(MethodDefinition method)
 {
     var methodInfo = method.GetElementMethod();
     return method.HasCustomAttributes ||
         methodInfo.MethodReturnType.HasCustomAttributes ||
         methodInfo.MethodReturnType.HasFieldMarshal ||
         methodInfo.MethodReturnType.HasMarshalInfo;
 }
예제 #2
0
        /// <summary>
        /// Encrypts the strings within the given assembly.
        /// </summary>
        /// <param name="definition">The assembly definition.</param>
        private void EncryptStringsInAssembly(AssemblyDefinition definition)
        {
            //Add an encryption function
            MethodReference decryptionMethod = null;

            //Generate a new type for decryption
            OutputHelper.WriteLine("Generating global decrypt method");
            foreach (TypeDefinition td in definition.MainModule.GetAllTypes())
                if (td.Name == "<Module>")
                {
                    MethodDefinition md = new MethodDefinition("Decrypt", MethodAttributes.HideBySig | MethodAttributes.Static | MethodAttributes.CompilerControlled, definition.Import(typeof(string)));

                    //Generate the parameters
                    md.Parameters.Add(new ParameterDefinition("v", ParameterAttributes.None, definition.Import(typeof(string))));
                    md.Parameters.Add(new ParameterDefinition("s", ParameterAttributes.None, definition.Import(typeof(int))));

                    //Add it
                    td.Methods.Add(md);
                    //We now need to create a method body
                    md.Body = new MethodBody(md);

                    //Output the encryption method body
                    switch (method)
                    {
                        case StringEncryptionMethod.Xor:
                            GenerateXorDecryptionMethod(definition, md.Body);
                            break;
                        default:
                            throw new ArgumentOutOfRangeException();
                    }

                    //Finally get the reference
                    decryptionMethod = md.GetElementMethod();
                }

            //Loop through the modules
            OutputHelper.WriteLine("Processing modules");
            foreach (ModuleDefinition moduleDefinition in definition.Modules)
            {
                //Go through each type
                foreach (TypeDefinition typeDefinition in moduleDefinition.GetAllTypes())
                {
                    //Go through each method
                    foreach (MethodDefinition methodDefinition in typeDefinition.Methods)
                    {
                        if (methodDefinition.HasBody)
                        {
                            OutputHelper.WriteMethod(typeDefinition, methodDefinition);
                            ProcessInstructions(definition, methodDefinition.Body, decryptionMethod);
                        }
                    }
                }
            }
        }