-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scan): provide a default scan factory (#76)
closes #68
- Loading branch information
Showing
51 changed files
with
1,445 additions
and
190 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,116 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using SecTester.Core; | ||
using SecTester.Core.Utils; | ||
using SecTester.Scan.Models; | ||
using SecTester.Scan.Models.HarSpec; | ||
|
||
namespace SecTester.Scan; | ||
|
||
public class DefaultScanFactory : ScanFactory | ||
{ | ||
internal const int MaxSlugLength = 200; | ||
|
||
private static readonly IEnumerable<Discovery> DefaultDiscoveryTypes = new List<Discovery> | ||
{ | ||
Discovery.Archive | ||
}; | ||
|
||
private readonly Configuration _configuration; | ||
private readonly ILogger _logger; | ||
|
||
private readonly Scans _scans; | ||
private readonly SystemTimeProvider _systemTimeProvider; | ||
|
||
public DefaultScanFactory(Configuration configuration, Scans scans, ILogger logger, | ||
SystemTimeProvider systemTimeProvider) | ||
{ | ||
_scans = scans ?? throw new ArgumentNullException(nameof(scans)); | ||
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); | ||
_systemTimeProvider = systemTimeProvider ?? throw new ArgumentNullException(nameof(systemTimeProvider)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public async Task<Scan> CreateScan(ScanSettingsOptions settingsOptions, ScanOptions? options) | ||
{ | ||
var scanConfig = await BuildScanConfig(new ScanSettings(settingsOptions)).ConfigureAwait(false); | ||
var scanId = await _scans.CreateScan(scanConfig).ConfigureAwait(false); | ||
|
||
return new Scan(scanId, _scans, _logger, options ?? new ScanOptions()); | ||
} | ||
|
||
private async Task<ScanConfig> BuildScanConfig(ScanSettings scanSettings) | ||
{ | ||
var fileId = await CreateAndUploadHar((Target)scanSettings.Target).ConfigureAwait(false); | ||
|
||
return new ScanConfig(scanSettings.Name!) | ||
{ | ||
FileId = fileId, | ||
Smart = scanSettings.Smart, | ||
PoolSize = scanSettings.PoolSize, | ||
SkipStaticParams = scanSettings.SkipStaticParams, | ||
Module = Module.Dast, | ||
DiscoveryTypes = DefaultDiscoveryTypes, | ||
AttackParamLocations = scanSettings.AttackParamLocations, | ||
Tests = scanSettings.Tests, | ||
Repeaters = scanSettings.RepeaterId is null | ||
? default | ||
: new List<string> | ||
{ | ||
scanSettings.RepeaterId | ||
}, | ||
SlowEpTimeout = | ||
scanSettings.SlowEpTimeout is null ? default : (int)scanSettings.SlowEpTimeout.Value.TotalSeconds, | ||
TargetTimeout = | ||
scanSettings.TargetTimeout is null ? default : (int)scanSettings.TargetTimeout.Value.TotalSeconds | ||
}; | ||
} | ||
|
||
private async Task<string> CreateAndUploadHar(Target target) | ||
{ | ||
var filename = GenerateFileName(target.Url); | ||
var har = await CreateHar(target).ConfigureAwait(false); | ||
|
||
return await _scans.UploadHar(new UploadHarOptions(har, filename, true)).ConfigureAwait(false); | ||
} | ||
|
||
private static string GenerateFileName(string url) | ||
{ | ||
var host = new Uri(url).Host; | ||
|
||
host = host.Length <= MaxSlugLength ? host : host.Substring(0, MaxSlugLength); | ||
|
||
return $"{host.TrimEnd(".-".ToCharArray())}-{Guid.NewGuid()}.har"; | ||
} | ||
|
||
private async Task<Entry> CreateHarEntry(Target target) | ||
{ | ||
return new Entry(_systemTimeProvider.Now, | ||
await target.ToHarRequest().ConfigureAwait(false), | ||
new ResponseMessage(200, "OK", "", new Content(-1, "text/plain")) | ||
{ | ||
HttpVersion = "HTTP/1.1" | ||
}, | ||
new Timings(), | ||
new Cache() | ||
); | ||
} | ||
|
||
private async Task<Har> CreateHar(Target target) | ||
{ | ||
var entry = await CreateHarEntry(target).ConfigureAwait(false); | ||
|
||
return new Har( | ||
new Log( | ||
new Tool(_configuration.Name, _configuration.Version)) | ||
{ | ||
Entries = new List<Entry> | ||
{ | ||
entry | ||
} | ||
} | ||
); | ||
} | ||
} |
This file contains 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 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,3 @@ | ||
namespace SecTester.Scan.Models.HarSpec; | ||
|
||
public record Cache; |
This file contains 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,3 @@ | ||
namespace SecTester.Scan.Models.HarSpec; | ||
|
||
public record Content(int Size, string MimeType); |
2 changes: 1 addition & 1 deletion
2
src/SecTester.Scan/Target/Har/Cookie.cs → src/SecTester.Scan/Models/HarSpec/Cookie.cs
This file contains 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 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,8 @@ | ||
using System; | ||
|
||
namespace SecTester.Scan.Models.HarSpec; | ||
|
||
public record Entry(DateTime StartedDateTime, RequestMessage RequestMessage, ResponseMessage ResponseMessage, Timings Timings, Cache Cache) | ||
{ | ||
public int Time { get; init; } | ||
} |
6 changes: 3 additions & 3 deletions
6
src/SecTester.Scan/Target/Har/Message.cs → ...ester.Scan/Models/HarSpec/EntryMessage.cs
This file contains 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 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,3 @@ | ||
namespace SecTester.Scan.Models.HarSpec; | ||
|
||
public record Har(Log Log); |
2 changes: 1 addition & 1 deletion
2
src/SecTester.Scan/Target/Har/Header.cs → src/SecTester.Scan/Models/HarSpec/Header.cs
This file contains 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
namespace SecTester.Scan.Target.Har; | ||
namespace SecTester.Scan.Models.HarSpec; | ||
|
||
public record Header(string Name, string Value) : Parameter(Name, Value); |
This file contains 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,9 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace SecTester.Scan.Models.HarSpec; | ||
|
||
public record Log(Tool Creator) | ||
{ | ||
public IEnumerable<Entry> Entries { get; init; } = new List<Entry>(); | ||
public string Version { get; init; } = "1.2"; | ||
} |
2 changes: 1 addition & 1 deletion
2
src/SecTester.Scan/Target/Har/Parameter.cs → ...ecTester.Scan/Models/HarSpec/Parameter.cs
This file contains 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
namespace SecTester.Scan.Target.Har; | ||
namespace SecTester.Scan.Models.HarSpec; | ||
|
||
public record Parameter(string Name, string Value); |
2 changes: 1 addition & 1 deletion
2
src/SecTester.Scan/Target/Har/PostData.cs → ...SecTester.Scan/Models/HarSpec/PostData.cs
This file contains 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
2 changes: 1 addition & 1 deletion
2
...ster.Scan/Target/Har/PostDataParameter.cs → ....Scan/Models/HarSpec/PostDataParameter.cs
This file contains 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
2 changes: 1 addition & 1 deletion
2
...cTester.Scan/Target/Har/QueryParameter.cs → ...ter.Scan/Models/HarSpec/QueryParameter.cs
This file contains 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
namespace SecTester.Scan.Target.Har; | ||
namespace SecTester.Scan.Models.HarSpec; | ||
|
||
public record QueryParameter(string Name, string Value) : Parameter(Name, Value); |
4 changes: 2 additions & 2 deletions
4
src/SecTester.Scan/Target/Har/Request.cs → ...ter.Scan/Models/HarSpec/RequestMessage.cs
This file contains 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 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,3 @@ | ||
namespace SecTester.Scan.Models.HarSpec; | ||
|
||
public record ResponseMessage(int Status, string StatusText, string RedirectUrl, Content Content) : EntryMessage; |
This file contains 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,3 @@ | ||
namespace SecTester.Scan.Models.HarSpec; | ||
|
||
public record Timings(int Send = 0, int Wait = 0, int Receive = 0); |
This file contains 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,3 @@ | ||
namespace SecTester.Scan.Models.HarSpec; | ||
|
||
public record Tool(string Name, string Version); |
This file contains 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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
using System; | ||
|
||
namespace SecTester.Scan.Models; | ||
|
||
public record Identifiable<T>(T Id) | ||
{ | ||
public T Id { get; } = Id ?? throw new ArgumentNullException(nameof(Id)); | ||
}; | ||
} |
This file contains 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 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
2 changes: 1 addition & 1 deletion
2
src/SecTester.Scan/Target/TargetOptions.cs → src/SecTester.Scan/Models/TargetOptions.cs
This file contains 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 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
Oops, something went wrong.