当前位置: 首页>>代码示例>>C#>>正文


C# AssemblyDefinition.Import方法代码示例

本文整理汇总了C#中Mono.Cecil.AssemblyDefinition.Import方法的典型用法代码示例。如果您正苦于以下问题:C# AssemblyDefinition.Import方法的具体用法?C# AssemblyDefinition.Import怎么用?C# AssemblyDefinition.Import使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Mono.Cecil.AssemblyDefinition的用法示例。


在下文中一共展示了AssemblyDefinition.Import方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: EncryptStringsInAssembly

        /// <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);
                        }
                    }
                }
            }
        }
开发者ID:modulexcite,项目名称:ncloak,代码行数:58,代码来源:StringEncryptionTask.cs

示例2: BuildDecryptMethod

        /// <summary>
        /// Builds the decrypt method.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="assembly">The assembly.</param>
        /// <returns></returns>
        private static MethodDefinition BuildDecryptMethod(ICloakContext context, AssemblyDefinition assembly)
        {
            var byteArrayType = assembly.Import(typeof (byte[]));
#if USE_FRIENDLY_NAMING
            MethodDefinition method = new MethodDefinition("DecryptData",
                                                           MethodAttributes.Private | MethodAttributes.HideBySig |
                                                           MethodAttributes.Static, byteArrayType);
#else
            MethodDefinition method = new MethodDefinition(context.NameManager.GenerateName(NamingTable.Method),
                                                           MethodAttributes.Private | MethodAttributes.HideBySig |
                                                           MethodAttributes.Static, assembly.Import(typeof(byte[])));
#endif
            method.Body.InitLocals = true;
            method.Body.MaxStackSize = 5;
            method.Parameters.Add(new ParameterDefinition(byteArrayType));
            method.Parameters.Add(new ParameterDefinition(byteArrayType));
            method.Parameters.Add(new ParameterDefinition(byteArrayType));

            //Declare the locals - first four have quick reference
            method.AddLocal(assembly, typeof(Rijndael));
            method.AddLocal(assembly, typeof(ICryptoTransform));
            method.AddLocal(assembly, typeof(MemoryStream));
            method.AddLocal(assembly, typeof(CryptoStream));
            var paddedPlain = method.AddLocal(assembly, typeof(byte[]));
            var length = method.AddLocal(assembly, typeof(int));
            var plain = method.AddLocal(assembly, typeof(byte[]));
            var returnArray = method.AddLocal(assembly, typeof(byte[]));
            var inferredBool = method.AddLocal(assembly, typeof(bool));

            //Add the body
            var il = method.Body.GetILProcessor();

            //Inject anti reflector code
            InjectAntiReflectorCode(il, il.Create(OpCodes.Nop));

            //Start the body
            var rijndaelCreate = typeof(Rijndael).GetMethod("Create", Type.EmptyTypes);
            il.Append(il.Create(OpCodes.Call, assembly.Import(rijndaelCreate)));
            il.Append(OpCodes.Stloc_0);
            il.Append(OpCodes.Ldloc_0);
            il.Append(OpCodes.Ldc_I4_1);
            var symmetricAlgMode = typeof(SymmetricAlgorithm).GetProperty("Mode");
            il.Append(il.Create(OpCodes.Callvirt, assembly.Import(symmetricAlgMode.GetSetMethod())));
            il.Append(OpCodes.Nop);
            il.Append(OpCodes.Ldloc_0);
            il.Append(OpCodes.Ldarg_1);
            il.Append(OpCodes.Ldarg_2);
            var createDecryptor = typeof(SymmetricAlgorithm).GetMethod("CreateDecryptor",
                                                                        new[] { typeof(byte[]), typeof(byte[]) });
            il.Append(il.Create(OpCodes.Callvirt, assembly.Import(createDecryptor)));
            il.Append(OpCodes.Stloc_1);
            var startOfThirdTry = il.Create(OpCodes.Nop);
            il.Append(startOfThirdTry);
            il.Append(OpCodes.Ldarg_0);

            //New memory stream
            var memoryStreamCtor = typeof(MemoryStream).GetConstructor(new [] { typeof(byte[]) });
            il.Append(il.Create(OpCodes.Newobj, assembly.Import(memoryStreamCtor)));
            il.Append(OpCodes.Stloc_2);
            var startOfSecondTry = il.Create(OpCodes.Nop);
            il.Append(startOfSecondTry);
            il.Append(OpCodes.Ldloc_2);
            il.Append(OpCodes.Ldloc_1);
            il.Append(OpCodes.Ldc_I4_0);

            //New crypto stream
            var cryptoStreamCtor =
                typeof(CryptoStream).GetConstructor(new[]
                                                                     {
                                                                         typeof (Stream), typeof (ICryptoTransform),
                                                                         typeof (CryptoStreamMode)
                                                                     });
            il.Append(il.Create(OpCodes.Newobj, assembly.Import(cryptoStreamCtor)));
            il.Append(OpCodes.Stloc_3);
            var startOfFirstTry = il.Create(OpCodes.Nop);
            il.Append(startOfFirstTry);
            il.Append(OpCodes.Ldarg_0);
            il.Append(OpCodes.Ldlen);
            il.Append(OpCodes.Conv_I4);
            il.Append(il.Create(OpCodes.Newarr, assembly.Import(typeof(byte))));
            il.Append(il.Create(OpCodes.Stloc_S, paddedPlain));
            il.Append(OpCodes.Ldloc_3);
            il.Append(il.Create(OpCodes.Ldloc_S, paddedPlain));
            il.Append(OpCodes.Ldc_I4_0);
            il.Append(il.Create(OpCodes.Ldloc_S, paddedPlain));
            il.Append(OpCodes.Ldlen);
            il.Append(OpCodes.Conv_I4);

            //Get the read method and read into this byte array (full length)
            var read = typeof(Stream).GetMethod("Read", new[] { typeof(byte[]), typeof(int), typeof(int) });
            il.Append(il.Create(OpCodes.Callvirt, assembly.Import(read)));
            il.Append(il.Create(OpCodes.Stloc_S, length));
            il.Append(il.Create(OpCodes.Ldloc_S, length));
            il.Append(il.Create(OpCodes.Newarr, assembly.Import(typeof(byte))));
//.........这里部分代码省略.........
开发者ID:modulexcite,项目名称:ncloak,代码行数:101,代码来源:TamperProofTask.cs

示例3: BuildHashMethod

        /// <summary>
        /// Builds the hash method.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="assembly">The assembly.</param>
        /// <returns></returns>
        private static MethodDefinition BuildHashMethod(ICloakContext context, AssemblyDefinition assembly)
        {
#if USE_FRIENDLY_NAMING
            MethodDefinition method = new MethodDefinition("Hash",
                                               MethodAttributes.Private | MethodAttributes.HideBySig |
                                               MethodAttributes.Static, assembly.Import(typeof(string)));
#else
            MethodDefinition method = new MethodDefinition(context.NameManager.GenerateName(NamingTable.Method),
                                               MethodAttributes.Private | MethodAttributes.HideBySig |
                                               MethodAttributes.Static, assembly.Import(typeof(string)));
#endif
            method.Parameters.Add(new ParameterDefinition(assembly.Import(typeof(byte[]))));
            method.Body.InitLocals = true;
            method.Body.MaxStackSize = 2;
            method.AddLocal(assembly, typeof(SHA256));
            method.AddLocal(assembly, typeof(string));

            //Easy method to output
            var il = method.Body.GetILProcessor();

            //Inject some anti reflector stuff
            InjectAntiReflectorCode(il, il.Create(OpCodes.Nop));

            //Create the SHA256 object
            var sha256Create = typeof(SHA256).GetMethod("Create", Type.EmptyTypes);
            il.Append(il.Create(OpCodes.Call, assembly.Import(sha256Create)));
            il.Append(OpCodes.Stloc_0);
            il.Append(OpCodes.Ldloc_0);
            il.Append(OpCodes.Ldarg_0);
            //Call the compute method
            var compute = typeof(HashAlgorithm).GetMethod("ComputeHash", new[] { typeof(byte[]) });
            il.Append(il.Create(OpCodes.Callvirt, assembly.Import(compute)));
            //Finally, convert to base 64
            var toBase64 = typeof(Convert).GetMethod("ToBase64String", new[] { typeof(byte[]) });
            il.Append(il.Create(OpCodes.Call, assembly.Import(toBase64)));
            il.Append(OpCodes.Stloc_1);
            var retVar = il.Create(OpCodes.Ldloc_1);
            il.Append(il.Create(OpCodes.Br_S, retVar));
            il.Append(retVar);
            il.Append(OpCodes.Ret);
            return method;
        }
开发者ID:modulexcite,项目名称:ncloak,代码行数:48,代码来源:TamperProofTask.cs

示例4: BuildLoadTypeMethod

        /// <summary>
        /// Builds the load type method.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="assembly">The assembly.</param>
        /// <param name="loadAssembly">The load assembly.</param>
        /// <returns></returns>
        private static MethodDefinition BuildLoadTypeMethod(ICloakContext context, AssemblyDefinition assembly, MethodReference loadAssembly)
        {
#if USE_FRIENDLY_NAMING
            MethodDefinition method = new MethodDefinition("LoadType",
                       MethodAttributes.Private | MethodAttributes.HideBySig, assembly.Import(typeof(Type)));
#else
            MethodDefinition method = new MethodDefinition(context.NameManager.GenerateName(NamingTable.Method),
                                   MethodAttributes.Private | MethodAttributes.HideBySig, assembly.Import(typeof(Type)));
#endif

            //Declare the resource parameter
            method.Parameters.Add(new ParameterDefinition(assembly.Import(typeof(string))));
            method.Parameters.Add(new ParameterDefinition(assembly.Import(typeof(string))));
            
            method.Body.InitLocals = true;
            method.Body.MaxStackSize = 2;
            method.AddLocal(assembly, typeof(Assembly)); //Assembly
            method.AddLocal(assembly, typeof(Type)); //Temp variable for return type
            method.AddLocal(assembly, typeof(bool)); //Temp variable for comparison

            //Start with injection
            var il = method.Body.GetILProcessor();
            InjectAntiReflectorCode(il, il.Create(OpCodes.Nop));

            //Start the code
            il.Append(OpCodes.Ldarg_0);
            il.Append(OpCodes.Ldarg_1);
            il.Append(il.Create(OpCodes.Call, loadAssembly));
            il.Append(OpCodes.Stloc_0);
            il.Append(OpCodes.Ldloc_0);
            il.Append(OpCodes.Ldnull);
            il.Append(OpCodes.Ceq);
            il.Append(OpCodes.Ldc_I4_0);
            il.Append(OpCodes.Ceq);
            il.Append(OpCodes.Stloc_2);
            il.Append(OpCodes.Ldloc_2);

            //Get type
            var getType = il.Create(OpCodes.Ldloc_0);
            il.Append(il.Create(OpCodes.Brtrue_S, getType));
            il.Append(OpCodes.Ldnull);
            il.Append(OpCodes.Stloc_1);

            //Prepare to return the temp type
            var returnType = il.Create(OpCodes.Ldloc_1);

            //Jump to it
            il.Append(il.Create(OpCodes.Br_S, returnType));
            il.Append(getType);
            il.Append(OpCodes.Ldarg_2);
            var getTypeMethod = typeof (Assembly).GetMethod("GetType", new[] {typeof (string)});
            il.Append(il.Create(OpCodes.Callvirt, assembly.Import(getTypeMethod)));
            il.Append(OpCodes.Stloc_1);
            il.Append(il.Create(OpCodes.Br_S, returnType));
            il.Append(returnType);
            il.Append(OpCodes.Ret);
            return method;
        }
开发者ID:modulexcite,项目名称:ncloak,代码行数:65,代码来源:TamperProofTask.cs

示例5: BuildLoadAssemblyMethod

        /// <summary>
        /// Builds the load assembly method.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="assembly">The assembly.</param>
        /// <param name="loadedAssemblies">The loaded assemblies.</param>
        /// <param name="executingAssembly">The executing assembly.</param>
        /// <param name="decryptData">The decrypt data.</param>
        /// <param name="hashData">The hash data.</param>
        /// <param name="passwordKey">The password key.</param>
        /// <param name="salt">The salt.</param>
        /// <returns></returns>
        private static MethodDefinition BuildLoadAssemblyMethod(ICloakContext context, AssemblyDefinition assembly, FieldReference loadedAssemblies, FieldReference executingAssembly, MethodReference decryptData, MethodReference hashData, string passwordKey, Guid salt)
        {
#if USE_FRIENDLY_NAMING
            MethodDefinition method = new MethodDefinition("LoadAssembly",
                       MethodAttributes.Private | MethodAttributes.HideBySig, assembly.Import(typeof(Assembly)));
#else
            MethodDefinition method = new MethodDefinition(context.NameManager.GenerateName(NamingTable.Method),
                                   MethodAttributes.Private | MethodAttributes.HideBySig, assembly.Import(typeof(Assembly)));
#endif

            //Declare the resource parameter
            method.Parameters.Add(new ParameterDefinition(assembly.Import(typeof(string))));

            method.Body.InitLocals = true;
            method.Body.MaxStackSize = 4;
            method.AddLocal(assembly, typeof(string)); //Hash
            method.AddLocal(assembly, typeof(Stream)); //Stream for loading
            method.AddLocal(assembly, typeof(byte[])); //Hash data
            method.AddLocal(assembly, typeof(byte[])); //Data
            var password = method.AddLocal(assembly, typeof(Rfc2898DeriveBytes)); //Password
            var keyBytes = method.AddLocal(assembly, typeof(byte[])); //Key bytes
            var initVector = method.AddLocal(assembly, typeof(byte[])); //Init vector
            var rawAssembly = method.AddLocal(assembly, typeof(byte[])); //Assembly raw bytes
            var actualAssembly = method.AddLocal(assembly, typeof(Assembly)); //Actual Assembly
            var tempAssembly = method.AddLocal(assembly, typeof(Assembly)); //Temp Assembly
            var tempBool = method.AddLocal(assembly, typeof(bool)); //Temp bool

            //Build the body
            var il = method.Body.GetILProcessor();
            InjectAntiReflectorCode(il, il.Create(OpCodes.Nop));

#if VERBOSE_OUTPUT
            DebugLine(assembly, il, "Loading ", il.Create(OpCodes.Ldarg_1));
#endif

            //Check the cache first
            il.Append(OpCodes.Ldarg_0);
            il.Append(il.Create(OpCodes.Ldfld, loadedAssemblies));
            il.Append(OpCodes.Ldarg_1);
            var containsKey = typeof (Dictionary<string, Assembly>).GetMethod("ContainsKey");
            il.Append(il.Create(OpCodes.Callvirt, assembly.Import(containsKey)));
            il.Append(OpCodes.Ldc_I4_0);
            il.Append(OpCodes.Ceq);
            il.Append(il.Create(OpCodes.Stloc_S, tempBool));
            il.Append(il.Create(OpCodes.Ldloc_S, tempBool));
            //Jump if we don't have it, other wise return it
            var startFindType = il.Create(OpCodes.Ldarg_0);
            il.Append(il.Create(OpCodes.Brtrue_S, startFindType));
            
            //Otherwise return the cached assembly
            il.Append(OpCodes.Ldarg_0);
            il.Append(il.Create(OpCodes.Ldfld, loadedAssemblies));
            il.Append(OpCodes.Ldarg_1);
            //We are retrieving the name item
            var getItem = typeof (Dictionary<string, Assembly>).GetProperty("Item");
            il.Append(il.Create(OpCodes.Callvirt, assembly.Import(getItem.GetGetMethod())));
            //Store the variable in our return arg
            il.Append(il.Create(OpCodes.Stloc_S, tempAssembly));
            //Branch to our return routine
            var returnSequence = il.Create(OpCodes.Nop);
            il.Append(il.Create(OpCodes.Br, returnSequence));

            //Back to finding our type
            //Let's check the hash first
            il.Append(startFindType);
            il.Append(il.Create(OpCodes.Ldfld, executingAssembly));
            il.Append(OpCodes.Ldarg_1);
            il.Append(il.Create(OpCodes.Ldstr, ".v0"));
            //Concat the arg1 (resourceName) and v0
            var concat = assembly.Import(typeof (String).GetMethod("Concat", new[] {typeof (string), typeof (string)}));
            il.Append(il.Create(OpCodes.Call, concat));
            //Now get this from the manifest
            var getManifest = assembly.Import(typeof (Assembly).GetMethod("GetManifestResourceStream", new[] {typeof (string)}));
            il.Append(il.Create(OpCodes.Callvirt, getManifest));
            //Store the result in our temp local
            il.Append(OpCodes.Stloc_1);
            var try1Start = il.Create(OpCodes.Nop);
            il.Append(try1Start);
            //Make sure it's not null
            il.Append(OpCodes.Ldloc_1);
            il.Append(OpCodes.Ldnull);
            il.Append(OpCodes.Ceq);
            il.Append(OpCodes.Ldc_I4_0);
            il.Append(OpCodes.Ceq);
            il.Append(il.Create(OpCodes.Stloc_S, tempBool));
            il.Append(il.Create(OpCodes.Ldloc_S, tempBool));
            //Return null if it is
            var startDeclareArray = il.Create(OpCodes.Ldloc_1);
//.........这里部分代码省略.........
开发者ID:modulexcite,项目名称:ncloak,代码行数:101,代码来源:TamperProofTask.cs

示例6: BuildLoadAssembliesMethod

        /// <summary>
        /// Builds the load assemblies method.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="assembly">The assembly.</param>
        /// <param name="executingAssembly">The executing assembly.</param>
        /// <param name="loadAssembly">The load assembly.</param>
        /// <returns></returns>
        private static MethodDefinition BuildLoadAssembliesMethod(ICloakContext context, AssemblyDefinition assembly, FieldReference executingAssembly, MethodReference loadAssembly)
        {
#if USE_FRIENDLY_NAMING
            MethodDefinition method = new MethodDefinition("LoadAssemblies",
                                               MethodAttributes.Private | MethodAttributes.HideBySig, assembly.Import(typeof(void)));
#else
            MethodDefinition method = new MethodDefinition(context.NameManager.GenerateName(NamingTable.Method),
                                               MethodAttributes.Private | MethodAttributes.HideBySig, assembly.Import(typeof(void)));
#endif
            method.Body.InitLocals = true;
            method.Body.MaxStackSize = 2;
            method.AddLocal(assembly, typeof (string)); //Resource name
            method.AddLocal(assembly, typeof (string[])); //Foreach temp
            method.AddLocal(assembly, typeof (int)); //Loop counter
            method.AddLocal(assembly, typeof(bool));

            //Build the body
            var il = method.Body.GetILProcessor();
            InjectAntiReflectorCode(il, il.Create(OpCodes.Nop));
            il.Append(OpCodes.Nop);
            il.Append(OpCodes.Ldarg_0);
            il.Append(il.Create(OpCodes.Ldfld, executingAssembly));

            //Get the resources - foreach get's converted to a standard loop
            var resourcesMethod = typeof (Assembly).GetMethod("GetManifestResourceNames");
            il.Append(il.Create(OpCodes.Callvirt, assembly.Import(resourcesMethod)));
            il.Append(OpCodes.Stloc_1);

            //Initialise the loop counter
            il.Append(OpCodes.Ldc_I4_0);
            il.Append(OpCodes.Stloc_2);

            //For loop comparison (well create somewhere to jump to)
            var loopComparisonStart = il.Create(OpCodes.Ldloc_2);

            //Check the comparison now
            il.Append(il.Create(OpCodes.Br_S, loopComparisonStart));
            var startCode = il.Create(OpCodes.Ldloc_1);
            il.Append(startCode);
            il.Append(OpCodes.Ldloc_2);
            il.Append(OpCodes.Ldelem_Ref);
            il.Append(OpCodes.Stloc_0);
            il.Append(OpCodes.Nop);
            il.Append(OpCodes.Ldloc_0);

            //Make sure it doesn't end with .v0 or .e
            il.Append(il.Create(OpCodes.Ldstr, ".v0"));
            var endsWith = assembly.Import(typeof (String).GetMethod("EndsWith", new[] {typeof (string)}));
            il.Append(il.Create(OpCodes.Callvirt, endsWith));

            //Jump out as need be
            var load0 = il.Create(OpCodes.Ldc_I4_0);
            il.Append(il.Create(OpCodes.Brtrue_S, load0));
            il.Append(OpCodes.Ldloc_0);
            
            //Check the .e
            il.Append(il.Create(OpCodes.Ldstr, ".e"));
            il.Append(il.Create(OpCodes.Callvirt, endsWith));
            il.Append(OpCodes.Ldc_I4_0);
            il.Append(OpCodes.Ceq);
            var storeTempBool = il.Create(OpCodes.Stloc_3);
            il.Append(il.Create(OpCodes.Br_S, storeTempBool));
            il.Append(load0);
            il.Append(storeTempBool);
            il.Append(OpCodes.Ldloc_3);
            var loadArg0 = il.Create(OpCodes.Ldarg_0);
            il.Append(il.Create(OpCodes.Brtrue_S, loadArg0));
            var loadLoopCounter = il.Create(OpCodes.Ldloc_2);
            il.Append(il.Create(OpCodes.Br_S, loadLoopCounter));
            il.Append(loadArg0);
            il.Append(OpCodes.Ldloc_0);

            //Finally load the assembly
            il.Append(il.Create(OpCodes.Call, loadAssembly));
            il.Append(OpCodes.Pop);
            il.Append(OpCodes.Nop);
            
            //Loop counter
            il.Append(loadLoopCounter);
            il.Append(OpCodes.Ldc_I4_1);
            il.Append(OpCodes.Add);
            il.Append(OpCodes.Stloc_2);

            //Loop comparison
            il.Append(loopComparisonStart);
            il.Append(OpCodes.Ldloc_1);
            il.Append(OpCodes.Ldlen);
            il.Append(OpCodes.Conv_I4);
            il.Append(OpCodes.Clt);
            //Store the result
            il.Append(OpCodes.Stloc_3);
            //Load it and compare
//.........这里部分代码省略.........
开发者ID:modulexcite,项目名称:ncloak,代码行数:101,代码来源:TamperProofTask.cs

示例7: BuildMainMethod

        /// <summary>
        /// Builds the main method.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <param name="context">The context.</param>
        /// <param name="programType">Type of the program.</param>
        /// <param name="getExecutingAssembly">The get executing assembly.</param>
        /// <param name="currentDomain">The current domain.</param>
        /// <param name="eventHandler">The event handler.</param>
        /// <param name="assemblyResolve">The assembly resolve.</param>
        /// <param name="resolveMethod">The resolve method.</param>
        /// <param name="startMethod">The start method.</param>
        /// <returns></returns>
        private static MethodDefinition BuildMainMethod(AssemblyDefinition assembly, ICloakContext context, TypeDefinition programType, MethodReference getExecutingAssembly, MethodReference currentDomain, MethodReference eventHandler, MethodReference assemblyResolve, MethodReference resolveMethod, MethodReference startMethod)
        {
#if USE_FRIENDLY_NAMING
            MethodDefinition entryPoint =
                new MethodDefinition("Main",
                                     MethodAttributes.Private | MethodAttributes.Static |
                                     MethodAttributes.HideBySig, assembly.Import(typeof(void)));
#else
            MethodDefinition entryPoint =
                new MethodDefinition(context.NameManager.GenerateName(NamingTable.Method),
                                     MethodAttributes.Private | MethodAttributes.Static |
                                     MethodAttributes.HideBySig, assembly.Import(typeof(void)));
#endif
            entryPoint.Body.InitLocals = true;
            entryPoint.Body.MaxStackSize = 4;

#if USE_APPDOMAIN
            //Initialise some locals
            entryPoint.AddLocal(assembly, typeof(AppDomain));
            entryPoint.AddLocal(assembly, typeof(Assembly));
#endif
            VariableDefinition result = new VariableDefinition(programType);
            entryPoint.Body.Variables.Add(result);

            //Add the method
            assembly.EntryPoint = entryPoint;

            //Declare the il to build the code
            ILProcessor il = entryPoint.Body.GetILProcessor();

            //First of all add the anti reflector code
            InjectAntiReflectorCode(il, il.Create(OpCodes.Nop));

            //Now output the code - essentially:
            /*
            AppDomain domain = AppDomain.CreateDomain("App");
            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            ProgramRunner runner = (ProgramRunner)domain.CreateInstanceAndUnwrap(executingAssembly.FullName, "TestBootstrapper.ProgramRunner");
            AppDomain.CurrentDomain.AssemblyResolve += runner.ResolveAssembly;
            runner.Start();
             */
#if USE_APPDOMAIN
#if USE_FRIENDLY_NAMING
            il.Append(il.Create(OpCodes.Ldstr, "AppDomainName"));
#else
            il.Append(il.Create(OpCodes.Ldstr, context.NameManager.GenerateName(NamingTable.Type)));
#endif

            //Get the AppDomain::Create(string) method
            var appDomainCreate = assembly.Import(typeof(AppDomain).GetMethod("CreateDomain", new[] { typeof(string) }));
            il.Append(il.Create(OpCodes.Call, appDomainCreate));
            il.Append(OpCodes.Stloc_0);

            //Get the Assembly::GetExecutingAssembly() method
            il.Append(il.Create(OpCodes.Call, getExecutingAssembly));

            il.Append(OpCodes.Stloc_1);
            il.Append(OpCodes.Ldloc_0);
            il.Append(OpCodes.Ldloc_1);

            //Assembly::get_FullName()
            var getFullName = typeof(Assembly).GetProperty("FullName");
            il.Append(il.Create(OpCodes.Callvirt, assembly.Import(getFullName.GetGetMethod())));

            il.Append(il.Create(OpCodes.Ldstr,
                                String.Format("{0}.{1}", programType.Namespace, programType.Name)));

            //AppDomain::CreateInstanceAndUnwrap(string, string)
            var createInstanceAndUnwrap = typeof(AppDomain).GetMethod("CreateInstanceAndUnwrap",
                                                                       new[]
                                                                                       {
                                                                                           typeof (string), typeof (string)
                                                                                       });
            il.Append(il.Create(OpCodes.Callvirt, assembly.Import(createInstanceAndUnwrap)));
            il.Append(il.Create(OpCodes.Castclass, programType));
            il.Append(OpCodes.Stloc_2);

            //AppDomain::get_CurrentDomain
            il.Append(il.Create(OpCodes.Call, currentDomain));
            il.Append(OpCodes.Ldloc_2);

            //Get the function
            il.Append(il.Create(OpCodes.Ldftn, resolveMethod));
            il.Append(il.Create(OpCodes.Newobj, eventHandler));
            il.Append(il.Create(OpCodes.Callvirt, assemblyResolve));

            il.Append(OpCodes.Nop);
//.........这里部分代码省略.........
开发者ID:modulexcite,项目名称:ncloak,代码行数:101,代码来源:TamperProofTask.cs

示例8: BuildProgramConstructor

        /// <summary>
        /// Builds the program constructor.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <param name="programType">Type of the program.</param>
        /// <param name="assemblyLock">The assembly lock.</param>
        /// <param name="executingAssembly">The executing assembly.</param>
        /// <param name="loadedAssemblies">The loaded assemblies.</param>
        /// <param name="currentDomain">The current domain.</param>
        /// <param name="eventHandler">The event handler.</param>
        /// <param name="assemblyResolve">The assembly resolve.</param>
        /// <param name="getExecutingAssembly">The get executing assembly.</param>
        /// <param name="resolveMethod">The resolve method.</param>
        private static void BuildProgramConstructor(AssemblyDefinition assembly, TypeDefinition programType, FieldReference assemblyLock, FieldReference executingAssembly, FieldReference loadedAssemblies, MethodReference currentDomain, MethodReference eventHandler, MethodReference assemblyResolve, MethodReference getExecutingAssembly, MethodReference resolveMethod)
        {
            var body = assembly.CreateDefaultConstructor(programType);
            body.MaxStackSize = 4;
            body.InitLocals = true;
            body.Variables.Add(new VariableDefinition(assembly.Import(typeof(AppDomain))));
            var il = body.GetILProcessor();

            //Inject anti reflector code
            InjectAntiReflectorCode(il, il.Create(OpCodes.Ldarg_0));

            //define the assembly lock
            var objConstructor = assembly.Import(typeof(object).GetConstructor(Type.EmptyTypes));
            il.Append(il.Create(OpCodes.Newobj, objConstructor));
            il.Append(il.Create(OpCodes.Stfld, assemblyLock));
            il.Append(OpCodes.Ldarg_0);
            il.Append(il.Create(OpCodes.Call, objConstructor));
            il.Append(OpCodes.Nop);
            il.Append(OpCodes.Nop);
            il.Append(OpCodes.Ldarg_0);
            var cacheConstructor = assembly.Import(typeof(Dictionary<string, Assembly>).GetConstructor(Type.EmptyTypes));
            il.Append(il.Create(OpCodes.Newobj, cacheConstructor));
            il.Append(il.Create(OpCodes.Stfld, loadedAssemblies));
            il.Append(OpCodes.Ldarg_0);
            il.Append(il.Create(OpCodes.Call, getExecutingAssembly));
            il.Append(il.Create(OpCodes.Stfld, executingAssembly));
            il.Append(il.Create(OpCodes.Call, currentDomain));
            il.Append(OpCodes.Stloc_0);
            il.Append(OpCodes.Ldloc_0);
            il.Append(OpCodes.Ldarg_0);
            il.Append(il.Create(OpCodes.Ldftn, resolveMethod));
            il.Append(il.Create(OpCodes.Newobj, eventHandler));
            il.Append(il.Create(OpCodes.Callvirt, assemblyResolve));
            il.Append(OpCodes.Nop);
            il.Append(OpCodes.Nop);
            il.Append(OpCodes.Ret);
        }
开发者ID:modulexcite,项目名称:ncloak,代码行数:50,代码来源:TamperProofTask.cs

示例9: BuildBootstrapper

        /// <summary>
        /// Builds the main entry point to the bootstrapper.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="assembly">The assembly.</param>
        /// <param name="passwordKey">The password key.</param>
        /// <param name="salt">The salt.</param>
        private static void BuildBootstrapper(ICloakContext context, AssemblyDefinition assembly, string passwordKey, Guid salt)
        {
            //See http://blog.paul-mason.co.nz/2010/02/tamper-proofing-implementation-part-2.html

            //First create the actual program runner
#if USE_FRIENDLY_NAMING
            TypeDefinition programType = new TypeDefinition("ProgramRunner",
                                                        "NCloak.Bootstrapper.Internal",
                                                        TypeAttributes.NotPublic | TypeAttributes.Class |
                                                        TypeAttributes.AutoClass | TypeAttributes.AnsiClass |
                                                        TypeAttributes.Serializable | TypeAttributes.BeforeFieldInit,
                                                        assembly.Import(typeof(object)));

#else
            TypeDefinition programType = new TypeDefinition(context.NameManager.GenerateName(NamingTable.Type),
                                                        context.NameManager.GenerateName(NamingTable.Type),
                                                        TypeAttributes.NotPublic | TypeAttributes.Class |
                                                        TypeAttributes.AutoClass | TypeAttributes.AnsiClass |
                                                        TypeAttributes.Serializable | TypeAttributes.BeforeFieldInit,
                                                        assembly.Import(typeof(object)));
#endif
            assembly.MainModule.Types.Add(programType);

            //Add the class level fields
#if USE_FRIENDLY_NAMING
            string assembliesLoadedVariableName = "assembliesLoaded";
            string assemblyLockVariableName = "assemblyLock";
            string executingAssemblyVariableName = "executingAssembly";
            string loadedAssembliesVariableName = "loadedAssemblies";
#else
            string assembliesLoadedVariableName = context.NameManager.GenerateName(NamingTable.Field);
            string assemblyLockVariableName = context.NameManager.GenerateName(NamingTable.Field);
            string executingAssemblyVariableName = context.NameManager.GenerateName(NamingTable.Field);
            string loadedAssembliesVariableName = context.NameManager.GenerateName(NamingTable.Field);
#endif
            var assembliesLoaded = new FieldDefinition(assembliesLoadedVariableName, FieldAttributes.Private, assembly.Import(typeof(bool)));
            var assemblyLock = new FieldDefinition(assemblyLockVariableName, FieldAttributes.Private | FieldAttributes.InitOnly, assembly.Import(typeof(object)));
            var executingAssembly = new FieldDefinition(executingAssemblyVariableName, FieldAttributes.Private | FieldAttributes.InitOnly, assembly.Import(typeof(Assembly)));
            var loadedAssemblies = new FieldDefinition(loadedAssembliesVariableName,FieldAttributes.Private | FieldAttributes.InitOnly, assembly.Import(typeof(Dictionary<string, Assembly>)));
            programType.Fields.Add(assembliesLoaded);
            programType.Fields.Add(assemblyLock);
            programType.Fields.Add(executingAssembly);
            programType.Fields.Add(loadedAssemblies);

            //Get some method references we share
            MethodReference currentDomain = assembly.Import(typeof(AppDomain).GetProperty("CurrentDomain").GetGetMethod());
            MethodReference eventHandler = assembly.Import(typeof(ResolveEventHandler).GetConstructor(new[] { typeof(object), typeof(IntPtr) }));
            MethodReference assemblyResolve = assembly.Import(typeof(AppDomain).GetEvent("AssemblyResolve").GetAddMethod());
            MethodReference getExecutingAssembly = assembly.Import(typeof(Assembly).GetMethod("GetExecutingAssembly"));

            //Define decrypt data method
            var decryptMethod = BuildDecryptMethod(context, assembly);
            programType.Methods.Add(decryptMethod);

            //Define hash data method
            var hashMethod = BuildHashMethod(context, assembly);
            programType.Methods.Add(hashMethod);

            //Define load assembly
            var loadAssemblyMethod = BuildLoadAssemblyMethod(context, assembly, loadedAssemblies, executingAssembly, decryptMethod, hashMethod, passwordKey, salt);
            programType.Methods.Add(loadAssemblyMethod);

            //Define load type method
            var loadTypeMethod = BuildLoadTypeMethod(context, assembly, loadAssemblyMethod);
            programType.Methods.Add(loadTypeMethod);

            //Define load assemblies method
            var loadAssembliesMethod = BuildLoadAssembliesMethod(context, assembly, executingAssembly, loadAssemblyMethod);
            programType.Methods.Add(loadAssembliesMethod);

            //Define resolve method
            var resolveMethod = BuildResolveMethod(context, assembly, assemblyLock, assembliesLoaded, loadAssembliesMethod);
            programType.Methods.Add(resolveMethod);

            //Define start method
            var startMethod = BuildStartMethod(context, assembly, executingAssembly, assembliesLoaded, loadTypeMethod, loadAssembliesMethod);
            programType.Methods.Add(startMethod);

            //Now define a constructor
            BuildProgramConstructor(assembly, programType, assemblyLock, executingAssembly, loadedAssemblies, currentDomain, eventHandler, assemblyResolve, getExecutingAssembly, resolveMethod);

            //Now create a type to hold the entry point
#if USE_FRIENDLY_NAMING
            TypeDefinition entryType = new TypeDefinition("Program",
                                            "NCloak.Bootstrapper",
                                            TypeAttributes.NotPublic | TypeAttributes.Class |
                                            TypeAttributes.AutoClass | TypeAttributes.AnsiClass |
                                            TypeAttributes.BeforeFieldInit,
                                            assembly.Import(typeof(object)));
#else
            TypeDefinition entryType = new TypeDefinition(context.NameManager.GenerateName(NamingTable.Type),
                                                        context.NameManager.GenerateName(NamingTable.Type),
                                                        TypeAttributes.NotPublic | TypeAttributes.Class |
//.........这里部分代码省略.........
开发者ID:modulexcite,项目名称:ncloak,代码行数:101,代码来源:TamperProofTask.cs

示例10: DebugLine

 private static void DebugLine(AssemblyDefinition assembly, CilWorker il, string text, params Instruction[] appendInstructions)
 {
     il.Append(il.Create(OpCodes.Ldstr, text));
     //Append any instructions we need to
     if (appendInstructions.Length > 0)
     {
         var concat =
             assembly.Import(typeof (String).GetMethod("Concat", new[] {typeof (string), typeof (string)}));
         foreach (Instruction i in appendInstructions)
         {
             il.Append(il.Create(OpCodes.Ldstr, ","));
             il.Append(il.Create(OpCodes.Call, concat));
             il.Append(i);
             il.Append(il.Create(OpCodes.Call, concat));
         }
     }
     var writeLine = assembly.Import(typeof (Console).GetMethod("WriteLine", new[] {typeof (string)}));
     il.Append(il.Create(OpCodes.Call, writeLine));
 }
开发者ID:modulexcite,项目名称:ncloak,代码行数:19,代码来源:TamperProofTask.cs

示例11: BuildStartMethod

        /// <summary>
        /// Builds the start method.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="assembly">The assembly.</param>
        /// <param name="executingAssembly">The executing assembly.</param>
        /// <param name="assembliesLoaded">The assemblies loaded.</param>
        /// <param name="loadType">Type of the load.</param>
        /// <param name="loadAssemblies">The load assemblies.</param>
        /// <returns></returns>
        private static MethodDefinition BuildStartMethod(ICloakContext context, AssemblyDefinition assembly, FieldReference executingAssembly, FieldReference assembliesLoaded, MethodReference loadType, MethodReference loadAssemblies)
        {
#if USE_FRIENDLY_NAMING
            MethodDefinition method = new MethodDefinition("Start",
                       MethodAttributes.Public | MethodAttributes.HideBySig, assembly.Import(typeof(void)));
#else
            MethodDefinition method = new MethodDefinition(context.NameManager.GenerateName(NamingTable.Method),
                                   MethodAttributes.Public | MethodAttributes.HideBySig, assembly.Import(typeof(void)));
#endif

            method.Body.InitLocals = true;
            method.Body.MaxStackSize = 3;
            method.AddLocal(assembly, typeof(string));   //entryAssemblyResource
            method.AddLocal(assembly, typeof(string));   //entryType
            method.AddLocal(assembly, typeof(string));   //entryMethod
            method.AddLocal(assembly, typeof(string));   //resourceName
            var s = method.AddLocal(assembly, typeof(Stream));   //s
            var sr = method.AddLocal(assembly, typeof(StreamReader));   //sr
            var type = method.AddLocal(assembly, typeof(Type));   //type
            var methodInfo = method.AddLocal(assembly, typeof(MethodInfo));   //method
            var tempStringArray = method.AddLocal(assembly, typeof (string[]));
            var tempInt = method.AddLocal(assembly, typeof(int));
            var tempBool = method.AddLocal(assembly, typeof(bool));

            //Get the il builder
            var il = method.Body.GetILProcessor();

            //Inject the anti reflector code
            InjectAntiReflectorCode(il, il.Create(OpCodes.Nop));

            //Start it off - declare some variables
            il.Append(OpCodes.Ldnull);
            il.Append(OpCodes.Stloc_0);
            il.Append(OpCodes.Ldnull);
            il.Append(OpCodes.Stloc_1);
            il.Append(OpCodes.Ldnull);
            il.Append(OpCodes.Stloc_2);
            il.Append(OpCodes.Nop);
            il.Append(OpCodes.Ldarg_0);

            //We need to find the entry point so start a loop to find
            il.Append(il.Create(OpCodes.Ldfld, executingAssembly));
            var getManifestResourceNames = assembly.Import(typeof (Assembly).GetMethod("GetManifestResourceNames"));
            il.Append(il.Create(OpCodes.Callvirt, getManifestResourceNames));
            il.Append(il.Create(OpCodes.Stloc_S, tempStringArray));

            //Init the loop counter
            il.Append(OpCodes.Ldc_I4_0);
            il.Append(il.Create(OpCodes.Stloc_S, tempInt));
            //Jump to the loop comparison
            var startLoopComparison = il.Create(OpCodes.Ldloc_S, tempInt);
            il.Append(il.Create(OpCodes.Br, startLoopComparison));

            //Load the current item in a temp variable
            var getTempVar = il.Create(OpCodes.Ldloc_S, tempStringArray);
            il.Append(getTempVar);
            il.Append(il.Create(OpCodes.Ldloc_S, tempInt));
            il.Append(OpCodes.Ldelem_Ref);
            il.Append(OpCodes.Stloc_3);

            //Now start the actual body
            il.Append(OpCodes.Nop);
            il.Append(OpCodes.Ldloc_3);
            il.Append(il.Create(OpCodes.Ldstr, ".e"));
            var endsWith = assembly.Import(typeof (String).GetMethod("EndsWith", new[] {typeof (string)}));
            il.Append(il.Create(OpCodes.Callvirt, endsWith));
            il.Append(OpCodes.Ldc_I4_0); 
            il.Append(OpCodes.Ceq);
            il.Append(il.Create(OpCodes.Stloc_S, tempBool));
            il.Append(il.Create(OpCodes.Ldloc_S, tempBool));

            //Branch to increment statement if not true (i.e. equal to 0/false)
            var startIncrement = il.Create(OpCodes.Nop);
            il.Append(il.Create(OpCodes.Brtrue, startIncrement));
            //Get the stream
            il.Append(OpCodes.Nop);
            il.Append(OpCodes.Ldarg_0);
            il.Append(il.Create(OpCodes.Ldfld, executingAssembly));
            il.Append(OpCodes.Ldloc_3);
            var getManifestResourceStream =
                assembly.Import(typeof (Assembly).GetMethod("GetManifestResourceStream", new[] {typeof (string)}));
            il.Append(il.Create(OpCodes.Callvirt, getManifestResourceStream));
            il.Append(il.Create(OpCodes.Stloc_S, s));
            //Make sure it's not null
            il.Append(il.Create(OpCodes.Ldloc_S, s));
            il.Append(OpCodes.Ldnull);
            il.Append(OpCodes.Ceq);
            il.Append(OpCodes.Ldc_I4_0);
            il.Append(OpCodes.Ceq);
            il.Append(il.Create(OpCodes.Stloc_S, tempBool));
//.........这里部分代码省略.........
开发者ID:modulexcite,项目名称:ncloak,代码行数:101,代码来源:TamperProofTask.cs

示例12: BuildResolveMethod

        /// <summary>
        /// Builds the resolve method.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="assembly">The definition.</param>
        /// <param name="assemblyLock">The assembly lock.</param>
        /// <param name="assembliesLoaded">The assemblies loaded.</param>
        /// <param name="loadAssemblies">The load assemblies.</param>
        /// <returns></returns>
        private static MethodDefinition BuildResolveMethod(ICloakContext context, AssemblyDefinition assembly, FieldReference assemblyLock, FieldReference assembliesLoaded, MethodReference loadAssemblies)
        {
#if USE_FRIENDLY_NAMING
            MethodDefinition method = new MethodDefinition("ResolveAssembly",
                                   MethodAttributes.Public | MethodAttributes.HideBySig, assembly.Import(typeof(Assembly)));
#else
            MethodDefinition method = new MethodDefinition(context.NameManager.GenerateName(NamingTable.Method),
                                   MethodAttributes.Public | MethodAttributes.HideBySig, assembly.Import(typeof(Assembly)));
#endif

            //Declare the resource parameter
            method.Parameters.Add(new ParameterDefinition("sender", Mono.Cecil.ParameterAttributes.None, assembly.Import(typeof(object))));
            method.Parameters.Add(new ParameterDefinition(assembly.Import(typeof(ResolveEventArgs))));
            
            method.Body.InitLocals = true;
            method.Body.MaxStackSize = 2;
            method.AddLocal(assembly, typeof(Assembly[])); //currentAssemblies
            method.AddLocal(assembly, typeof(Assembly));   //a
            method.AddLocal(assembly, typeof(Assembly));   //temp assembly
            method.AddLocal(assembly, typeof (object));    //temp object
            var tempBool = method.AddLocal(assembly, typeof (bool)); //temp bool
            var tempAssemblyArray = method.AddLocal(assembly, typeof(Assembly[])); //temp assembly array
            var tempInt = method.AddLocal(assembly, typeof (int)); //temp int
        
            //Get the il builder
            var il = method.Body.GetILProcessor();

            //Inject the anti reflector code
            InjectAntiReflectorCode(il, il.Create(OpCodes.Nop));
#if VERBOSE_OUTPUT
            DebugLine(assembly, il, "Resolving");
#endif

            //Start a lock
            il.Append(OpCodes.Ldarg_0);
            il.Append(il.Create(OpCodes.Ldfld, assemblyLock));
            il.Append(OpCodes.Dup);
            il.Append(OpCodes.Stloc_3);
            var monitorEnter = assembly.Import(typeof (Monitor).GetMethod("Enter", new[] {typeof (object)}));
            il.Append(il.Create(OpCodes.Call, monitorEnter));
            il.Append(OpCodes.Nop);
            var tryStart = il.Create(OpCodes.Nop);
            il.Append(tryStart);
            //Check if the assemblies are loaded
            il.Append(OpCodes.Ldarg_0);
            il.Append(il.Create(OpCodes.Ldfld, assembliesLoaded));
            il.Append(il.Create(OpCodes.Stloc_S, tempBool));
            il.Append(il.Create(OpCodes.Ldloc_S, tempBool));

            //If it is set, skip to searching the current appdomain
            var startSearchingCurrentAppDomain = il.Create(OpCodes.Nop);
            il.Append(il.Create(OpCodes.Brtrue_S, startSearchingCurrentAppDomain));
            il.Append(OpCodes.Nop);
            il.Append(OpCodes.Ldarg_0);
            il.Append(OpCodes.Ldc_I4_1);
            il.Append(il.Create(OpCodes.Stfld, assembliesLoaded));
            il.Append(OpCodes.Ldarg_0);
            il.Append(il.Create(OpCodes.Call, loadAssemblies));
            il.Append(OpCodes.Nop);
            il.Append(OpCodes.Nop);
            il.Append(startSearchingCurrentAppDomain);
            //After the finally
            var afterFinally = il.Create(OpCodes.Nop);
            //Exit the finally
            il.Append(il.Create(OpCodes.Leave_S, afterFinally));
            //We're in the finally now
            var finallyStart = il.Create(OpCodes.Ldloc_3);
            il.Append(finallyStart);
            var monitorExit = assembly.Import(typeof (Monitor).GetMethod("Exit", new[] {typeof (object)}));
            il.Append(il.Create(OpCodes.Call, monitorExit));
            il.Append(OpCodes.Nop);
            il.Append(OpCodes.Endfinally);
            il.Append(afterFinally);

            //Get the current domains assemblies
            var currentDomain = assembly.Import(typeof (AppDomain).GetProperty("CurrentDomain").GetGetMethod());
            il.Append(il.Create(OpCodes.Call, currentDomain));
            var getAssemblies = assembly.Import(typeof (AppDomain).GetMethod("GetAssemblies"));
            il.Append(il.Create(OpCodes.Callvirt, getAssemblies));
            il.Append(OpCodes.Stloc_0);
            il.Append(OpCodes.Nop);

            //Lets start a loop in lieu of foreach
            il.Append(OpCodes.Ldloc_0);
            il.Append(il.Create(OpCodes.Stloc_S, tempAssemblyArray));
            il.Append(OpCodes.Ldc_I4_0);
            il.Append(il.Create(OpCodes.Stloc_S, tempInt));

            //Loop Condition
            var loopCondition = il.Create(OpCodes.Ldloc_S, tempInt);
            il.Append(il.Create(OpCodes.Br_S, loopCondition));
//.........这里部分代码省略.........
开发者ID:modulexcite,项目名称:ncloak,代码行数:101,代码来源:TamperProofTask.cs

示例13: GenerateXorDecryptionMethod

        /// <summary>
        /// Generates the xor decryption method.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <param name="body">The body.</param>
        private static void GenerateXorDecryptionMethod(AssemblyDefinition assembly, MethodBody body)
        {
            var worker = body.GetILProcessor();

            //Generate the decryption method
            //Since this is XOR it is the same as the encryption method
            //In reality its a bit of a joke calling this encryption as its really
            //just obfusaction
            /*
                char[] characters = value.ToCharArray();
                for (int i = 0; i < characters.Length; i++)
                {
                    characters[i] = (char)(characters[i] ^ salt);
                }
                return new String(characters);
             */
            
            //Declare a local to store the char array
            body.InitLocals = true;
            body.Method.AddLocal(typeof(char[]));
            body.Method.AddLocal(typeof(int));
            body.Method.AddLocal(typeof(string));
            body.Method.AddLocal(typeof(bool));
            
            //Start with a nop
            worker.Append(worker.Create(OpCodes.Nop));

            //Load the first argument into the register
            Instruction ldArg0 = worker.Create(OpCodes.Ldarg_0);
            worker.Append(ldArg0);

            //Call ToCharArray on this -- need to find it first
            var toCharArrayMethodRef = assembly.Import(typeof(string).GetMethod("ToCharArray", Type.EmptyTypes));
            //Import the method first
            toCharArrayMethodRef = body.ImportMethod(toCharArrayMethodRef);
            Instruction toCharArray = worker.Create(OpCodes.Callvirt, toCharArrayMethodRef);
            worker.Append(toCharArray);
            //Store it in the first local
            Instruction stLoc0 = worker.Create(OpCodes.Stloc_0);
            worker.Append(stLoc0);

            //Set up the loop
            worker.Append(worker.Create(OpCodes.Ldc_I4_0));
            Instruction stLoc1 = worker.Create(OpCodes.Stloc_1);
            worker.Append(stLoc1);
            
            //We'll insert a br.s here later....

            //Insert another nop and do the rest of our loop
            Instruction loopNop = worker.Create(OpCodes.Nop);
            worker.Append(loopNop);
            worker.Append(worker.Create(OpCodes.Ldloc_0));
            worker.Append(worker.Create(OpCodes.Ldloc_1));
            worker.Append(worker.Create(OpCodes.Ldloc_0));
            worker.Append(worker.Create(OpCodes.Ldloc_1));
            worker.Append(worker.Create(OpCodes.Ldelem_U2)); //Load the array
            worker.Append(worker.Create(OpCodes.Ldarg_1));
            worker.Append(worker.Create(OpCodes.Xor)); //Do the xor
            worker.Append(worker.Create(OpCodes.Conv_U2));
            worker.Append(worker.Create(OpCodes.Stelem_I2)); //Store back in the array
            worker.Append(worker.Create(OpCodes.Nop));
            worker.Append(worker.Create(OpCodes.Ldloc_1));
            worker.Append(worker.Create(OpCodes.Ldc_I4_1));
            worker.Append(worker.Create(OpCodes.Add));
            worker.Append(worker.Create(OpCodes.Stloc_1));
            Instruction ldLoc = worker.Create(OpCodes.Ldloc_1);
            worker.Append(ldLoc);
            //Link to this line from an earlier statement
            worker.InsertAfter(stLoc1, worker.Create(OpCodes.Br_S, ldLoc));
            worker.Append(worker.Create(OpCodes.Ldloc_0));
            worker.Append(worker.Create(OpCodes.Ldlen));
            worker.Append(worker.Create(OpCodes.Conv_I4));
            worker.Append(worker.Create(OpCodes.Clt)); //i < array.Length
            worker.Append(worker.Create(OpCodes.Stloc_3));
            worker.Append(worker.Create(OpCodes.Ldloc_3));
            worker.Append(worker.Create(OpCodes.Brtrue_S, loopNop)); //Do the loop

            //Return a new string
            worker.Append(worker.Create(OpCodes.Ldloc_0));
            //Find the constructor we want to use
            MethodReference constructor = assembly.Import(typeof(string).GetConstructor(new [] { typeof(char[])}));
            constructor = body.ImportMethod(constructor);
            worker.Append(worker.Create(OpCodes.Newobj, constructor));
            Instruction stloc2 = worker.Create(OpCodes.Stloc_2);
            worker.Append(stloc2);
            Instruction ldloc2 = worker.Create(OpCodes.Ldloc_2);
            worker.Append(ldloc2);
            worker.InsertAfter(stloc2, worker.Create(OpCodes.Br_S, ldloc2));
            worker.Append(worker.Create(OpCodes.Ret));
        }
开发者ID:modulexcite,项目名称:ncloak,代码行数:95,代码来源:StringEncryptionTask.cs


注:本文中的Mono.Cecil.AssemblyDefinition.Import方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。