Exemple #1
0
 void ProcessMember(IDnlibDef member, Rules rules, ProtectionSettingsStack stack)
 {
     stack.Push(ProcessAttributes(ReadObfuscationAttributes(member)));
     ApplySettings(member, rules, stack.GetInfos());
     ProcessBody(member as MethodDef, rules, stack);
     stack.Pop();
 }
Exemple #2
0
 void ProcessMember(IDnlibDef member, ProtectionSettingsStack stack)
 {
     using (stack.Apply(member, ReadInfos(member)))
     {
         ProcessBody(member as MethodDef, stack);
     }
 }
		void ProcessModule(ModuleDefMD module, ProtectionSettingsStack stack) {
			context.Annotations.Set(module, ModuleSettingsKey, new ProtectionSettingsStack(stack));
			foreach (var type in module.Types) {
				using (stack.Apply(type, ReadInfos(type)))
					ProcessTypeMembers(type, stack);
			}
		}
Exemple #4
0
        void ProcessModule(ModuleDefMD module, Rules rules, string snKeyPath, string snKeyPass,
                           List <ObfuscationAttributeInfo> settingAttrs,
                           Dictionary <Regex, List <ObfuscationAttributeInfo> > namespaceAttrs)
        {
            context.Annotations.Set(module, SNKey, LoadSNKey(context, snKeyPath == null ? null : Path.Combine(project.BaseDirectory, snKeyPath), snKeyPass));

            var moduleStack = new ProtectionSettingsStack();

            moduleStack.Push(ProcessAttributes(settingAttrs));
            ApplySettings(module, rules, moduleStack.GetInfos());

            var nsSettings = namespaceAttrs.ToDictionary(kvp => kvp.Key, kvp => {
                var nsStack = new ProtectionSettingsStack(moduleStack);
                nsStack.Push(ProcessAttributes(kvp.Value));
                return(nsStack);
            });

            foreach (var type in module.Types)
            {
                var typeStack = MatchNamespace(nsSettings, type.Namespace) ?? moduleStack;
                typeStack.Push(ProcessAttributes(ReadObfuscationAttributes(type)));

                ApplySettings(type, rules, typeStack.GetInfos());
                ProcessTypeMembers(type, rules, typeStack);

                typeStack.Pop();
            }
        }
		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);
		}
		void ProcessBody(MethodDef method, ProtectionSettingsStack stack) {
			if (method == null || method.Body == null)
				return;

			var declType = method.DeclaringType;
			foreach (var instr in method.Body.Instructions)
				if (instr.Operand is MethodDef) {
					var cgType = ((MethodDef)instr.Operand).DeclaringType;
					if (cgType.DeclaringType == declType && cgType.IsCompilerGenerated()) {
						using (stack.Apply(cgType, ReadInfos(cgType)))
							ProcessTypeMembers(cgType, stack);
					}
				}
		}
		void ProcessTypeMembers(TypeDef type, ProtectionSettingsStack stack) {
			foreach (var nestedType in type.NestedTypes) {
				using (stack.Apply(nestedType, ReadInfos(nestedType)))
					ProcessTypeMembers(nestedType, stack);
			}

			foreach (var property in type.Properties) {
				using (stack.Apply(property, ReadInfos(property))) {
					if (property.GetMethod != null)
						ProcessMember(property.GetMethod, stack);

					if (property.SetMethod != null)
						ProcessMember(property.SetMethod, stack);

					foreach (var m in property.OtherMethods)
						ProcessMember(m, stack);
				}
			}

			foreach (var evt in type.Events) {
				using (stack.Apply(evt, ReadInfos(evt))) {
					if (evt.AddMethod != null)
						ProcessMember(evt.AddMethod, stack);

					if (evt.RemoveMethod != null)
						ProcessMember(evt.RemoveMethod, stack);

					if (evt.InvokeMethod != null)
						ProcessMember(evt.InvokeMethod, stack);

					foreach (var m in evt.OtherMethods)
						ProcessMember(m, stack);
				}
			}

			foreach (var method in type.Methods) {
				if (method.SemanticsAttributes == 0)
					ProcessMember(method, stack);
			}

			foreach (var field in type.Fields) {
				ProcessMember(field, stack);
			}
		}
