public ActionResult RunAnalysis(string first_id, string second_id) { CompareCommandOptions opts = new CompareCommandOptions(); opts.FirstRunId = first_id; opts.SecondRunId = second_id; foreach (BaseCompare c in AttackSurfaceAnalyzerCLI.GetComparators()) { // The GUI *should* prevent us from getting here. But this is extra protection. // We won't start new collections while existing ones are ongoing. if (c.IsRunning() == RUN_STATUS.RUNNING) { return(Json("Comparators already running!")); } } using (var cmd = new SqliteCommand(SQL_CHECK_IF_COMPARISON_PREVIOUSLY_COMPLETED, DatabaseManager.Connection, DatabaseManager.Transaction)) { cmd.Parameters.AddWithValue("@base_run_id", opts.FirstRunId); cmd.Parameters.AddWithValue("@compare_run_id", opts.SecondRunId); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { return(Json("Using cached comparison calculations.")); } } } Task.Factory.StartNew <Dictionary <string, object> >(() => AttackSurfaceAnalyzerCLI.CompareRuns(opts)); return(Json("Started Analysis")); }
public ActionResult RunAnalysis(string firstId, string secondId) { CompareCommandOptions opts = new CompareCommandOptions(); opts.FirstRunId = firstId; opts.SecondRunId = secondId; opts.Analyze = true; if (AttackSurfaceAnalyzerClient.GetComparators().Where(c => c.IsRunning() == RUN_STATUS.RUNNING).Any()) { return(Json("Comparators already running!")); } using (var cmd = new SqliteCommand(SQL_CHECK_IF_COMPARISON_PREVIOUSLY_COMPLETED, DatabaseManager.Connection, DatabaseManager.Transaction)) { cmd.Parameters.AddWithValue("@base_run_id", opts.FirstRunId); cmd.Parameters.AddWithValue("@compare_run_id", opts.SecondRunId); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { return(Json("Using cached comparison calculations.")); } } } Task.Factory.StartNew(() => AttackSurfaceAnalyzerClient.CompareRuns(opts)); return(Json("Started Analysis")); }
public ActionResult RunAnalysisWithAnalyses(string SelectedBaseRunId, string SelectedCompareRunId, IFormFile AnalysisFilterFile) { var filePath = Path.GetTempFileName(); CompareCommandOptions opts = new CompareCommandOptions(); opts.FirstRunId = SelectedBaseRunId; opts.SecondRunId = SelectedCompareRunId; opts.Analyze = true; opts.SaveToDatabase = true; if (AnalysisFilterFile != null) { using (var stream = System.IO.File.Create(filePath)) { AnalysisFilterFile.CopyTo(stream); } opts.AnalysesFile = filePath; } if (AttackSurfaceAnalyzerClient.GetComparators().Where(c => c.IsRunning() == RUN_STATUS.RUNNING).Any()) { return(Json("Comparators already running!")); } if (DatabaseManager.GetComparisonCompleted(opts.FirstRunId, opts.SecondRunId)) { return(Json("Using cached comparison calculations.")); } Task.Factory.StartNew(() => AttackSurfaceAnalyzerClient.CompareRuns(opts)); return(Json("Started Analysis")); }
public void VerifyFileMonitorAsFile() { var RuleName = "AndRule"; var andRule = new AsaRule(RuleName) { Expression = "0 AND 1", ResultType = RESULT_TYPE.FILE, Flag = ANALYSIS_RESULT_TYPE.FATAL, Clauses = new List <Clause>() { new Clause(Operation.Equals, "Path") { Label = "0", Data = new List <string>() { "TestPath1" } }, new Clause(Operation.IsTrue, "IsExecutable") { Label = "1" } } }; var analyzer = new AsaAnalyzer(); var opts = new CompareCommandOptions(null, "SecondRun") { ApplySubObjectRulesToMonitor = true }; var results = AttackSurfaceAnalyzerClient.AnalyzeMonitored(opts, analyzer, new MonitorObject[] { testPathOneObject }, new RuleFile() { AsaRules = new AsaRule[] { andRule } }); Assert.IsTrue(results.Any(x => x.Value.Any(y => y.Identity == testPathOneObject.Identity && y.Rules.Contains(andRule)))); opts = new CompareCommandOptions(null, "SecondRun") { ApplySubObjectRulesToMonitor = false }; results = AttackSurfaceAnalyzerClient.AnalyzeMonitored(opts, analyzer, new MonitorObject[] { testPathOneObject }, new RuleFile() { AsaRules = new AsaRule[] { andRule } }); Assert.IsFalse(results.Any(x => x.Value.Any(y => y.Identity == testPathOneObject.Identity && y.Rules.Contains(andRule)))); }
public ActionResult RunAnalysisWithAnalyses(string SelectedBaseRunId, string SelectedCompareRunId, IFormFile AnalysisFilterFile) { var filePath = Path.GetTempFileName(); CompareCommandOptions opts = new CompareCommandOptions(); opts.FirstRunId = SelectedBaseRunId; opts.SecondRunId = SelectedCompareRunId; opts.Analyze = true; opts.SaveToDatabase = true; if (AnalysisFilterFile != null) { using (var stream = System.IO.File.Create(filePath)) { AnalysisFilterFile.CopyTo(stream); } opts.AnalysesFile = filePath; } if (AttackSurfaceAnalyzerClient.GetComparators().Where(c => c.IsRunning() == RUN_STATUS.RUNNING).Any()) { return(Json("Comparators already running!")); } using (var cmd = new SqliteCommand(SQL_CHECK_IF_COMPARISON_PREVIOUSLY_COMPLETED, DatabaseManager.Connection, DatabaseManager.Transaction)) { cmd.Parameters.AddWithValue("@base_run_id", opts.FirstRunId); cmd.Parameters.AddWithValue("@compare_run_id", opts.SecondRunId); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { return(Json("Using cached comparison calculations.")); } } } Task.Factory.StartNew(() => AttackSurfaceAnalyzerClient.CompareRuns(opts)); return(Json("Started Analysis")); }
public static ConcurrentDictionary <(RESULT_TYPE, CHANGE_TYPE), ConcurrentQueue <CompareResult> > CompareRuns(CompareCommandOptions opts) { if (opts is null) { throw new ArgumentNullException(nameof(opts)); } if (opts.SaveToDatabase) { DatabaseManager.InsertCompareRun(opts.FirstRunId, opts.SecondRunId, RUN_STATUS.RUNNING); } comparators = new List <BaseCompare>(); Dictionary <string, string> EndEvent = new Dictionary <string, string>(); BaseCompare c = new BaseCompare(); var watch = System.Diagnostics.Stopwatch.StartNew(); if (!c.TryCompare(opts.FirstRunId, opts.SecondRunId)) { Log.Warning(Strings.Get("Err_Comparing") + " : {0}", c.GetType().Name); } watch.Stop(); TimeSpan t = TimeSpan.FromMilliseconds(watch.ElapsedMilliseconds); string answer = string.Format(CultureInfo.InvariantCulture, "{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds); Log.Information(Strings.Get("Completed"), "Comparing", answer); if (opts.Analyze) { watch = Stopwatch.StartNew(); Analyzer analyzer = new Analyzer(DatabaseManager.RunIdToPlatform(opts.FirstRunId), opts.AnalysesFile); var violations = analyzer.VerifyRules(); if (violations.Any()) { foreach (var violation in violations) { Log.Warning(violation); } Log.Error("Encountered {0} issues with rules in {1}. Skipping analysis.", violations.Count, opts.AnalysesFile ?? "Embedded"); } else { if (c.Results.Count > 0) { foreach (var key in c.Results.Keys) { if (c.Results[key] is ConcurrentQueue <CompareResult> queue) { Parallel.ForEach(queue, (res) => { res.Rules = analyzer.Analyze(res); res.Analysis = res.Rules.Max(x => x.Flag); }); } } } } watch.Stop(); t = TimeSpan.FromMilliseconds(watch.ElapsedMilliseconds); answer = string.Format(CultureInfo.InvariantCulture, "{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds); Log.Information(Strings.Get("Completed"), "Analysis", answer); } watch = Stopwatch.StartNew(); if (opts.SaveToDatabase) { foreach (var key in c.Results.Keys) { if (c.Results.TryGetValue(key, out ConcurrentQueue <CompareResult>?obj)) { if (obj is ConcurrentQueue <CompareResult> Queue) { foreach (var result in Queue) { DatabaseManager.InsertAnalyzed(result); } } } } } watch.Stop(); t = TimeSpan.FromMilliseconds(watch.ElapsedMilliseconds); answer = string.Format(CultureInfo.InvariantCulture, "{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds); Log.Information(Strings.Get("Completed"), "Flushing", answer); if (opts.SaveToDatabase) { DatabaseManager.UpdateCompareRun(opts.FirstRunId, opts.SecondRunId, RUN_STATUS.COMPLETED); } DatabaseManager.Commit(); AsaTelemetry.TrackEvent("End Command", EndEvent); return(c.Results); }
private static int RunExportCollectCommand(ExportCollectCommandOptions opts) { #if DEBUG Logger.Setup(true, opts.Verbose, opts.Quiet); #else Logger.Setup(opts.Debug, opts.Verbose, opts.Quiet); #endif if (opts.OutputPath != null && !Directory.Exists(opts.OutputPath)) { Log.Fatal(Strings.Get("Err_OutputPathNotExist"), opts.OutputPath); return(0); } SetupOrDie(opts.DatabaseFilename); CheckFirstRun(); AsaTelemetry.Setup(); if (opts.FirstRunId is null || opts.SecondRunId is null) { Log.Information("Provided null run Ids using latest two runs."); List <string> runIds = DatabaseManager.GetLatestRunIds(2, RUN_TYPE.COLLECT); if (runIds.Count < 2) { Log.Fatal(Strings.Get("Err_CouldntDetermineTwoRun")); System.Environment.Exit(-1); } else { opts.SecondRunId = runIds.First(); opts.FirstRunId = runIds.ElementAt(1); } } if (opts.FirstRunId is null || opts.SecondRunId is null) { return((int)ASA_ERROR.INVALID_ID); } Log.Information(Strings.Get("Comparing"), opts.FirstRunId, opts.SecondRunId); Dictionary <string, string> StartEvent = new Dictionary <string, string>(); StartEvent.Add("OutputPathSet", (opts.OutputPath != null).ToString(CultureInfo.InvariantCulture)); AsaTelemetry.TrackEvent("{0} Export Compare", StartEvent); CompareCommandOptions options = new CompareCommandOptions(opts.FirstRunId, opts.SecondRunId) { DatabaseFilename = opts.DatabaseFilename, AnalysesFile = opts.AnalysesFile, Analyze = opts.Analyze, SaveToDatabase = opts.SaveToDatabase }; var results = CompareRuns(options).Select(x => new KeyValuePair <string, object>($"{x.Key.Item1}_{x.Key.Item2}", x.Value)).ToDictionary(x => x.Key, x => x.Value); JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings() { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore, Converters = new List <JsonConverter>() { new StringEnumConverter() }, ContractResolver = new AsaExportContractResolver() }); var outputPath = opts.OutputPath; if (outputPath is null) { outputPath = Directory.GetCurrentDirectory(); } if (opts.ExplodedOutput) { results.Add("metadata", AsaHelpers.GenerateMetadata()); string path = Path.Combine(outputPath, AsaHelpers.MakeValidFileName(opts.FirstRunId + "_vs_" + opts.SecondRunId)); Directory.CreateDirectory(path); foreach (var key in results.Keys) { string filePath = Path.Combine(path, AsaHelpers.MakeValidFileName(key)); using (StreamWriter sw = new StreamWriter(filePath)) //lgtm[cs/path-injection] { using (JsonWriter writer = new JsonTextWriter(sw)) { serializer.Serialize(writer, results[key]); } } } Log.Information(Strings.Get("OutputWrittenTo"), (new DirectoryInfo(path)).FullName); } else { string path = Path.Combine(outputPath, AsaHelpers.MakeValidFileName(opts.FirstRunId + "_vs_" + opts.SecondRunId + "_summary.json.txt")); var output = new Dictionary <string, object>(); output["results"] = results; output["metadata"] = AsaHelpers.GenerateMetadata(); using (StreamWriter sw = new StreamWriter(path)) //lgtm[cs/path-injection] { using (JsonWriter writer = new JsonTextWriter(sw)) { serializer.Serialize(writer, output); } } Log.Information(Strings.Get("OutputWrittenTo"), (new FileInfo(path)).FullName); } return(0); }
public static Dictionary <string, object> CompareRuns(CompareCommandOptions opts) { if (opts is null) { throw new ArgumentNullException(nameof(opts)); } if (opts.SaveToDatabase) { DatabaseManager.InsertCompareRun(opts.FirstRunId, opts.SecondRunId, RUN_STATUS.RUNNING); } var results = new Dictionary <string, object>(); comparators = new List <BaseCompare>(); Dictionary <string, string> EndEvent = new Dictionary <string, string>(); BaseCompare c = new BaseCompare(); var watch = System.Diagnostics.Stopwatch.StartNew(); if (!c.TryCompare(opts.FirstRunId, opts.SecondRunId)) { Log.Warning(Strings.Get("Err_Comparing") + " : {0}", c.GetType().Name); } c.Results.ToList().ForEach(x => results.Add(x.Key, x.Value)); watch.Stop(); TimeSpan t = TimeSpan.FromMilliseconds(watch.ElapsedMilliseconds); string answer = string.Format(CultureInfo.InvariantCulture, "{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds); Log.Information(Strings.Get("Completed"), "Comparing", answer); if (opts.Analyze) { watch = System.Diagnostics.Stopwatch.StartNew(); Analyzer analyzer = new Analyzer(DatabaseManager.RunIdToPlatform(opts.FirstRunId), opts.AnalysesFile); if (!analyzer.VerifyRules()) { return(results); } if (results.Count > 0) { foreach (var key in results.Keys) { try { Parallel.ForEach(results[key] as ConcurrentQueue <CompareResult>, (res) => { res.Rules = analyzer.Analyze(res); res.Analysis = res.Rules.Max(x => x.Flag); }); } catch (ArgumentNullException) { } } } watch.Stop(); t = TimeSpan.FromMilliseconds(watch.ElapsedMilliseconds); answer = string.Format(CultureInfo.InvariantCulture, "{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds); Log.Information(Strings.Get("Completed"), "Analysis", answer); } watch = System.Diagnostics.Stopwatch.StartNew(); if (opts.SaveToDatabase) { foreach (var key in results.Keys) { try { if (results[key] is ConcurrentQueue <CompareResult> queue) { foreach (var result in queue) { DatabaseManager.InsertAnalyzed(result); } } } catch (NullReferenceException) { Log.Debug(key); } } } watch.Stop(); t = TimeSpan.FromMilliseconds(watch.ElapsedMilliseconds); answer = string.Format(CultureInfo.InvariantCulture, "{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds); Log.Information(Strings.Get("Completed"), "Flushing", answer); if (opts.SaveToDatabase) { DatabaseManager.UpdateCompareRun(opts.FirstRunId, opts.SecondRunId, RUN_STATUS.COMPLETED); } DatabaseManager.Commit(); AsaTelemetry.TrackEvent("End Command", EndEvent); return(results); }