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 #2
0
 void ProcessMember(IDnlibDef member, ProtectionSettingsStack stack)
 {
     using (stack.Apply(member, ReadInfos(member)))
     {
         ProcessBody(member as MethodDef, 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);
		}
		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 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);
            }
        }
 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);
 }
        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 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);
        }