示例#1
0
        /// <summary>
        /// Save the Threat Model passed as argument as Template.
        /// </summary>
        /// <param name="model">Threat Model to be saved as Template.</param>
        /// <param name="definition">Definition of the information to export.</param>
        /// <param name="name">Name of the Template.</param>
        /// <param name="description">Description of the Template.</param>
        /// <param name="path">Path to the Template to be created.</param>
        public static void SaveTemplate(this IThreatModel model, [NotNull] DuplicationDefinition definition,
                                        [Required] string name, string description, [Required] string path)
        {
            var newModel = model.Duplicate(name, definition);

            newModel.Description = description;
            var serialization = ThreatModelManager.Serialize(newModel);

            var extension = Path.GetExtension(path)?.ToLower();

            switch (extension)
            {
            case ".tmt":
                var package = Package.Create(path);
                package.Add(Resources.ThreatModelTemplateFile, serialization);
                package.Save();
                break;

            case ".tmk":
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                using (var file = File.OpenWrite(path))
                {
                    using (var writer = new BinaryWriter(file))
                    {
                        writer.Write(serialization);
                    }
                }
                break;
            }
        }
        private void AddItemTemplates([NotNull] DuplicationDefinition definition, [NotNull] List <Guid> schemas)
        {
            if (_fullyItemTemplates.Checked)
            {
                definition.AllEntityTemplates        = true;
                definition.AllFlowTemplates          = true;
                definition.AllTrustBoundaryTemplates = true;
                AddSchemas(schemas, _template.EntityTemplates?.Select(x => x.Properties));
                AddSchemas(schemas, _template.FlowTemplates?.Select(x => x.Properties));
                AddSchemas(schemas, _template.TrustBoundaryTemplates?.Select(x => x.Properties));
            }
            else
            {
                var entityTemplates = _itemTemplates.CheckedItems.OfType <IEntityTemplate>().ToArray();
                if (entityTemplates.Any())
                {
                    definition.EntityTemplates = entityTemplates.Select(x => x.Id).ToArray();
                    AddSchemas(schemas, entityTemplates.Select(x => x.Properties));
                }

                var flowTemplates = _itemTemplates.CheckedItems.OfType <IFlowTemplate>().ToArray();
                if (flowTemplates.Any())
                {
                    definition.FlowTemplates = flowTemplates.Select(x => x.Id).ToArray();
                    AddSchemas(schemas, entityTemplates.Select(x => x.Properties));
                }

                var trustBoundaryTemplates = _itemTemplates.CheckedItems.OfType <ITrustBoundaryTemplate>().ToArray();
                if (trustBoundaryTemplates.Any())
                {
                    definition.TrustBoundaryTemplates = trustBoundaryTemplates.Select(x => x.Id).ToArray();
                    AddSchemas(schemas, trustBoundaryTemplates.Select(x => x.Properties));
                }
            }
        }
        private void AddThreatTypes([NotNull] DuplicationDefinition definition, [NotNull] List <Guid> schemas,
                                    [NotNull] List <Guid> mitigations, [NotNull] List <int> severities, [NotNull] List <int> strengths)
        {
            if (_fullyThreatTypes.Checked)
            {
                definition.AllThreatTypes = true;
                AddMitigations(mitigations, _template.ThreatTypes);
                AddSeverities(severities, _template.ThreatTypes);
                AddStrengths(strengths, _template.ThreatTypes);
                AddSchemas(schemas, _template.ThreatTypes?.Select(x => x.Properties));
            }
            else
            {
                var threatTypes = _threatTypes.CheckedItems.OfType <IThreatType>().ToArray();
                if (threatTypes.Any())
                {
                    definition.ThreatTypes = threatTypes.Select(x => x.Id).ToArray();

                    AddMitigations(mitigations, threatTypes);
                    AddSeverities(severities, threatTypes);
                    AddStrengths(strengths, threatTypes);
                    AddSchemas(schemas, threatTypes.Select(x => x.Properties));
                }
            }
        }
 private void AddSeverities([NotNull] DuplicationDefinition definition,
                            [NotNull] List <int> severities, [NotNull] List <Guid> schemas)
 {
     definition.Severities = severities;
     AddSchemas(schemas, _template.Severities?
                .Where(x => severities?.Contains(x.Id) ?? false)
                .Select(x => x.Properties));
 }
 private void AddStrengths([NotNull] DuplicationDefinition definition,
                           [NotNull] List <int> strengths, [NotNull] List <Guid> schemas)
 {
     definition.Strengths = strengths;
     AddSchemas(schemas, _model.Strengths?
                .Where(x => strengths?.Contains(x.Id) ?? false)
                .Select(x => x.Properties));
 }
