Beispiel #1
0
        void MarkModule(ProjectModule projModule, ModuleDefMD module, Rules rules, bool isMain)
        {
            string snKeyPath = projModule.SNKeyPath, snKeyPass = projModule.SNKeyPassword;
            string snPubKeyPath    = projModule.SNPubKeyPath;
            bool   snDelaySig      = projModule.SNDelaySig;
            string snSigKeyPath    = projModule.SNSigKeyPath;
            string snPubSigKeyPath = projModule.SNPubSigKeyPath;

            var stack = new ProtectionSettingsStack(context, protections);

            var layer = new List <ProtectionSettingsInfo>();

            // Add rules
            foreach (var rule in rules)
            {
                layer.Add(ToInfo(rule.Key, rule.Value));
            }

            // Add obfuscation attributes
            foreach (var attr in ReadObfuscationAttributes(module.Assembly))
            {
                if (string.IsNullOrEmpty(attr.FeatureName))
                {
                    ProtectionSettingsInfo info;
                    if (ToInfo(attr, out info))
                    {
                        layer.Add(info);
                    }
                }
                else if (attr.FeatureName.Equals("generate debug symbol", StringComparison.OrdinalIgnoreCase))
                {
                    if (!isMain)
                    {
                        throw new ArgumentException("Only main module can set 'generate debug symbol'.");
                    }
                    project.Debug = bool.Parse(attr.FeatureValue);
                }
                else if (attr.FeatureName.Equals("random seed", StringComparison.OrdinalIgnoreCase))
                {
                    if (!isMain)
                    {
                        throw new ArgumentException("Only main module can set 'random seed'.");
                    }
                    project.Seed = attr.FeatureValue;
                }
                else if (attr.FeatureName.Equals("strong name key", StringComparison.OrdinalIgnoreCase))
                {
                    snKeyPath = Path.Combine(project.BaseDirectory, attr.FeatureValue);
                }
                else if (attr.FeatureName.Equals("strong name key password", StringComparison.OrdinalIgnoreCase))
                {
                    snKeyPass = attr.FeatureValue;
                }
                else if (attr.FeatureName.Equals("packer", StringComparison.OrdinalIgnoreCase))
                {
                    if (!isMain)
                    {
                        throw new ArgumentException("Only main module can set 'packer'.");
                    }
                    new ObfAttrParser(packers).ParsePackerString(attr.FeatureValue, out packer, out packerParams);
                }
                else if (attr.FeatureName.Equals("external module", StringComparison.OrdinalIgnoreCase))
                {
                    if (!isMain)
                    {
                        throw new ArgumentException("Only main module can add external modules.");
                    }
                    var rawModule = new ProjectModule {
                        Path = attr.FeatureValue
                    }.LoadRaw(project.BaseDirectory);
                    extModules.Add(rawModule);
                }
                else
                {
                    AddRule(attr, layer);
                }
            }

            if (project.Debug && module.PdbState == null)
            {
                module.LoadPdb();
            }

            snKeyPath       = snKeyPath == null ? null : Path.Combine(project.BaseDirectory, snKeyPath);
            snPubKeyPath    = snPubKeyPath == null ? null : Path.Combine(project.BaseDirectory, snPubKeyPath);
            snSigKeyPath    = snSigKeyPath == null ? null : Path.Combine(project.BaseDirectory, snSigKeyPath);
            snPubSigKeyPath = snPubSigKeyPath == null ? null : Path.Combine(project.BaseDirectory, snPubSigKeyPath);

            var snKey = LoadSNKey(context, snKeyPath, snKeyPass);

            context.Annotations.Set(module, SNKey, snKey);

            var snPubKey = LoadSNPubKey(context, snPubKeyPath);

            context.Annotations.Set(module, SNPubKey, snPubKey);

            context.Annotations.Set(module, SNDelaySig, snDelaySig);

            var snSigKey = LoadSNKey(context, snSigKeyPath, snKeyPass);

            context.Annotations.Set(module, SNSigKey, snSigKey);

            var snSigPubKey = LoadSNPubKey(context, snPubSigKeyPath);

            context.Annotations.Set(module, SNSigPubKey, snSigPubKey);

            using (stack.Apply(module, layer))
                ProcessModule(module, stack);
        }
