mirror of
https://github.com/volodymyrsmirnov/MalwareMultiScan.git
synced 2025-08-24 05:22:22 +00:00
commit
149ace39dd
@ -6,14 +6,15 @@ namespace MalwareMultiScan.Api.Attributes
|
||||
/// <summary>
|
||||
/// Validate URI to be an absolute http(s) URL.
|
||||
/// </summary>
|
||||
internal class IsHttpUrlAttribute : ValidationAttribute
|
||||
public class IsHttpUrlAttribute : ValidationAttribute
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
||||
{
|
||||
if (!Uri.TryCreate((string)value, UriKind.Absolute, out var uri)
|
||||
|| !uri.IsAbsoluteUri
|
||||
|| uri.Scheme != "http" && uri.Scheme != "https")
|
||||
if (!Uri.TryCreate((string) value, UriKind.Absolute, out var uri)
|
||||
|| !uri.IsAbsoluteUri
|
||||
|| uri.Scheme.ToLowerInvariant() != "http"
|
||||
&& uri.Scheme.ToLowerInvariant() != "https")
|
||||
return new ValidationResult("Only absolute http(s) URLs are supported");
|
||||
|
||||
return ValidationResult.Success;
|
||||
|
@ -16,12 +16,12 @@ namespace MalwareMultiScan.Api.Attributes
|
||||
var maxSize = validationContext
|
||||
.GetRequiredService<IConfiguration>()
|
||||
.GetValue<long>("MaxFileSize");
|
||||
|
||||
|
||||
var formFile = (IFormFile) value;
|
||||
|
||||
|
||||
if (formFile == null || formFile.Length > maxSize)
|
||||
return new ValidationResult($"File exceeds the maximum size of {maxSize} bytes");
|
||||
|
||||
|
||||
return ValidationResult.Success;
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +1,33 @@
|
||||
using System.Threading.Tasks;
|
||||
using MalwareMultiScan.Api.Services;
|
||||
using MalwareMultiScan.Api.Services.Interfaces;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MalwareMultiScan.Api.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// Downloads controller.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/download")]
|
||||
[Produces("application/octet-stream")]
|
||||
public class DownloadController : Controller
|
||||
{
|
||||
private readonly ScanResultService _scanResultService;
|
||||
private readonly IScanResultService _scanResultService;
|
||||
|
||||
public DownloadController(ScanResultService scanResultService)
|
||||
/// <summary>
|
||||
/// Initialize downloads controller.
|
||||
/// </summary>
|
||||
/// <param name="scanResultService">Scan result service.</param>
|
||||
public DownloadController(IScanResultService scanResultService)
|
||||
{
|
||||
_scanResultService = scanResultService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Download file by id.
|
||||
/// </summary>
|
||||
/// <param name="id">File id.</param>
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
|
@ -2,48 +2,65 @@ using System.ComponentModel.DataAnnotations;
|
||||
using System.Threading.Tasks;
|
||||
using MalwareMultiScan.Api.Attributes;
|
||||
using MalwareMultiScan.Api.Data.Models;
|
||||
using MalwareMultiScan.Api.Services;
|
||||
using MalwareMultiScan.Api.Services.Interfaces;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MalwareMultiScan.Api.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// Queue controller.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/queue")]
|
||||
[Produces("application/json")]
|
||||
public class QueueController : Controller
|
||||
{
|
||||
private readonly ScanResultService _scanResultService;
|
||||
private readonly IScanResultService _scanResultService;
|
||||
|
||||
public QueueController(ScanResultService scanResultService)
|
||||
/// <summary>
|
||||
/// Initialize queue controller.
|
||||
/// </summary>
|
||||
/// <param name="scanResultService">Scan result service.</param>
|
||||
public QueueController(IScanResultService scanResultService)
|
||||
{
|
||||
_scanResultService = scanResultService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queue file for scanning.
|
||||
/// </summary>
|
||||
/// <param name="file">File from form data.</param>
|
||||
[HttpPost("file")]
|
||||
[ProducesResponseType(typeof(ScanResult), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> ScanFile(
|
||||
[Required, MaxFileSize] IFormFile file)
|
||||
[Required] [MaxFileSize] IFormFile file)
|
||||
{
|
||||
var result = await _scanResultService.CreateScanResult();
|
||||
|
||||
string storedFileId;
|
||||
|
||||
await using (var uploadFileStream = file.OpenReadStream())
|
||||
storedFileId = await _scanResultService.StoreFile(file.Name, uploadFileStream);
|
||||
{
|
||||
storedFileId = await _scanResultService.StoreFile(file.FileName, uploadFileStream);
|
||||
}
|
||||
|
||||
await _scanResultService.QueueUrlScan(result, Url.Action("Index", "Download", new {id = storedFileId},
|
||||
Request.Scheme, Request.Host.Value));
|
||||
Request?.Scheme, Request?.Host.Value));
|
||||
|
||||
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")]
|
||||
[ProducesResponseType(typeof(ScanResult), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> ScanUrl(
|
||||
[FromForm, Required, IsHttpUrl] string url)
|
||||
[FromForm] [Required] [IsHttpUrl] string url)
|
||||
{
|
||||
var result = await _scanResultService.CreateScanResult();
|
||||
|
||||
|
@ -1,23 +1,34 @@
|
||||
using System.Threading.Tasks;
|
||||
using MalwareMultiScan.Api.Data.Models;
|
||||
using MalwareMultiScan.Api.Services;
|
||||
using MalwareMultiScan.Api.Services.Interfaces;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MalwareMultiScan.Api.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// Scan results controller.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/results")]
|
||||
[Produces("application/json")]
|
||||
public class ScanResultsController : Controller
|
||||
{
|
||||
private readonly ScanResultService _scanResultService;
|
||||
|
||||
public ScanResultsController(ScanResultService scanResultService)
|
||||
private readonly IScanResultService _scanResultService;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize scan results controller.
|
||||
/// </summary>
|
||||
/// <param name="scanResultService">Scan result service.</param>
|
||||
public ScanResultsController(IScanResultService scanResultService)
|
||||
{
|
||||
_scanResultService = scanResultService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get scan result by id.
|
||||
/// </summary>
|
||||
/// <param name="id">Scan result id.</param>
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(typeof(ScanResult), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
|
@ -1,8 +1,18 @@
|
||||
namespace MalwareMultiScan.Api.Data.Configuration
|
||||
{
|
||||
/// <summary>
|
||||
/// Scan backend.
|
||||
/// </summary>
|
||||
public class ScanBackend
|
||||
{
|
||||
/// <summary>
|
||||
/// Backend id.
|
||||
/// </summary>
|
||||
public string Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Backend state.
|
||||
/// </summary>
|
||||
public bool Enabled { get; set; }
|
||||
}
|
||||
}
|
@ -4,12 +4,21 @@ using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace MalwareMultiScan.Api.Data.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Scan result.
|
||||
/// </summary>
|
||||
public class ScanResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Result id.
|
||||
/// </summary>
|
||||
[BsonId]
|
||||
[BsonRepresentation(BsonType.ObjectId)]
|
||||
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; } =
|
||||
new Dictionary<string, ScanResultEntry>();
|
||||
}
|
||||
|
@ -1,12 +1,28 @@
|
||||
using System;
|
||||
|
||||
namespace MalwareMultiScan.Api.Data.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Scan result entry.
|
||||
/// </summary>
|
||||
public class ScanResultEntry
|
||||
{
|
||||
/// <summary>
|
||||
/// Completion status.
|
||||
/// </summary>
|
||||
public bool Completed { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that scanning completed without error.
|
||||
/// </summary>
|
||||
public bool? Succeeded { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Scanning duration in seconds.
|
||||
/// </summary>
|
||||
public long Duration { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Detected names of threats.
|
||||
/// </summary>
|
||||
public string[] Threats { get; set; }
|
||||
}
|
||||
}
|
@ -1,34 +1,28 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using EasyNetQ;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.GridFS;
|
||||
|
||||
namespace MalwareMultiScan.Api.Extensions
|
||||
{
|
||||
[ExcludeFromCodeCoverage]
|
||||
internal static class ServiceCollectionExtensions
|
||||
{
|
||||
public static void AddMongoDb(this IServiceCollection services, IConfiguration configuration)
|
||||
internal static void AddMongoDb(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddSingleton(
|
||||
serviceProvider =>
|
||||
{
|
||||
var db = new MongoClient(configuration.GetConnectionString("Mongo"));
|
||||
var client = new MongoClient(configuration.GetConnectionString("Mongo"));
|
||||
var db = client.GetDatabase(configuration.GetValue<string>("DatabaseName"));
|
||||
|
||||
return db.GetDatabase(
|
||||
configuration.GetValue<string>("DatabaseName"));
|
||||
});
|
||||
services.AddSingleton(client);
|
||||
services.AddSingleton(db);
|
||||
services.AddSingleton<IGridFSBucket>(new GridFSBucket(db));
|
||||
}
|
||||
|
||||
public static void AddRabbitMq(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddSingleton(x =>
|
||||
RabbitHutch.CreateBus(configuration.GetConnectionString("RabbitMQ")));
|
||||
}
|
||||
|
||||
public static void AddDockerForwardedHeadersOptions(this IServiceCollection services)
|
||||
internal static void AddDockerForwardedHeadersOptions(this IServiceCollection services)
|
||||
{
|
||||
services.Configure<ForwardedHeadersOptions>(options =>
|
||||
{
|
||||
|
@ -2,6 +2,11 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<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>
|
||||
|
||||
<ItemGroup>
|
||||
@ -11,7 +16,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="EasyNetQ" Version="5.6.0" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.11.3" />
|
||||
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.11.3" />
|
||||
<PackageReference Include="YamlDotNet" Version="8.1.2" />
|
||||
|
@ -1,9 +1,11 @@
|
||||
using EasyNetQ.LightInject;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace MalwareMultiScan.Api
|
||||
{
|
||||
public static class Program
|
||||
[ExcludeFromCodeCoverage]
|
||||
internal static class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
|
@ -1,31 +1,29 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using EasyNetQ;
|
||||
using MalwareMultiScan.Api.Services.Interfaces;
|
||||
using MalwareMultiScan.Backends.Messages;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MalwareMultiScan.Api.Services
|
||||
namespace MalwareMultiScan.Api.Services.Implementations
|
||||
{
|
||||
/// <summary>
|
||||
/// Receiver hosted service.
|
||||
/// </summary>
|
||||
public class ReceiverHostedService : IHostedService
|
||||
/// <inheritdoc />
|
||||
public class ReceiverHostedService : IReceiverHostedService
|
||||
{
|
||||
private readonly IBus _bus;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<ReceiverHostedService> _logger;
|
||||
private readonly ScanResultService _scanResultService;
|
||||
private readonly IScanResultService _scanResultService;
|
||||
|
||||
/// <summary>
|
||||
/// Create receiver hosted service.
|
||||
/// Initialize receiver hosted service.
|
||||
/// </summary>
|
||||
/// <param name="bus">Service bus.</param>
|
||||
/// <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, ScanResultService scanResultService,
|
||||
public ReceiverHostedService(IBus bus, IConfiguration configuration, IScanResultService scanResultService,
|
||||
ILogger<ReceiverHostedService> logger)
|
||||
{
|
||||
_bus = bus;
|
||||
@ -34,13 +32,14 @@ namespace MalwareMultiScan.Api.Services
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_bus.Receive<ScanResultMessage>(_configuration.GetValue<string>("ResultsSubscriptionId"), async message =>
|
||||
{
|
||||
message.Threats ??= new string[] { };
|
||||
|
||||
|
||||
_logger.LogInformation(
|
||||
$"Received a result from {message.Backend} for {message.Id} " +
|
||||
$"with threats {string.Join(",", message.Threats)}");
|
||||
@ -56,11 +55,10 @@ namespace MalwareMultiScan.Api.Services
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_bus.Dispose();
|
||||
_bus?.Dispose();
|
||||
|
||||
_logger.LogInformation(
|
||||
"Stopped hosted service for receiving scan results");
|
@ -4,29 +4,28 @@ using System.Threading.Tasks;
|
||||
using EasyNetQ;
|
||||
using MalwareMultiScan.Api.Data.Configuration;
|
||||
using MalwareMultiScan.Api.Data.Models;
|
||||
using MalwareMultiScan.Api.Services.Interfaces;
|
||||
using MalwareMultiScan.Backends.Messages;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using YamlDotNet.Serialization;
|
||||
using YamlDotNet.Serialization.NamingConventions;
|
||||
|
||||
namespace MalwareMultiScan.Api.Services
|
||||
namespace MalwareMultiScan.Api.Services.Implementations
|
||||
{
|
||||
/// <summary>
|
||||
/// Scan backends service.
|
||||
/// </summary>
|
||||
public class ScanBackendService
|
||||
/// <inheritdoc />
|
||||
public class ScanBackendService : IScanBackendService
|
||||
{
|
||||
private readonly IBus _bus;
|
||||
private readonly ILogger<ScanBackendService> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Create scan backend service.
|
||||
/// Initialise scan backend service.
|
||||
/// </summary>
|
||||
/// <param name="configuration">Configuration.</param>
|
||||
/// <param name="bus">Service bus.</param>
|
||||
/// <param name="bus">EasyNetQ bus.</param>
|
||||
/// <param name="logger">Logger.</param>
|
||||
/// <exception cref="FileNotFoundException">Missing BackendsConfiguration YAML file.</exception>
|
||||
/// <exception cref="FileNotFoundException">Missing backends.yaml configuration.</exception>
|
||||
public ScanBackendService(IConfiguration configuration, IBus bus, ILogger<ScanBackendService> logger)
|
||||
{
|
||||
_bus = bus;
|
||||
@ -46,17 +45,10 @@ namespace MalwareMultiScan.Api.Services
|
||||
List = deserializer.Deserialize<ScanBackend[]>(configurationContent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// List of available scan backends.
|
||||
/// </summary>
|
||||
/// <inheritdoc />
|
||||
public ScanBackend[] List { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Queue URL scan.
|
||||
/// </summary>
|
||||
/// <param name="result">Scan result instance.</param>
|
||||
/// <param name="backend">Scan backend.</param>
|
||||
/// <param name="fileUrl">File download URL.</param>
|
||||
/// <inheritdoc />
|
||||
public async Task QueueUrlScan(ScanResult result, ScanBackend backend, string fileUrl)
|
||||
{
|
||||
_logger.LogInformation(
|
@ -2,40 +2,37 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MalwareMultiScan.Api.Data.Models;
|
||||
using MalwareMultiScan.Api.Services.Interfaces;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.GridFS;
|
||||
|
||||
namespace MalwareMultiScan.Api.Services
|
||||
namespace MalwareMultiScan.Api.Services.Implementations
|
||||
{
|
||||
/// <summary>
|
||||
/// Scan results service.
|
||||
/// </summary>
|
||||
public class ScanResultService
|
||||
/// <inheritdoc />
|
||||
public class ScanResultService : IScanResultService
|
||||
{
|
||||
private const string CollectionName = "ScanResults";
|
||||
|
||||
private readonly GridFSBucket _bucket;
|
||||
private readonly IGridFSBucket _bucket;
|
||||
private readonly IMongoCollection<ScanResult> _collection;
|
||||
private readonly ScanBackendService _scanBackendService;
|
||||
private readonly IScanBackendService _scanBackendService;
|
||||
|
||||
/// <summary>
|
||||
/// Create scan result service.
|
||||
/// 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, ScanBackendService scanBackendService)
|
||||
public ScanResultService(IMongoDatabase db, IGridFSBucket bucket, IScanBackendService scanBackendService)
|
||||
{
|
||||
_bucket = bucket;
|
||||
_scanBackendService = scanBackendService;
|
||||
|
||||
_bucket = new GridFSBucket(db);
|
||||
_collection = db.GetCollection<ScanResult>(CollectionName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create scan result.
|
||||
/// </summary>
|
||||
/// <returns>Scan result.</returns>
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<ScanResult> CreateScanResult()
|
||||
{
|
||||
var scanResult = new ScanResult
|
||||
@ -50,11 +47,7 @@ namespace MalwareMultiScan.Api.Services
|
||||
return scanResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get scan result.
|
||||
/// </summary>
|
||||
/// <param name="id">Scan result id.</param>
|
||||
/// <returns>Scan result.</returns>
|
||||
/// <inheritdoc />
|
||||
public async Task<ScanResult> GetScanResult(string id)
|
||||
{
|
||||
var result = await _collection.FindAsync(
|
||||
@ -63,15 +56,7 @@ namespace MalwareMultiScan.Api.Services
|
||||
return await result.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update scan status for the backend.
|
||||
/// </summary>
|
||||
/// <param name="resultId">Result id.</param>
|
||||
/// <param name="backendId">Backend id.</param>
|
||||
/// <param name="duration">Duration of scan.</param>
|
||||
/// <param name="completed">If the scan has been completed.</param>
|
||||
/// <param name="succeeded">If the scan has been succeeded.</param>
|
||||
/// <param name="threats">List of found threats.</param>
|
||||
/// <inheritdoc />
|
||||
public async Task UpdateScanResultForBackend(string resultId, string backendId, long duration,
|
||||
bool completed = false, bool succeeded = false, string[] threats = null)
|
||||
{
|
||||
@ -86,23 +71,14 @@ namespace MalwareMultiScan.Api.Services
|
||||
}));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queue URL scan.
|
||||
/// </summary>
|
||||
/// <param name="result">Scan result instance.</param>
|
||||
/// <param name="fileUrl">File URL.</param>
|
||||
/// <inheritdoc />
|
||||
public async Task QueueUrlScan(ScanResult result, string fileUrl)
|
||||
{
|
||||
foreach (var backend in _scanBackendService.List.Where(b => b.Enabled))
|
||||
await _scanBackendService.QueueUrlScan(result, backend, fileUrl);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Store file.
|
||||
/// </summary>
|
||||
/// <param name="fileName">File name.</param>
|
||||
/// <param name="fileStream">File stream.</param>
|
||||
/// <returns>Stored file id.</returns>
|
||||
/// <inheritdoc />
|
||||
public async Task<string> StoreFile(string fileName, Stream fileStream)
|
||||
{
|
||||
var objectId = await _bucket.UploadFromStreamAsync(
|
||||
@ -111,20 +87,23 @@ namespace MalwareMultiScan.Api.Services
|
||||
return objectId.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Obtain stored file stream.
|
||||
/// </summary>
|
||||
/// <param name="id">File id.</param>
|
||||
/// <returns>File seekable stream.</returns>
|
||||
/// <inheritdoc />
|
||||
public async Task<Stream> ObtainFile(string id)
|
||||
{
|
||||
if (!ObjectId.TryParse(id, out var objectId))
|
||||
return null;
|
||||
|
||||
return await _bucket.OpenDownloadStreamAsync(objectId, new GridFSDownloadOptions
|
||||
try
|
||||
{
|
||||
Seekable = true
|
||||
});
|
||||
return await _bucket.OpenDownloadStreamAsync(objectId, new GridFSDownloadOptions
|
||||
{
|
||||
Seekable = true
|
||||
});
|
||||
}
|
||||
catch (GridFSFileNotFoundException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace MalwareMultiScan.Api.Services.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// Receiver hosted service.
|
||||
/// </summary>
|
||||
public interface IReceiverHostedService : IHostedService
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
using System.Threading.Tasks;
|
||||
using MalwareMultiScan.Api.Data.Configuration;
|
||||
using MalwareMultiScan.Api.Data.Models;
|
||||
|
||||
namespace MalwareMultiScan.Api.Services.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// Scan backend service.
|
||||
/// </summary>
|
||||
public interface IScanBackendService
|
||||
{
|
||||
/// <summary>
|
||||
/// Get list of parsed backends.
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using MalwareMultiScan.Api.Data.Models;
|
||||
|
||||
namespace MalwareMultiScan.Api.Services.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// Scan result service.
|
||||
/// </summary>
|
||||
public interface IScanResultService
|
||||
{
|
||||
/// <summary>
|
||||
/// Create scan result.
|
||||
/// </summary>
|
||||
/// <returns>Scan result entry with id.</returns>
|
||||
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);
|
||||
|
||||
/// <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,
|
||||
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);
|
||||
|
||||
/// <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);
|
||||
|
||||
/// <summary>
|
||||
/// Obtain file.
|
||||
/// </summary>
|
||||
/// <param name="id">Unique file id.</param>
|
||||
/// <returns>Seekable file stream opened for reading.</returns>
|
||||
Task<Stream> ObtainFile(string id);
|
||||
}
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using MalwareMultiScan.Api.Extensions;
|
||||
using MalwareMultiScan.Api.Services;
|
||||
using MalwareMultiScan.Api.Services.Implementations;
|
||||
using MalwareMultiScan.Api.Services.Interfaces;
|
||||
using MalwareMultiScan.Backends.Extensions;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
@ -7,7 +10,8 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace MalwareMultiScan.Api
|
||||
{
|
||||
public class Startup
|
||||
[ExcludeFromCodeCoverage]
|
||||
internal class Startup
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
@ -19,7 +23,7 @@ namespace MalwareMultiScan.Api
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddDockerForwardedHeadersOptions();
|
||||
|
||||
|
||||
services.Configure<KestrelServerOptions>(options =>
|
||||
{
|
||||
options.Limits.MaxRequestBodySize = _configuration.GetValue<int>("MaxFileSize");
|
||||
@ -28,8 +32,8 @@ namespace MalwareMultiScan.Api
|
||||
services.AddMongoDb(_configuration);
|
||||
services.AddRabbitMq(_configuration);
|
||||
|
||||
services.AddSingleton<ScanBackendService>();
|
||||
services.AddSingleton<ScanResultService>();
|
||||
services.AddSingleton<IScanBackendService, ScanBackendService>();
|
||||
services.AddSingleton<IScanResultService, ScanResultService>();
|
||||
|
||||
services.AddControllers();
|
||||
|
||||
|
@ -1,77 +1,64 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||
|
||||
namespace MalwareMultiScan.Backends.Backends.Abstracts
|
||||
{
|
||||
/// <inheritdoc />
|
||||
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; }
|
||||
|
||||
/// <summary>
|
||||
/// Path to the backend.
|
||||
/// </summary>
|
||||
protected abstract string BackendPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Parse StdErr instead of StdOut.
|
||||
/// </summary>
|
||||
protected virtual bool ParseStdErr { get; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// Throw on non-zero exit code.
|
||||
/// </summary>
|
||||
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);
|
||||
|
||||
public override async Task<string[]> ScanAsync(string path, CancellationToken cancellationToken)
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Task<string[]> ScanAsync(string path, CancellationToken cancellationToken)
|
||||
{
|
||||
var process = new Process
|
||||
{
|
||||
StartInfo = new ProcessStartInfo(BackendPath, GetBackendArguments(path))
|
||||
{
|
||||
RedirectStandardError = true,
|
||||
RedirectStandardOutput = true,
|
||||
WorkingDirectory = Path.GetDirectoryName(BackendPath) ?? Directory.GetCurrentDirectory()
|
||||
}
|
||||
};
|
||||
var exitCode = _processRunner.RunTillCompletion(BackendPath, GetBackendArguments(path), cancellationToken,
|
||||
out var standardOutput, out var standardError);
|
||||
|
||||
_logger.LogInformation(
|
||||
$"Starting process {process.StartInfo.FileName} " +
|
||||
$"with arguments {process.StartInfo.Arguments} " +
|
||||
$"in working directory {process.StartInfo.WorkingDirectory}");
|
||||
if (ThrowOnNonZeroExitCode && exitCode != 0)
|
||||
throw new ApplicationException($"Process has terminated with an exit code {exitCode}");
|
||||
|
||||
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}");
|
||||
|
||||
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
|
||||
return Task.FromResult(MatchRegex
|
||||
.Matches(ParseStdErr ? standardError : standardOutput)
|
||||
.Where(x => x.Success)
|
||||
.Select(x => x.Groups["threat"].Value)
|
||||
.ToArray();
|
||||
.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
@ -7,12 +8,17 @@ using MalwareMultiScan.Backends.Interfaces;
|
||||
|
||||
namespace MalwareMultiScan.Backends.Backends.Abstracts
|
||||
{
|
||||
/// <inheritdoc />
|
||||
[ExcludeFromCodeCoverage]
|
||||
public abstract class AbstractScanBackend : IScanBackend
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public abstract string Id { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public abstract Task<string[]> ScanAsync(string path, CancellationToken cancellationToken);
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string[]> ScanAsync(Uri uri, CancellationToken cancellationToken)
|
||||
{
|
||||
using var httpClient = new HttpClient();
|
||||
@ -22,6 +28,7 @@ namespace MalwareMultiScan.Backends.Backends.Abstracts
|
||||
return await ScanAsync(uriStream, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string[]> ScanAsync(Stream stream, CancellationToken cancellationToken)
|
||||
{
|
||||
var tempFile = Path.GetTempFileName();
|
||||
|
@ -1,24 +1,31 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using MalwareMultiScan.Backends.Backends.Abstracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||
|
||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class ClamavScanBackend : AbstractLocalProcessScanBackend
|
||||
{
|
||||
public ClamavScanBackend(ILogger logger) : base(logger)
|
||||
/// <inheritdoc />
|
||||
public ClamavScanBackend(IProcessRunner processRunner) : base(processRunner)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Id { get; } = "clamav";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string BackendPath { get; } = "/usr/bin/clamdscan";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Regex MatchRegex { get; } =
|
||||
new Regex(@"(\S+): (?<threat>[\S]+) FOUND", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override bool ThrowOnNonZeroExitCode { get; } = false;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string GetBackendArguments(string path)
|
||||
{
|
||||
return $"-m --fdpass --no-summary {path}";
|
||||
|
@ -1,23 +1,29 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using MalwareMultiScan.Backends.Backends.Abstracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||
|
||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class ComodoScanBackend : AbstractLocalProcessScanBackend
|
||||
{
|
||||
public ComodoScanBackend(ILogger logger) : base(logger)
|
||||
/// <inheritdoc />
|
||||
public ComodoScanBackend(IProcessRunner processRunner) : base(processRunner)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Id { get; } = "comodo";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string BackendPath { get; } = "/opt/COMODO/cmdscan";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Regex MatchRegex { get; } =
|
||||
new Regex(@".* ---> Found Virus, Malware Name is (?<threat>.*)",
|
||||
RegexOptions.Compiled | RegexOptions.Multiline);
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string GetBackendArguments(string path)
|
||||
{
|
||||
return $"-v -s {path}";
|
||||
|
@ -1,22 +1,28 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using MalwareMultiScan.Backends.Backends.Abstracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||
|
||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class DrWebScanBackend : AbstractLocalProcessScanBackend
|
||||
{
|
||||
public DrWebScanBackend(ILogger logger) : base(logger)
|
||||
/// <inheritdoc />
|
||||
public DrWebScanBackend(IProcessRunner processRunner) : base(processRunner)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Id { get; } = "drweb";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string BackendPath { get; } = "/usr/bin/drweb-ctl";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Regex MatchRegex { get; } =
|
||||
new Regex(@".* - infected with (?<threat>[\S ]+)", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string GetBackendArguments(string path)
|
||||
{
|
||||
return $"scan {path}";
|
||||
|
@ -6,20 +6,25 @@ using MalwareMultiScan.Backends.Interfaces;
|
||||
|
||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class DummyScanBackend : IScanBackend
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public string Id { get; } = "dummy";
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<string[]> ScanAsync(string path, CancellationToken cancellationToken)
|
||||
{
|
||||
return Scan();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<string[]> ScanAsync(Uri uri, CancellationToken cancellationToken)
|
||||
{
|
||||
return Scan();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<string[]> ScanAsync(Stream stream, CancellationToken cancellationToken)
|
||||
{
|
||||
return Scan();
|
||||
@ -29,7 +34,7 @@ namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||
{
|
||||
await Task.Delay(
|
||||
TimeSpan.FromSeconds(5));
|
||||
|
||||
|
||||
return new[] {"Malware.Dummy.Result"};
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +1,28 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using MalwareMultiScan.Backends.Backends.Abstracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||
|
||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class KesScanBackend : AbstractLocalProcessScanBackend
|
||||
{
|
||||
public KesScanBackend(ILogger logger) : base(logger)
|
||||
/// <inheritdoc />
|
||||
public KesScanBackend(IProcessRunner processRunner) : base(processRunner)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Id { get; } = "kes";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string BackendPath { get; } = "/bin/bash";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Regex MatchRegex { get; } =
|
||||
new Regex(@"[ +]DetectName.*: (?<threat>.*)", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string GetBackendArguments(string path)
|
||||
{
|
||||
return $"/usr/bin/kesl-scan {path}";
|
||||
|
@ -1,24 +1,31 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using MalwareMultiScan.Backends.Backends.Abstracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||
|
||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class McAfeeScanBackend : AbstractLocalProcessScanBackend
|
||||
{
|
||||
public McAfeeScanBackend(ILogger logger) : base(logger)
|
||||
/// <inheritdoc />
|
||||
public McAfeeScanBackend(IProcessRunner processRunner) : base(processRunner)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Id { get; } = "mcafee";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string BackendPath { get; } = "/usr/local/uvscan/uvscan";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override bool ThrowOnNonZeroExitCode { get; } = false;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Regex MatchRegex { get; } =
|
||||
new Regex(@".* ... Found: (?<threat>.*).", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string GetBackendArguments(string path)
|
||||
{
|
||||
return $"--SECURE {path}";
|
||||
|
@ -1,24 +1,31 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using MalwareMultiScan.Backends.Backends.Abstracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||
|
||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class SophosScanBackend : AbstractLocalProcessScanBackend
|
||||
{
|
||||
public SophosScanBackend(ILogger logger) : base(logger)
|
||||
/// <inheritdoc />
|
||||
public SophosScanBackend(IProcessRunner processRunner) : base(processRunner)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Id { get; } = "sophos";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string BackendPath { get; } = "/opt/sophos-av/bin/savscan";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override bool ThrowOnNonZeroExitCode { get; } = false;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Regex MatchRegex { get; } =
|
||||
new Regex(@">>> Virus '(?<threat>.*)' found in file .*", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string GetBackendArguments(string path)
|
||||
{
|
||||
return $"-f -archive -ss {path}";
|
||||
|
@ -1,25 +1,32 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using MalwareMultiScan.Backends.Backends.Abstracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||
|
||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
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";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string BackendPath { get; } = "/opt/mpclient";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Regex MatchRegex { get; } =
|
||||
new Regex(@"EngineScanCallback\(\): Threat (?<threat>[\S]+) identified",
|
||||
RegexOptions.Compiled | RegexOptions.Multiline);
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override bool ParseStdErr { get; } = true;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string GetBackendArguments(string path)
|
||||
{
|
||||
return path;
|
||||
|
@ -1,14 +1,48 @@
|
||||
namespace MalwareMultiScan.Backends.Enums
|
||||
{
|
||||
/// <summary>
|
||||
/// Backend type.
|
||||
/// </summary>
|
||||
public enum BackendType
|
||||
{
|
||||
/// <summary>
|
||||
/// Dummy
|
||||
/// </summary>
|
||||
Dummy,
|
||||
|
||||
/// <summary>
|
||||
/// Windows Defender.
|
||||
/// </summary>
|
||||
Defender,
|
||||
|
||||
/// <summary>
|
||||
/// ClamAV.
|
||||
/// </summary>
|
||||
Clamav,
|
||||
|
||||
/// <summary>
|
||||
/// DrWeb.
|
||||
/// </summary>
|
||||
DrWeb,
|
||||
|
||||
/// <summary>
|
||||
/// KES.
|
||||
/// </summary>
|
||||
Kes,
|
||||
|
||||
/// <summary>
|
||||
/// Comodo.
|
||||
/// </summary>
|
||||
Comodo,
|
||||
|
||||
/// <summary>
|
||||
/// Sophos.
|
||||
/// </summary>
|
||||
Sophos,
|
||||
|
||||
/// <summary>
|
||||
/// McAfee.
|
||||
/// </summary>
|
||||
McAfee
|
||||
}
|
||||
}
|
@ -1,32 +1,72 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using EasyNetQ;
|
||||
using MalwareMultiScan.Backends.Backends.Implementations;
|
||||
using MalwareMultiScan.Backends.Enums;
|
||||
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.Logging;
|
||||
|
||||
namespace MalwareMultiScan.Backends.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions for IServiceCollection.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
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(),
|
||||
BackendType.Defender => new WindowsDefenderScanBackend(logger),
|
||||
BackendType.Clamav => new ClamavScanBackend(logger),
|
||||
BackendType.DrWeb => new DrWebScanBackend(logger),
|
||||
BackendType.Kes => new KesScanBackend(logger),
|
||||
BackendType.Comodo => new ComodoScanBackend(logger),
|
||||
BackendType.Sophos => new SophosScanBackend(logger),
|
||||
BackendType.McAfee => new McAfeeScanBackend(logger),
|
||||
_ => throw new NotImplementedException()
|
||||
});
|
||||
case BackendType.Dummy:
|
||||
services.AddSingleton<IScanBackend, DummyScanBackend>();
|
||||
break;
|
||||
case BackendType.Defender:
|
||||
services.AddSingleton<IScanBackend, WindowsDefenderScanBackend>();
|
||||
break;
|
||||
case BackendType.Clamav:
|
||||
services.AddSingleton<IScanBackend, ClamavScanBackend>();
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// Scan backend.
|
||||
/// </summary>
|
||||
public interface IScanBackend
|
||||
{
|
||||
/// <summary>
|
||||
/// Unique backend id.
|
||||
/// </summary>
|
||||
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);
|
||||
|
||||
/// <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);
|
||||
|
||||
/// <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);
|
||||
}
|
||||
}
|
@ -2,11 +2,19 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<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>
|
||||
|
||||
<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.Logging.Abstractions" Version="3.1.9" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.9" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -2,10 +2,19 @@ using System;
|
||||
|
||||
namespace MalwareMultiScan.Backends.Messages
|
||||
{
|
||||
/// <summary>
|
||||
/// Scan request message.
|
||||
/// </summary>
|
||||
public class ScanRequestMessage
|
||||
{
|
||||
/// <summary>
|
||||
/// Result id.
|
||||
/// </summary>
|
||||
public string Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Remote URL.
|
||||
/// </summary>
|
||||
public Uri Uri { get; set; }
|
||||
}
|
||||
}
|
@ -1,15 +1,33 @@
|
||||
using System;
|
||||
|
||||
namespace MalwareMultiScan.Backends.Messages
|
||||
{
|
||||
/// <summary>
|
||||
/// Scan result message.
|
||||
/// </summary>
|
||||
public class ScanResultMessage
|
||||
{
|
||||
/// <summary>
|
||||
/// Result id.
|
||||
/// </summary>
|
||||
public string Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Backend.
|
||||
/// </summary>
|
||||
public string Backend { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Status.
|
||||
/// </summary>
|
||||
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; }
|
||||
}
|
||||
}
|
@ -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>
|
||||
<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>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="EasyNetQ" Version="5.6.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.9" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -1,14 +1,15 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading.Tasks;
|
||||
using MalwareMultiScan.Backends.Enums;
|
||||
using MalwareMultiScan.Backends.Extensions;
|
||||
using MalwareMultiScan.Scanner.Services;
|
||||
using MalwareMultiScan.Scanner.Services.Implementations;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace MalwareMultiScan.Scanner
|
||||
{
|
||||
public static class Program
|
||||
[ExcludeFromCodeCoverage]
|
||||
internal static class Program
|
||||
{
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
@ -22,8 +23,8 @@ namespace MalwareMultiScan.Scanner
|
||||
{
|
||||
services.AddLogging();
|
||||
|
||||
services.AddScanningBackend(
|
||||
context.Configuration.GetValue<BackendType>("BackendType"));
|
||||
services.AddRabbitMq(context.Configuration);
|
||||
services.AddScanningBackend(context.Configuration);
|
||||
|
||||
services.AddHostedService<ScanHostedService>();
|
||||
}).RunConsoleAsync();
|
||||
|
@ -5,32 +5,41 @@ using System.Threading.Tasks;
|
||||
using EasyNetQ;
|
||||
using MalwareMultiScan.Backends.Interfaces;
|
||||
using MalwareMultiScan.Backends.Messages;
|
||||
using MalwareMultiScan.Scanner.Services.Interfaces;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
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 IBus _bus;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<ScanHostedService> _logger;
|
||||
|
||||
private IBus _bus;
|
||||
|
||||
public ScanHostedService(ILogger<ScanHostedService> logger, IConfiguration configuration, IScanBackend backend)
|
||||
/// <summary>
|
||||
/// Initialise scan hosted service.
|
||||
/// </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;
|
||||
_bus = bus;
|
||||
_configuration = configuration;
|
||||
_backend = backend;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_bus = RabbitHutch.CreateBus(
|
||||
_configuration.GetConnectionString("RabbitMQ"));
|
||||
|
||||
_bus.Receive<ScanRequestMessage>(_backend.Id, Scan);
|
||||
|
||||
_logger.LogInformation(
|
||||
@ -39,6 +48,7 @@ namespace MalwareMultiScan.Scanner.Services
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_bus.Dispose();
|
||||
@ -62,9 +72,9 @@ namespace MalwareMultiScan.Scanner.Services
|
||||
Id = message.Id,
|
||||
Backend = _backend.Id
|
||||
};
|
||||
|
||||
|
||||
var stopwatch = new Stopwatch();
|
||||
|
||||
|
||||
stopwatch.Start();
|
||||
|
||||
try
|
||||
@ -91,10 +101,10 @@ namespace MalwareMultiScan.Scanner.Services
|
||||
}
|
||||
|
||||
result.Duration = stopwatch.ElapsedMilliseconds / 1000;
|
||||
|
||||
|
||||
_logger.LogInformation(
|
||||
$"Sending scan results with status {result.Succeeded}");
|
||||
|
||||
|
||||
await _bus.SendAsync(
|
||||
_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
|
||||
{
|
||||
}
|
||||
}
|
57
MalwareMultiScan.Tests/Api/AttributesTests.cs
Normal file
57
MalwareMultiScan.Tests/Api/AttributesTests.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using MalwareMultiScan.Api.Attributes;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Configuration.Memory;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace MalwareMultiScan.Tests.Api
|
||||
{
|
||||
public class AttributesTests
|
||||
{
|
||||
[Test]
|
||||
public void TestIsHttpUrlAttribute()
|
||||
{
|
||||
var attribute = new IsHttpUrlAttribute();
|
||||
|
||||
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"));
|
||||
Assert.False(attribute.IsValid(null));
|
||||
Assert.False(attribute.IsValid(string.Empty));
|
||||
Assert.False(attribute.IsValid("test"));
|
||||
Assert.False(attribute.IsValid("ftp://test.com"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMaxFileSizeAttribute()
|
||||
{
|
||||
var attribute = new MaxFileSizeAttribute();
|
||||
|
||||
var configuration = new ConfigurationRoot(new List<IConfigurationProvider>
|
||||
{
|
||||
new MemoryConfigurationProvider(new MemoryConfigurationSource())
|
||||
})
|
||||
{
|
||||
["MaxFileSize"] = "100"
|
||||
};
|
||||
|
||||
var context = new ValidationContext(
|
||||
string.Empty, Mock.Of<IServiceProvider>(x =>
|
||||
x.GetService(typeof(IConfiguration)) == configuration
|
||||
), null);
|
||||
|
||||
Assert.True(attribute.GetValidationResult(
|
||||
Mock.Of<IFormFile>(x => x.Length == 10), context) == ValidationResult.Success);
|
||||
|
||||
Assert.True(attribute.GetValidationResult(
|
||||
Mock.Of<IFormFile>(x => x.Length == 100), context) == ValidationResult.Success);
|
||||
|
||||
Assert.False(attribute.GetValidationResult(
|
||||
Mock.Of<IFormFile>(x => x.Length == 101), context) == ValidationResult.Success);
|
||||
}
|
||||
}
|
||||
}
|
103
MalwareMultiScan.Tests/Api/ControllersTests.cs
Normal file
103
MalwareMultiScan.Tests/Api/ControllersTests.cs
Normal file
@ -0,0 +1,103 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using MalwareMultiScan.Api.Controllers;
|
||||
using MalwareMultiScan.Api.Data.Models;
|
||||
using MalwareMultiScan.Api.Services.Interfaces;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace MalwareMultiScan.Tests.Api
|
||||
{
|
||||
public class ControllersTests
|
||||
{
|
||||
private DownloadController _downloadController;
|
||||
private QueueController _queueController;
|
||||
private ScanResultsController _scanResultsController;
|
||||
private Mock<IScanResultService> _scanResultServiceMock;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_scanResultServiceMock = new Mock<IScanResultService>();
|
||||
|
||||
_scanResultServiceMock
|
||||
.Setup(x => x.ObtainFile("existing"))
|
||||
.Returns(Task.FromResult(new MemoryStream() as Stream));
|
||||
|
||||
_scanResultServiceMock
|
||||
.Setup(x => x.ObtainFile("non-existing"))
|
||||
.Returns(Task.FromResult((Stream) null));
|
||||
|
||||
_scanResultServiceMock
|
||||
.Setup(x => x.GetScanResult("existing"))
|
||||
.Returns(Task.FromResult(new ScanResult
|
||||
{
|
||||
Id = "existing"
|
||||
}));
|
||||
|
||||
_scanResultServiceMock
|
||||
.Setup(x => x.GetScanResult("non-existing"))
|
||||
.Returns(Task.FromResult((ScanResult) null));
|
||||
|
||||
_scanResultServiceMock
|
||||
.Setup(x => x.ObtainFile("non-existing"))
|
||||
.Returns(Task.FromResult((Stream) null));
|
||||
|
||||
_scanResultServiceMock
|
||||
.Setup(x => x.CreateScanResult())
|
||||
.Returns(Task.FromResult(new ScanResult()));
|
||||
|
||||
_downloadController = new DownloadController(_scanResultServiceMock.Object);
|
||||
_scanResultsController = new ScanResultsController(_scanResultServiceMock.Object);
|
||||
|
||||
_queueController = new QueueController(_scanResultServiceMock.Object)
|
||||
{
|
||||
Url = Mock.Of<IUrlHelper>()
|
||||
};
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestFileDownload()
|
||||
{
|
||||
Assert.True(await _downloadController.Index("existing") is FileStreamResult);
|
||||
Assert.True(await _downloadController.Index("non-existing") is NotFoundResult);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestScanResults()
|
||||
{
|
||||
Assert.True(
|
||||
await _scanResultsController.Index("existing") is OkObjectResult okObjectResult &&
|
||||
okObjectResult.Value is ScanResult scanResult && scanResult.Id == "existing");
|
||||
|
||||
Assert.True(await _scanResultsController.Index("non-existing") is NotFoundResult);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestFileQueue()
|
||||
{
|
||||
Assert.True(await _queueController.ScanFile(
|
||||
Mock.Of<IFormFile>(x =>
|
||||
x.FileName == "test.exe" &&
|
||||
x.OpenReadStream() == new MemoryStream())
|
||||
) is CreatedAtActionResult);
|
||||
|
||||
_scanResultServiceMock.Verify(
|
||||
x => x.StoreFile("test.exe", It.IsAny<Stream>()), Times.Once);
|
||||
|
||||
_scanResultServiceMock.Verify(
|
||||
x => x.QueueUrlScan(It.IsAny<ScanResult>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestUrlQueue()
|
||||
{
|
||||
Assert.True(await _queueController.ScanUrl("http://test.com") is CreatedAtActionResult);
|
||||
|
||||
_scanResultServiceMock.Verify(
|
||||
x => x.QueueUrlScan(It.IsAny<ScanResult>(), "http://test.com"), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
77
MalwareMultiScan.Tests/Api/ReceiverHostedServiceTests.cs
Normal file
77
MalwareMultiScan.Tests/Api/ReceiverHostedServiceTests.cs
Normal file
@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using EasyNetQ;
|
||||
using MalwareMultiScan.Api.Services.Implementations;
|
||||
using MalwareMultiScan.Api.Services.Interfaces;
|
||||
using MalwareMultiScan.Backends.Messages;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Configuration.Memory;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace MalwareMultiScan.Tests.Api
|
||||
{
|
||||
public class ReceiverHostedServiceTests
|
||||
{
|
||||
private Mock<IBus> _busMock;
|
||||
private IReceiverHostedService _receiverHostedService;
|
||||
private Mock<IScanResultService> _scanResultServiceMock;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_busMock = new Mock<IBus>();
|
||||
|
||||
_busMock
|
||||
.Setup(x => x.Receive("mms.results", It.IsAny<Func<ScanResultMessage, Task>>()))
|
||||
.Callback<string, Func<ScanResultMessage, Task>>((s, func) =>
|
||||
{
|
||||
var task = func.Invoke(new ScanResultMessage
|
||||
{
|
||||
Id = "test",
|
||||
Backend = "dummy",
|
||||
Duration = 100,
|
||||
Succeeded = true,
|
||||
Threats = new[] {"Test"}
|
||||
});
|
||||
|
||||
task.Wait();
|
||||
});
|
||||
|
||||
_scanResultServiceMock = new Mock<IScanResultService>();
|
||||
|
||||
var configuration = new ConfigurationRoot(new List<IConfigurationProvider>
|
||||
{
|
||||
new MemoryConfigurationProvider(new MemoryConfigurationSource())
|
||||
})
|
||||
{
|
||||
["ResultsSubscriptionId"] = "mms.results"
|
||||
};
|
||||
|
||||
_receiverHostedService = new ReceiverHostedService(
|
||||
_busMock.Object,
|
||||
configuration,
|
||||
_scanResultServiceMock.Object,
|
||||
Mock.Of<ILogger<ReceiverHostedService>>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestBusReceiveScanResultMessage()
|
||||
{
|
||||
await _receiverHostedService.StartAsync(default);
|
||||
|
||||
_scanResultServiceMock.Verify(x => x.UpdateScanResultForBackend(
|
||||
"test", "dummy", 100, true, true, new[] {"Test"}), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestBusIsDisposedOnStop()
|
||||
{
|
||||
await _receiverHostedService.StopAsync(default);
|
||||
|
||||
_busMock.Verify(x => x.Dispose(), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
63
MalwareMultiScan.Tests/Api/ScanBackendServiceTests.cs
Normal file
63
MalwareMultiScan.Tests/Api/ScanBackendServiceTests.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using EasyNetQ;
|
||||
using MalwareMultiScan.Api.Data.Configuration;
|
||||
using MalwareMultiScan.Api.Data.Models;
|
||||
using MalwareMultiScan.Api.Services.Implementations;
|
||||
using MalwareMultiScan.Api.Services.Interfaces;
|
||||
using MalwareMultiScan.Backends.Messages;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Configuration.Memory;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace MalwareMultiScan.Tests.Api
|
||||
{
|
||||
public class ScanBackendServiceTests
|
||||
{
|
||||
private Mock<IBus> _busMock;
|
||||
private IScanBackendService _scanBackendService;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
var configuration = new ConfigurationRoot(new List<IConfigurationProvider>
|
||||
{
|
||||
new MemoryConfigurationProvider(new MemoryConfigurationSource())
|
||||
})
|
||||
{
|
||||
["BackendsConfiguration"] = "backends.yaml"
|
||||
};
|
||||
|
||||
_busMock = new Mock<IBus>();
|
||||
|
||||
_scanBackendService = new ScanBackendService(
|
||||
configuration,
|
||||
_busMock.Object,
|
||||
Mock.Of<ILogger<ScanBackendService>>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestBackendsAreNotEmpty()
|
||||
{
|
||||
Assert.IsNotEmpty(_scanBackendService.List);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestQueueUrlScan()
|
||||
{
|
||||
await _scanBackendService.QueueUrlScan(
|
||||
new ScanResult {Id = "test"},
|
||||
new ScanBackend {Id = "dummy"},
|
||||
"http://test.com"
|
||||
);
|
||||
|
||||
_busMock.Verify(x => x.SendAsync(
|
||||
"dummy", It.Is<ScanRequestMessage>(r =>
|
||||
r.Id == "test" &&
|
||||
r.Uri == new Uri("http://test.com"))), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
121
MalwareMultiScan.Tests/Api/ScanResultServiceTests.cs
Normal file
121
MalwareMultiScan.Tests/Api/ScanResultServiceTests.cs
Normal file
@ -0,0 +1,121 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using MalwareMultiScan.Api.Data.Configuration;
|
||||
using MalwareMultiScan.Api.Data.Models;
|
||||
using MalwareMultiScan.Api.Services.Implementations;
|
||||
using MalwareMultiScan.Api.Services.Interfaces;
|
||||
using Mongo2Go;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.GridFS;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace MalwareMultiScan.Tests.Api
|
||||
{
|
||||
public class ScanResultServiceTest
|
||||
{
|
||||
private MongoDbRunner _mongoDbRunner;
|
||||
private IScanResultService _resultService;
|
||||
private Mock<IScanBackendService> _scanBackendService;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_mongoDbRunner = MongoDbRunner.Start();
|
||||
|
||||
var connection = new MongoClient(_mongoDbRunner.ConnectionString);
|
||||
var database = connection.GetDatabase("Test");
|
||||
var gridFsBucket = new GridFSBucket(database);
|
||||
|
||||
_scanBackendService = new Mock<IScanBackendService>();
|
||||
|
||||
_scanBackendService
|
||||
.SetupGet(x => x.List)
|
||||
.Returns(() => new[]
|
||||
{
|
||||
new ScanBackend {Id = "dummy", Enabled = true},
|
||||
new ScanBackend {Id = "clamav", Enabled = true},
|
||||
new ScanBackend {Id = "disabled", Enabled = false}
|
||||
});
|
||||
|
||||
_resultService = new ScanResultService(
|
||||
database, gridFsBucket, _scanBackendService.Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_mongoDbRunner.Dispose();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestFileStorageStoreObtain()
|
||||
{
|
||||
var originalData = new byte[] {0xDE, 0xAD, 0xBE, 0xEF};
|
||||
|
||||
string fileId;
|
||||
|
||||
await using (var dataStream = new MemoryStream(originalData))
|
||||
{
|
||||
fileId = await _resultService.StoreFile("test.txt", dataStream);
|
||||
}
|
||||
|
||||
Assert.NotNull(fileId);
|
||||
|
||||
Assert.IsNull(await _resultService.ObtainFile("wrong-id"));
|
||||
Assert.IsNull(await _resultService.ObtainFile(ObjectId.GenerateNewId().ToString()));
|
||||
|
||||
await using var fileSteam = await _resultService.ObtainFile(fileId);
|
||||
|
||||
var readData = new[]
|
||||
{
|
||||
(byte) fileSteam.ReadByte(),
|
||||
(byte) fileSteam.ReadByte(),
|
||||
(byte) fileSteam.ReadByte(),
|
||||
(byte) fileSteam.ReadByte()
|
||||
};
|
||||
|
||||
Assert.That(readData, Is.EquivalentTo(originalData));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestScanResultCreateGet()
|
||||
{
|
||||
var result = await _resultService.CreateScanResult();
|
||||
|
||||
Assert.NotNull(result.Id);
|
||||
Assert.That(result.Results, Contains.Key("dummy"));
|
||||
|
||||
await _resultService.UpdateScanResultForBackend(
|
||||
result.Id, "dummy", 100, true, true, new[] {"Test"});
|
||||
|
||||
result = await _resultService.GetScanResult(result.Id);
|
||||
|
||||
Assert.NotNull(result.Id);
|
||||
Assert.That(result.Results, Contains.Key("dummy"));
|
||||
|
||||
var dummyResult = result.Results["dummy"];
|
||||
|
||||
Assert.IsTrue(dummyResult.Completed);
|
||||
Assert.IsTrue(dummyResult.Succeeded);
|
||||
Assert.AreEqual(dummyResult.Duration, 100);
|
||||
Assert.IsNotEmpty(dummyResult.Threats);
|
||||
Assert.That(dummyResult.Threats, Is.EquivalentTo(new[] {"Test"}));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestQueueUrlScan()
|
||||
{
|
||||
var result = await _resultService.CreateScanResult();
|
||||
|
||||
await _resultService.QueueUrlScan(
|
||||
result, "http://url.com");
|
||||
|
||||
_scanBackendService.Verify(x => x.QueueUrlScan(
|
||||
It.IsAny<ScanResult>(),
|
||||
It.IsAny<ScanBackend>(),
|
||||
"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);
|
||||
}
|
||||
}
|
23
MalwareMultiScan.Tests/MalwareMultiScan.Tests.csproj
Normal file
23
MalwareMultiScan.Tests/MalwareMultiScan.Tests.csproj
Normal file
@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.9" />
|
||||
<PackageReference Include="Mongo2Go" Version="2.2.14" />
|
||||
<PackageReference Include="Moq" Version="4.14.7" />
|
||||
<PackageReference Include="nunit" Version="3.12.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MalwareMultiScan.Api\MalwareMultiScan.Api.csproj" />
|
||||
<ProjectReference Include="..\MalwareMultiScan.Scanner\MalwareMultiScan.Scanner.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</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);
|
||||
}
|
||||
}
|
||||
}
|
@ -13,6 +13,8 @@ ProjectSection(SolutionItems) = preProject
|
||||
docker-compose.yaml = docker-compose.yaml
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MalwareMultiScan.Tests", "MalwareMultiScan.Tests\MalwareMultiScan.Tests.csproj", "{9896162D-8FC7-4911-933F-A78C94128923}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -31,5 +33,9 @@ Global
|
||||
{8A16A3C4-2AE3-4F63-8280-635FF7878080}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8A16A3C4-2AE3-4F63-8280-635FF7878080}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8A16A3C4-2AE3-4F63-8280-635FF7878080}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{9896162D-8FC7-4911-933F-A78C94128923}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{9896162D-8FC7-4911-933F-A78C94128923}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9896162D-8FC7-4911-933F-A78C94128923}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9896162D-8FC7-4911-933F-A78C94128923}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
Loading…
x
Reference in New Issue
Block a user