Esempio n. 1
0
        public IThreatModel Load(LocationType locationType, [Required] string location,
                                 IEnumerable <IExtensionMetadata> extensions, bool strict = true)
        {
            IThreatModel result = null;

            if (File.Exists(location))
            {
                var threatModelContent = File.ReadAllBytes(location);
                if (threatModelContent != null)
                {
                    try
                    {
                        result = ThreatModelManager.Deserialize(threatModelContent, !strict);
                    }
                    catch (JsonSerializationException e)
                    {
                        throw new ThreatModelOpeningFailureException("A serialization issue has occurred.", e);
                    }
                }
            }
            else
            {
                throw new FileNotFoundException("Unable to find the specified file.", location);
            }

            return(result);
        }
Esempio n. 2
0
        public IThreatModel Load(LocationType locationType, [Required] string location,
                                 IEnumerable <IExtensionMetadata> extensions, bool strict = true)
        {
            IThreatModel result = null;

            if (File.Exists(location))
            {
                var package = new Package(location);

                var threatModelContent = package.Read(ThreatModelFile);
                if (threatModelContent != null)
                {
                    try
                    {
                        result = ThreatModelManager.Deserialize(threatModelContent, !strict);
                    }
                    catch (JsonSerializationException e)
                    {
                        HandleJsonSerializationException(package, extensions, e);
                        throw;
                    }
                }
            }
            else
            {
                throw new FileNotFoundException("Unable to find the specified file.", location);
            }

            return(result);
        }
Esempio n. 3
0
        public IThreatModel OpenModel(string fileName)
        {
            IThreatModel result = null;

            var package = new Package(fileName);

            var threatModelContent = package.Read("threatmodel.json");

            if (threatModelContent != null)
            {
                try
                {
                    result = ThreatModelManager.Deserialize(threatModelContent, true);
                }
                catch (JsonSerializationException e)
                {
                    Console.WriteLine($"Some required extension was missing.\nMore details: {e.Message}");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Exception {e.ToString()}");
                }
            }

            return(result);
        }
Esempio n. 4
0
        public bool Save([NotNull] IThreatModel model, LocationType locationType, [Required] string location,
                         bool autoAddDateTime, IEnumerable <IExtensionMetadata> extensions, out string newLocation)
        {
            bool result = false;

            newLocation = null;

            if (model is IThreatModel tm)
            {
                var tmSerialized = ThreatModelManager.Serialize(tm);

                newLocation = autoAddDateTime
                    ? Path.Combine(Path.GetDirectoryName(location),
                                   $"{StripDateTimeSuffix(Path.GetFileNameWithoutExtension(location))}_{DateTime.Now.ToString("yyyyMMddHHmmss")}{Path.GetExtension(location)}")
                    : location;

                File.WriteAllBytes(newLocation, tmSerialized);

                if (autoAddDateTime)
                {
                    File.Copy(newLocation, Path.Combine(Path.GetDirectoryName(location), $"{StripDateTimeSuffix(Path.GetFileNameWithoutExtension(location))}{Path.GetExtension(location)}"), true);
                }

                result = true;
            }

            return(result);
        }
        public bool Save([NotNull] IThreatModel model, LocationType locationType, [Required] string location,
                         bool autoAddDateTime, IEnumerable <IExtensionMetadata> extensions, out string newLocation)
        {
            bool result = false;

            newLocation = null;

            if (model is IThreatModel tm)
            {
                var tmSerialized = ThreatModelManager.Serialize(tm);
                var extList      = new ExtensionsList
                {
                    Extensions =
                        new List <ExtensionInfo>(extensions.Where(x => x != null).Select(x => new ExtensionInfo(x.Id, x.Label)))
                };
                var extSerialized = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(extList, new JsonSerializerSettings()
                {
                    TypeNameHandling = TypeNameHandling.None,
                }));

                newLocation = autoAddDateTime
                    ? Path.Combine(Path.GetDirectoryName(location),
                                   $"{StripDateTimeSuffix(Path.GetFileNameWithoutExtension(location))}_{DateTime.Now.ToString("yyyyMMddHHmmss")}{Path.GetExtension(location)}")
                    : location;

                var package = Package.Create(newLocation);
                package.Add(ThreatModelFile, tmSerialized);
                package.Add(ExtensionsFile, extSerialized);
                package.Save();

                result = true;
            }

            return(result);
        }
Esempio n. 6
0
        public IThreatModel ConvertModel(string fileName)
        {
            var importer = new Importer();

            var result = ThreatModelManager.GetDefaultInstance();

            result.InitializeStandardSeverities();
            result.InitializeStandardStrengths();

            importer.Import(result, fileName, 1.0f, null, out var diagrams, out var externalInteractors,
                            out var processes, out var dataStores, out var flows, out var trustBoundaries, out var itemTypes,
                            out var threatTypes, out var customThreatTypes, out var threats, out var missingThreats);

            return(result);
        }
Esempio n. 7
0
        public bool Save(IThreatModel model, LocationType locationType, string location, SecureString password)
        {
            bool result = false;

            if (model is IThreatModel tm)
            {
                var tmSerialized = ThreatModelManager.Serialize(tm);

                var package = Package.Create(location);
                package.Add(ThreatModelFile, tmSerialized, password);
                package.Save();

                result = true;
            }

            return(result);
        }
Esempio n. 8
0
        static IThreatModel ConvertModel(ModelLoader loader, string fileName)
        {
            Console.Write($"--- Converting file {Path.GetFileName(fileName)}");

            var model = loader.ConvertModel(fileName);

            if (model != null)
            {
                var tmSerialized = ThreatModelManager.Serialize(model);
                var dest         = Path.Combine(Path.GetDirectoryName(fileName), $"{Path.GetFileNameWithoutExtension(fileName)}.tm");
                var package      = Package.Create(dest);
                package.Add("threatmodel.json", tmSerialized);
                package.Save();
            }

            Console.WriteLine(" - done.");

            return(model);
        }
Esempio n. 9
0
        public IThreatModel Load(LocationType locationType, string location, SecureString password)
        {
            IThreatModel result = null;

            if (File.Exists(location))
            {
                var package = new Package(location);

                var threatModelContent = package.Read(ThreatModelFile, password);
                if (threatModelContent != null)
                {
                    result = ThreatModelManager.Deserialize(threatModelContent);
                }
            }
            else
            {
                throw new FileNotFoundException();
            }

            return(result);
        }
Esempio n. 10
0
        private void _browse_Click(object sender, EventArgs e)
        {
            if (_openFile.ShowDialog(ActiveForm) == DialogResult.OK)
            {
                if (_template != null)
                {
                    ThreatModelManager.Remove(_template.Id);
                }
                _schemas.Items.Clear();

                _fileName.Text = _openFile.FileName;

                _template = TemplateManager.OpenTemplate(_fileName.Text);
                var schemas = _template?.Schemas?.OrderBy(x => x.Name).ToArray();
                if (schemas?.Any() ?? false)
                {
                    _schemas.Items.AddRange(schemas);
                }

                _ok.Enabled = IsValid();
            }
        }