private void CreateGenerationRule([NotNull] ThreatModel model, [NotNull] ThreatType source, [NotNull] IThreatType target) { SelectionRuleNode include = AnalyzeGenerationRule(model, target.Model, source.IncludeFilter); SelectionRuleNode exclude = AnalyzeGenerationRule(model, target.Model, source.ExcludeFilter); SelectionRule rule = null; var andNode = new AndRuleNode() { Name = "AND" }; if (include != null) { andNode.Children.Add(include); } if (exclude != null) { andNode.Children.Add(new NotRuleNode() { Name = "NOT", Child = exclude }); } if (andNode.Children.Any()) { andNode.Children.Add(new BooleanRuleNode("Out of Scope", Resources.DefaultNamespace, Resources.TmtFlowPropertySchema, false) { Scope = AutoThreatGeneration.Engine.Scope.Object }); rule = new SelectionRule() { Root = andNode }; var schemaManager = new AutoThreatGenPropertySchemaManager(target.Model); var propertyType = schemaManager.GetPropertyType(); var property = target.GetProperty(propertyType) ?? target.AddProperty(propertyType, null); if (property is IPropertyJsonSerializableObject jsonSerializableObject) { jsonSerializableObject.Value = rule; } } }
public static bool GenerateThreatEvents(this IDataFlow flow) { bool result = false; if (flow.Model is IThreatModel model) { var schemaManager = new AutoThreatGenPropertySchemaManager(model); var propertyType = schemaManager.GetPropertyType(); if (propertyType is IJsonSerializableObjectPropertyType jsonPropertyType) { var threatTypes = model.ThreatTypes?.Where(x => x.HasProperty(jsonPropertyType)).ToArray(); if (threatTypes?.Any() ?? false) { ApplyThreatTypes(flow, threatTypes, jsonPropertyType); result = true; } } } return(result); }
public static SelectionRule GetRule(this IPropertiesContainer container) { SelectionRule result = null; if (container is IThreatModelChild child && child.Model is IThreatModel model) { var schemaManager = new AutoThreatGenPropertySchemaManager(model); var propertyType = schemaManager?.GetPropertyType(); if (propertyType != null) { var p = container.GetProperty(propertyType); if (p is IPropertyJsonSerializableObject jsonProperty) { if (jsonProperty.Value is SelectionRule rule) { result = rule; } } } } return(result); }
public static bool GenerateThreatEvents(this IThreatModel model) { bool result = false; var schemaManager = new AutoThreatGenPropertySchemaManager(model); var propertyType = schemaManager.GetPropertyType(); if (propertyType is IJsonSerializableObjectPropertyType jsonPropertyType) { var threatTypes = model.ThreatTypes?.Where(x => x.HasProperty(jsonPropertyType)).ToArray(); if (threatTypes?.Any() ?? false) { ApplyThreatTypes(model, threatTypes, jsonPropertyType); var entities = model.Entities?.ToArray(); if (entities?.Any() ?? false) { foreach (var entity in entities) { ApplyThreatTypes(entity, threatTypes, jsonPropertyType); } } var dataFlows = model.DataFlows?.ToArray(); if (dataFlows?.Any() ?? false) { foreach (var dataFlow in dataFlows) { ApplyThreatTypes(dataFlow, threatTypes, jsonPropertyType); } } result = true; } } return(result); }
public static bool ApplyMitigations(this IThreatEvent threatEvent, IJsonSerializableObjectPropertyType propertyType = null) { bool result = false; if (threatEvent.ThreatType is IThreatType threatType && threatEvent.Model is IThreatModel model && threatEvent.Parent is IIdentity identity) { if (propertyType == null) { var schemaManager = new AutoThreatGenPropertySchemaManager(model); propertyType = schemaManager.GetPropertyType() as IJsonSerializableObjectPropertyType; } if (propertyType != null) { var mitigations = threatType.Mitigations?.ToArray(); if (mitigations?.Any() ?? false) { ISeverity maximumSeverity = null; var generated = false; foreach (var mitigation in mitigations) { var mProperty = mitigation.GetProperty(propertyType); if (mProperty is IPropertyJsonSerializableObject jsonMProperty && jsonMProperty.Value is MitigationSelectionRule mRule && mRule.Evaluate(identity)) { var strength = mitigation.Strength; if (mRule.StrengthId.HasValue && model.GetStrength(mRule.StrengthId.Value) is IStrength strengthOverride) { strength = strengthOverride; } if (mRule.Status.HasValue) { generated = (threatEvent.AddMitigation(mitigation.Mitigation, strength, mRule.Status.Value) != null); } else { generated = (threatEvent.AddMitigation(mitigation.Mitigation, strength) != null); } result |= generated; if (generated && mRule.SeverityId.HasValue && model.GetSeverity(mRule.SeverityId.Value) is ISeverity severity && (maximumSeverity == null || maximumSeverity.Id > severity.Id)) { maximumSeverity = severity; } } } if (maximumSeverity != null && maximumSeverity.Id < threatEvent.SeverityId) { threatEvent.Severity = maximumSeverity; } } } } return(result); }