Пример #1
0
        internal object Invoke(object[] parameters)
        {
            var assemblyDef = new AssemblyDefUser("NoFuserExAssembly");

            Logger.Verbose($"Assembly created: {assemblyDef.Name}");
            var moduleDef = new ModuleDefUser("NoFuserExModule");

            Logger.Verbose($"Module created: {moduleDef.Name}");
            assemblyDef.Modules.Add(moduleDef);
            Logger.VeryVerbose("Module added to the assembly.");

            moduleDef.Types.Add(typeDef);
            Logger.VeryVerbose("Type injected to module.");

            foreach (var type in moduleDef.GetTypes())
            {
                foreach (var method in type.Methods)
                {
                    if (method.DeclaringType.Name != methodDef.DeclaringType.Name)
                    {
                        continue;
                    }
                    if (method.Name != methodDef.Name)
                    {
                        continue;
                    }
                    methodToInvoke = method.MDToken.ToInt32();
                    Logger.VeryVerbose($"Token found: {methodToInvoke}");
                }
            }

            if (methodToInvoke == 0)
            {
                Logger.Exception(new Exception("Error searching the token."));
            }

            using (var stream = new MemoryStream()) {
                assemblyDef.Write(stream, new ModuleWriterOptions {
                    Logger = DummyLogger.NoThrowInstance
                });
                Logger.VeryVerbose("Assembly writed.");

                var assembly = Assembly.Load(stream.ToArray());
                Logger.VeryVerbose("Created assembly loaded.");

                var module = assembly.ManifestModule;
                var method = module.ResolveMethod(methodToInvoke);
                Logger.VeryVerbose($"Method to invoke: {method.Name}");

                if (method.IsStatic)
                {
                    return(method.Invoke(null, parameters));
                }

                Logger.Verbose("Method is not static, creating instance...");
                var instance = Activator.CreateInstance(method.DeclaringType);
                return(method.Invoke(instance, parameters));
            }
        }
        public static void ExportMethodsToDll(string filename, List <MethodDef> nativeMethods, ModuleDefMD asmModule)
        {
            ModuleDef moduleDef = (ModuleDef) new ModuleDefUser((UTF8String)filename);

            moduleDef.Kind = ModuleKind.Dll;
            AssemblyDefUser assemblyDefUser = new AssemblyDefUser(new UTF8String("TestAssembly"), new Version(1, 2, 3, 4));

            assemblyDefUser.Modules.Add(moduleDef);
            moduleDef.Types.Add((TypeDef) new TypeDefUser((UTF8String)"Startup", (UTF8String)"MyType", moduleDef.CorLibTypes.Object.TypeDefOrRef));
            TypeDef typeDef = moduleDef.Types.FirstOrDefault <TypeDef>((Func <TypeDef, bool>)(m => m.Name == "MyType"));

            for (int i = 0; i < nativeMethods.Count; i++)
            {
                MethodDef     methodDef     = asmModule.GlobalType.Methods.FirstOrDefault <MethodDef>((Func <MethodDef, bool>)(m => m.Name == (string)nativeMethods[i].Name + "_IL"));
                MethodDefUser methodDefUser = new MethodDefUser(new UTF8String(Encoding.UTF8.GetBytes((string)nativeMethods[i].Name)), methodDef.MethodSig, methodDef.Attributes);
                methodDefUser.Body          = methodDef.Body;
                methodDefUser.DeclaringType = (TypeDef)null;
                methodDefUser.Name          = (UTF8String)Convert.ToBase64String(Encoding.UTF8.GetBytes((string)nativeMethods[i].Name));
                typeDef.Methods.Add((MethodDef)methodDefUser);
            }
            ModuleWriterOptions options = new ModuleWriterOptions();

            assemblyDefUser.Write(filename, options);
        }
        public static void ExportMethodsToDll(String filename, List <MethodDef> nativeMethods, ModuleDefMD asmModule)
        {
            // Create new module
            ModuleDef mod = new ModuleDefUser(filename);

            mod.Kind = ModuleKind.Dll;
            var newAsm = new AssemblyDefUser(new UTF8String("TestAssembly"), new Version(1, 2, 3, 4));

            newAsm.Modules.Add(mod);

            // Add some type
            mod.Types.Add(new TypeDefUser("Startup", "MyType", mod.CorLibTypes.Object.TypeDefOrRef));
            var currentType = mod.Types.FirstOrDefault(m => m.Name == "MyType");


            for (int i = 0; i < nativeMethods.Count; i++)
            {
                var ilMethod = asmModule.GlobalType.Methods.FirstOrDefault(m => m.Name == nativeMethods[i].Name + "_IL");

                // We do clone the method since we have to set the DeclaringType to zero. But we don't want to mess with the reference
                var clonedILMethod = new MethodDefUser(new UTF8String(Encoding.UTF8.GetBytes(nativeMethods[i].Name)), ilMethod.MethodSig, ilMethod.Attributes);

                clonedILMethod.Body          = ilMethod.Body;
                clonedILMethod.DeclaringType = null;
                clonedILMethod.Name          = Convert.ToBase64String(Encoding.UTF8.GetBytes(nativeMethods[i].Name));

                currentType.Methods.Add(clonedILMethod);
            }

            var testAssemblyWriterOptions = new ModuleWriterOptions();

            // Do we want to suppress errors ?
            // testAssemblyWriterOptions.Logger = DummyLogger.NoThrowInstance;

            newAsm.Write(filename, testAssemblyWriterOptions);
        }