diff --git a/Samples/SampleThreatModelGenerator/ModelLoader.cs b/Samples/SampleThreatModelGenerator/ModelLoader.cs new file mode 100644 index 00000000..00f05698 --- /dev/null +++ b/Samples/SampleThreatModelGenerator/ModelLoader.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text.RegularExpressions; +using ThreatsManager.Engine; +using ThreatsManager.Interfaces; +using ThreatsManager.Interfaces.ObjectModel; +using ThreatsManager.Utilities; +using ThreatsManager.AutoThreatGeneration.Initializers; + +namespace SampleThreatModelGenerator +{ + class ModelLoader + { + private List _missingTypes = new List(); + + public ModelLoader() + { + KnownTypesBinder.TypeNotFound += OnTypeNotFound; + ExtensionsConfigurationManager.SetConfigurationUserLevel(ConfigurationUserLevel.None); + Manager.Instance.LoadExtensions(ExecutionMode.Simplified); + Manager.Instance.ApplyExtensionInitializers(); + } + + private void OnTypeNotFound(string assemblyName, string typeName) + { + if (string.CompareOrdinal(assemblyName, "mscorlib") == 0) + { + var regex = new Regex(@"\[\[(?[.\w]*), (?[.\w]*)\]\]"); + var match = regex.Match(typeName); + if (match.Success) + { + assemblyName = match.Groups["assembly"].Value; + typeName = match.Groups["class"].Value; + } + } + var name = $"{assemblyName}#{typeName}"; + + if (!_missingTypes.Contains(name)) + { + _missingTypes.Add(name); + var parts = typeName.Split('.'); + Console.WriteLine($"Document uses type {parts.Last()} from {assemblyName}, which is unknown.\nThe document will be loaded but some information may be missing."); + } + } + + public IThreatModel LoadDefaultModel() + { + var autoGenRuleInitializer = new AutoGenRuleInitializer(); + autoGenRuleInitializer.Initialize(); + + // Create a default threat model instance + var model = ThreatModelManager.GetDefaultInstance(); + model.Name = "Threat Model"; + + return model; + } + } +} + diff --git a/Samples/SampleThreatModelGenerator/Program.cs b/Samples/SampleThreatModelGenerator/Program.cs new file mode 100644 index 00000000..9d66d9df --- /dev/null +++ b/Samples/SampleThreatModelGenerator/Program.cs @@ -0,0 +1,87 @@ +using System.IO; +using ThreatsManager.Interfaces.ObjectModel.Entities; +using ThreatsManager.Utilities; +using ThreatsManager.Packaging; +using System.Drawing; +using System; + +namespace SampleThreatModelGenerator +{ + internal class Program + { + static void Main(string[] args) + { + if (args.Length == 1 && !File.Exists(args[0])) + { + // initialize the threatmodel + var loader = new ModelLoader(); + var model = loader.LoadDefaultModel(); + + // add diagram + var diagram = model.AddDiagram("Diagram 1"); + + // add external interactor + var externalinteractor = model.AddEntity("User"); + diagram.AddShape(externalinteractor, new PointF(-300, 200)); + + // add trust boundary + var trustboundary = model.AddGroup("Trust Boundary 1"); + diagram.AddGroupShape(trustboundary.Id, new PointF(300, 100), new SizeF(500, 200)); + + // add processes and their shapes, parents + var process1 = model.AddEntity("Web Front-End"); + diagram.AddShape(process1, new PointF(100, 50)); + process1.SetParent(trustboundary); + + var process2 = model.AddEntity("Exposed APIs"); + diagram.AddShape(process2, new PointF(100, 350)); + process2.SetParent(trustboundary); + + var process3 = model.AddEntity("Serverless BL"); + diagram.AddShape(process3, new PointF(500, 50)); + process3.SetParent(trustboundary); + + var datastore = model.AddEntity("Database"); + diagram.AddShape(datastore, new PointF(500, 350)); + datastore.SetParent(trustboundary); + + var process4 = model.AddEntity("On-premises systems"); + diagram.AddShape(process4, new PointF(900, 50)); + + // add dataflows between them + var dataflow1 = model.AddDataFlow("Get static content", externalinteractor.Id, process1.Id); + diagram.AddLink(dataflow1); + + var dataflow2 = model.AddDataFlow("Get/set data", externalinteractor.Id, process2.Id); + diagram.AddLink(dataflow2); + + var dataflow3 = model.AddDataFlow("Get/set data into DB", process2.Id, datastore.Id); + diagram.AddLink(dataflow3); + + var dataflow4 = model.AddDataFlow("Get data from DB", process3.Id, datastore.Id); + diagram.AddLink(dataflow4); + + var dataflow5 = model.AddDataFlow("Push to on-premises", process3.Id, process4.Id); + diagram.AddLink(dataflow5); + + // set output file + string output_file = args[0]; + + // Save the model to a file in JSON format. + var fileName = output_file; + var json = ThreatModelManager.Serialize(model); + var package = Package.Create(fileName); + package.Add("threatmodel.json", json); + package.Save(); + } + else if (args.Length == 1) + { + Console.WriteLine($"Specify non-existent filename as {args[0]} already exists."); + } + else + { + Console.WriteLine("Provide output filename in cmd line args."); + } + } + } +} diff --git a/Samples/SampleThreatModelGenerator/README.md b/Samples/SampleThreatModelGenerator/README.md new file mode 100644 index 00000000..f408bc5f --- /dev/null +++ b/Samples/SampleThreatModelGenerator/README.md @@ -0,0 +1,14 @@ +# SampleThreatModelGenerator + +A very simple program to generate a sample threat model as shown below. + +- The ModelLoader.cs initializes the ThreatModel engine. +- In the Main program, entities like external interactors, processes, datastores and trustboundaries and other dataflow links were added as required like in below image, + + +![A Sample Model](./Resources/sample.png) + +- Final output that will be generated is as shown below, + + +![Output.png](./Resources/output.png) diff --git a/Samples/SampleThreatModelGenerator/Resources/output.png b/Samples/SampleThreatModelGenerator/Resources/output.png new file mode 100644 index 00000000..00dda571 Binary files /dev/null and b/Samples/SampleThreatModelGenerator/Resources/output.png differ diff --git a/Samples/SampleThreatModelGenerator/Resources/sample.png b/Samples/SampleThreatModelGenerator/Resources/sample.png new file mode 100644 index 00000000..e62eafc2 Binary files /dev/null and b/Samples/SampleThreatModelGenerator/Resources/sample.png differ diff --git a/Samples/SampleThreatModelGenerator/SampleThreatModelGenerator.csproj b/Samples/SampleThreatModelGenerator/SampleThreatModelGenerator.csproj new file mode 100644 index 00000000..07714944 --- /dev/null +++ b/Samples/SampleThreatModelGenerator/SampleThreatModelGenerator.csproj @@ -0,0 +1,114 @@ + + + + + Debug + AnyCPU + {716D8BC8-8F05-4C21-B024-8F2BA25AACA7} + Exe + SampleThreatModelGenerator + SampleThreatModelGenerator + v4.7.2 + 512 + true + true + SAK + SAK + SAK + SAK + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + + + ..\packages\PostSharp.Redist.6.10.5\lib\net45\PostSharp.dll + + + ..\packages\PostSharp.Patterns.Aggregation.Redist.6.10.5\lib\net45\PostSharp.Patterns.Aggregation.dll + + + ..\packages\PostSharp.Patterns.Common.Redist.6.10.5\lib\net47\PostSharp.Patterns.Common.dll + + + ..\packages\PostSharp.Patterns.Model.Redist.6.10.5\lib\net45\PostSharp.Patterns.Model.dll + + + ..\packages\PostSharp.Patterns.Threading.Redist.6.10.5\lib\net45\PostSharp.Patterns.Threading.dll + + + + + + ..\packages\System.Configuration.ConfigurationManager.6.0.0\lib\net461\System.Configuration.ConfigurationManager.dll + + + + + + ..\packages\System.Drawing.Common.6.0.0\lib\net461\System.Drawing.Common.dll + + + + + ..\packages\System.Security.AccessControl.6.0.0\lib\net461\System.Security.AccessControl.dll + + + ..\packages\System.Security.Cryptography.ProtectedData.6.0.0\lib\net461\System.Security.Cryptography.ProtectedData.dll + + + ..\packages\System.Security.Permissions.6.0.0\lib\net461\System.Security.Permissions.dll + + + ..\packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll + + + + + + + + + + + ..\packages\ThreatsManager.Engine.1.5.2\lib\net472\ThreatsManager.Engine.dll + + + ..\packages\ThreatsManager.Interfaces.1.5.2\lib\net472\ThreatsManager.Interfaces.dll + + + ..\packages\ThreatsManager.Packaging.1.5.2\lib\net472\ThreatsManager.Packaging.dll + + + ..\packages\ThreatsManager.Utilities.1.5.2\lib\net472\ThreatsManager.Utilities.dll + + + + + + + + + + + + diff --git a/Samples/Samples.sln b/Samples/Samples.sln index e074c6a5..efea6b05 100644 --- a/Samples/Samples.sln +++ b/Samples/Samples.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWinFormExtensions", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleThreatModelAnalyzer", "SimpleThreatModelAnalyzer\SimpleThreatModelAnalyzer.csproj", "{02557E72-71EE-493B-9A7B-21BC743BAF22}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleThreatModelGenerator", "SampleThreatModelGenerator\SampleThreatModelGenerator.csproj", "{716D8BC8-8F05-4C21-B024-8F2BA25AACA7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {02557E72-71EE-493B-9A7B-21BC743BAF22}.Debug|Any CPU.Build.0 = Debug|Any CPU {02557E72-71EE-493B-9A7B-21BC743BAF22}.Release|Any CPU.ActiveCfg = Release|Any CPU {02557E72-71EE-493B-9A7B-21BC743BAF22}.Release|Any CPU.Build.0 = Release|Any CPU + {716D8BC8-8F05-4C21-B024-8F2BA25AACA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {716D8BC8-8F05-4C21-B024-8F2BA25AACA7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {716D8BC8-8F05-4C21-B024-8F2BA25AACA7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {716D8BC8-8F05-4C21-B024-8F2BA25AACA7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE