mirror of
https://github.com/volodymyrsmirnov/MalwareMultiScan.git
synced 2025-08-24 05:22:22 +00:00
finished unit tests and docstrings
This commit is contained in:
parent
b2902c128a
commit
b68c285ce5
@ -11,9 +11,9 @@ namespace MalwareMultiScan.Api.Attributes
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
||||||
{
|
{
|
||||||
if (!Uri.TryCreate((string)value, UriKind.Absolute, out var uri)
|
if (!Uri.TryCreate((string) value, UriKind.Absolute, out var uri)
|
||||||
|| !uri.IsAbsoluteUri
|
|| !uri.IsAbsoluteUri
|
||||||
|| uri.Scheme.ToLowerInvariant() != "http"
|
|| uri.Scheme.ToLowerInvariant() != "http"
|
||||||
&& uri.Scheme.ToLowerInvariant() != "https")
|
&& uri.Scheme.ToLowerInvariant() != "https")
|
||||||
return new ValidationResult("Only absolute http(s) URLs are supported");
|
return new ValidationResult("Only absolute http(s) URLs are supported");
|
||||||
|
|
||||||
|
@ -16,12 +16,12 @@ namespace MalwareMultiScan.Api.Attributes
|
|||||||
var maxSize = validationContext
|
var maxSize = validationContext
|
||||||
.GetRequiredService<IConfiguration>()
|
.GetRequiredService<IConfiguration>()
|
||||||
.GetValue<long>("MaxFileSize");
|
.GetValue<long>("MaxFileSize");
|
||||||
|
|
||||||
var formFile = (IFormFile) value;
|
var formFile = (IFormFile) value;
|
||||||
|
|
||||||
if (formFile == null || formFile.Length > maxSize)
|
if (formFile == null || formFile.Length > maxSize)
|
||||||
return new ValidationResult($"File exceeds the maximum size of {maxSize} bytes");
|
return new ValidationResult($"File exceeds the maximum size of {maxSize} bytes");
|
||||||
|
|
||||||
return ValidationResult.Success;
|
return ValidationResult.Success;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,9 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Api.Controllers
|
namespace MalwareMultiScan.Api.Controllers
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Downloads controller.
|
||||||
|
/// </summary>
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/download")]
|
[Route("api/download")]
|
||||||
[Produces("application/octet-stream")]
|
[Produces("application/octet-stream")]
|
||||||
@ -12,11 +15,19 @@ namespace MalwareMultiScan.Api.Controllers
|
|||||||
{
|
{
|
||||||
private readonly IScanResultService _scanResultService;
|
private readonly IScanResultService _scanResultService;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize downloads controller.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="scanResultService">Scan result service.</param>
|
||||||
public DownloadController(IScanResultService scanResultService)
|
public DownloadController(IScanResultService scanResultService)
|
||||||
{
|
{
|
||||||
_scanResultService = scanResultService;
|
_scanResultService = scanResultService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Download file by id.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">File id.</param>
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
@ -8,6 +8,9 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Api.Controllers
|
namespace MalwareMultiScan.Api.Controllers
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Queue controller.
|
||||||
|
/// </summary>
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/queue")]
|
[Route("api/queue")]
|
||||||
[Produces("application/json")]
|
[Produces("application/json")]
|
||||||
@ -15,23 +18,33 @@ namespace MalwareMultiScan.Api.Controllers
|
|||||||
{
|
{
|
||||||
private readonly IScanResultService _scanResultService;
|
private readonly IScanResultService _scanResultService;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize queue controller.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="scanResultService">Scan result service.</param>
|
||||||
public QueueController(IScanResultService scanResultService)
|
public QueueController(IScanResultService scanResultService)
|
||||||
{
|
{
|
||||||
_scanResultService = scanResultService;
|
_scanResultService = scanResultService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Queue file for scanning.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="file">File from form data.</param>
|
||||||
[HttpPost("file")]
|
[HttpPost("file")]
|
||||||
[ProducesResponseType(typeof(ScanResult), StatusCodes.Status201Created)]
|
[ProducesResponseType(typeof(ScanResult), StatusCodes.Status201Created)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
public async Task<IActionResult> ScanFile(
|
public async Task<IActionResult> ScanFile(
|
||||||
[Required, MaxFileSize] IFormFile file)
|
[Required] [MaxFileSize] IFormFile file)
|
||||||
{
|
{
|
||||||
var result = await _scanResultService.CreateScanResult();
|
var result = await _scanResultService.CreateScanResult();
|
||||||
|
|
||||||
string storedFileId;
|
string storedFileId;
|
||||||
|
|
||||||
await using (var uploadFileStream = file.OpenReadStream())
|
await using (var uploadFileStream = file.OpenReadStream())
|
||||||
|
{
|
||||||
storedFileId = await _scanResultService.StoreFile(file.FileName, uploadFileStream);
|
storedFileId = await _scanResultService.StoreFile(file.FileName, uploadFileStream);
|
||||||
|
}
|
||||||
|
|
||||||
await _scanResultService.QueueUrlScan(result, Url.Action("Index", "Download", new {id = storedFileId},
|
await _scanResultService.QueueUrlScan(result, Url.Action("Index", "Download", new {id = storedFileId},
|
||||||
Request?.Scheme, Request?.Host.Value));
|
Request?.Scheme, Request?.Host.Value));
|
||||||
@ -39,11 +52,15 @@ namespace MalwareMultiScan.Api.Controllers
|
|||||||
return CreatedAtAction("Index", "ScanResults", new {id = result.Id}, result);
|
return CreatedAtAction("Index", "ScanResults", new {id = result.Id}, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Queue URL for scanning.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">URL from form data.</param>
|
||||||
[HttpPost("url")]
|
[HttpPost("url")]
|
||||||
[ProducesResponseType(typeof(ScanResult), StatusCodes.Status201Created)]
|
[ProducesResponseType(typeof(ScanResult), StatusCodes.Status201Created)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
public async Task<IActionResult> ScanUrl(
|
public async Task<IActionResult> ScanUrl(
|
||||||
[FromForm, Required, IsHttpUrl] string url)
|
[FromForm] [Required] [IsHttpUrl] string url)
|
||||||
{
|
{
|
||||||
var result = await _scanResultService.CreateScanResult();
|
var result = await _scanResultService.CreateScanResult();
|
||||||
|
|
||||||
|
@ -1,25 +1,34 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MalwareMultiScan.Api.Data.Models;
|
using MalwareMultiScan.Api.Data.Models;
|
||||||
using MalwareMultiScan.Api.Services;
|
|
||||||
using MalwareMultiScan.Api.Services.Implementations;
|
|
||||||
using MalwareMultiScan.Api.Services.Interfaces;
|
using MalwareMultiScan.Api.Services.Interfaces;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Api.Controllers
|
namespace MalwareMultiScan.Api.Controllers
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Scan results controller.
|
||||||
|
/// </summary>
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/results")]
|
[Route("api/results")]
|
||||||
[Produces("application/json")]
|
[Produces("application/json")]
|
||||||
public class ScanResultsController : Controller
|
public class ScanResultsController : Controller
|
||||||
{
|
{
|
||||||
private readonly IScanResultService _scanResultService;
|
private readonly IScanResultService _scanResultService;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize scan results controller.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="scanResultService">Scan result service.</param>
|
||||||
public ScanResultsController(IScanResultService scanResultService)
|
public ScanResultsController(IScanResultService scanResultService)
|
||||||
{
|
{
|
||||||
_scanResultService = scanResultService;
|
_scanResultService = scanResultService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get scan result by id.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">Scan result id.</param>
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
[ProducesResponseType(typeof(ScanResult), StatusCodes.Status200OK)]
|
[ProducesResponseType(typeof(ScanResult), StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
@ -1,8 +1,18 @@
|
|||||||
namespace MalwareMultiScan.Api.Data.Configuration
|
namespace MalwareMultiScan.Api.Data.Configuration
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Scan backend.
|
||||||
|
/// </summary>
|
||||||
public class ScanBackend
|
public class ScanBackend
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Backend id.
|
||||||
|
/// </summary>
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Backend state.
|
||||||
|
/// </summary>
|
||||||
public bool Enabled { get; set; }
|
public bool Enabled { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,12 +4,21 @@ using MongoDB.Bson.Serialization.Attributes;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Api.Data.Models
|
namespace MalwareMultiScan.Api.Data.Models
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Scan result.
|
||||||
|
/// </summary>
|
||||||
public class ScanResult
|
public class ScanResult
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Result id.
|
||||||
|
/// </summary>
|
||||||
[BsonId]
|
[BsonId]
|
||||||
[BsonRepresentation(BsonType.ObjectId)]
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Result entries where key is backend id and value is <see cref="ScanResultEntry"/>.
|
||||||
|
/// </summary>
|
||||||
public Dictionary<string, ScanResultEntry> Results { get; set; } =
|
public Dictionary<string, ScanResultEntry> Results { get; set; } =
|
||||||
new Dictionary<string, ScanResultEntry>();
|
new Dictionary<string, ScanResultEntry>();
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,28 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace MalwareMultiScan.Api.Data.Models
|
namespace MalwareMultiScan.Api.Data.Models
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Scan result entry.
|
||||||
|
/// </summary>
|
||||||
public class ScanResultEntry
|
public class ScanResultEntry
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Completion status.
|
||||||
|
/// </summary>
|
||||||
public bool Completed { get; set; }
|
public bool Completed { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Indicates that scanning completed without error.
|
||||||
|
/// </summary>
|
||||||
public bool? Succeeded { get; set; }
|
public bool? Succeeded { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Scanning duration in seconds.
|
||||||
|
/// </summary>
|
||||||
public long Duration { get; set; }
|
public long Duration { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Detected names of threats.
|
||||||
|
/// </summary>
|
||||||
public string[] Threats { get; set; }
|
public string[] Threats { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,5 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using EasyNetQ;
|
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.HttpOverrides;
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
@ -23,12 +22,6 @@ namespace MalwareMultiScan.Api.Extensions
|
|||||||
services.AddSingleton<IGridFSBucket>(new GridFSBucket(db));
|
services.AddSingleton<IGridFSBucket>(new GridFSBucket(db));
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void AddRabbitMq(this IServiceCollection services, IConfiguration configuration)
|
|
||||||
{
|
|
||||||
services.AddSingleton(x =>
|
|
||||||
RabbitHutch.CreateBus(configuration.GetConnectionString("RabbitMQ")));
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static void AddDockerForwardedHeadersOptions(this IServiceCollection services)
|
internal static void AddDockerForwardedHeadersOptions(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.Configure<ForwardedHeadersOptions>(options =>
|
services.Configure<ForwardedHeadersOptions>(options =>
|
||||||
|
@ -2,6 +2,11 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
<Company>Volodymyr Smirnov</Company>
|
||||||
|
<Product>MalwareMultiScan Api</Product>
|
||||||
|
<AssemblyVersion>1.0.0</AssemblyVersion>
|
||||||
|
<FileVersion>1.0.0</FileVersion>
|
||||||
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@ -11,7 +16,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="EasyNetQ" Version="5.6.0" />
|
|
||||||
<PackageReference Include="MongoDB.Driver" Version="2.11.3" />
|
<PackageReference Include="MongoDB.Driver" Version="2.11.3" />
|
||||||
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.11.3" />
|
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.11.3" />
|
||||||
<PackageReference Include="YamlDotNet" Version="8.1.2" />
|
<PackageReference Include="YamlDotNet" Version="8.1.2" />
|
||||||
|
@ -5,7 +5,7 @@ using Microsoft.Extensions.Hosting;
|
|||||||
namespace MalwareMultiScan.Api
|
namespace MalwareMultiScan.Api
|
||||||
{
|
{
|
||||||
[ExcludeFromCodeCoverage]
|
[ExcludeFromCodeCoverage]
|
||||||
public static class Program
|
internal static class Program
|
||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
@ -8,13 +8,21 @@ using Microsoft.Extensions.Logging;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Api.Services.Implementations
|
namespace MalwareMultiScan.Api.Services.Implementations
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
public class ReceiverHostedService : IReceiverHostedService
|
public class ReceiverHostedService : IReceiverHostedService
|
||||||
{
|
{
|
||||||
private readonly IBus _bus;
|
private readonly IBus _bus;
|
||||||
private readonly IConfiguration _configuration;
|
private readonly IConfiguration _configuration;
|
||||||
private readonly ILogger<ReceiverHostedService> _logger;
|
private readonly ILogger<ReceiverHostedService> _logger;
|
||||||
private readonly IScanResultService _scanResultService;
|
private readonly IScanResultService _scanResultService;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize receiver hosted service.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bus">EasyNetQ bus.</param>
|
||||||
|
/// <param name="configuration">Configuration.</param>
|
||||||
|
/// <param name="scanResultService">Scan result service.</param>
|
||||||
|
/// <param name="logger">Logger.</param>
|
||||||
public ReceiverHostedService(IBus bus, IConfiguration configuration, IScanResultService scanResultService,
|
public ReceiverHostedService(IBus bus, IConfiguration configuration, IScanResultService scanResultService,
|
||||||
ILogger<ReceiverHostedService> logger)
|
ILogger<ReceiverHostedService> logger)
|
||||||
{
|
{
|
||||||
@ -23,13 +31,15 @@ namespace MalwareMultiScan.Api.Services.Implementations
|
|||||||
_scanResultService = scanResultService;
|
_scanResultService = scanResultService;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public Task StartAsync(CancellationToken cancellationToken)
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_bus.Receive<ScanResultMessage>(_configuration.GetValue<string>("ResultsSubscriptionId"), async message =>
|
_bus.Receive<ScanResultMessage>(_configuration.GetValue<string>("ResultsSubscriptionId"), async message =>
|
||||||
{
|
{
|
||||||
message.Threats ??= new string[] { };
|
message.Threats ??= new string[] { };
|
||||||
|
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
$"Received a result from {message.Backend} for {message.Id} " +
|
$"Received a result from {message.Backend} for {message.Id} " +
|
||||||
$"with threats {string.Join(",", message.Threats)}");
|
$"with threats {string.Join(",", message.Threats)}");
|
||||||
@ -44,7 +54,8 @@ namespace MalwareMultiScan.Api.Services.Implementations
|
|||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public Task StopAsync(CancellationToken cancellationToken)
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_bus?.Dispose();
|
_bus?.Dispose();
|
||||||
|
@ -13,11 +13,19 @@ using YamlDotNet.Serialization.NamingConventions;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Api.Services.Implementations
|
namespace MalwareMultiScan.Api.Services.Implementations
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
public class ScanBackendService : IScanBackendService
|
public class ScanBackendService : IScanBackendService
|
||||||
{
|
{
|
||||||
private readonly IBus _bus;
|
private readonly IBus _bus;
|
||||||
private readonly ILogger<ScanBackendService> _logger;
|
private readonly ILogger<ScanBackendService> _logger;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialise scan backend service.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="configuration">Configuration.</param>
|
||||||
|
/// <param name="bus">EasyNetQ bus.</param>
|
||||||
|
/// <param name="logger">Logger.</param>
|
||||||
|
/// <exception cref="FileNotFoundException">Missing backends.yaml configuration.</exception>
|
||||||
public ScanBackendService(IConfiguration configuration, IBus bus, ILogger<ScanBackendService> logger)
|
public ScanBackendService(IConfiguration configuration, IBus bus, ILogger<ScanBackendService> logger)
|
||||||
{
|
{
|
||||||
_bus = bus;
|
_bus = bus;
|
||||||
@ -36,9 +44,11 @@ namespace MalwareMultiScan.Api.Services.Implementations
|
|||||||
|
|
||||||
List = deserializer.Deserialize<ScanBackend[]>(configurationContent);
|
List = deserializer.Deserialize<ScanBackend[]>(configurationContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public ScanBackend[] List { get; }
|
public ScanBackend[] List { get; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public async Task QueueUrlScan(ScanResult result, ScanBackend backend, string fileUrl)
|
public async Task QueueUrlScan(ScanResult result, ScanBackend backend, string fileUrl)
|
||||||
{
|
{
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
|
@ -9,6 +9,7 @@ using MongoDB.Driver.GridFS;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Api.Services.Implementations
|
namespace MalwareMultiScan.Api.Services.Implementations
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
public class ScanResultService : IScanResultService
|
public class ScanResultService : IScanResultService
|
||||||
{
|
{
|
||||||
private const string CollectionName = "ScanResults";
|
private const string CollectionName = "ScanResults";
|
||||||
@ -16,15 +17,22 @@ namespace MalwareMultiScan.Api.Services.Implementations
|
|||||||
private readonly IGridFSBucket _bucket;
|
private readonly IGridFSBucket _bucket;
|
||||||
private readonly IMongoCollection<ScanResult> _collection;
|
private readonly IMongoCollection<ScanResult> _collection;
|
||||||
private readonly IScanBackendService _scanBackendService;
|
private readonly IScanBackendService _scanBackendService;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize scan result service.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="db">Mongo database.</param>
|
||||||
|
/// <param name="bucket">GridFS bucket.</param>
|
||||||
|
/// <param name="scanBackendService">Scan backend service.</param>
|
||||||
public ScanResultService(IMongoDatabase db, IGridFSBucket bucket, IScanBackendService scanBackendService)
|
public ScanResultService(IMongoDatabase db, IGridFSBucket bucket, IScanBackendService scanBackendService)
|
||||||
{
|
{
|
||||||
_bucket = bucket;
|
_bucket = bucket;
|
||||||
_scanBackendService = scanBackendService;
|
_scanBackendService = scanBackendService;
|
||||||
|
|
||||||
_collection = db.GetCollection<ScanResult>(CollectionName);
|
_collection = db.GetCollection<ScanResult>(CollectionName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public async Task<ScanResult> CreateScanResult()
|
public async Task<ScanResult> CreateScanResult()
|
||||||
{
|
{
|
||||||
var scanResult = new ScanResult
|
var scanResult = new ScanResult
|
||||||
@ -38,7 +46,8 @@ namespace MalwareMultiScan.Api.Services.Implementations
|
|||||||
|
|
||||||
return scanResult;
|
return scanResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public async Task<ScanResult> GetScanResult(string id)
|
public async Task<ScanResult> GetScanResult(string id)
|
||||||
{
|
{
|
||||||
var result = await _collection.FindAsync(
|
var result = await _collection.FindAsync(
|
||||||
@ -46,7 +55,8 @@ namespace MalwareMultiScan.Api.Services.Implementations
|
|||||||
|
|
||||||
return await result.FirstOrDefaultAsync();
|
return await result.FirstOrDefaultAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public async Task UpdateScanResultForBackend(string resultId, string backendId, long duration,
|
public async Task UpdateScanResultForBackend(string resultId, string backendId, long duration,
|
||||||
bool completed = false, bool succeeded = false, string[] threats = null)
|
bool completed = false, bool succeeded = false, string[] threats = null)
|
||||||
{
|
{
|
||||||
@ -60,13 +70,15 @@ namespace MalwareMultiScan.Api.Services.Implementations
|
|||||||
Threats = threats ?? new string[] { }
|
Threats = threats ?? new string[] { }
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public async Task QueueUrlScan(ScanResult result, string fileUrl)
|
public async Task QueueUrlScan(ScanResult result, string fileUrl)
|
||||||
{
|
{
|
||||||
foreach (var backend in _scanBackendService.List.Where(b => b.Enabled))
|
foreach (var backend in _scanBackendService.List.Where(b => b.Enabled))
|
||||||
await _scanBackendService.QueueUrlScan(result, backend, fileUrl);
|
await _scanBackendService.QueueUrlScan(result, backend, fileUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public async Task<string> StoreFile(string fileName, Stream fileStream)
|
public async Task<string> StoreFile(string fileName, Stream fileStream)
|
||||||
{
|
{
|
||||||
var objectId = await _bucket.UploadFromStreamAsync(
|
var objectId = await _bucket.UploadFromStreamAsync(
|
||||||
@ -74,7 +86,8 @@ namespace MalwareMultiScan.Api.Services.Implementations
|
|||||||
|
|
||||||
return objectId.ToString();
|
return objectId.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public async Task<Stream> ObtainFile(string id)
|
public async Task<Stream> ObtainFile(string id)
|
||||||
{
|
{
|
||||||
if (!ObjectId.TryParse(id, out var objectId))
|
if (!ObjectId.TryParse(id, out var objectId))
|
||||||
|
@ -2,8 +2,10 @@ using Microsoft.Extensions.Hosting;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Api.Services.Interfaces
|
namespace MalwareMultiScan.Api.Services.Interfaces
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Receiver hosted service.
|
||||||
|
/// </summary>
|
||||||
public interface IReceiverHostedService : IHostedService
|
public interface IReceiverHostedService : IHostedService
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,10 +4,22 @@ using MalwareMultiScan.Api.Data.Models;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Api.Services.Interfaces
|
namespace MalwareMultiScan.Api.Services.Interfaces
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Scan backend service.
|
||||||
|
/// </summary>
|
||||||
public interface IScanBackendService
|
public interface IScanBackendService
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Get list of parsed backends.
|
||||||
|
/// </summary>
|
||||||
ScanBackend[] List { get; }
|
ScanBackend[] List { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Queue URL for scan.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="result">Result entry.</param>
|
||||||
|
/// <param name="backend">Backend entry.</param>
|
||||||
|
/// <param name="fileUrl">Remote URL.</param>
|
||||||
Task QueueUrlScan(ScanResult result, ScanBackend backend, string fileUrl);
|
Task QueueUrlScan(ScanResult result, ScanBackend backend, string fileUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,19 +4,56 @@ using MalwareMultiScan.Api.Data.Models;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Api.Services.Interfaces
|
namespace MalwareMultiScan.Api.Services.Interfaces
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Scan result service.
|
||||||
|
/// </summary>
|
||||||
public interface IScanResultService
|
public interface IScanResultService
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Create scan result.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Scan result entry with id.</returns>
|
||||||
Task<ScanResult> CreateScanResult();
|
Task<ScanResult> CreateScanResult();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get scan result.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">Result id.</param>
|
||||||
|
/// <returns>Scan result entry with id.</returns>
|
||||||
Task<ScanResult> GetScanResult(string id);
|
Task<ScanResult> GetScanResult(string id);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update scan result entry.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="resultId">Result id.</param>
|
||||||
|
/// <param name="backendId">Backend id.</param>
|
||||||
|
/// <param name="duration">Duration.</param>
|
||||||
|
/// <param name="completed">Completion status.</param>
|
||||||
|
/// <param name="succeeded">Indicates that scanning completed without error.</param>
|
||||||
|
/// <param name="threats">Detected names of threats.</param>
|
||||||
Task UpdateScanResultForBackend(string resultId, string backendId, long duration,
|
Task UpdateScanResultForBackend(string resultId, string backendId, long duration,
|
||||||
bool completed = false, bool succeeded = false, string[] threats = null);
|
bool completed = false, bool succeeded = false, string[] threats = null);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Queue URL for scanning.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="result">Result entry.</param>
|
||||||
|
/// <param name="fileUrl">Remote URL.</param>
|
||||||
Task QueueUrlScan(ScanResult result, string fileUrl);
|
Task QueueUrlScan(ScanResult result, string fileUrl);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Store file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fileName">File name.</param>
|
||||||
|
/// <param name="fileStream">File stream.</param>
|
||||||
|
/// <returns>Unique file id.</returns>
|
||||||
Task<string> StoreFile(string fileName, Stream fileStream);
|
Task<string> StoreFile(string fileName, Stream fileStream);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Obtain file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">Unique file id.</param>
|
||||||
|
/// <returns>Seekable file stream opened for reading.</returns>
|
||||||
Task<Stream> ObtainFile(string id);
|
Task<Stream> ObtainFile(string id);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,6 +2,7 @@ using System.Diagnostics.CodeAnalysis;
|
|||||||
using MalwareMultiScan.Api.Extensions;
|
using MalwareMultiScan.Api.Extensions;
|
||||||
using MalwareMultiScan.Api.Services.Implementations;
|
using MalwareMultiScan.Api.Services.Implementations;
|
||||||
using MalwareMultiScan.Api.Services.Interfaces;
|
using MalwareMultiScan.Api.Services.Interfaces;
|
||||||
|
using MalwareMultiScan.Backends.Extensions;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
@ -22,7 +23,7 @@ namespace MalwareMultiScan.Api
|
|||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddDockerForwardedHeadersOptions();
|
services.AddDockerForwardedHeadersOptions();
|
||||||
|
|
||||||
services.Configure<KestrelServerOptions>(options =>
|
services.Configure<KestrelServerOptions>(options =>
|
||||||
{
|
{
|
||||||
options.Limits.MaxRequestBodySize = _configuration.GetValue<int>("MaxFileSize");
|
options.Limits.MaxRequestBodySize = _configuration.GetValue<int>("MaxFileSize");
|
||||||
|
@ -1,77 +1,64 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Extensions.Logging;
|
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Backends.Abstracts
|
namespace MalwareMultiScan.Backends.Backends.Abstracts
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
public abstract class AbstractLocalProcessScanBackend : AbstractScanBackend
|
public abstract class AbstractLocalProcessScanBackend : AbstractScanBackend
|
||||||
{
|
{
|
||||||
private readonly ILogger _logger;
|
private readonly IProcessRunner _processRunner;
|
||||||
|
|
||||||
protected AbstractLocalProcessScanBackend(ILogger logger)
|
/// <inheritdoc />
|
||||||
|
protected AbstractLocalProcessScanBackend(IProcessRunner processRunner)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_processRunner = processRunner;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Regex to extract names of threats.
|
||||||
|
/// </summary>
|
||||||
protected abstract Regex MatchRegex { get; }
|
protected abstract Regex MatchRegex { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Path to the backend.
|
||||||
|
/// </summary>
|
||||||
protected abstract string BackendPath { get; }
|
protected abstract string BackendPath { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parse StdErr instead of StdOut.
|
||||||
|
/// </summary>
|
||||||
protected virtual bool ParseStdErr { get; } = false;
|
protected virtual bool ParseStdErr { get; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Throw on non-zero exit code.
|
||||||
|
/// </summary>
|
||||||
protected virtual bool ThrowOnNonZeroExitCode { get; } = true;
|
protected virtual bool ThrowOnNonZeroExitCode { get; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get backend process parameters.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">Path to the temporary file.</param>
|
||||||
|
/// <returns>Formatted string with parameters and path.</returns>
|
||||||
protected abstract string GetBackendArguments(string path);
|
protected abstract string GetBackendArguments(string path);
|
||||||
|
|
||||||
public override async Task<string[]> ScanAsync(string path, CancellationToken cancellationToken)
|
/// <inheritdoc />
|
||||||
|
public override Task<string[]> ScanAsync(string path, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var process = new Process
|
var exitCode = _processRunner.RunTillCompletion(BackendPath, GetBackendArguments(path), cancellationToken,
|
||||||
{
|
out var standardOutput, out var standardError);
|
||||||
StartInfo = new ProcessStartInfo(BackendPath, GetBackendArguments(path))
|
|
||||||
{
|
|
||||||
RedirectStandardError = true,
|
|
||||||
RedirectStandardOutput = true,
|
|
||||||
WorkingDirectory = Path.GetDirectoryName(BackendPath) ?? Directory.GetCurrentDirectory()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
_logger.LogInformation(
|
if (ThrowOnNonZeroExitCode && exitCode != 0)
|
||||||
$"Starting process {process.StartInfo.FileName} " +
|
throw new ApplicationException($"Process has terminated with an exit code {exitCode}");
|
||||||
$"with arguments {process.StartInfo.Arguments} " +
|
|
||||||
$"in working directory {process.StartInfo.WorkingDirectory}");
|
|
||||||
|
|
||||||
process.Start();
|
return Task.FromResult(MatchRegex
|
||||||
|
|
||||||
cancellationToken.Register(() =>
|
|
||||||
{
|
|
||||||
if (process.HasExited)
|
|
||||||
return;
|
|
||||||
|
|
||||||
process.Kill(true);
|
|
||||||
|
|
||||||
throw new TimeoutException("Scanning failed to complete within the timeout");
|
|
||||||
});
|
|
||||||
|
|
||||||
process.WaitForExit();
|
|
||||||
|
|
||||||
_logger.LogInformation($"Process has exited with code {process.ExitCode}");
|
|
||||||
|
|
||||||
var standardOutput = await process.StandardOutput.ReadToEndAsync();
|
|
||||||
var standardError = await process.StandardError.ReadToEndAsync();
|
|
||||||
|
|
||||||
_logger.LogDebug($"Process standard output: {standardOutput}");
|
|
||||||
_logger.LogDebug($"Process standard error: {standardError}");
|
|
||||||
|
|
||||||
if (ThrowOnNonZeroExitCode && process.ExitCode != 0)
|
|
||||||
throw new ApplicationException($"Process has terminated with an exit code {process.ExitCode}");
|
|
||||||
|
|
||||||
return MatchRegex
|
|
||||||
.Matches(ParseStdErr ? standardError : standardOutput)
|
.Matches(ParseStdErr ? standardError : standardOutput)
|
||||||
.Where(x => x.Success)
|
.Where(x => x.Success)
|
||||||
.Select(x => x.Groups["threat"].Value)
|
.Select(x => x.Groups["threat"].Value)
|
||||||
.ToArray();
|
.ToArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@ -7,12 +8,17 @@ using MalwareMultiScan.Backends.Interfaces;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Backends.Abstracts
|
namespace MalwareMultiScan.Backends.Backends.Abstracts
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
[ExcludeFromCodeCoverage]
|
||||||
public abstract class AbstractScanBackend : IScanBackend
|
public abstract class AbstractScanBackend : IScanBackend
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
public abstract string Id { get; }
|
public abstract string Id { get; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public abstract Task<string[]> ScanAsync(string path, CancellationToken cancellationToken);
|
public abstract Task<string[]> ScanAsync(string path, CancellationToken cancellationToken);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public async Task<string[]> ScanAsync(Uri uri, CancellationToken cancellationToken)
|
public async Task<string[]> ScanAsync(Uri uri, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
using var httpClient = new HttpClient();
|
using var httpClient = new HttpClient();
|
||||||
@ -22,6 +28,7 @@ namespace MalwareMultiScan.Backends.Backends.Abstracts
|
|||||||
return await ScanAsync(uriStream, cancellationToken);
|
return await ScanAsync(uriStream, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public async Task<string[]> ScanAsync(Stream stream, CancellationToken cancellationToken)
|
public async Task<string[]> ScanAsync(Stream stream, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var tempFile = Path.GetTempFileName();
|
var tempFile = Path.GetTempFileName();
|
||||||
|
@ -1,24 +1,31 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using MalwareMultiScan.Backends.Backends.Abstracts;
|
using MalwareMultiScan.Backends.Backends.Abstracts;
|
||||||
using Microsoft.Extensions.Logging;
|
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
public class ClamavScanBackend : AbstractLocalProcessScanBackend
|
public class ClamavScanBackend : AbstractLocalProcessScanBackend
|
||||||
{
|
{
|
||||||
public ClamavScanBackend(ILogger logger) : base(logger)
|
/// <inheritdoc />
|
||||||
|
public ClamavScanBackend(IProcessRunner processRunner) : base(processRunner)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public override string Id { get; } = "clamav";
|
public override string Id { get; } = "clamav";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override string BackendPath { get; } = "/usr/bin/clamdscan";
|
protected override string BackendPath { get; } = "/usr/bin/clamdscan";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override Regex MatchRegex { get; } =
|
protected override Regex MatchRegex { get; } =
|
||||||
new Regex(@"(\S+): (?<threat>[\S]+) FOUND", RegexOptions.Compiled | RegexOptions.Multiline);
|
new Regex(@"(\S+): (?<threat>[\S]+) FOUND", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override bool ThrowOnNonZeroExitCode { get; } = false;
|
protected override bool ThrowOnNonZeroExitCode { get; } = false;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override string GetBackendArguments(string path)
|
protected override string GetBackendArguments(string path)
|
||||||
{
|
{
|
||||||
return $"-m --fdpass --no-summary {path}";
|
return $"-m --fdpass --no-summary {path}";
|
||||||
|
@ -1,23 +1,29 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using MalwareMultiScan.Backends.Backends.Abstracts;
|
using MalwareMultiScan.Backends.Backends.Abstracts;
|
||||||
using Microsoft.Extensions.Logging;
|
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
public class ComodoScanBackend : AbstractLocalProcessScanBackend
|
public class ComodoScanBackend : AbstractLocalProcessScanBackend
|
||||||
{
|
{
|
||||||
public ComodoScanBackend(ILogger logger) : base(logger)
|
/// <inheritdoc />
|
||||||
|
public ComodoScanBackend(IProcessRunner processRunner) : base(processRunner)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public override string Id { get; } = "comodo";
|
public override string Id { get; } = "comodo";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override string BackendPath { get; } = "/opt/COMODO/cmdscan";
|
protected override string BackendPath { get; } = "/opt/COMODO/cmdscan";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override Regex MatchRegex { get; } =
|
protected override Regex MatchRegex { get; } =
|
||||||
new Regex(@".* ---> Found Virus, Malware Name is (?<threat>.*)",
|
new Regex(@".* ---> Found Virus, Malware Name is (?<threat>.*)",
|
||||||
RegexOptions.Compiled | RegexOptions.Multiline);
|
RegexOptions.Compiled | RegexOptions.Multiline);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override string GetBackendArguments(string path)
|
protected override string GetBackendArguments(string path)
|
||||||
{
|
{
|
||||||
return $"-v -s {path}";
|
return $"-v -s {path}";
|
||||||
|
@ -1,22 +1,28 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using MalwareMultiScan.Backends.Backends.Abstracts;
|
using MalwareMultiScan.Backends.Backends.Abstracts;
|
||||||
using Microsoft.Extensions.Logging;
|
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
public class DrWebScanBackend : AbstractLocalProcessScanBackend
|
public class DrWebScanBackend : AbstractLocalProcessScanBackend
|
||||||
{
|
{
|
||||||
public DrWebScanBackend(ILogger logger) : base(logger)
|
/// <inheritdoc />
|
||||||
|
public DrWebScanBackend(IProcessRunner processRunner) : base(processRunner)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public override string Id { get; } = "drweb";
|
public override string Id { get; } = "drweb";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override string BackendPath { get; } = "/usr/bin/drweb-ctl";
|
protected override string BackendPath { get; } = "/usr/bin/drweb-ctl";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override Regex MatchRegex { get; } =
|
protected override Regex MatchRegex { get; } =
|
||||||
new Regex(@".* - infected with (?<threat>[\S ]+)", RegexOptions.Compiled | RegexOptions.Multiline);
|
new Regex(@".* - infected with (?<threat>[\S ]+)", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override string GetBackendArguments(string path)
|
protected override string GetBackendArguments(string path)
|
||||||
{
|
{
|
||||||
return $"scan {path}";
|
return $"scan {path}";
|
||||||
|
@ -6,20 +6,25 @@ using MalwareMultiScan.Backends.Interfaces;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
public class DummyScanBackend : IScanBackend
|
public class DummyScanBackend : IScanBackend
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
public string Id { get; } = "dummy";
|
public string Id { get; } = "dummy";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public Task<string[]> ScanAsync(string path, CancellationToken cancellationToken)
|
public Task<string[]> ScanAsync(string path, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return Scan();
|
return Scan();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public Task<string[]> ScanAsync(Uri uri, CancellationToken cancellationToken)
|
public Task<string[]> ScanAsync(Uri uri, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return Scan();
|
return Scan();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public Task<string[]> ScanAsync(Stream stream, CancellationToken cancellationToken)
|
public Task<string[]> ScanAsync(Stream stream, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return Scan();
|
return Scan();
|
||||||
@ -29,7 +34,7 @@ namespace MalwareMultiScan.Backends.Backends.Implementations
|
|||||||
{
|
{
|
||||||
await Task.Delay(
|
await Task.Delay(
|
||||||
TimeSpan.FromSeconds(5));
|
TimeSpan.FromSeconds(5));
|
||||||
|
|
||||||
return new[] {"Malware.Dummy.Result"};
|
return new[] {"Malware.Dummy.Result"};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,28 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using MalwareMultiScan.Backends.Backends.Abstracts;
|
using MalwareMultiScan.Backends.Backends.Abstracts;
|
||||||
using Microsoft.Extensions.Logging;
|
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
public class KesScanBackend : AbstractLocalProcessScanBackend
|
public class KesScanBackend : AbstractLocalProcessScanBackend
|
||||||
{
|
{
|
||||||
public KesScanBackend(ILogger logger) : base(logger)
|
/// <inheritdoc />
|
||||||
|
public KesScanBackend(IProcessRunner processRunner) : base(processRunner)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public override string Id { get; } = "kes";
|
public override string Id { get; } = "kes";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override string BackendPath { get; } = "/bin/bash";
|
protected override string BackendPath { get; } = "/bin/bash";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override Regex MatchRegex { get; } =
|
protected override Regex MatchRegex { get; } =
|
||||||
new Regex(@"[ +]DetectName.*: (?<threat>.*)", RegexOptions.Compiled | RegexOptions.Multiline);
|
new Regex(@"[ +]DetectName.*: (?<threat>.*)", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override string GetBackendArguments(string path)
|
protected override string GetBackendArguments(string path)
|
||||||
{
|
{
|
||||||
return $"/usr/bin/kesl-scan {path}";
|
return $"/usr/bin/kesl-scan {path}";
|
||||||
|
@ -1,24 +1,31 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using MalwareMultiScan.Backends.Backends.Abstracts;
|
using MalwareMultiScan.Backends.Backends.Abstracts;
|
||||||
using Microsoft.Extensions.Logging;
|
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
public class McAfeeScanBackend : AbstractLocalProcessScanBackend
|
public class McAfeeScanBackend : AbstractLocalProcessScanBackend
|
||||||
{
|
{
|
||||||
public McAfeeScanBackend(ILogger logger) : base(logger)
|
/// <inheritdoc />
|
||||||
|
public McAfeeScanBackend(IProcessRunner processRunner) : base(processRunner)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public override string Id { get; } = "mcafee";
|
public override string Id { get; } = "mcafee";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override string BackendPath { get; } = "/usr/local/uvscan/uvscan";
|
protected override string BackendPath { get; } = "/usr/local/uvscan/uvscan";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override bool ThrowOnNonZeroExitCode { get; } = false;
|
protected override bool ThrowOnNonZeroExitCode { get; } = false;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override Regex MatchRegex { get; } =
|
protected override Regex MatchRegex { get; } =
|
||||||
new Regex(@".* ... Found: (?<threat>.*).", RegexOptions.Compiled | RegexOptions.Multiline);
|
new Regex(@".* ... Found: (?<threat>.*).", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override string GetBackendArguments(string path)
|
protected override string GetBackendArguments(string path)
|
||||||
{
|
{
|
||||||
return $"--SECURE {path}";
|
return $"--SECURE {path}";
|
||||||
|
@ -1,24 +1,31 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using MalwareMultiScan.Backends.Backends.Abstracts;
|
using MalwareMultiScan.Backends.Backends.Abstracts;
|
||||||
using Microsoft.Extensions.Logging;
|
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
public class SophosScanBackend : AbstractLocalProcessScanBackend
|
public class SophosScanBackend : AbstractLocalProcessScanBackend
|
||||||
{
|
{
|
||||||
public SophosScanBackend(ILogger logger) : base(logger)
|
/// <inheritdoc />
|
||||||
|
public SophosScanBackend(IProcessRunner processRunner) : base(processRunner)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public override string Id { get; } = "sophos";
|
public override string Id { get; } = "sophos";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override string BackendPath { get; } = "/opt/sophos-av/bin/savscan";
|
protected override string BackendPath { get; } = "/opt/sophos-av/bin/savscan";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override bool ThrowOnNonZeroExitCode { get; } = false;
|
protected override bool ThrowOnNonZeroExitCode { get; } = false;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override Regex MatchRegex { get; } =
|
protected override Regex MatchRegex { get; } =
|
||||||
new Regex(@">>> Virus '(?<threat>.*)' found in file .*", RegexOptions.Compiled | RegexOptions.Multiline);
|
new Regex(@">>> Virus '(?<threat>.*)' found in file .*", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override string GetBackendArguments(string path)
|
protected override string GetBackendArguments(string path)
|
||||||
{
|
{
|
||||||
return $"-f -archive -ss {path}";
|
return $"-f -archive -ss {path}";
|
||||||
|
@ -1,25 +1,32 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using MalwareMultiScan.Backends.Backends.Abstracts;
|
using MalwareMultiScan.Backends.Backends.Abstracts;
|
||||||
using Microsoft.Extensions.Logging;
|
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
public class WindowsDefenderScanBackend : AbstractLocalProcessScanBackend
|
public class WindowsDefenderScanBackend : AbstractLocalProcessScanBackend
|
||||||
{
|
{
|
||||||
public WindowsDefenderScanBackend(ILogger logger) : base(logger)
|
/// <inheritdoc />
|
||||||
|
public WindowsDefenderScanBackend(IProcessRunner processRunner) : base(processRunner)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public override string Id { get; } = "windows-defender";
|
public override string Id { get; } = "windows-defender";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override string BackendPath { get; } = "/opt/mpclient";
|
protected override string BackendPath { get; } = "/opt/mpclient";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override Regex MatchRegex { get; } =
|
protected override Regex MatchRegex { get; } =
|
||||||
new Regex(@"EngineScanCallback\(\): Threat (?<threat>[\S]+) identified",
|
new Regex(@"EngineScanCallback\(\): Threat (?<threat>[\S]+) identified",
|
||||||
RegexOptions.Compiled | RegexOptions.Multiline);
|
RegexOptions.Compiled | RegexOptions.Multiline);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override bool ParseStdErr { get; } = true;
|
protected override bool ParseStdErr { get; } = true;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override string GetBackendArguments(string path)
|
protected override string GetBackendArguments(string path)
|
||||||
{
|
{
|
||||||
return path;
|
return path;
|
||||||
|
@ -1,14 +1,48 @@
|
|||||||
namespace MalwareMultiScan.Backends.Enums
|
namespace MalwareMultiScan.Backends.Enums
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Backend type.
|
||||||
|
/// </summary>
|
||||||
public enum BackendType
|
public enum BackendType
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Dummy
|
||||||
|
/// </summary>
|
||||||
Dummy,
|
Dummy,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Windows Defender.
|
||||||
|
/// </summary>
|
||||||
Defender,
|
Defender,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// ClamAV.
|
||||||
|
/// </summary>
|
||||||
Clamav,
|
Clamav,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// DrWeb.
|
||||||
|
/// </summary>
|
||||||
DrWeb,
|
DrWeb,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// KES.
|
||||||
|
/// </summary>
|
||||||
Kes,
|
Kes,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Comodo.
|
||||||
|
/// </summary>
|
||||||
Comodo,
|
Comodo,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sophos.
|
||||||
|
/// </summary>
|
||||||
Sophos,
|
Sophos,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// McAfee.
|
||||||
|
/// </summary>
|
||||||
McAfee
|
McAfee
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,32 +1,72 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using EasyNetQ;
|
||||||
using MalwareMultiScan.Backends.Backends.Implementations;
|
using MalwareMultiScan.Backends.Backends.Implementations;
|
||||||
using MalwareMultiScan.Backends.Enums;
|
using MalwareMultiScan.Backends.Enums;
|
||||||
using MalwareMultiScan.Backends.Interfaces;
|
using MalwareMultiScan.Backends.Interfaces;
|
||||||
|
using MalwareMultiScan.Backends.Services.Implementations;
|
||||||
|
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Extensions
|
namespace MalwareMultiScan.Backends.Extensions
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Extensions for IServiceCollection.
|
||||||
|
/// </summary>
|
||||||
|
[ExcludeFromCodeCoverage]
|
||||||
public static class ServiceCollectionExtensions
|
public static class ServiceCollectionExtensions
|
||||||
{
|
{
|
||||||
public static void AddScanningBackend(this IServiceCollection services, BackendType type)
|
/// <summary>
|
||||||
|
/// Add RabbitMQ service.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services">Service collection.</param>
|
||||||
|
/// <param name="configuration">Configuration.</param>
|
||||||
|
public static void AddRabbitMq(this IServiceCollection services, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
using var provider = services.BuildServiceProvider();
|
services.AddSingleton(x =>
|
||||||
|
RabbitHutch.CreateBus(configuration.GetConnectionString("RabbitMQ")));
|
||||||
|
}
|
||||||
|
|
||||||
var logger = provider.GetService<ILogger<IScanBackend>>();
|
/// <summary>
|
||||||
|
/// Add scanning backend.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services">Service collection.</param>
|
||||||
|
/// <param name="configuration">Configuration.</param>
|
||||||
|
/// <exception cref="ArgumentOutOfRangeException">Unknown backend.</exception>
|
||||||
|
public static void AddScanningBackend(this IServiceCollection services, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IProcessRunner, ProcessRunner>();
|
||||||
|
|
||||||
services.AddSingleton<IScanBackend>(type switch
|
switch (configuration.GetValue<BackendType>("BackendType"))
|
||||||
{
|
{
|
||||||
BackendType.Dummy => new DummyScanBackend(),
|
case BackendType.Dummy:
|
||||||
BackendType.Defender => new WindowsDefenderScanBackend(logger),
|
services.AddSingleton<IScanBackend, DummyScanBackend>();
|
||||||
BackendType.Clamav => new ClamavScanBackend(logger),
|
break;
|
||||||
BackendType.DrWeb => new DrWebScanBackend(logger),
|
case BackendType.Defender:
|
||||||
BackendType.Kes => new KesScanBackend(logger),
|
services.AddSingleton<IScanBackend, WindowsDefenderScanBackend>();
|
||||||
BackendType.Comodo => new ComodoScanBackend(logger),
|
break;
|
||||||
BackendType.Sophos => new SophosScanBackend(logger),
|
case BackendType.Clamav:
|
||||||
BackendType.McAfee => new McAfeeScanBackend(logger),
|
services.AddSingleton<IScanBackend, ClamavScanBackend>();
|
||||||
_ => throw new NotImplementedException()
|
break;
|
||||||
});
|
case BackendType.DrWeb:
|
||||||
|
services.AddSingleton<IScanBackend, DrWebScanBackend>();
|
||||||
|
break;
|
||||||
|
case BackendType.Kes:
|
||||||
|
services.AddSingleton<IScanBackend, KesScanBackend>();
|
||||||
|
break;
|
||||||
|
case BackendType.Comodo:
|
||||||
|
services.AddSingleton<IScanBackend, ComodoScanBackend>();
|
||||||
|
break;
|
||||||
|
case BackendType.Sophos:
|
||||||
|
services.AddSingleton<IScanBackend, SophosScanBackend>();
|
||||||
|
break;
|
||||||
|
case BackendType.McAfee:
|
||||||
|
services.AddSingleton<IScanBackend, McAfeeScanBackend>();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,11 +5,38 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Interfaces
|
namespace MalwareMultiScan.Backends.Interfaces
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Scan backend.
|
||||||
|
/// </summary>
|
||||||
public interface IScanBackend
|
public interface IScanBackend
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Unique backend id.
|
||||||
|
/// </summary>
|
||||||
public string Id { get; }
|
public string Id { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Scan file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">File path.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token.</param>
|
||||||
|
/// <returns>List of detected threats.</returns>
|
||||||
public Task<string[]> ScanAsync(string path, CancellationToken cancellationToken);
|
public Task<string[]> ScanAsync(string path, CancellationToken cancellationToken);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Scan URL.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="uri">URL.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token.</param>
|
||||||
|
/// <returns>List of detected threats.</returns>
|
||||||
public Task<string[]> ScanAsync(Uri uri, CancellationToken cancellationToken);
|
public Task<string[]> ScanAsync(Uri uri, CancellationToken cancellationToken);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Scan stream.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stream">Stream.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token.</param>
|
||||||
|
/// <returns>List of detected threats.</returns>
|
||||||
public Task<string[]> ScanAsync(Stream stream, CancellationToken cancellationToken);
|
public Task<string[]> ScanAsync(Stream stream, CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,11 +2,19 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
<Company>Volodymyr Smirnov</Company>
|
||||||
|
<Product>MalwareMultiScan Backends</Product>
|
||||||
|
<AssemblyVersion>1.0.0</AssemblyVersion>
|
||||||
|
<FileVersion>1.0.0</FileVersion>
|
||||||
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="EasyNetQ" Version="5.6.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.9" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.9" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.9" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.9" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.9" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.9" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -2,10 +2,19 @@ using System;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Messages
|
namespace MalwareMultiScan.Backends.Messages
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Scan request message.
|
||||||
|
/// </summary>
|
||||||
public class ScanRequestMessage
|
public class ScanRequestMessage
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Result id.
|
||||||
|
/// </summary>
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remote URL.
|
||||||
|
/// </summary>
|
||||||
public Uri Uri { get; set; }
|
public Uri Uri { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,15 +1,33 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Messages
|
namespace MalwareMultiScan.Backends.Messages
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Scan result message.
|
||||||
|
/// </summary>
|
||||||
public class ScanResultMessage
|
public class ScanResultMessage
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Result id.
|
||||||
|
/// </summary>
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Backend.
|
||||||
|
/// </summary>
|
||||||
public string Backend { get; set; }
|
public string Backend { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Status.
|
||||||
|
/// </summary>
|
||||||
public bool Succeeded { get; set; }
|
public bool Succeeded { get; set; }
|
||||||
public string[] Threats { get; set; }
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// List of detected threats.
|
||||||
|
/// </summary>
|
||||||
|
public string[] Threats { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Duration.
|
||||||
|
/// </summary>
|
||||||
public long Duration { get; set; }
|
public long Duration { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,81 @@
|
|||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading;
|
||||||
|
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace MalwareMultiScan.Backends.Services.Implementations
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
[ExcludeFromCodeCoverage]
|
||||||
|
public class ProcessRunner : IProcessRunner
|
||||||
|
{
|
||||||
|
private readonly ILogger<ProcessRunner> _logger;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize process runner.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger">Logger.</param>
|
||||||
|
public ProcessRunner(ILogger<ProcessRunner> logger)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path"></param>
|
||||||
|
/// <param name="arguments"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <param name="standardOutput"></param>
|
||||||
|
/// <param name="standardError"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="TimeoutException"></exception>
|
||||||
|
public int RunTillCompletion(string path, string arguments, CancellationToken cancellationToken,
|
||||||
|
out string standardOutput, out string standardError)
|
||||||
|
{
|
||||||
|
var process = new Process
|
||||||
|
{
|
||||||
|
StartInfo = new ProcessStartInfo(path, arguments)
|
||||||
|
{
|
||||||
|
RedirectStandardError = true,
|
||||||
|
RedirectStandardOutput = true,
|
||||||
|
WorkingDirectory = Path.GetDirectoryName(path) ?? Directory.GetCurrentDirectory()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
_logger.LogInformation(
|
||||||
|
$"Starting process {process.StartInfo.FileName} " +
|
||||||
|
$"with arguments {process.StartInfo.Arguments} " +
|
||||||
|
$"in working directory {process.StartInfo.WorkingDirectory}");
|
||||||
|
|
||||||
|
process.Start();
|
||||||
|
|
||||||
|
cancellationToken.Register(() =>
|
||||||
|
{
|
||||||
|
if (process.HasExited)
|
||||||
|
return;
|
||||||
|
|
||||||
|
process.Kill(true);
|
||||||
|
|
||||||
|
throw new TimeoutException("Scanning failed to complete within the timeout");
|
||||||
|
});
|
||||||
|
|
||||||
|
process.WaitForExit();
|
||||||
|
|
||||||
|
_logger.LogInformation($"Process has exited with code {process.ExitCode}");
|
||||||
|
|
||||||
|
standardOutput = process.StandardOutput.ReadToEnd();
|
||||||
|
standardError = process.StandardError.ReadToEnd();
|
||||||
|
|
||||||
|
_logger.LogDebug($"Process standard output: {standardOutput}");
|
||||||
|
_logger.LogDebug($"Process standard error: {standardError}");
|
||||||
|
|
||||||
|
return process.ExitCode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace MalwareMultiScan.Backends.Services.Interfaces
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Process runner.
|
||||||
|
/// </summary>
|
||||||
|
public interface IProcessRunner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Run process till completion.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">Path to binary.</param>
|
||||||
|
/// <param name="arguments">Arguments.</param>
|
||||||
|
/// <param name="token">Cancellation token.</param>
|
||||||
|
/// <param name="standardOutput">Standard output of a process.</param>
|
||||||
|
/// <param name="standardError">Standard error of a process.</param>
|
||||||
|
/// <returns>Exit code.</returns>
|
||||||
|
int RunTillCompletion(string path, string arguments, CancellationToken token,
|
||||||
|
out string standardOutput, out string standardError);
|
||||||
|
}
|
||||||
|
}
|
@ -2,11 +2,15 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
<Company>Volodymyr Smirnov</Company>
|
||||||
|
<Product>MalwareMultiScan Scanner</Product>
|
||||||
|
<AssemblyVersion>1.0.0</AssemblyVersion>
|
||||||
|
<FileVersion>1.0.0</FileVersion>
|
||||||
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="EasyNetQ" Version="5.6.0" />
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.9" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.8" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MalwareMultiScan.Backends.Enums;
|
|
||||||
using MalwareMultiScan.Backends.Extensions;
|
using MalwareMultiScan.Backends.Extensions;
|
||||||
using MalwareMultiScan.Scanner.Services;
|
using MalwareMultiScan.Scanner.Services.Implementations;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Scanner
|
namespace MalwareMultiScan.Scanner
|
||||||
{
|
{
|
||||||
public static class Program
|
[ExcludeFromCodeCoverage]
|
||||||
|
internal static class Program
|
||||||
{
|
{
|
||||||
public static async Task Main(string[] args)
|
public static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
@ -22,8 +23,8 @@ namespace MalwareMultiScan.Scanner
|
|||||||
{
|
{
|
||||||
services.AddLogging();
|
services.AddLogging();
|
||||||
|
|
||||||
services.AddScanningBackend(
|
services.AddRabbitMq(context.Configuration);
|
||||||
context.Configuration.GetValue<BackendType>("BackendType"));
|
services.AddScanningBackend(context.Configuration);
|
||||||
|
|
||||||
services.AddHostedService<ScanHostedService>();
|
services.AddHostedService<ScanHostedService>();
|
||||||
}).RunConsoleAsync();
|
}).RunConsoleAsync();
|
||||||
|
@ -5,32 +5,41 @@ using System.Threading.Tasks;
|
|||||||
using EasyNetQ;
|
using EasyNetQ;
|
||||||
using MalwareMultiScan.Backends.Interfaces;
|
using MalwareMultiScan.Backends.Interfaces;
|
||||||
using MalwareMultiScan.Backends.Messages;
|
using MalwareMultiScan.Backends.Messages;
|
||||||
|
using MalwareMultiScan.Scanner.Services.Interfaces;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Scanner.Services
|
namespace MalwareMultiScan.Scanner.Services.Implementations
|
||||||
{
|
{
|
||||||
internal class ScanHostedService : IHostedService
|
/// <inheritdoc />
|
||||||
|
public class ScanHostedService : IScanHostedService
|
||||||
{
|
{
|
||||||
private readonly IScanBackend _backend;
|
private readonly IScanBackend _backend;
|
||||||
|
private readonly IBus _bus;
|
||||||
private readonly IConfiguration _configuration;
|
private readonly IConfiguration _configuration;
|
||||||
private readonly ILogger<ScanHostedService> _logger;
|
private readonly ILogger<ScanHostedService> _logger;
|
||||||
|
|
||||||
private IBus _bus;
|
/// <summary>
|
||||||
|
/// Initialise scan hosted service.
|
||||||
public ScanHostedService(ILogger<ScanHostedService> logger, IConfiguration configuration, IScanBackend backend)
|
/// </summary>
|
||||||
|
/// <param name="logger">Logger.</param>
|
||||||
|
/// <param name="backend">Scan backend.</param>
|
||||||
|
/// <param name="bus">EasyNetQ bus.</param>
|
||||||
|
/// <param name="configuration">Configuration.</param>
|
||||||
|
public ScanHostedService(
|
||||||
|
ILogger<ScanHostedService> logger,
|
||||||
|
IScanBackend backend, IBus bus,
|
||||||
|
IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_bus = bus;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
_backend = backend;
|
_backend = backend;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public Task StartAsync(CancellationToken cancellationToken)
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_bus = RabbitHutch.CreateBus(
|
|
||||||
_configuration.GetConnectionString("RabbitMQ"));
|
|
||||||
|
|
||||||
_bus.Receive<ScanRequestMessage>(_backend.Id, Scan);
|
_bus.Receive<ScanRequestMessage>(_backend.Id, Scan);
|
||||||
|
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
@ -39,6 +48,7 @@ namespace MalwareMultiScan.Scanner.Services
|
|||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public Task StopAsync(CancellationToken cancellationToken)
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_bus.Dispose();
|
_bus.Dispose();
|
||||||
@ -62,9 +72,9 @@ namespace MalwareMultiScan.Scanner.Services
|
|||||||
Id = message.Id,
|
Id = message.Id,
|
||||||
Backend = _backend.Id
|
Backend = _backend.Id
|
||||||
};
|
};
|
||||||
|
|
||||||
var stopwatch = new Stopwatch();
|
var stopwatch = new Stopwatch();
|
||||||
|
|
||||||
stopwatch.Start();
|
stopwatch.Start();
|
||||||
|
|
||||||
try
|
try
|
||||||
@ -91,10 +101,10 @@ namespace MalwareMultiScan.Scanner.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
result.Duration = stopwatch.ElapsedMilliseconds / 1000;
|
result.Duration = stopwatch.ElapsedMilliseconds / 1000;
|
||||||
|
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
$"Sending scan results with status {result.Succeeded}");
|
$"Sending scan results with status {result.Succeeded}");
|
||||||
|
|
||||||
await _bus.SendAsync(
|
await _bus.SendAsync(
|
||||||
_configuration.GetValue<string>("ResultsSubscriptionId"), result);
|
_configuration.GetValue<string>("ResultsSubscriptionId"), result);
|
||||||
}
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
|
namespace MalwareMultiScan.Scanner.Services.Interfaces
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Scan hosted service.
|
||||||
|
/// </summary>
|
||||||
|
public interface IScanHostedService : IHostedService
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
@ -8,7 +8,7 @@ using Microsoft.Extensions.Configuration.Memory;
|
|||||||
using Moq;
|
using Moq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Tests
|
namespace MalwareMultiScan.Tests.Api
|
||||||
{
|
{
|
||||||
public class AttributesTests
|
public class AttributesTests
|
||||||
{
|
{
|
||||||
@ -16,7 +16,7 @@ namespace MalwareMultiScan.Tests
|
|||||||
public void TestIsHttpUrlAttribute()
|
public void TestIsHttpUrlAttribute()
|
||||||
{
|
{
|
||||||
var attribute = new IsHttpUrlAttribute();
|
var attribute = new IsHttpUrlAttribute();
|
||||||
|
|
||||||
Assert.True(attribute.IsValid("http://test.com/file.zip"));
|
Assert.True(attribute.IsValid("http://test.com/file.zip"));
|
||||||
Assert.True(attribute.IsValid("https://test.com/file.zip"));
|
Assert.True(attribute.IsValid("https://test.com/file.zip"));
|
||||||
Assert.True(attribute.IsValid("HTTPS://test.com"));
|
Assert.True(attribute.IsValid("HTTPS://test.com"));
|
||||||
@ -30,7 +30,7 @@ namespace MalwareMultiScan.Tests
|
|||||||
public void TestMaxFileSizeAttribute()
|
public void TestMaxFileSizeAttribute()
|
||||||
{
|
{
|
||||||
var attribute = new MaxFileSizeAttribute();
|
var attribute = new MaxFileSizeAttribute();
|
||||||
|
|
||||||
var configuration = new ConfigurationRoot(new List<IConfigurationProvider>
|
var configuration = new ConfigurationRoot(new List<IConfigurationProvider>
|
||||||
{
|
{
|
||||||
new MemoryConfigurationProvider(new MemoryConfigurationSource())
|
new MemoryConfigurationProvider(new MemoryConfigurationSource())
|
||||||
@ -46,10 +46,10 @@ namespace MalwareMultiScan.Tests
|
|||||||
|
|
||||||
Assert.True(attribute.GetValidationResult(
|
Assert.True(attribute.GetValidationResult(
|
||||||
Mock.Of<IFormFile>(x => x.Length == 10), context) == ValidationResult.Success);
|
Mock.Of<IFormFile>(x => x.Length == 10), context) == ValidationResult.Success);
|
||||||
|
|
||||||
Assert.True(attribute.GetValidationResult(
|
Assert.True(attribute.GetValidationResult(
|
||||||
Mock.Of<IFormFile>(x => x.Length == 100), context) == ValidationResult.Success);
|
Mock.Of<IFormFile>(x => x.Length == 100), context) == ValidationResult.Success);
|
||||||
|
|
||||||
Assert.False(attribute.GetValidationResult(
|
Assert.False(attribute.GetValidationResult(
|
||||||
Mock.Of<IFormFile>(x => x.Length == 101), context) == ValidationResult.Success);
|
Mock.Of<IFormFile>(x => x.Length == 101), context) == ValidationResult.Success);
|
||||||
}
|
}
|
@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using Moq;
|
using Moq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Tests
|
namespace MalwareMultiScan.Tests.Api
|
||||||
{
|
{
|
||||||
public class ControllersTests
|
public class ControllersTests
|
||||||
{
|
{
|
||||||
@ -16,7 +16,7 @@ namespace MalwareMultiScan.Tests
|
|||||||
private QueueController _queueController;
|
private QueueController _queueController;
|
||||||
private ScanResultsController _scanResultsController;
|
private ScanResultsController _scanResultsController;
|
||||||
private Mock<IScanResultService> _scanResultServiceMock;
|
private Mock<IScanResultService> _scanResultServiceMock;
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void SetUp()
|
public void SetUp()
|
||||||
{
|
{
|
||||||
@ -25,7 +25,7 @@ namespace MalwareMultiScan.Tests
|
|||||||
_scanResultServiceMock
|
_scanResultServiceMock
|
||||||
.Setup(x => x.ObtainFile("existing"))
|
.Setup(x => x.ObtainFile("existing"))
|
||||||
.Returns(Task.FromResult(new MemoryStream() as Stream));
|
.Returns(Task.FromResult(new MemoryStream() as Stream));
|
||||||
|
|
||||||
_scanResultServiceMock
|
_scanResultServiceMock
|
||||||
.Setup(x => x.ObtainFile("non-existing"))
|
.Setup(x => x.ObtainFile("non-existing"))
|
||||||
.Returns(Task.FromResult((Stream) null));
|
.Returns(Task.FromResult((Stream) null));
|
||||||
@ -36,22 +36,22 @@ namespace MalwareMultiScan.Tests
|
|||||||
{
|
{
|
||||||
Id = "existing"
|
Id = "existing"
|
||||||
}));
|
}));
|
||||||
|
|
||||||
_scanResultServiceMock
|
_scanResultServiceMock
|
||||||
.Setup(x => x.GetScanResult("non-existing"))
|
.Setup(x => x.GetScanResult("non-existing"))
|
||||||
.Returns(Task.FromResult((ScanResult) null));
|
.Returns(Task.FromResult((ScanResult) null));
|
||||||
|
|
||||||
_scanResultServiceMock
|
_scanResultServiceMock
|
||||||
.Setup(x => x.ObtainFile("non-existing"))
|
.Setup(x => x.ObtainFile("non-existing"))
|
||||||
.Returns(Task.FromResult((Stream) null));
|
.Returns(Task.FromResult((Stream) null));
|
||||||
|
|
||||||
_scanResultServiceMock
|
_scanResultServiceMock
|
||||||
.Setup(x => x.CreateScanResult())
|
.Setup(x => x.CreateScanResult())
|
||||||
.Returns(Task.FromResult(new ScanResult()));
|
.Returns(Task.FromResult(new ScanResult()));
|
||||||
|
|
||||||
_downloadController = new DownloadController(_scanResultServiceMock.Object);
|
_downloadController = new DownloadController(_scanResultServiceMock.Object);
|
||||||
_scanResultsController = new ScanResultsController(_scanResultServiceMock.Object);
|
_scanResultsController = new ScanResultsController(_scanResultServiceMock.Object);
|
||||||
|
|
||||||
_queueController = new QueueController(_scanResultServiceMock.Object)
|
_queueController = new QueueController(_scanResultServiceMock.Object)
|
||||||
{
|
{
|
||||||
Url = Mock.Of<IUrlHelper>()
|
Url = Mock.Of<IUrlHelper>()
|
||||||
@ -71,7 +71,7 @@ namespace MalwareMultiScan.Tests
|
|||||||
Assert.True(
|
Assert.True(
|
||||||
await _scanResultsController.Index("existing") is OkObjectResult okObjectResult &&
|
await _scanResultsController.Index("existing") is OkObjectResult okObjectResult &&
|
||||||
okObjectResult.Value is ScanResult scanResult && scanResult.Id == "existing");
|
okObjectResult.Value is ScanResult scanResult && scanResult.Id == "existing");
|
||||||
|
|
||||||
Assert.True(await _scanResultsController.Index("non-existing") is NotFoundResult);
|
Assert.True(await _scanResultsController.Index("non-existing") is NotFoundResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,16 +86,16 @@ namespace MalwareMultiScan.Tests
|
|||||||
|
|
||||||
_scanResultServiceMock.Verify(
|
_scanResultServiceMock.Verify(
|
||||||
x => x.StoreFile("test.exe", It.IsAny<Stream>()), Times.Once);
|
x => x.StoreFile("test.exe", It.IsAny<Stream>()), Times.Once);
|
||||||
|
|
||||||
_scanResultServiceMock.Verify(
|
_scanResultServiceMock.Verify(
|
||||||
x => x.QueueUrlScan(It.IsAny<ScanResult>(), It.IsAny<string>()), Times.Once);
|
x => x.QueueUrlScan(It.IsAny<ScanResult>(), It.IsAny<string>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task TestUrlQueue()
|
public async Task TestUrlQueue()
|
||||||
{
|
{
|
||||||
Assert.True(await _queueController.ScanUrl("http://test.com") is CreatedAtActionResult);
|
Assert.True(await _queueController.ScanUrl("http://test.com") is CreatedAtActionResult);
|
||||||
|
|
||||||
_scanResultServiceMock.Verify(
|
_scanResultServiceMock.Verify(
|
||||||
x => x.QueueUrlScan(It.IsAny<ScanResult>(), "http://test.com"), Times.Once);
|
x => x.QueueUrlScan(It.IsAny<ScanResult>(), "http://test.com"), Times.Once);
|
||||||
}
|
}
|
@ -11,19 +11,19 @@ using Microsoft.Extensions.Logging;
|
|||||||
using Moq;
|
using Moq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Tests
|
namespace MalwareMultiScan.Tests.Api
|
||||||
{
|
{
|
||||||
public class ReceiverHostedServiceTests
|
public class ReceiverHostedServiceTests
|
||||||
{
|
{
|
||||||
private IReceiverHostedService _receiverHostedService;
|
|
||||||
private Mock<IBus> _busMock;
|
private Mock<IBus> _busMock;
|
||||||
|
private IReceiverHostedService _receiverHostedService;
|
||||||
private Mock<IScanResultService> _scanResultServiceMock;
|
private Mock<IScanResultService> _scanResultServiceMock;
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void SetUp()
|
public void SetUp()
|
||||||
{
|
{
|
||||||
_busMock = new Mock<IBus>();
|
_busMock = new Mock<IBus>();
|
||||||
|
|
||||||
_busMock
|
_busMock
|
||||||
.Setup(x => x.Receive("mms.results", It.IsAny<Func<ScanResultMessage, Task>>()))
|
.Setup(x => x.Receive("mms.results", It.IsAny<Func<ScanResultMessage, Task>>()))
|
||||||
.Callback<string, Func<ScanResultMessage, Task>>((s, func) =>
|
.Callback<string, Func<ScanResultMessage, Task>>((s, func) =>
|
||||||
@ -39,9 +39,9 @@ namespace MalwareMultiScan.Tests
|
|||||||
|
|
||||||
task.Wait();
|
task.Wait();
|
||||||
});
|
});
|
||||||
|
|
||||||
_scanResultServiceMock = new Mock<IScanResultService>();
|
_scanResultServiceMock = new Mock<IScanResultService>();
|
||||||
|
|
||||||
var configuration = new ConfigurationRoot(new List<IConfigurationProvider>
|
var configuration = new ConfigurationRoot(new List<IConfigurationProvider>
|
||||||
{
|
{
|
||||||
new MemoryConfigurationProvider(new MemoryConfigurationSource())
|
new MemoryConfigurationProvider(new MemoryConfigurationSource())
|
||||||
@ -49,11 +49,11 @@ namespace MalwareMultiScan.Tests
|
|||||||
{
|
{
|
||||||
["ResultsSubscriptionId"] = "mms.results"
|
["ResultsSubscriptionId"] = "mms.results"
|
||||||
};
|
};
|
||||||
|
|
||||||
_receiverHostedService = new ReceiverHostedService(
|
_receiverHostedService = new ReceiverHostedService(
|
||||||
_busMock.Object,
|
_busMock.Object,
|
||||||
configuration,
|
configuration,
|
||||||
_scanResultServiceMock.Object,
|
_scanResultServiceMock.Object,
|
||||||
Mock.Of<ILogger<ReceiverHostedService>>());
|
Mock.Of<ILogger<ReceiverHostedService>>());
|
||||||
}
|
}
|
||||||
|
|
@ -13,12 +13,12 @@ using Microsoft.Extensions.Logging;
|
|||||||
using Moq;
|
using Moq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Tests
|
namespace MalwareMultiScan.Tests.Api
|
||||||
{
|
{
|
||||||
public class ScanBackendServiceTests
|
public class ScanBackendServiceTests
|
||||||
{
|
{
|
||||||
private IScanBackendService _scanBackendService;
|
|
||||||
private Mock<IBus> _busMock;
|
private Mock<IBus> _busMock;
|
||||||
|
private IScanBackendService _scanBackendService;
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void SetUp()
|
public void SetUp()
|
||||||
@ -30,12 +30,12 @@ namespace MalwareMultiScan.Tests
|
|||||||
{
|
{
|
||||||
["BackendsConfiguration"] = "backends.yaml"
|
["BackendsConfiguration"] = "backends.yaml"
|
||||||
};
|
};
|
||||||
|
|
||||||
_busMock = new Mock<IBus>();
|
_busMock = new Mock<IBus>();
|
||||||
|
|
||||||
_scanBackendService = new ScanBackendService(
|
_scanBackendService = new ScanBackendService(
|
||||||
configuration,
|
configuration,
|
||||||
_busMock.Object,
|
_busMock.Object,
|
||||||
Mock.Of<ILogger<ScanBackendService>>());
|
Mock.Of<ILogger<ScanBackendService>>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,8 +49,8 @@ namespace MalwareMultiScan.Tests
|
|||||||
public async Task TestQueueUrlScan()
|
public async Task TestQueueUrlScan()
|
||||||
{
|
{
|
||||||
await _scanBackendService.QueueUrlScan(
|
await _scanBackendService.QueueUrlScan(
|
||||||
new ScanResult { Id = "test"},
|
new ScanResult {Id = "test"},
|
||||||
new ScanBackend { Id = "dummy"},
|
new ScanBackend {Id = "dummy"},
|
||||||
"http://test.com"
|
"http://test.com"
|
||||||
);
|
);
|
||||||
|
|
@ -11,32 +11,32 @@ using MongoDB.Driver.GridFS;
|
|||||||
using Moq;
|
using Moq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace MalwareMultiScan.Tests
|
namespace MalwareMultiScan.Tests.Api
|
||||||
{
|
{
|
||||||
public class ScanResultServiceTest
|
public class ScanResultServiceTest
|
||||||
{
|
{
|
||||||
private IScanResultService _resultService;
|
|
||||||
private MongoDbRunner _mongoDbRunner;
|
private MongoDbRunner _mongoDbRunner;
|
||||||
|
private IScanResultService _resultService;
|
||||||
private Mock<IScanBackendService> _scanBackendService;
|
private Mock<IScanBackendService> _scanBackendService;
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void SetUp()
|
public void SetUp()
|
||||||
{
|
{
|
||||||
_mongoDbRunner = MongoDbRunner.Start();
|
_mongoDbRunner = MongoDbRunner.Start();
|
||||||
|
|
||||||
var connection = new MongoClient(_mongoDbRunner.ConnectionString);
|
var connection = new MongoClient(_mongoDbRunner.ConnectionString);
|
||||||
var database = connection.GetDatabase("Test");
|
var database = connection.GetDatabase("Test");
|
||||||
var gridFsBucket = new GridFSBucket(database);
|
var gridFsBucket = new GridFSBucket(database);
|
||||||
|
|
||||||
_scanBackendService = new Mock<IScanBackendService>();
|
_scanBackendService = new Mock<IScanBackendService>();
|
||||||
|
|
||||||
_scanBackendService
|
_scanBackendService
|
||||||
.SetupGet(x => x.List)
|
.SetupGet(x => x.List)
|
||||||
.Returns(() => new[]
|
.Returns(() => new[]
|
||||||
{
|
{
|
||||||
new ScanBackend {Id = "dummy", Enabled = true},
|
new ScanBackend {Id = "dummy", Enabled = true},
|
||||||
new ScanBackend {Id = "clamav", Enabled = true},
|
new ScanBackend {Id = "clamav", Enabled = true},
|
||||||
new ScanBackend {Id = "disabled", Enabled = false},
|
new ScanBackend {Id = "disabled", Enabled = false}
|
||||||
});
|
});
|
||||||
|
|
||||||
_resultService = new ScanResultService(
|
_resultService = new ScanResultService(
|
||||||
@ -57,23 +57,25 @@ namespace MalwareMultiScan.Tests
|
|||||||
string fileId;
|
string fileId;
|
||||||
|
|
||||||
await using (var dataStream = new MemoryStream(originalData))
|
await using (var dataStream = new MemoryStream(originalData))
|
||||||
|
{
|
||||||
fileId = await _resultService.StoreFile("test.txt", dataStream);
|
fileId = await _resultService.StoreFile("test.txt", dataStream);
|
||||||
|
}
|
||||||
|
|
||||||
Assert.NotNull(fileId);
|
Assert.NotNull(fileId);
|
||||||
|
|
||||||
Assert.IsNull(await _resultService.ObtainFile("wrong-id"));
|
Assert.IsNull(await _resultService.ObtainFile("wrong-id"));
|
||||||
Assert.IsNull(await _resultService.ObtainFile(ObjectId.GenerateNewId().ToString()));
|
Assert.IsNull(await _resultService.ObtainFile(ObjectId.GenerateNewId().ToString()));
|
||||||
|
|
||||||
await using var fileSteam = await _resultService.ObtainFile(fileId);
|
await using var fileSteam = await _resultService.ObtainFile(fileId);
|
||||||
|
|
||||||
var readData = new[]
|
var readData = new[]
|
||||||
{
|
{
|
||||||
(byte)fileSteam.ReadByte(),
|
(byte) fileSteam.ReadByte(),
|
||||||
(byte)fileSteam.ReadByte(),
|
(byte) fileSteam.ReadByte(),
|
||||||
(byte)fileSteam.ReadByte(),
|
(byte) fileSteam.ReadByte(),
|
||||||
(byte)fileSteam.ReadByte()
|
(byte) fileSteam.ReadByte()
|
||||||
};
|
};
|
||||||
|
|
||||||
Assert.That(readData, Is.EquivalentTo(originalData));
|
Assert.That(readData, Is.EquivalentTo(originalData));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +83,7 @@ namespace MalwareMultiScan.Tests
|
|||||||
public async Task TestScanResultCreateGet()
|
public async Task TestScanResultCreateGet()
|
||||||
{
|
{
|
||||||
var result = await _resultService.CreateScanResult();
|
var result = await _resultService.CreateScanResult();
|
||||||
|
|
||||||
Assert.NotNull(result.Id);
|
Assert.NotNull(result.Id);
|
||||||
Assert.That(result.Results, Contains.Key("dummy"));
|
Assert.That(result.Results, Contains.Key("dummy"));
|
||||||
|
|
||||||
@ -94,7 +96,7 @@ namespace MalwareMultiScan.Tests
|
|||||||
Assert.That(result.Results, Contains.Key("dummy"));
|
Assert.That(result.Results, Contains.Key("dummy"));
|
||||||
|
|
||||||
var dummyResult = result.Results["dummy"];
|
var dummyResult = result.Results["dummy"];
|
||||||
|
|
||||||
Assert.IsTrue(dummyResult.Completed);
|
Assert.IsTrue(dummyResult.Completed);
|
||||||
Assert.IsTrue(dummyResult.Succeeded);
|
Assert.IsTrue(dummyResult.Succeeded);
|
||||||
Assert.AreEqual(dummyResult.Duration, 100);
|
Assert.AreEqual(dummyResult.Duration, 100);
|
||||||
@ -109,9 +111,9 @@ namespace MalwareMultiScan.Tests
|
|||||||
|
|
||||||
await _resultService.QueueUrlScan(
|
await _resultService.QueueUrlScan(
|
||||||
result, "http://url.com");
|
result, "http://url.com");
|
||||||
|
|
||||||
_scanBackendService.Verify(x => x.QueueUrlScan(
|
_scanBackendService.Verify(x => x.QueueUrlScan(
|
||||||
It.IsAny<ScanResult>(),
|
It.IsAny<ScanResult>(),
|
||||||
It.IsAny<ScanBackend>(),
|
It.IsAny<ScanBackend>(),
|
||||||
"http://url.com"), Times.Exactly(2));
|
"http://url.com"), Times.Exactly(2));
|
||||||
}
|
}
|
162
MalwareMultiScan.Tests/Backends/BackendsTests.cs
Normal file
162
MalwareMultiScan.Tests/Backends/BackendsTests.cs
Normal file
@ -0,0 +1,162 @@
|
|||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MalwareMultiScan.Backends.Backends.Implementations;
|
||||||
|
using MalwareMultiScan.Backends.Interfaces;
|
||||||
|
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||||
|
using Moq;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace MalwareMultiScan.Tests.Backends
|
||||||
|
{
|
||||||
|
public class BackendsTests
|
||||||
|
{
|
||||||
|
private static IProcessRunner GetProcessRunner(int exitCode, string stdOutput, string stdError)
|
||||||
|
{
|
||||||
|
var processRunnerMock = new Mock<IProcessRunner>();
|
||||||
|
|
||||||
|
processRunnerMock
|
||||||
|
.Setup(p => p.RunTillCompletion(
|
||||||
|
It.IsAny<string>(),
|
||||||
|
It.IsAny<string>(),
|
||||||
|
It.IsAny<CancellationToken>(),
|
||||||
|
out It.Ref<string>.IsAny,
|
||||||
|
out It.Ref<string>.IsAny
|
||||||
|
))
|
||||||
|
.Callback(new RunTillCompletion((string path, string arguments, CancellationToken token,
|
||||||
|
out string pOut, out string pErr) =>
|
||||||
|
{
|
||||||
|
pOut = stdOutput;
|
||||||
|
pErr = stdError;
|
||||||
|
})).Returns(exitCode);
|
||||||
|
|
||||||
|
return processRunnerMock.Object;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task TestDummy()
|
||||||
|
{
|
||||||
|
var backend = new DummyScanBackend();
|
||||||
|
|
||||||
|
Assert.Contains("Malware.Dummy.Result", await backend.ScanAsync("test.exe", default));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task TestVirusDetected(IScanBackend backend)
|
||||||
|
{
|
||||||
|
Assert.Contains("Malware-Test-Result", await backend.ScanAsync("test.exe", default));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task TestVirusNotDetected(IScanBackend backend)
|
||||||
|
{
|
||||||
|
Assert.IsEmpty(await backend.ScanAsync("test.exe", default));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task TestClamav()
|
||||||
|
{
|
||||||
|
await TestVirusDetected(new ClamavScanBackend(
|
||||||
|
GetProcessRunner(1, "/worker/test.exe: Malware-Test-Result FOUND\n", null)));
|
||||||
|
|
||||||
|
await TestVirusNotDetected(new ClamavScanBackend(
|
||||||
|
GetProcessRunner(0, "/worker/test.exe: OK\n", "")));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task TestWindowsDefender()
|
||||||
|
{
|
||||||
|
await TestVirusDetected(new WindowsDefenderScanBackend(
|
||||||
|
GetProcessRunner(0, null, "main(): Scanning /worker/test.exe...\n" +
|
||||||
|
"EngineScanCallback(): Scanning input\n" +
|
||||||
|
"EngineScanCallback(): Threat Malware-Test-Result identified.")));
|
||||||
|
|
||||||
|
await TestVirusNotDetected(new WindowsDefenderScanBackend(
|
||||||
|
GetProcessRunner(0, null, "main(): Scanning /worker/test.exe...\n" +
|
||||||
|
"EngineScanCallback(): Scanning input")));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task TestSophos()
|
||||||
|
{
|
||||||
|
await TestVirusDetected(new SophosScanBackend(
|
||||||
|
GetProcessRunner(3, ">>> Virus 'Malware-Test-Result' found in file /worker/test.exe\n", null)));
|
||||||
|
|
||||||
|
await TestVirusNotDetected(new SophosScanBackend(
|
||||||
|
GetProcessRunner(0, "", null)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task TestMcAfee()
|
||||||
|
{
|
||||||
|
await TestVirusDetected(new McAfeeScanBackend(
|
||||||
|
GetProcessRunner(1, "McAfee VirusScan Command Line for Linux64 Version: 6.0.4.564\n" +
|
||||||
|
"Copyright (C) 2013 McAfee, Inc.\n" +
|
||||||
|
"(408) 988-3832 EVALUATION COPY - October 28 2020\n\n" +
|
||||||
|
"AV Engine version: 5600.1067 for Linux64\n" +
|
||||||
|
"Dat set version: 9787 created Oct 27 2020\n" +
|
||||||
|
"Scanning for 668682 viruses, trojans and variants.\n\n" +
|
||||||
|
"/worker/test.exe ... Found: Malware-Test-Result.\n\n" +
|
||||||
|
"Time: 00:00.00", null)));
|
||||||
|
|
||||||
|
await TestVirusNotDetected(new McAfeeScanBackend(
|
||||||
|
GetProcessRunner(0, "McAfee VirusScan Command Line for Linux64 Version: 6.0.4.564\n" +
|
||||||
|
"Copyright (C) 2013 McAfee, Inc.\n" +
|
||||||
|
"(408) 988-3832 EVALUATION COPY - October 28 2020\n\n" +
|
||||||
|
"AV Engine version: 5600.1067 for Linux64\n" +
|
||||||
|
"Dat set version: 9787 created Oct 27 2020\n" +
|
||||||
|
"Scanning for 668682 viruses, trojans and variants.\n\n" +
|
||||||
|
"Time: 00:00.00", null)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task TestKes()
|
||||||
|
{
|
||||||
|
await TestVirusDetected(new KesScanBackend(
|
||||||
|
GetProcessRunner(0, "ObjectId: 22\n\t\t" +
|
||||||
|
" FileName : /worker/test.exe\n" +
|
||||||
|
" DangerLevel : High\n" +
|
||||||
|
" DetectType : Virware\n" +
|
||||||
|
" DetectName : Malware-Test-Result\n" +
|
||||||
|
" CompoundObject : No\n" +
|
||||||
|
" AddTime : 2020-10-29 13:05:20\n" +
|
||||||
|
" FileSize : 68\n", null)));
|
||||||
|
|
||||||
|
await TestVirusNotDetected(new KesScanBackend(
|
||||||
|
GetProcessRunner(0, "No files in Storage for the query\n", null)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task TestDrWeb()
|
||||||
|
{
|
||||||
|
await TestVirusDetected(new DrWebScanBackend(
|
||||||
|
GetProcessRunner(0,
|
||||||
|
"/worker/test.exe - infected with Malware-Test-Result\n" +
|
||||||
|
"Scanned objects: 1, scan errors: 0, threats found: 1, threats neutralized: 0.\n" +
|
||||||
|
"Scanned 0.07 KB in 5.39 s with speed 0.01 KB/s.", null)));
|
||||||
|
|
||||||
|
await TestVirusNotDetected(new DrWebScanBackend(
|
||||||
|
GetProcessRunner(0,
|
||||||
|
"/worker/test.exe - Ok\n" +
|
||||||
|
"Scanned objects: 1, scan errors: 0, threats found: 0, threats neutralized: 0.", null)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task TestComodo()
|
||||||
|
{
|
||||||
|
await TestVirusDetected(new ComodoScanBackend(
|
||||||
|
GetProcessRunner(0, "-----== Scan Start ==-----\n" +
|
||||||
|
"/worker/test.exe ---> Found Virus, Malware Name is Malware-Test-Result\n" +
|
||||||
|
"-----== Scan End ==-----\n" +
|
||||||
|
"Number of Scanned Files: 1\n" +
|
||||||
|
"Number of Found Viruses: 0", null)));
|
||||||
|
|
||||||
|
await TestVirusNotDetected(new ComodoScanBackend(
|
||||||
|
GetProcessRunner(0, "-----== Scan Start ==-----\n" +
|
||||||
|
"/worker/test.exe ---> Not Virus\n" +
|
||||||
|
"-----== Scan End ==-----\n" +
|
||||||
|
"Number of Scanned Files: 1\n" +
|
||||||
|
"Number of Found Viruses: 0", null)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private delegate void RunTillCompletion(string path, string arguments, CancellationToken token,
|
||||||
|
out string stdOut, out string stdErr);
|
||||||
|
}
|
||||||
|
}
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\MalwareMultiScan.Api\MalwareMultiScan.Api.csproj" />
|
<ProjectReference Include="..\MalwareMultiScan.Api\MalwareMultiScan.Api.csproj" />
|
||||||
|
<ProjectReference Include="..\MalwareMultiScan.Scanner\MalwareMultiScan.Scanner.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
86
MalwareMultiScan.Tests/Scanner/ScanHostedServiceTests.cs
Normal file
86
MalwareMultiScan.Tests/Scanner/ScanHostedServiceTests.cs
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using EasyNetQ;
|
||||||
|
using MalwareMultiScan.Backends.Interfaces;
|
||||||
|
using MalwareMultiScan.Backends.Messages;
|
||||||
|
using MalwareMultiScan.Scanner.Services.Implementations;
|
||||||
|
using MalwareMultiScan.Scanner.Services.Interfaces;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Configuration.Memory;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace MalwareMultiScan.Tests.Scanner
|
||||||
|
{
|
||||||
|
public class ScanHostedServiceTests
|
||||||
|
{
|
||||||
|
private Mock<IBus> _busMock;
|
||||||
|
private Mock<IScanBackend> _scanBackendMock;
|
||||||
|
private IScanHostedService _scanHostedService;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
var configuration = new ConfigurationRoot(new List<IConfigurationProvider>
|
||||||
|
{
|
||||||
|
new MemoryConfigurationProvider(new MemoryConfigurationSource())
|
||||||
|
})
|
||||||
|
{
|
||||||
|
["ResultsSubscriptionId"] = "mms.results"
|
||||||
|
};
|
||||||
|
|
||||||
|
_busMock = new Mock<IBus>();
|
||||||
|
|
||||||
|
_busMock
|
||||||
|
.Setup(x => x.Receive("dummy", It.IsAny<Func<ScanRequestMessage, Task>>()))
|
||||||
|
.Callback<string, Func<ScanRequestMessage, Task>>((s, func) =>
|
||||||
|
{
|
||||||
|
var task = func.Invoke(new ScanRequestMessage
|
||||||
|
{
|
||||||
|
Id = "test",
|
||||||
|
Uri = new Uri("http://test.com")
|
||||||
|
});
|
||||||
|
|
||||||
|
task.Wait();
|
||||||
|
});
|
||||||
|
|
||||||
|
_scanBackendMock = new Mock<IScanBackend>();
|
||||||
|
|
||||||
|
_scanBackendMock
|
||||||
|
.Setup(x => x.ScanAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Returns(Task.FromResult(new[] {"Test"}));
|
||||||
|
|
||||||
|
_scanBackendMock
|
||||||
|
.SetupGet(x => x.Id)
|
||||||
|
.Returns("dummy");
|
||||||
|
|
||||||
|
_scanHostedService = new ScanHostedService(
|
||||||
|
Mock.Of<ILogger<ScanHostedService>>(),
|
||||||
|
_scanBackendMock.Object,
|
||||||
|
_busMock.Object,
|
||||||
|
configuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task TestBusReceiveScanResultMessage()
|
||||||
|
{
|
||||||
|
await _scanHostedService.StartAsync(default);
|
||||||
|
|
||||||
|
_busMock.Verify(x => x.SendAsync("mms.results", It.Is<ScanResultMessage>(
|
||||||
|
m => m.Succeeded && m.Backend == "dummy" && m.Id == "test" && m.Threats.Contains("Test")
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task TestBusIsDisposedOnStop()
|
||||||
|
{
|
||||||
|
await _scanHostedService.StopAsync(default);
|
||||||
|
|
||||||
|
_busMock.Verify(x => x.Dispose(), Times.Once);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user