Ejemplo n.º 1
0
        private void LoadSymbols(ModuleDefMD module)
        {
            if (module == null || string.IsNullOrWhiteSpace(fileName))
                return;
            // Happens if a module has been removed but then the exact same instance
            // was re-added.
            if (module.PdbState != null)
                return;

            // search for pdb in same directory as dll
            string pdbName = Path.Combine(Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName) + ".pdb");
            if (File.Exists(pdbName)) {
                module.LoadPdb(pdbName);
                return;
            }

            // TODO: use symbol cache, get symbols from microsoft
        }
Ejemplo n.º 2
0
        void MarkModule(ModuleDefMD module, bool isMain)
        {
            var settingAttrs = new List<ObfuscationAttributeInfo>();
            string snKeyPath = null, snKeyPass = null;
            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 (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);
                }
                if (project.Debug) {
                    module.LoadPdb();
                }

                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 if (attr.FeatureName == "") {
                    settingAttrs.Add(attr);
                }
                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);
                        }
                    }
                }
            }

            ProcessModule(module, snKeyPath, snKeyPass, settingAttrs, namespaceAttrs);
        }
Ejemplo n.º 3
0
        void MarkModule(ProjectModule projModule, ModuleDefMD module, Rules rules, bool isMain)
        {
            string snKeyPath = projModule.SNKeyPath, snKeyPass = projModule.SNKeyPassword;
            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.LoadPdb();
            }

            snKeyPath = snKeyPath == null ? null : Path.Combine(project.BaseDirectory, snKeyPath);
            var snKey = LoadSNKey(context, snKeyPath, snKeyPass);
            context.Annotations.Set(module, SNKey, snKey);

            using (stack.Apply(module, layer))
                ProcessModule(module, stack);
        }