-
Notifications
You must be signed in to change notification settings - Fork 100
FEATURE: Nessus + CIS CAT Converters #2574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ejohn20
wants to merge
4
commits into
main
Choose a base branch
from
2531/converter-nessus-cis-cat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
|
|
||
| using Microsoft.CodeAnalysis.Sarif.Converters.CisCatObjectModel; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Sarif.Converters | ||
| { | ||
| public class CisCatConverter : ToolFileConverterBase | ||
| { | ||
| private readonly LogReader<CisCatReport> logReader; | ||
|
|
||
| public CisCatConverter() | ||
| { | ||
| logReader = new CisCatReportReader(); | ||
| } | ||
|
|
||
| public override string ToolName => ToolFormat.CisCat; | ||
|
|
||
| public override void Convert(Stream input, IResultLogWriter output, OptionallyEmittedData dataToInsert) | ||
| { | ||
| input = input ?? throw new ArgumentNullException(nameof(input)); | ||
| output = output ?? throw new ArgumentNullException(nameof(output)); | ||
|
|
||
| //Read CIS CAT data | ||
| CisCatReport log = logReader.ReadLog(input); | ||
|
|
||
| //Top level run object for the scan data | ||
| var run = new Run(); | ||
|
|
||
| //Set the tool details | ||
| run.Tool = new Tool(); | ||
| run.Tool.Driver = CreateDriver(log); | ||
|
|
||
| //Set the list of tool rules | ||
| run.Tool.Driver.Rules = new List<ReportingDescriptor>(); | ||
| foreach (CisCatRule rule in log.Rules) | ||
| { | ||
| run.Tool.Driver.Rules.Add(CreateReportDescriptor(rule)); | ||
| } | ||
|
|
||
| var results = new List<Result>(); | ||
| foreach (CisCatRule rule in log.Rules.Where(i => !i.IsPass())) | ||
| { | ||
| results.Add(CreateResult(rule)); | ||
| } | ||
|
|
||
| PersistResults(output, results, run); | ||
| } | ||
|
|
||
| internal ToolComponent CreateDriver(CisCatReport report) | ||
| { | ||
|
|
||
| var driver = new ToolComponent(); | ||
|
|
||
| driver.Name = this.ToolName; | ||
| driver.FullName = report.BenchmarkTitle; | ||
| driver.Version = report.BenchmarkVersion; | ||
| driver.SemanticVersion = report.BenchmarkVersion; | ||
| driver.InformationUri = new Uri("https://www.cisecurity.org/cybersecurity-tools/cis-cat-pro_pre"); | ||
|
|
||
| driver.SetProperty("benchmarkId", report.BenchmarkId); | ||
| driver.SetProperty("profileId", report.ProfileId); | ||
| driver.SetProperty("profileTitle", report.ProfileTitle); | ||
| driver.SetProperty("score", report.Score); | ||
|
|
||
| return driver; | ||
| } | ||
|
|
||
| internal ReportingDescriptor CreateReportDescriptor(CisCatRule rule) | ||
| { | ||
| ReportingDescriptor descriptor = new ReportingDescriptor(); | ||
|
|
||
| descriptor.Id = rule.RuleId; | ||
| descriptor.Name = rule.RuleTitle; | ||
| descriptor.ShortDescription = new MultiformatMessageString() | ||
| { | ||
| Text = rule.RuleTitle, | ||
| Markdown = rule.RuleTitle, | ||
| }; | ||
| descriptor.FullDescription = new MultiformatMessageString() | ||
| { | ||
| Text = rule.RuleTitle, | ||
| Markdown = rule.RuleTitle, | ||
| }; | ||
| descriptor.Help = new MultiformatMessageString() | ||
| { | ||
| Text = rule.RuleTitle, | ||
| Markdown = rule.RuleTitle, | ||
| }; | ||
|
|
||
| //Use for GH Security Advisories | ||
| //set result level and rank (Critical - Low risk rating) | ||
| FailureLevel level = FailureLevel.None; | ||
| ResultKind kind = ResultKind.None; | ||
| double rank = RankConstants.None; | ||
| getResultSeverity(rule.Result, out level, out kind, out rank); | ||
|
|
||
| //Create only if a valid is assigned | ||
| if (rank != RankConstants.None) | ||
Check warningCode scanning / CodeQL Equality check on floating point values
Equality checks on floating point values can yield unexpected results.
|
||
| { | ||
| descriptor.SetProperty("security-severity", rank.ToString("F1")); | ||
| } | ||
|
|
||
| //Tags for GH filtering | ||
| var tags = new List<string>() | ||
| { | ||
| "security", | ||
| }; | ||
|
|
||
| descriptor.SetProperty("tags", tags); | ||
|
|
||
| return descriptor; | ||
| } | ||
|
|
||
| internal Result CreateResult(CisCatRule rule) | ||
| { | ||
| //set the result metadata | ||
| Result result = new Result | ||
| { | ||
| RuleId = rule.RuleId, | ||
| Message = new Message { Text = rule.RuleTitle }, | ||
| }; | ||
|
|
||
| //set result kind, level and rank (Critical - Low risk rating) | ||
| FailureLevel level = FailureLevel.None; | ||
| ResultKind kind = ResultKind.None; | ||
| double rank = RankConstants.None; | ||
| getResultSeverity(rule.Result, out level, out kind, out rank); | ||
|
|
||
| //Set result object data | ||
| result.Level = level; | ||
| result.Kind = kind; | ||
| result.Rank = rank; | ||
|
|
||
| //Set the unique fingerprint | ||
| result.Fingerprints = new Dictionary<string, string>(); | ||
| result.Fingerprints.Add("0", HashUtilities.ComputeSha256HashValue(rule.RuleId).ToLower()); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private void getResultSeverity(string result, out FailureLevel level, out ResultKind kind, out double rank) | ||
| { | ||
| // Default values | ||
| level = FailureLevel.None; | ||
| kind = ResultKind.None; | ||
| rank = RankConstants.None; | ||
|
|
||
| //Kind & Level determine the status | ||
| //Result: "fail": Level = Error, Kind = Fail | ||
| //Result: "info|notchecked|pass|unknown": Level = None, Kind = Informational|NotApplicable|Pass|Review | ||
| switch (result) | ||
| { | ||
| case "pass": | ||
| level = FailureLevel.None; | ||
| kind = ResultKind.Pass; | ||
| rank = RankConstants.None; | ||
| break; | ||
| case "fail": | ||
| level = FailureLevel.Error; | ||
| kind = ResultKind.Fail; | ||
| rank = RankConstants.High; | ||
| break; | ||
| case "notchecked": | ||
| level = FailureLevel.None; | ||
| kind = ResultKind.NotApplicable; | ||
| rank = RankConstants.None; | ||
| break; | ||
| case "informational": | ||
| level = FailureLevel.None; | ||
| kind = ResultKind.Informational; | ||
| rank = RankConstants.None; | ||
| break; | ||
| case "unknown": | ||
| default: | ||
| level = FailureLevel.Warning; | ||
| kind = ResultKind.Fail; | ||
| rank = RankConstants.Medium; | ||
| break; | ||
| }; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System.Collections.Generic; | ||
|
|
||
| using Newtonsoft.Json; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Sarif.Converters.CisCatObjectModel | ||
| { | ||
| public class CisCatReport | ||
| { | ||
| [JsonProperty("benchmark-id")] | ||
| public string BenchmarkId { get; set; } | ||
|
|
||
| [JsonProperty("benchmark-title")] | ||
| public string BenchmarkTitle { get; set; } | ||
|
|
||
| [JsonProperty("benchmark-version")] | ||
| public string BenchmarkVersion { get; set; } | ||
|
|
||
| [JsonProperty("profile-id")] | ||
| public string ProfileId { get; set; } | ||
|
|
||
| [JsonProperty("profile-title")] | ||
| public string ProfileTitle { get; set; } | ||
|
|
||
| [JsonProperty("score")] | ||
| public string Score { get; set; } | ||
|
|
||
| [JsonProperty("rules")] | ||
| public IEnumerable<CisCatRule> Rules { get; set; } | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
src/Sarif.Converters/CisCatObjectModel/CisCatReportReader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.IO; | ||
|
|
||
| using Newtonsoft.Json; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Sarif.Converters.CisCatObjectModel | ||
| { | ||
| public class CisCatReportReader : LogReader<CisCatReport> | ||
| { | ||
| public override CisCatReport ReadLog(Stream input) | ||
| { | ||
| string reportData; | ||
|
|
||
| using (TextReader streamReader = new StreamReader(input)) | ||
| { | ||
| reportData = streamReader.ReadToEnd(); | ||
| } | ||
|
|
||
| return JsonConvert.DeserializeObject<CisCatReport>(reportData); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Newtonsoft.Json; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Sarif.Converters.CisCatObjectModel | ||
| { | ||
| public class CisCatRule | ||
| { | ||
| [JsonProperty("rule-id")] | ||
| public string RuleId { get; set; } | ||
|
|
||
| [JsonProperty("rule-title")] | ||
| public string RuleTitle { get; set; } | ||
|
|
||
| [JsonProperty("result")] | ||
| public string Result { get; set; } | ||
|
|
||
| public bool IsPass() | ||
| { | ||
| return this.Result == "pass"; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest to only log a rule if its referenced by a result and avoid logging duplicated rules.
This can be done in the results loop below and checking if the rule already exists in the `log.Rules' list.