예제 #1
0
        static void Main(string[] args)
        {
            var definition = AssemblyDefinition.ReadAssembly(@"E:\git\HookDispatch\DebugPlugin\bin\Release\DebugPlugin.dll");

            var module = definition.MainModule;

            foreach (var type_definition in module.Types)
            {
                if (type_definition.Namespace == "Oxide.Plugins" && type_definition.Name == "DebugPlugin")
                {
                    var instance = new DirectCallMethod(module, type_definition);

                    AssemblyDefinition final_definition = null;

                    byte[] patched_assembly;
                    using (var stream = new MemoryStream())
                    {
                        definition.Write(stream);
                        patched_assembly = stream.ToArray();
                        stream.Position  = 0;
                        final_definition = AssemblyDefinition.ReadAssembly(stream);
                    }

                    var assembly = Assembly.Load(patched_assembly);

                    var type = assembly.GetType("Oxide.Plugins.DebugPlugin");
                    if (type == null)
                    {
                        Puts("Unable to find main plugin class");
                        return;
                    }

                    var plugin = Activator.CreateInstance(type) as Plugin;

                    try
                    {
                        RunBenchmark(plugin);
                    }
                    catch (Exception ex)
                    {
                        Puts(ex);
                    }

                    Puts("\nPress any key to exit...");
                    Console.ReadKey();
                }
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            var definition = AssemblyDefinition.ReadAssembly(@"E:\git\HookDispatch\DebugPlugin\bin\Release\DebugPlugin.dll");

            var module = definition.MainModule;
            foreach (var type_definition in module.Types)
            {
                if (type_definition.Namespace == "Oxide.Plugins" && type_definition.Name == "DebugPlugin")
                {
                    var instance = new DirectCallMethod(module, type_definition);

                    AssemblyDefinition final_definition = null;

                    byte[] patched_assembly;
                    using (var stream = new MemoryStream())
                    {
                        definition.Write(stream);
                        patched_assembly = stream.ToArray();
                        stream.Position = 0;
                        final_definition = AssemblyDefinition.ReadAssembly(stream);
                    }

                    var assembly = Assembly.Load(patched_assembly);

                    var type = assembly.GetType("Oxide.Plugins.DebugPlugin");
                    if (type == null)
                    {
                        Puts("Unable to find main plugin class");
                        return;
                    }

                    var plugin = Activator.CreateInstance(type) as Plugin;

                    try
                    {
                        RunBenchmark(plugin);
                    }
                    catch (Exception ex)
                    {
                        Puts(ex);
                    }

                    Puts("\nPress any key to exit...");
                    Console.ReadKey();
                }
            }
        }
예제 #3
0
        private void PatchAssembly(AssemblyDefinition assemblyDefinition)
        {
            try
            {
                DllLoader.LogDebug($"PatchAssembly({assemblyDefinition})");
                foreach (var type in assemblyDefinition.MainModule.Types)
                {
                    var pluginType     = false;
                    var typeDefinition = type.BaseType?.Resolve();

                    while (typeDefinition != null)
                    {
                        if (typeDefinition.FullName == "Oxide.Plugins.CSharpPlugin")
                        {
                            pluginType = true;
                            break;
                        }

                        typeDefinition = typeDefinition.BaseType?.Resolve();
                    }

                    if (!pluginType)
                    {
                        continue;
                    }

                    _ = new DirectCallMethod(assemblyDefinition.MainModule, type);
                }

                DllLoader.LogDebug("Patch Completed!");
            }
            catch (Exception ex)
            {
                Interface.Oxide.LogException($"Fail to PatchAssembly: {assemblyDefinition}", ex);
                throw;
            }
        }
        private void PatchAssembly(Action <byte[]> callback)
        {
            Action action1 = null;

            if (this.isPatching)
            {
                Interface.Oxide.LogWarning("Already patching plugin assembly: {0} (ignoring)", new object[] { this.PluginNames.ToSentence <string>() });
                return;
            }
            float now = Interface.Oxide.Now;

            this.isPatching = true;
            ThreadPool.QueueUserWorkItem((object _) => {
                AssemblyDefinition assemblyDefinition;
                try
                {
                    using (MemoryStream memoryStream = new MemoryStream(this.RawAssembly))
                    {
                        assemblyDefinition = AssemblyDefinition.ReadAssembly(memoryStream);
                    }
                    ConstructorInfo constructor      = typeof(UnauthorizedAccessException).GetConstructor(new Type[] { typeof(string) });
                    MethodReference methodReference1 = assemblyDefinition.MainModule.Import(constructor);
                    Action <TypeDefinition> methods  = null;
                    methods = (TypeDefinition type) => {
                        string fullName;
                        string str;
                        foreach (MethodDefinition method in type.Methods)
                        {
                            bool flag = false;
                            if (method.Body != null)
                            {
                                bool flag1 = false;
                                foreach (VariableDefinition variable in method.Body.Variables)
                                {
                                    if (!CompiledAssembly.IsNamespaceBlacklisted(variable.VariableType.FullName))
                                    {
                                        continue;
                                    }
                                    Mono.Cecil.Cil.MethodBody methodBody = new Mono.Cecil.Cil.MethodBody(method);
                                    methodBody.Instructions.Add(Instruction.Create(OpCodes.Ldstr, string.Concat("System access is restricted, you are not allowed to use ", variable.VariableType.FullName)));
                                    methodBody.Instructions.Add(Instruction.Create(OpCodes.Newobj, methodReference1));
                                    methodBody.Instructions.Add(Instruction.Create(OpCodes.Throw));
                                    method.Body = methodBody;
                                    flag1       = true;
                                    break;
                                }
                                if (flag1)
                                {
                                    continue;
                                }
                                Collection <Instruction> instructions = method.Body.Instructions;
                                ILProcessor lProcessor  = method.Body.GetILProcessor();
                                Instruction instruction = instructions.First <Instruction>();
                                for (int i = 0; i < instructions.Count && !flag; i++)
                                {
                                    Instruction item = instructions[i];
                                    if (item.OpCode == OpCodes.Ldtoken)
                                    {
                                        IMetadataTokenProvider operand = item.Operand as IMetadataTokenProvider;
                                        str         = (operand != null ? operand.ToString() : null);
                                        string str1 = str;
                                        if (CompiledAssembly.IsNamespaceBlacklisted(str1))
                                        {
                                            lProcessor.InsertBefore(instruction, Instruction.Create(OpCodes.Ldstr, string.Concat("System access is restricted, you are not allowed to use ", str1)));
                                            lProcessor.InsertBefore(instruction, Instruction.Create(OpCodes.Newobj, methodReference1));
                                            lProcessor.InsertBefore(instruction, Instruction.Create(OpCodes.Throw));
                                            flag = true;
                                        }
                                    }
                                    else if (item.OpCode == OpCodes.Call || item.OpCode == OpCodes.Calli || item.OpCode == OpCodes.Callvirt || item.OpCode == OpCodes.Ldftn)
                                    {
                                        MethodReference methodReference = item.Operand as MethodReference;
                                        string str2 = (methodReference != null ? methodReference.DeclaringType.FullName : null);
                                        if (str2 == "System.Type" && methodReference.Name == "GetType" || CompiledAssembly.IsNamespaceBlacklisted(str2))
                                        {
                                            lProcessor.InsertBefore(instruction, Instruction.Create(OpCodes.Ldstr, string.Concat("System access is restricted, you are not allowed to use ", str2)));
                                            lProcessor.InsertBefore(instruction, Instruction.Create(OpCodes.Newobj, methodReference1));
                                            lProcessor.InsertBefore(instruction, Instruction.Create(OpCodes.Throw));
                                            flag = true;
                                        }
                                    }
                                    else if (item.OpCode == OpCodes.Ldfld)
                                    {
                                        FieldReference fieldReference = item.Operand as FieldReference;
                                        fullName    = (fieldReference != null ? fieldReference.FieldType.FullName : null);
                                        string str3 = fullName;
                                        if (CompiledAssembly.IsNamespaceBlacklisted(str3))
                                        {
                                            lProcessor.InsertBefore(instruction, Instruction.Create(OpCodes.Ldstr, string.Concat("System access is restricted, you are not allowed to use ", str3)));
                                            lProcessor.InsertBefore(instruction, Instruction.Create(OpCodes.Newobj, methodReference1));
                                            lProcessor.InsertBefore(instruction, Instruction.Create(OpCodes.Throw));
                                            flag = true;
                                        }
                                    }
                                }
                            }
                            else if (method.HasPInvokeInfo)
                            {
                                MethodDefinition attributes           = method;
                                attributes.Attributes                 = attributes.Attributes & (Mono.Cecil.MethodAttributes.MemberAccessMask | Mono.Cecil.MethodAttributes.Private | Mono.Cecil.MethodAttributes.FamANDAssem | Mono.Cecil.MethodAttributes.Assembly | Mono.Cecil.MethodAttributes.Family | Mono.Cecil.MethodAttributes.FamORAssem | Mono.Cecil.MethodAttributes.Public | Mono.Cecil.MethodAttributes.Static | Mono.Cecil.MethodAttributes.Final | Mono.Cecil.MethodAttributes.Virtual | Mono.Cecil.MethodAttributes.HideBySig | Mono.Cecil.MethodAttributes.VtableLayoutMask | Mono.Cecil.MethodAttributes.NewSlot | Mono.Cecil.MethodAttributes.CheckAccessOnOverride | Mono.Cecil.MethodAttributes.Abstract | Mono.Cecil.MethodAttributes.SpecialName | Mono.Cecil.MethodAttributes.UnmanagedExport | Mono.Cecil.MethodAttributes.RTSpecialName | Mono.Cecil.MethodAttributes.HasSecurity | Mono.Cecil.MethodAttributes.RequireSecObject);
                                Mono.Cecil.Cil.MethodBody methodBody1 = new Mono.Cecil.Cil.MethodBody(method);
                                methodBody1.Instructions.Add(Instruction.Create(OpCodes.Ldstr, "PInvoke access is restricted, you are not allowed to use PInvoke"));
                                methodBody1.Instructions.Add(Instruction.Create(OpCodes.Newobj, methodReference1));
                                methodBody1.Instructions.Add(Instruction.Create(OpCodes.Throw));
                                method.Body = methodBody1;
                            }
                            if (!flag)
                            {
                                continue;
                            }
                            Mono.Cecil.Cil.MethodBody body = method.Body;
                            if (body != null)
                            {
                                body.OptimizeMacros();
                            }
                            else
                            {
                            }
                        }
                        foreach (TypeDefinition nestedType in type.NestedTypes)
                        {
                            methods(nestedType);
                        }
                    };
                    foreach (TypeDefinition typeDefinition in assemblyDefinition.MainModule.Types)
                    {
                        methods(typeDefinition);
                        if (this.IsCompilerGenerated(typeDefinition))
                        {
                            continue;
                        }
                        if (typeDefinition.Namespace != "Oxide.Plugins")
                        {
                            if (!(typeDefinition.FullName != "<Module>") || this.PluginNames.Any <string>((string plugin) => typeDefinition.FullName.StartsWith(string.Concat("Oxide.Plugins.", plugin))))
                            {
                                continue;
                            }
                            Interface.Oxide.LogWarning(((int)this.PluginNames.Length == 1 ? string.Concat(this.PluginNames[0], " has polluted the global namespace by defining ", typeDefinition.FullName) : string.Concat("A plugin has polluted the global namespace by defining ", typeDefinition.FullName)), Array.Empty <object>());
                        }
                        else if (!this.PluginNames.Contains <string>(typeDefinition.Name))
                        {
                            Interface.Oxide.LogWarning(((int)this.PluginNames.Length == 1 ? string.Concat(this.PluginNames[0], " has polluted the global namespace by defining ", typeDefinition.Name) : string.Concat("A plugin has polluted the global namespace by defining ", typeDefinition.Name)), Array.Empty <object>());
                        }
                        else
                        {
                            Collection <MethodDefinition> methodDefinitions = typeDefinition.Methods;
                            Func <MethodDefinition, bool> u003cu003e9_204   = CompiledAssembly.< > c.< > 9__20_4;
                            if (u003cu003e9_204 == null)
                            {
                                u003cu003e9_204 = (MethodDefinition m) => {
                                    if (m.IsStatic || !m.IsConstructor || m.HasParameters)
                                    {
                                        return(false);
                                    }
                                    return(!m.IsPublic);
                                };
                                CompiledAssembly.< > c.< > 9__20_4 = u003cu003e9_204;
                            }
                            if (methodDefinitions.FirstOrDefault <MethodDefinition>(u003cu003e9_204) == null)
                            {
                                DirectCallMethod directCallMethod = new DirectCallMethod(assemblyDefinition.MainModule, typeDefinition);
                            }
                            else
                            {
                                CompilablePlugin compilablePlugin = this.CompilablePlugins.SingleOrDefault <CompilablePlugin>((CompilablePlugin p) => p.Name == typeDefinition.Name);
                                if (compilablePlugin == null)
                                {
                                    continue;
                                }
                                compilablePlugin.CompilerErrors = "Primary constructor in main class must be public";
                            }
                        }
                    }
                    foreach (TypeDefinition typeDefinition1 in assemblyDefinition.MainModule.Types)
                    {
                        if (typeDefinition1.Namespace != "Oxide.Plugins" || !this.PluginNames.Contains <string>(typeDefinition1.Name))
                        {
                            continue;
                        }
                        Collection <MethodDefinition> methods1        = typeDefinition1.Methods;
                        Func <MethodDefinition, bool> u003cu003e9_206 = CompiledAssembly.< > c.< > 9__20_6;
                        if (u003cu003e9_206 == null)
                        {
                            u003cu003e9_206 = (MethodDefinition m) => {
                                if (m.IsStatic || m.HasGenericParameters || m.ReturnType.IsGenericParameter || m.IsSetter)
                                {
                                    return(false);
                                }
                                return(!m.IsGetter);
                            };
                            CompiledAssembly.< > c.< > 9__20_6 = u003cu003e9_206;
                        }
                        foreach (MethodDefinition methodDefinition in methods1.Where <MethodDefinition>(u003cu003e9_206))
                        {
                            foreach (ParameterDefinition parameter in methodDefinition.Parameters)
                            {
                                foreach (CustomAttribute customAttribute in parameter.CustomAttributes)
                                {
                                }
                            }
                        }
                    }
                    using (MemoryStream memoryStream1 = new MemoryStream())
                    {
                        assemblyDefinition.Write(memoryStream1);
                        this.PatchedAssembly = memoryStream1.ToArray();
                    }
                    OxideMod oxide       = Interface.Oxide;
                    Action u003cu003e9_2 = action1;
                    if (u003cu003e9_2 == null)
                    {
                        Action u003cu003e4_this = () => {
                            this.isPatching = false;
                            callback(this.PatchedAssembly);
                        };
                        Action action = u003cu003e4_this;
                        action1       = u003cu003e4_this;
                        u003cu003e9_2 = action;
                    }
                    oxide.NextTick(u003cu003e9_2);
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    Interface.Oxide.NextTick(() => {
                        this.isPatching = false;
                        Interface.Oxide.LogException(string.Concat("Exception while patching: ", this.PluginNames.ToSentence <string>()), exception);
                        callback(null);
                    });
                }
            });
        }