Beispiel #1
0
 /// <summary>
 ///     Adds plugins in the assembly to the protection list.
 /// </summary>
 /// <param name="context">The working context.</param>
 /// <param name="protections">The working list of protections.</param>
 /// <param name="packers">The working list of packers.</param>
 /// <param name="components">The working list of components.</param>
 /// <param name="asm">The assembly.</param>
 // Token: 0x060001D3 RID: 467 RVA: 0x0000F594 File Offset: 0x0000D794
 protected static void AddPlugins(ConfuserContext context, IList <Protection> protections, IList <Packer> packers, IList <ConfuserComponent> components, Assembly asm)
 {
     Module[] loadedModules = asm.GetLoadedModules();
     for (int j = 0; j < loadedModules.Length; j++)
     {
         Module module = loadedModules[j];
         Type[] types  = module.GetTypes();
         for (int k = 0; k < types.Length; k++)
         {
             Type i = types[k];
             if (!i.IsAbstract && PluginDiscovery.HasAccessibleDefConstructor(i))
             {
                 if (typeof(Protection).IsAssignableFrom(i))
                 {
                     try
                     {
                         protections.Add((Protection)Activator.CreateInstance(i));
                         goto IL_126;
                     }
                     catch (Exception ex)
                     {
                         context.Logger.ErrorException("Failed to instantiate protection '" + i.Name + "'.", ex);
                         goto IL_126;
                     }
                 }
                 if (typeof(Packer).IsAssignableFrom(i))
                 {
                     try
                     {
                         packers.Add((Packer)Activator.CreateInstance(i));
                         goto IL_126;
                     }
                     catch (Exception ex2)
                     {
                         context.Logger.ErrorException("Failed to instantiate packer '" + i.Name + "'.", ex2);
                         goto IL_126;
                     }
                 }
                 if (typeof(ConfuserComponent).IsAssignableFrom(i))
                 {
                     try
                     {
                         components.Add((ConfuserComponent)Activator.CreateInstance(i));
                     }
                     catch (Exception ex3)
                     {
                         context.Logger.ErrorException("Failed to instantiate component '" + i.Name + "'.", ex3);
                     }
                 }
             }
             IL_126 :;
         }
     }
     context.CheckCancellation();
 }
Beispiel #2
0
        /// <summary>
        ///     Protects the stub using original project settings replace the current output with the protected stub.
        /// </summary>
        /// <param name="context">The working context.</param>
        /// <param name="fileName">The result file name.</param>
        /// <param name="module">The stub module.</param>
        /// <param name="snKey">The strong name key.</param>
        /// <param name="prot">The packer protection that applies to the stub.</param>
        protected void ProtectStub(ConfuserContext context, string fileName, byte[] module, StrongNameKey snKey, Protection prot = null)
        {
            string tmpDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            string outDir = Path.Combine(tmpDir, Path.GetRandomFileName());

            Directory.CreateDirectory(tmpDir);
            File.WriteAllBytes(Path.Combine(tmpDir, fileName), module);

            var proj = new ConfuserProject();

            proj.Seed = context.Project.Seed;
            foreach (Rule rule in context.Project.Rules)
            {
                proj.Rules.Add(rule);
            }
            proj.Add(new ProjectModule {
                Path = fileName
            });
            proj.BaseDirectory   = tmpDir;
            proj.OutputDirectory = outDir;

            PluginDiscovery discovery = null;

            if (prot != null)
            {
                var rule = new Rule {
                    Preset  = ProtectionPreset.None,
                    Inherit = true,
                    Pattern = "true"
                };
                rule.Add(new SettingItem <Protection> {
                    Id     = prot.Id,
                    Action = SettingItemAction.Add
                });
                proj.Rules.Add(rule);
                discovery = new PackerDiscovery(prot);
            }

            try {
                ConfuserEngine.Run(new ConfuserParameters {
                    Logger          = new PackerLogger(context.Logger),
                    PluginDiscovery = discovery,
                    Marker          = new PackerMarker(snKey),
                    Project         = proj,
                    PackerInitiated = true
                }, context.token).Wait();
            }
            catch (AggregateException ex) {
                context.Logger.Error("Failed to protect packer stub.");
                throw new ConfuserException(ex);
            }

            context.OutputModules = new[] { File.ReadAllBytes(Path.Combine(outDir, fileName)) };
            context.OutputPaths   = new[] { fileName };
        }