Beispiel #2
0
        /// <summary>
        ///     Loads the assembly and marks the project.
        /// </summary>
        /// <param name="proj">The project.</param>
        /// <param name="context">The working context.</param>
        /// <returns><see cref="MarkerResult" /> storing the marked modules and packer information.</returns>
        protected internal virtual MarkerResult MarkProject(ConfuserProject proj, ConfuserContext context)
        {
            Packer packer = null;
            Dictionary <string, string> packerParams = null;

            if (proj.Packer != null)
            {
                if (!packers.ContainsKey(proj.Packer.Id))
                {
                    context.Logger.ErrorFormat("Cannot find packer with ID '{0}'.", proj.Packer.Id);
                    throw new ConfuserException(null);
                }
                if (proj.Debug)
                {
                    context.Logger.Warn("Generated Debug symbols might not be usable with packers!");
                }

                packer       = packers[proj.Packer.Id];
                packerParams = new Dictionary <string, string>(proj.Packer, StringComparer.OrdinalIgnoreCase);
            }

            var modules    = new List <Tuple <ProjectModule, ModuleDefMD> >();
            var extModules = new List <byte[]>();

            foreach (ProjectModule module in proj)
            {
                if (module.IsExternal)
                {
                    extModules.Add(module.LoadRaw(proj.BaseDirectory));
                    continue;
                }

                ModuleDefMD modDef = module.Resolve(proj.BaseDirectory, context.Resolver.DefaultModuleContext);
                context.CheckCancellation();

                if (proj.Debug)
                {
                    modDef.LoadPdb();
                }

                context.Resolver.AddToCache(modDef);
                modules.Add(Tuple.Create(module, modDef));
            }

            foreach (var module in modules)
            {
                context.Logger.InfoFormat("Loading '{0}'...", module.Item1.Path);
                Rules rules = ParseRules(proj, module.Item1, context);

                context.Annotations.Set(module.Item2, SNKey, LoadSNKey(context, module.Item1.SNKeyPath == null ? null : Path.Combine(proj.BaseDirectory, module.Item1.SNKeyPath), module.Item1.SNKeyPassword));
                context.Annotations.Set(module.Item2, RulesKey, rules);

                foreach (IDnlibDef def in module.Item2.FindDefinitions())
                {
                    ApplyRules(context, def, rules);
                    context.CheckCancellation();
                }

                // Packer parameters are stored in modules
                if (packerParams != null)
                {
                    ProtectionParameters.GetParameters(context, module.Item2)[packer] = packerParams;
                }
            }
            return(new MarkerResult(modules.Select(module => module.Item2).ToList(), packer, extModules));
        }
Beispiel #3
0
        void MarkModule(ProjectModule projModule, ModuleDefMD module, Rules rules, bool isMain)
        {
            var    settingAttrs = new List <ObfuscationAttributeInfo>();
            string snKeyPath = projModule.SNKeyPath, snKeyPass = projModule.SNKeyPassword;
            Dictionary <Regex, List <ObfuscationAttributeInfo> > namespaceAttrs;

            if (!crossModuleAttrs.TryGetValue(module.Name, out namespaceAttrs))
            {
                namespaceAttrs = new Dictionary <Regex, List <ObfuscationAttributeInfo> >();
            }

            foreach (var attr in ReadObfuscationAttributes(module.Assembly))
            {
                if (string.IsNullOrEmpty(attr.FeatureName))
                {
                    settingAttrs.Add(attr);
                }
                else if (attr.FeatureName.Equals("generate debug symbol", StringComparison.OrdinalIgnoreCase))
                {
                    if (!isMain)
                    {
                        throw new ArgumentException("Only main module can set 'generate debug symbol'.");
                    }
                    project.Debug = bool.Parse(attr.FeatureValue);
                }
                else if (attr.FeatureName.Equals("random seed", StringComparison.OrdinalIgnoreCase))
                {
                    if (!isMain)
                    {
                        throw new ArgumentException("Only main module can set 'random seed'.");
                    }
                    project.Seed = attr.FeatureValue;
                }
                else if (attr.FeatureName.Equals("strong name key", StringComparison.OrdinalIgnoreCase))
                {
                    snKeyPath = Path.Combine(project.BaseDirectory, attr.FeatureValue);
                }
                else if (attr.FeatureName.Equals("strong name key password", StringComparison.OrdinalIgnoreCase))
                {
                    snKeyPass = attr.FeatureValue;
                }
                else if (attr.FeatureName.Equals("packer", StringComparison.OrdinalIgnoreCase))
                {
                    if (!isMain)
                    {
                        throw new ArgumentException("Only main module can set 'packer'.");
                    }
                    new ObfAttrParser(packers).ParsePackerString(attr.FeatureValue, out packer, out packerParams);
                }
                else if (attr.FeatureName.Equals("external module", StringComparison.OrdinalIgnoreCase))
                {
                    if (!isMain)
                    {
                        throw new ArgumentException("Only main module can add external modules.");
                    }
                    var rawModule = new ProjectModule {
                        Path = attr.FeatureValue
                    }.LoadRaw(project.BaseDirectory);
                    extModules.Add(rawModule);
                }
                else
                {
                    var match = NSInModulePattern.Match(attr.FeatureName);
                    if (match.Success)
                    {
                        if (!isMain)
                        {
                            throw new ArgumentException("Only main module can set cross module obfuscation.");
                        }
                        var    ns           = TranslateNamespaceRegex(match.Groups[1].Value);
                        string targetModule = match.Groups[2].Value;
                        var    x            = attr;
                        x.FeatureName = "";
                        Dictionary <Regex, List <ObfuscationAttributeInfo> > targetModuleAttrs;
                        if (!crossModuleAttrs.TryGetValue(targetModule, out targetModuleAttrs))
                        {
                            targetModuleAttrs = new Dictionary <Regex, List <ObfuscationAttributeInfo> >();
                            crossModuleAttrs[targetModule] = targetModuleAttrs;
                        }
                        targetModuleAttrs.AddListEntry(ns, x);
                    }
                    else
                    {
                        match = NSPattern.Match(attr.FeatureName);
                        if (match.Success)
                        {
                            var ns = TranslateNamespaceRegex(match.Groups[1].Value);
                            var x  = attr;
                            x.FeatureName = "";
                            namespaceAttrs.AddListEntry(ns, x);
                        }
                    }
                }
            }

            if (project.Debug)
            {
                module.LoadPdb();
            }

            ProcessModule(module, rules, snKeyPath, snKeyPass, settingAttrs, namespaceAttrs);
        }