Exemple #8
0
        void ProcessBody(MethodDef method, Rules rules, ProtectionSettingsStack stack)
        {
            if (method == null || method.Body == null)
            {
                return;
            }

            var declType = method.DeclaringType;

            foreach (var instr in method.Body.Instructions)
            {
                if (instr.Operand is MethodDef)
                {
                    var cgType = ((MethodDef)instr.Operand).DeclaringType;
                    if (cgType.DeclaringType == declType && cgType.IsCompilerGenerated())
                    {
                        ApplySettings(cgType, rules, stack.GetInfos());
                        ProcessTypeMembers(cgType, rules, stack);
                    }
                }
            }
        }
        void ProcessBody(MethodDef method, ProtectionSettingsStack stack)
        {
            if (method == null || method.Body == null)
                return;

            var declType = method.DeclaringType;
            foreach (var instr in method.Body.Instructions)
                if (instr.Operand is MethodDef) {
                    var cgType = ((MethodDef)instr.Operand).DeclaringType;
                    if (cgType.DeclaringType == declType && cgType.IsCompilerGenerated()) {
                        using (stack.Apply(cgType, ReadInfos(cgType)))
                            ProcessTypeMembers(cgType, stack);
                    }
                }
        }
        void ProcessTypeMembers(TypeDef type, ProtectionSettingsStack stack)
        {
            foreach (var nestedType in type.NestedTypes) {
                stack.Push(ProcessAttributes(ReadObfuscationAttributes(nestedType)));

                ApplySettings(nestedType, stack.GetInfos());
                ProcessTypeMembers(nestedType, stack);

                stack.Pop();
            }

            foreach (var prop in type.Properties) {
                stack.Push(ProcessAttributes(ReadObfuscationAttributes(prop)));

                ApplySettings(prop, stack.GetInfos());
                if (prop.GetMethod != null) {
                    ProcessMember(prop.GetMethod, stack);
                }
                if (prop.SetMethod != null) {
                    ProcessMember(prop.SetMethod, stack);
                }
                foreach (var m in prop.OtherMethods)
                    ProcessMember(m, stack);

                stack.Pop();
            }

            foreach (var evt in type.Events) {
                stack.Push(ProcessAttributes(ReadObfuscationAttributes(evt)));

                ApplySettings(evt, stack.GetInfos());
                if (evt.AddMethod != null) {
                    ProcessMember(evt.AddMethod, stack);
                }
                if (evt.RemoveMethod != null) {
                    ProcessMember(evt.RemoveMethod, stack);
                }
                if (evt.InvokeMethod != null) {
                    ProcessMember(evt.InvokeMethod, stack);
                }
                foreach (var m in evt.OtherMethods)
                    ProcessMember(m, stack);

                stack.Pop();
            }

            foreach (var method in type.Methods) {
                if (method.SemanticsAttributes == 0)
                    ProcessMember(method, stack);
            }

            foreach (var field in type.Fields) {
                ProcessMember(field, stack);
            }
        }
 void ProcessMember(IDnlibDef member, ProtectionSettingsStack stack)
 {
     stack.Push(ProcessAttributes(ReadObfuscationAttributes(member)));
     ApplySettings(member, stack.GetInfos());
     stack.Pop();
 }
Exemple #12
0
 public ProtectionSettingsStack(ProtectionSettingsStack copy)
 {
     stack = new Stack <ProtectionSettingsInfo[]>(copy.stack);
 }
 public PopHolder(ProtectionSettingsStack parent)
 {
     this.parent = parent;
 }
 public ProtectionSettingsStack(ProtectionSettingsStack copy)
 {
     context = copy.context;
     stack = new Stack<Tuple<ProtectionSettings, ProtectionSettingsInfo[]>>(copy.stack);
     parser = copy.parser;
 }
        void ProcessTypeMembers(TypeDef type, ProtectionSettingsStack stack)
        {
            foreach (var nestedType in type.NestedTypes) {
                using (stack.Apply(nestedType, ReadInfos(nestedType)))
                    ProcessTypeMembers(nestedType, stack);
            }

            foreach (var property in type.Properties) {
                using (stack.Apply(property, ReadInfos(property))) {
                    if (property.GetMethod != null)
                        ProcessMember(property.GetMethod, stack);

                    if (property.SetMethod != null)
                        ProcessMember(property.SetMethod, stack);

                    foreach (var m in property.OtherMethods)
                        ProcessMember(m, stack);
                }
            }

            foreach (var evt in type.Events) {
                using (stack.Apply(evt, ReadInfos(evt))) {
                    if (evt.AddMethod != null)
                        ProcessMember(evt.AddMethod, stack);

                    if (evt.RemoveMethod != null)
                        ProcessMember(evt.RemoveMethod, stack);

                    if (evt.InvokeMethod != null)
                        ProcessMember(evt.InvokeMethod, stack);

                    foreach (var m in evt.OtherMethods)
                        ProcessMember(m, stack);
                }
            }

            foreach (var method in type.Methods) {
                if (method.SemanticsAttributes == 0)
                    ProcessMember(method, stack);
            }

            foreach (var field in type.Fields) {
                ProcessMember(field, stack);
            }
        }
 void ProcessModule(ModuleDefMD module, ProtectionSettingsStack stack)
 {
     context.Annotations.Set(module, ModuleSettingsKey, new ProtectionSettingsStack(stack));
     foreach (var type in module.Types) {
         using (stack.Apply(type, ReadInfos(type)))
             ProcessTypeMembers(type, stack);
     }
 }
 void ProcessMember(IDnlibDef member, ProtectionSettingsStack stack)
 {
     using (stack.Apply(member, ReadInfos(member)))
         ProcessBody(member as MethodDef, stack);
 }