示例#6
0
        /// <summary>
        /// Save the Threat Model passed as argument as Template.
        /// </summary>
        /// <param name="model">Threat Model to be saved as Template.</param>
        /// <param name="definition">Definition of the information to export.</param>
        /// <param name="name">Name of the Template.</param>
        /// <param name="description">Description of the Template.</param>
        /// <param name="path">Path to the Template to be created.</param>
        public static void SaveTemplate(this IThreatModel model, [NotNull] DuplicationDefinition definition,
                                        [Required] string name, string description, [Required] string path)
        {
            var newModel = model.Duplicate(name, definition);

            newModel.Description = description;
            var serialization = ThreatModelManager.Serialize(newModel);

            var package = Package.Create(path);

            package.Add(Resources.ThreatModelTemplateFile, serialization);
            package.Save();
        }
        private void _ok_Click(object sender, EventArgs e)
        {
            var list = _schemas.CheckedItems.OfType <IPropertySchema>().Select(x => x.Id).ToArray();

            if (list.Any())
            {
                var def = new DuplicationDefinition()
                {
                    PropertySchemas = list
                };

                _model.SaveTemplate(def, _name.Text, _description.Text, _fileName.Text);
            }
        }
        private void AddPropertySchemas([NotNull] DuplicationDefinition definition, [NotNull] List <Guid> schemas)
        {
            IEnumerable <Guid> additional;

            if (_fullySchemas.Checked)
            {
                additional = _template.Schemas?.Where(x => !x.System).Select(x => x.Id).ToArray();
            }
            else
            {
                additional = _schemas.CheckedItems.OfType <IPropertySchema>().Select(x => x.Id).ToArray();
            }

            definition.PropertySchemas = additional == null ? schemas : schemas.Union(additional);
        }
 private void AddThreatActors([NotNull] DuplicationDefinition definition, [NotNull] List <Guid> schemas)
 {
     if (_fullyThreatActors.Checked)
     {
         definition.AllThreatActors = true;
         AddSchemas(schemas, _template.ThreatActors?.Select(x => x.Properties));
     }
     else
     {
         var threatActors = _threatActors.CheckedItems.OfType <IThreatActor>().ToArray();
         if (threatActors.Any())
         {
             definition.ThreatActors = threatActors.Select(x => x.Id).ToArray();
             AddSchemas(schemas, threatActors.Select(x => x.Properties));
         }
     }
 }
        private DuplicationDefinition GetDuplicationDefinition()
        {
            DuplicationDefinition result      = new DuplicationDefinition();
            List <Guid>           schemas     = new List <Guid>();
            List <Guid>           mitigations = new List <Guid>();
            List <int>            severities  = new List <int>();
            List <int>            strengths   = new List <int>();

            AddThreatActors(result, schemas);
            AddThreatTypes(result, schemas, mitigations, severities, strengths);
            AddMitigations(result, mitigations, schemas);
            AddItemTemplates(result, schemas);
            AddStrengths(result, strengths, schemas);
            AddSeverities(result, severities, schemas);
            AddPropertySchemas(result, schemas);

            return(result);
        }
示例#11
0
        private void _ok_Click(object sender, EventArgs e)
        {
            var list = _schemas.CheckedItems.OfType <IPropertySchema>().Select(x => x.Id).ToArray();

            if (list.Any())
            {
                var def = new DuplicationDefinition()
                {
                    PropertySchemas = list
                };

                if (_model.Merge(_template, def) && _applySchemas.Checked)
                {
                    foreach (var id in list)
                    {
                        _model.ApplySchema(id);
                    }
                }
            }
        }
        private void AddMitigations([NotNull] DuplicationDefinition definition,
                                    [NotNull] List <Guid> mitigations, [NotNull] List <Guid> schemas)
        {
            if (_fullyMitigations.Checked)
            {
                definition.AllMitigations = true;
                AddSchemas(schemas, _template.Mitigations?.Select(x => x.Properties));
            }
            else
            {
                var m = _mitigations.CheckedItems.OfType <IMitigation>().Select(x => x.Id).ToArray();
                IEnumerable <Guid> mitigationIds = null;
                if (m.Any())
                {
                    if (mitigations.Any())
                    {
                        mitigationIds = mitigations.Union(m).ToArray();
                    }
                    else
                    {
                        mitigationIds = m;
                    }
                }
                else
                {
                    if (mitigations.Any())
                    {
                        mitigationIds = mitigations;
                    }
                }

                definition.Mitigations = mitigationIds;
                AddSchemas(schemas, _template.Mitigations
                           .Where(x => mitigationIds?.Contains(x.Id) ?? false)
                           .Select(x => x.Properties));
            }
        }