Beispiel #3
0
 /// <summary>
 ///     Retrieves the available protection plugins.
 /// </summary>
 /// <param name="context">The working context.</param>
 /// <param name="protections">The working list of protections.</param>
 /// <param name="packers">The working list of packers.</param>
 /// <param name="components">The working list of components.</param>
 // Token: 0x060001D4 RID: 468 RVA: 0x0000F718 File Offset: 0x0000D918
 protected virtual void GetPluginsInternal(ConfuserContext context, IList <Protection> protections, IList <Packer> packers, IList <ConfuserComponent> components)
 {
     try
     {
         Assembly protAsm = Assembly.Load("Confuser.Protections");
         PluginDiscovery.AddPlugins(context, protections, packers, components, protAsm);
     }
     catch (Exception ex)
     {
         context.Logger.ErrorException("Failed to load built-in protections.", ex);
     }
     try
     {
         Assembly renameAsm = Assembly.Load("Confuser.Renamer");
         PluginDiscovery.AddPlugins(context, protections, packers, components, renameAsm);
     }
     catch (Exception ex2)
     {
         context.Logger.ErrorException("Failed to load renamer.", ex2);
     }
     try
     {
         Assembly renameAsm2 = Assembly.Load("Confuser.DynCipher");
         PluginDiscovery.AddPlugins(context, protections, packers, components, renameAsm2);
     }
     catch (Exception ex3)
     {
         context.Logger.ErrorException("Failed to load dynamic cipher library.", ex3);
     }
     foreach (string pluginPath in context.Project.PluginPaths)
     {
         string realPath = Path.Combine(context.BaseDirectory, pluginPath);
         try
         {
             Assembly plugin = Assembly.LoadFile(realPath);
             PluginDiscovery.AddPlugins(context, protections, packers, components, plugin);
         }
         catch (Exception ex4)
         {
             context.Logger.ErrorException("Failed to load plugin '" + pluginPath + "'.", ex4);
         }
     }
 }
        /// <summary>
        ///     Protects the stub using original project settings replace the current output with the protected stub.
        /// </summary>
        /// <param name="context">The working context.</param>
        /// <param name="fileName">The result file name.</param>
        /// <param name="module">The stub module.</param>
        /// <param name="snKey">The strong name key.</param>
        /// <param name="prot">The packer protection that applies to the stub.</param>
        // Token: 0x060001E8 RID: 488 RVA: 0x0000F8AC File Offset: 0x0000DAAC
        protected void ProtectStub(ConfuserContext context, string fileName, byte[] module, StrongNameKey snKey, Protection prot = null)
        {
            string tmpDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            string outDir = Path.Combine(tmpDir, Path.GetRandomFileName());

            Directory.CreateDirectory(tmpDir);
            for (int i = 0; i < context.OutputModules.Count; i++)
            {
                string path = Path.GetFullPath(Path.Combine(tmpDir, context.OutputPaths[i]));
                string dir  = Path.GetDirectoryName(path);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                File.WriteAllBytes(path, context.OutputModules[i]);
            }
            File.WriteAllBytes(Path.Combine(tmpDir, fileName), module);
            ConfuserProject proj = new ConfuserProject();

            proj.Seed = context.Project.Seed;
            foreach (Rule rule in context.Project.Rules)
            {
                proj.Rules.Add(rule);
            }
            proj.Add(new ProjectModule
            {
                Path = fileName
            });
            proj.BaseDirectory   = tmpDir;
            proj.OutputDirectory = outDir;
            foreach (string path2 in context.Project.ProbePaths)
            {
                proj.ProbePaths.Add(path2);
            }
            proj.ProbePaths.Add(context.Project.BaseDirectory);
            PluginDiscovery discovery = null;

            if (prot != null)
            {
                Rule rule2 = new Rule("true", ProtectionPreset.None, false)
                {
                    Preset  = ProtectionPreset.None,
                    Inherit = true,
                    Pattern = "true"
                };
                rule2.Add(new SettingItem <Protection>(null, SettingItemAction.Add)
                {
                    Id     = prot.Id,
                    Action = SettingItemAction.Add
                });
                proj.Rules.Add(rule2);
                discovery = new PackerDiscovery(prot);
            }
            try
            {
                ConfuserEngine.Run(new ConfuserParameters
                {
                    Logger          = new PackerLogger(context.Logger),
                    PluginDiscovery = discovery,
                    Marker          = new PackerMarker(snKey),
                    Project         = proj,
                    PackerInitiated = true
                }, new CancellationToken?(context.token)).Wait();
            }
            catch (AggregateException ex)
            {
                context.Logger.Error("Failed to protect packer stub.");
                throw new ConfuserException(ex);
            }
            context.OutputModules = new byte[][]
            {
                File.ReadAllBytes(Path.Combine(outDir, fileName))
            };
            context.OutputPaths = new string[]
            {
                fileName
            };
        }
        /// <summary>
        ///     Protects the stub using original project settings replace the current output with the protected stub.
        /// </summary>
        /// <param name="context">The working context.</param>
        /// <param name="fileName">The result file name.</param>
        /// <param name="module">The stub module.</param>
        /// <param name="snKey">The strong name key.</param>
        /// <param name="prot">The packer protection that applies to the stub.</param>
        protected void ProtectStub(ConfuserContext context, string fileName, byte[] module, StrongNameKey snKey, StrongNamePublicKey snPubKey, StrongNameKey snSigKey, StrongNamePublicKey snPubSigKey, bool snDelaySig, Protection prot = null)
        {
            string tmpDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            try {
                string outDir = Path.Combine(tmpDir, Path.GetRandomFileName());
                Directory.CreateDirectory(tmpDir);

                for (int i = 0; i < context.OutputModules.Count; i++)
                {
                    string path = Path.GetFullPath(Path.Combine(tmpDir, context.OutputPaths[i]));
                    var    dir  = Path.GetDirectoryName(path);
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    File.WriteAllBytes(path, context.OutputModules[i]);
                }

                File.WriteAllBytes(Path.Combine(tmpDir, fileName), module);

                var proj = new ConfuserProject();
                proj.Seed = context.Project.Seed;
                foreach (Rule rule in context.Project.Rules)
                {
                    proj.Rules.Add(rule);
                }
                proj.Add(new ProjectModule {
                    Path = fileName
                });
                proj.BaseDirectory   = tmpDir;
                proj.OutputDirectory = outDir;
                foreach (var path in context.Project.ProbePaths)
                {
                    proj.ProbePaths.Add(path);
                }
                proj.ProbePaths.Add(context.Project.BaseDirectory);

                PluginDiscovery discovery = null;
                if (prot != null)
                {
                    var rule = new Rule {
                        Preset  = ProtectionPreset.None,
                        Inherit = true,
                        Pattern = "true"
                    };
                    rule.Add(new SettingItem <Protection> {
                        Id     = prot.Id,
                        Action = SettingItemAction.Add
                    });
                    proj.Rules.Add(rule);
                    discovery = new PackerDiscovery(prot);
                }

                try {
                    ConfuserEngine
                    .Run(
                        new ConfuserParameters {
                        Logger          = new PackerLogger(context.Logger),
                        PluginDiscovery = discovery,
                        Marker          = new PackerMarker(snKey, snPubKey, snDelaySig, snSigKey, snPubSigKey),
                        Project         = proj,
                        PackerInitiated = true
                    }, context.token).Wait();
                }
                catch (AggregateException ex) {
                    context.Logger.Error("Failed to protect packer stub.");
                    throw new ConfuserException(ex);
                }

                context.OutputModules = new[] { File.ReadAllBytes(Path.Combine(outDir, fileName)) };
                context.OutputPaths   = new[] { fileName };
            }
            finally {
                try {
                    if (Directory.Exists(tmpDir))
                    {
                        Directory.Delete(tmpDir, true);
                    }
                }
                catch (IOException ex) {
                    context.Logger.WarnException("Failed to remove temporary files of packer.", ex);
                }
            }
        }