Exemple #18
0
 public PopHolder(ProtectionSettingsStack parent)
 {
     this.parent = parent;
 }
Exemple #19
0
 public ProtectionSettingsStack(ProtectionSettingsStack copy)
 {
     context = copy.context;
     stack   = new Stack <Tuple <ProtectionSettings, ProtectionSettingsInfo[]> >(copy.stack);
     parser  = copy.parser;
 }
Exemple #20
0
        void ProcessTypeMembers(TypeDef type, Rules rules, ProtectionSettingsStack stack)
        {
            foreach (var nestedType in type.NestedTypes)
            {
                stack.Push(ProcessAttributes(ReadObfuscationAttributes(nestedType)));

                ApplySettings(nestedType, rules, stack.GetInfos());
                ProcessTypeMembers(nestedType, rules, stack);

                stack.Pop();
            }

            foreach (var prop in type.Properties)
            {
                stack.Push(ProcessAttributes(ReadObfuscationAttributes(prop)));

                ApplySettings(prop, rules, stack.GetInfos());
                if (prop.GetMethod != null)
                {
                    ProcessMember(prop.GetMethod, rules, stack);
                }
                if (prop.SetMethod != null)
                {
                    ProcessMember(prop.SetMethod, rules, stack);
                }
                foreach (var m in prop.OtherMethods)
                {
                    ProcessMember(m, rules, stack);
                }

                stack.Pop();
            }

            foreach (var evt in type.Events)
            {
                stack.Push(ProcessAttributes(ReadObfuscationAttributes(evt)));

                ApplySettings(evt, rules, stack.GetInfos());
                if (evt.AddMethod != null)
                {
                    ProcessMember(evt.AddMethod, rules, stack);
                }
                if (evt.RemoveMethod != null)
                {
                    ProcessMember(evt.RemoveMethod, rules, stack);
                }
                if (evt.InvokeMethod != null)
                {
                    ProcessMember(evt.InvokeMethod, rules, stack);
                }
                foreach (var m in evt.OtherMethods)
                {
                    ProcessMember(m, rules, stack);
                }

                stack.Pop();
            }

            foreach (var method in type.Methods)
            {
                if (method.SemanticsAttributes == 0)
                {
                    ProcessMember(method, rules, stack);
                }
            }

            foreach (var field in type.Fields)
            {
                ProcessMember(field, rules, stack);
            }
        }
        void ProcessModule(ModuleDefMD module, string snKeyPath, string snKeyPass,
		                   List<ObfuscationAttributeInfo> settingAttrs,
		                   Dictionary<Regex, List<ObfuscationAttributeInfo>> namespaceAttrs)
        {
            context.Annotations.Set(module, SNKey, LoadSNKey(context, snKeyPath, snKeyPass));

            var moduleStack = new ProtectionSettingsStack();
            moduleStack.Push(ProcessAttributes(settingAttrs));
            ApplySettings(module, moduleStack.GetInfos());

            var nsSettings = namespaceAttrs.ToDictionary(kvp => kvp.Key, kvp => {
                var nsStack = new ProtectionSettingsStack(moduleStack);
                nsStack.Push(ProcessAttributes(kvp.Value));
                return nsStack;
            });

            foreach (var type in module.Types) {
                var typeStack = MatchNamespace(nsSettings, type.Namespace) ?? moduleStack;
                typeStack.Push(ProcessAttributes(ReadObfuscationAttributes(type)));

                ApplySettings(type, typeStack.GetInfos());
                ProcessTypeMembers(type, typeStack);

                typeStack.Pop();
            }
        }
Exemple #22
0
 void ProcessMember(IDnlibDef member, ProtectionSettingsStack stack)
 {
     stack.Push(ProcessAttributes(ReadObfuscationAttributes(member)));
     ApplySettings(member, stack.GetInfos());
     stack.Pop();
 }
 public ProtectionSettingsStack(ProtectionSettingsStack copy)
 {
     stack = new Stack<ProtectionSettingsInfo[]>(copy.stack);
 }
        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);
        }