mirror of
https://github.com/volodymyrsmirnov/MalwareMultiScan.git
synced 2025-08-24 05:22:22 +00:00
finalize refactoring & ready for the PR
This commit is contained in:
parent
14c9c26979
commit
62cdcdbb49
@ -1,5 +1,5 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
<component name="ProjectRunConfigurationManager">
|
||||||
<configuration default="false" name="MalwareMultiScan.Worker/Dockerfile" type="docker-deploy" factoryName="dockerfile" server-name="Docker">
|
<configuration default="false" name="MalwareMultiScan.Scanner/Dockerfile" type="docker-deploy" factoryName="dockerfile" server-name="Docker">
|
||||||
<deployment type="dockerfile">
|
<deployment type="dockerfile">
|
||||||
<settings>
|
<settings>
|
||||||
<option name="imageTag" value="mindcollapse/malware-multi-scan-worker" />
|
<option name="imageTag" value="mindcollapse/malware-multi-scan-worker" />
|
||||||
@ -10,7 +10,7 @@
|
|||||||
<option name="contextFolderPath" value="." />
|
<option name="contextFolderPath" value="." />
|
||||||
<option name="entrypoint" value="" />
|
<option name="entrypoint" value="" />
|
||||||
<option name="commandLineOptions" value="" />
|
<option name="commandLineOptions" value="" />
|
||||||
<option name="sourceFilePath" value="MalwareMultiScan.Worker/Dockerfile" />
|
<option name="sourceFilePath" value="MalwareMultiScan.Scanner/Dockerfile" />
|
||||||
</settings>
|
</settings>
|
||||||
</deployment>
|
</deployment>
|
||||||
<method v="2" />
|
<method v="2" />
|
@ -3,13 +3,17 @@ using System.ComponentModel.DataAnnotations;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Api.Attributes
|
namespace MalwareMultiScan.Api.Attributes
|
||||||
{
|
{
|
||||||
public class HttpUrlValidationAttribute : ValidationAttribute
|
/// <summary>
|
||||||
|
/// Validate URI to be an absolute http(s) URL.
|
||||||
|
/// </summary>
|
||||||
|
internal class IsHttpUrlAttribute : ValidationAttribute
|
||||||
{
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
||||||
{
|
{
|
||||||
var uri = (Uri) value;
|
if (!Uri.TryCreate((string)value, UriKind.Absolute, out var uri)
|
||||||
|
|| !uri.IsAbsoluteUri
|
||||||
if (uri == null || !uri.IsAbsoluteUri || uri.Scheme != "http" && uri.Scheme != "https")
|
|| uri.Scheme != "http" && uri.Scheme != "https")
|
||||||
return new ValidationResult("Only absolute http(s) URLs are supported");
|
return new ValidationResult("Only absolute http(s) URLs are supported");
|
||||||
|
|
||||||
return ValidationResult.Success;
|
return ValidationResult.Success;
|
28
MalwareMultiScan.Api/Attributes/MaxFileSizeAttribute.cs
Normal file
28
MalwareMultiScan.Api/Attributes/MaxFileSizeAttribute.cs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace MalwareMultiScan.Api.Attributes
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Validate uploaded file size for the max file size defined in settings.
|
||||||
|
/// </summary>
|
||||||
|
public class MaxFileSizeAttribute : ValidationAttribute
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,7 +5,9 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Api.Controllers
|
namespace MalwareMultiScan.Api.Controllers
|
||||||
{
|
{
|
||||||
|
[ApiController]
|
||||||
[Route("download")]
|
[Route("download")]
|
||||||
|
[Produces("application/octet-stream")]
|
||||||
public class DownloadController : Controller
|
public class DownloadController : Controller
|
||||||
{
|
{
|
||||||
private readonly ScanResultService _scanResultService;
|
private readonly ScanResultService _scanResultService;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MalwareMultiScan.Api.Attributes;
|
using MalwareMultiScan.Api.Attributes;
|
||||||
using MalwareMultiScan.Api.Data.Models;
|
using MalwareMultiScan.Api.Data.Models;
|
||||||
@ -23,37 +23,33 @@ namespace MalwareMultiScan.Api.Controllers
|
|||||||
[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([FromForm] IFormFile file)
|
public async Task<IActionResult> ScanFile(
|
||||||
|
[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.Name, uploadFileStream);
|
storedFileId = await _scanResultService.StoreFile(file.Name, 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));
|
||||||
|
|
||||||
return Created(Url.Action("Index", "ScanResults", new {id = result.Id},
|
return CreatedAtAction("Index", "ScanResults", new {id = result.Id}, result);
|
||||||
Request.Scheme, Request.Host.Value), result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[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([FromForm] [HttpUrlValidation] Uri url)
|
public async Task<IActionResult> ScanUrl(
|
||||||
|
[FromForm, Required, IsHttpUrl] string url)
|
||||||
{
|
{
|
||||||
var result = await _scanResultService.CreateScanResult();
|
var result = await _scanResultService.CreateScanResult();
|
||||||
|
|
||||||
var resultUrl = Url.Action("Index", "ScanResults", new {id = result.Id},
|
await _scanResultService.QueueUrlScan(result, url);
|
||||||
Request.Scheme, Request.Host.Value);
|
|
||||||
|
|
||||||
await _scanResultService.QueueUrlScan(result, url.ToString());
|
return CreatedAtAction("Index", "ScanResults", new {id = result.Id}, result);
|
||||||
|
|
||||||
return Created(resultUrl, result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -20,6 +20,7 @@ namespace MalwareMultiScan.Api.Controllers
|
|||||||
|
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
[ProducesResponseType(typeof(ScanResult), StatusCodes.Status200OK)]
|
[ProducesResponseType(typeof(ScanResult), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
public async Task<IActionResult> Index(string id)
|
public async Task<IActionResult> Index(string id)
|
||||||
{
|
{
|
||||||
var scanResult = await _scanResultService.GetScanResult(id);
|
var scanResult = await _scanResultService.GetScanResult(id);
|
||||||
|
@ -3,6 +3,6 @@ namespace MalwareMultiScan.Api.Data.Configuration
|
|||||||
public class ScanBackend
|
public class ScanBackend
|
||||||
{
|
{
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
public string Name { get; set; }
|
public bool Enabled { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,7 +3,7 @@ namespace MalwareMultiScan.Api.Data.Models
|
|||||||
public class ScanResultEntry
|
public class ScanResultEntry
|
||||||
{
|
{
|
||||||
public bool Completed { get; set; }
|
public bool Completed { get; set; }
|
||||||
public bool Succeeded { get; set; }
|
public bool? Succeeded { get; set; }
|
||||||
public string[] Threats { get; set; }
|
public string[] Threats { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
using System.Net;
|
||||||
|
using EasyNetQ;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
|
||||||
|
namespace MalwareMultiScan.Api.Extensions
|
||||||
|
{
|
||||||
|
internal static class ServiceCollectionExtensions
|
||||||
|
{
|
||||||
|
public static void AddMongoDb(this IServiceCollection services, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
services.AddSingleton(
|
||||||
|
serviceProvider =>
|
||||||
|
{
|
||||||
|
var db = new MongoClient(configuration.GetConnectionString("Mongo"));
|
||||||
|
|
||||||
|
return db.GetDatabase(
|
||||||
|
configuration.GetValue<string>("DatabaseName"));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddRabbitMq(this IServiceCollection services, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
services.AddSingleton(x =>
|
||||||
|
RabbitHutch.CreateBus(configuration.GetConnectionString("RabbitMQ")));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddDockerForwardedHeadersOptions(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.Configure<ForwardedHeadersOptions>(options =>
|
||||||
|
{
|
||||||
|
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
|
||||||
|
options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("::ffff:10.0.0.0"), 104));
|
||||||
|
options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("::ffff:192.168.0.0"), 112));
|
||||||
|
options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("::ffff:172.16.0.0"), 108));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -14,18 +14,10 @@
|
|||||||
<PackageReference Include="EasyNetQ" Version="5.6.0" />
|
<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="Swashbuckle.AspNetCore" Version="5.6.3" />
|
|
||||||
<PackageReference Include="YamlDotNet" Version="8.1.2" />
|
<PackageReference Include="YamlDotNet" Version="8.1.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<_ContentIncludedByDefault Remove="Properties\launchSettings.json" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\MalwareMultiScan.Backends\MalwareMultiScan.Backends.csproj" />
|
<ProjectReference Include="..\MalwareMultiScan.Backends\MalwareMultiScan.Backends.csproj" />
|
||||||
<ProjectReference Include="..\MalwareMultiScan.Shared\MalwareMultiScan.Shared.csproj" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -13,7 +13,7 @@ namespace MalwareMultiScan.Api
|
|||||||
private static IHostBuilder CreateHostBuilder(string[] args)
|
private static IHostBuilder CreateHostBuilder(string[] args)
|
||||||
{
|
{
|
||||||
return Host.CreateDefaultBuilder(args)
|
return Host.CreateDefaultBuilder(args)
|
||||||
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
|
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,13 +8,23 @@ using Microsoft.Extensions.Logging;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Api.Services
|
namespace MalwareMultiScan.Api.Services
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Receiver hosted service.
|
||||||
|
/// </summary>
|
||||||
public class ReceiverHostedService : IHostedService
|
public class ReceiverHostedService : IHostedService
|
||||||
{
|
{
|
||||||
private readonly IBus _bus;
|
private readonly IBus _bus;
|
||||||
private readonly IConfiguration _configuration;
|
private readonly IConfiguration _configuration;
|
||||||
private readonly ScanResultService _scanResultService;
|
|
||||||
private readonly ILogger<ReceiverHostedService> _logger;
|
private readonly ILogger<ReceiverHostedService> _logger;
|
||||||
|
private readonly ScanResultService _scanResultService;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create receiver hosted service.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bus">Service 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, ScanResultService scanResultService,
|
||||||
ILogger<ReceiverHostedService> logger)
|
ILogger<ReceiverHostedService> logger)
|
||||||
{
|
{
|
||||||
@ -24,6 +34,7 @@ namespace MalwareMultiScan.Api.Services
|
|||||||
_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 =>
|
||||||
@ -33,7 +44,8 @@ namespace MalwareMultiScan.Api.Services
|
|||||||
$"with threats {string.Join(",", message.Threats)}");
|
$"with threats {string.Join(",", message.Threats)}");
|
||||||
|
|
||||||
await _scanResultService.UpdateScanResultForBackend(
|
await _scanResultService.UpdateScanResultForBackend(
|
||||||
message.Id, message.Backend, true, true, message.Threats);
|
message.Id, message.Backend, true,
|
||||||
|
message.Succeeded, message.Threats ?? new string[] { });
|
||||||
});
|
});
|
||||||
|
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
@ -42,6 +54,8 @@ namespace MalwareMultiScan.Api.Services
|
|||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public Task StopAsync(CancellationToken cancellationToken)
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_bus.Dispose();
|
_bus.Dispose();
|
||||||
|
@ -12,11 +12,21 @@ using YamlDotNet.Serialization.NamingConventions;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Api.Services
|
namespace MalwareMultiScan.Api.Services
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Scan backends service.
|
||||||
|
/// </summary>
|
||||||
public class ScanBackendService
|
public class ScanBackendService
|
||||||
{
|
{
|
||||||
private readonly IBus _bus;
|
private readonly IBus _bus;
|
||||||
private readonly ILogger<ScanBackendService> _logger;
|
private readonly ILogger<ScanBackendService> _logger;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create scan backend service.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="configuration">Configuration.</param>
|
||||||
|
/// <param name="bus">Service bus.</param>
|
||||||
|
/// <param name="logger">Logger.</param>
|
||||||
|
/// <exception cref="FileNotFoundException">Missing BackendsConfiguration YAML file.</exception>
|
||||||
public ScanBackendService(IConfiguration configuration, IBus bus, ILogger<ScanBackendService> logger)
|
public ScanBackendService(IConfiguration configuration, IBus bus, ILogger<ScanBackendService> logger)
|
||||||
{
|
{
|
||||||
_bus = bus;
|
_bus = bus;
|
||||||
@ -36,8 +46,17 @@ namespace MalwareMultiScan.Api.Services
|
|||||||
List = deserializer.Deserialize<ScanBackend[]>(configurationContent);
|
List = deserializer.Deserialize<ScanBackend[]>(configurationContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// List of available scan backends.
|
||||||
|
/// </summary>
|
||||||
public ScanBackend[] List { get; }
|
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>
|
||||||
public async Task QueueUrlScan(ScanResult result, ScanBackend backend, string fileUrl)
|
public async Task QueueUrlScan(ScanResult result, ScanBackend backend, string fileUrl)
|
||||||
{
|
{
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
|
@ -8,29 +8,41 @@ using MongoDB.Driver.GridFS;
|
|||||||
|
|
||||||
namespace MalwareMultiScan.Api.Services
|
namespace MalwareMultiScan.Api.Services
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Scan results service.
|
||||||
|
/// </summary>
|
||||||
public class ScanResultService
|
public class ScanResultService
|
||||||
{
|
{
|
||||||
private const string CollectionName = "ScanResults";
|
private const string CollectionName = "ScanResults";
|
||||||
|
|
||||||
private readonly GridFSBucket _bucket;
|
private readonly GridFSBucket _bucket;
|
||||||
|
|
||||||
private readonly IMongoCollection<ScanResult> _collection;
|
private readonly IMongoCollection<ScanResult> _collection;
|
||||||
|
|
||||||
private readonly ScanBackendService _scanBackendService;
|
private readonly ScanBackendService _scanBackendService;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create scan result service.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="db">Mongo database.</param>
|
||||||
|
/// <param name="scanBackendService">Scan backend service.</param>
|
||||||
public ScanResultService(IMongoDatabase db, ScanBackendService scanBackendService)
|
public ScanResultService(IMongoDatabase db, ScanBackendService scanBackendService)
|
||||||
{
|
{
|
||||||
_scanBackendService = scanBackendService;
|
_scanBackendService = scanBackendService;
|
||||||
|
|
||||||
_collection = db.GetCollection<ScanResult>(CollectionName);
|
|
||||||
_bucket = new GridFSBucket(db);
|
_bucket = new GridFSBucket(db);
|
||||||
|
_collection = db.GetCollection<ScanResult>(CollectionName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create scan result.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Scan result.</returns>
|
||||||
public async Task<ScanResult> CreateScanResult()
|
public async Task<ScanResult> CreateScanResult()
|
||||||
{
|
{
|
||||||
var scanResult = new ScanResult
|
var scanResult = new ScanResult
|
||||||
{
|
{
|
||||||
Results = _scanBackendService.List.ToDictionary(
|
Results = _scanBackendService.List
|
||||||
k => k.Id, v => new ScanResultEntry())
|
.Where(b => b.Enabled)
|
||||||
|
.ToDictionary(k => k.Id, v => new ScanResultEntry())
|
||||||
};
|
};
|
||||||
|
|
||||||
await _collection.InsertOneAsync(scanResult);
|
await _collection.InsertOneAsync(scanResult);
|
||||||
@ -38,6 +50,11 @@ namespace MalwareMultiScan.Api.Services
|
|||||||
return scanResult;
|
return scanResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get scan result.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">Scan result id.</param>
|
||||||
|
/// <returns>Scan result.</returns>
|
||||||
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,27 +63,44 @@ namespace MalwareMultiScan.Api.Services
|
|||||||
return await result.FirstOrDefaultAsync();
|
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="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>
|
||||||
public async Task UpdateScanResultForBackend(string resultId, string backendId,
|
public async Task UpdateScanResultForBackend(string resultId, string backendId,
|
||||||
bool completed = false, bool succeeded = false, string[] threats = null)
|
bool completed = false, bool succeeded = false, string[] threats = null)
|
||||||
{
|
{
|
||||||
var filterScanResult = Builders<ScanResult>.Filter.Where(r => r.Id == resultId);
|
await _collection.UpdateOneAsync(
|
||||||
|
Builders<ScanResult>.Filter.Where(r => r.Id == resultId),
|
||||||
var updateScanResult = Builders<ScanResult>.Update.Set(r => r.Results[backendId], new ScanResultEntry
|
Builders<ScanResult>.Update.Set(r => r.Results[backendId], new ScanResultEntry
|
||||||
{
|
{
|
||||||
Completed = completed,
|
Completed = completed,
|
||||||
Succeeded = succeeded,
|
Succeeded = succeeded,
|
||||||
Threats = threats
|
Threats = threats ?? new string[] { }
|
||||||
});
|
}));
|
||||||
|
|
||||||
await _collection.UpdateOneAsync(filterScanResult, updateScanResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Queue URL scan.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="result">Scan result instance.</param>
|
||||||
|
/// <param name="fileUrl">File URL.</param>
|
||||||
public async Task QueueUrlScan(ScanResult result, string fileUrl)
|
public async Task QueueUrlScan(ScanResult result, string fileUrl)
|
||||||
{
|
{
|
||||||
foreach (var backend in _scanBackendService.List)
|
foreach (var backend in _scanBackendService.List.Where(b => b.Enabled))
|
||||||
await _scanBackendService.QueueUrlScan(result, backend, fileUrl);
|
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>
|
||||||
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(
|
||||||
@ -75,6 +109,11 @@ namespace MalwareMultiScan.Api.Services
|
|||||||
return objectId.ToString();
|
return objectId.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Obtain stored file stream.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">File id.</param>
|
||||||
|
/// <returns>File seekable stream.</returns>
|
||||||
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))
|
||||||
|
@ -1,12 +1,8 @@
|
|||||||
using EasyNetQ;
|
using MalwareMultiScan.Api.Extensions;
|
||||||
using MalwareMultiScan.Api.Services;
|
using MalwareMultiScan.Api.Services;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.HttpOverrides;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.OpenApi.Models;
|
|
||||||
using MongoDB.Driver;
|
|
||||||
|
|
||||||
namespace MalwareMultiScan.Api
|
namespace MalwareMultiScan.Api
|
||||||
{
|
{
|
||||||
@ -21,62 +17,24 @@ namespace MalwareMultiScan.Api
|
|||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddSingleton(x =>
|
services.AddDockerForwardedHeadersOptions();
|
||||||
RabbitHutch.CreateBus(_configuration.GetConnectionString("RabbitMQ")));
|
|
||||||
|
services.AddMongoDb(_configuration);
|
||||||
|
services.AddRabbitMq(_configuration);
|
||||||
|
|
||||||
services.AddSingleton<ScanBackendService>();
|
services.AddSingleton<ScanBackendService>();
|
||||||
services.AddSingleton<ScanResultService>();
|
services.AddSingleton<ScanResultService>();
|
||||||
|
|
||||||
services.AddSingleton(
|
|
||||||
serviceProvider =>
|
|
||||||
{
|
|
||||||
var db = new MongoClient(_configuration.GetConnectionString("Mongo"));
|
|
||||||
|
|
||||||
return db.GetDatabase(
|
|
||||||
_configuration.GetValue<string>("DatabaseName"));
|
|
||||||
});
|
|
||||||
|
|
||||||
services.AddControllers();
|
services.AddControllers();
|
||||||
services.AddHttpClient();
|
|
||||||
|
|
||||||
services.AddSwaggerGen(options =>
|
|
||||||
{
|
|
||||||
options.SwaggerDoc("MalwareMultiScan",
|
|
||||||
new OpenApiInfo
|
|
||||||
{
|
|
||||||
Title = "MalwareMultiScan",
|
|
||||||
Version = "1.0.0"
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
services.AddHostedService<ReceiverHostedService>();
|
services.AddHostedService<ReceiverHostedService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
public void Configure(IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
app.UseForwardedHeaders();
|
||||||
var forwardingOptions = new ForwardedHeadersOptions
|
|
||||||
{
|
|
||||||
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
|
||||||
};
|
|
||||||
|
|
||||||
forwardingOptions.KnownNetworks.Clear();
|
|
||||||
forwardingOptions.KnownProxies.Clear();
|
|
||||||
|
|
||||||
app.UseForwardedHeaders(forwardingOptions);
|
|
||||||
|
|
||||||
app.UseEndpoints(endpoints => endpoints.MapControllers());
|
app.UseEndpoints(endpoints => endpoints.MapControllers());
|
||||||
|
|
||||||
app.UseSwagger();
|
|
||||||
|
|
||||||
app.UseSwaggerUI(options =>
|
|
||||||
{
|
|
||||||
options.DocumentTitle = "MalwareMultiScan";
|
|
||||||
|
|
||||||
options.SwaggerEndpoint(
|
|
||||||
$"/swagger/{options.DocumentTitle}/swagger.json", options.DocumentTitle);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,9 +0,0 @@
|
|||||||
{
|
|
||||||
"Logging": {
|
|
||||||
"LogLevel": {
|
|
||||||
"Default": "Information",
|
|
||||||
"Microsoft": "Warning",
|
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -14,8 +14,7 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
"DatabaseName": "MalwareMultiScan",
|
"DatabaseName": "MalwareMultiScan",
|
||||||
|
|
||||||
"ResultsSubscriptionId": "mms.results",
|
"ResultsSubscriptionId": "mms.results",
|
||||||
|
"MaxFileSize": 1048576,
|
||||||
"BackendsConfiguration": "backends.yaml"
|
"BackendsConfiguration": "backends.yaml"
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
- id: dummy
|
- id: dummy
|
||||||
name: Dummy Backend
|
enabled: true
|
@ -20,7 +20,7 @@ namespace MalwareMultiScan.Backends.Backends.Abstracts
|
|||||||
|
|
||||||
protected abstract Regex MatchRegex { get; }
|
protected abstract Regex MatchRegex { get; }
|
||||||
protected abstract string BackendPath { get; }
|
protected abstract string BackendPath { get; }
|
||||||
protected virtual bool ParseStdErr { get; }
|
protected virtual bool ParseStdErr { get; } = false;
|
||||||
protected virtual bool ThrowOnNonZeroExitCode { get; } = true;
|
protected virtual bool ThrowOnNonZeroExitCode { get; } = true;
|
||||||
|
|
||||||
protected abstract string GetBackendArguments(string path);
|
protected abstract string GetBackendArguments(string path);
|
||||||
|
@ -4,7 +4,6 @@ using System.Net.Http;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MalwareMultiScan.Backends.Interfaces;
|
using MalwareMultiScan.Backends.Interfaces;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Backends.Abstracts
|
namespace MalwareMultiScan.Backends.Backends.Abstracts
|
||||||
{
|
{
|
||||||
@ -23,13 +22,6 @@ namespace MalwareMultiScan.Backends.Backends.Abstracts
|
|||||||
return await ScanAsync(uriStream, cancellationToken);
|
return await ScanAsync(uriStream, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string[]> ScanAsync(IFormFile file, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
await using var fileStream = file.OpenReadStream();
|
|
||||||
|
|
||||||
return await ScanAsync(fileStream, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
||||||
|
@ -3,7 +3,6 @@ using System.IO;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MalwareMultiScan.Backends.Interfaces;
|
using MalwareMultiScan.Backends.Interfaces;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Backends.Implementations
|
namespace MalwareMultiScan.Backends.Backends.Implementations
|
||||||
{
|
{
|
||||||
@ -21,11 +20,6 @@ namespace MalwareMultiScan.Backends.Backends.Implementations
|
|||||||
return Scan();
|
return Scan();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<string[]> ScanAsync(IFormFile file, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
return Scan();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task<string[]> ScanAsync(Stream stream, CancellationToken cancellationToken)
|
public Task<string[]> ScanAsync(Stream stream, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return Scan();
|
return Scan();
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
FROM mindcollapse/malware-multi-scan-worker:latest
|
FROM mindcollapse/malware-multi-scan-scanner:latest
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND noninteractive
|
ENV DEBIAN_FRONTEND noninteractive
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
FROM mindcollapse/malware-multi-scan-worker:latest
|
FROM mindcollapse/malware-multi-scan-scanner:latest
|
||||||
|
|
||||||
RUN apt-get update && apt-get install wget -y
|
RUN apt-get update && apt-get install wget -y
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
FROM mindcollapse/malware-multi-scan-worker:latest
|
FROM mindcollapse/malware-multi-scan-scanner:latest
|
||||||
|
|
||||||
ARG DRWEB_KEY
|
ARG DRWEB_KEY
|
||||||
ENV DRWEB_KEY=$DRWEB_KEY
|
ENV DRWEB_KEY=$DRWEB_KEY
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
FROM mindcollapse/malware-multi-scan-worker:latest
|
FROM mindcollapse/malware-multi-scan-scanner:latest
|
||||||
|
|
||||||
ARG KES_KEY
|
ARG KES_KEY
|
||||||
ENV KES_KEY=$KES_KEY
|
ENV KES_KEY=$KES_KEY
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
FROM mindcollapse/malware-multi-scan-worker:latest
|
FROM mindcollapse/malware-multi-scan-scanner:latest
|
||||||
|
|
||||||
RUN apt-get update && apt-get install unzip wget -y
|
RUN apt-get update && apt-get install unzip wget -y
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
FROM mindcollapse/malware-multi-scan-worker:latest
|
FROM mindcollapse/malware-multi-scan-scanner:latest
|
||||||
|
|
||||||
RUN apt-get update && apt-get install wget -y
|
RUN apt-get update && apt-get install wget -y
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ WORKDIR /opt/loadlibrary/engine
|
|||||||
RUN curl -L "https://go.microsoft.com/fwlink/?LinkID=121721&arch=x86" --output mpan-fe.exe
|
RUN curl -L "https://go.microsoft.com/fwlink/?LinkID=121721&arch=x86" --output mpan-fe.exe
|
||||||
RUN cabextract mpan-fe.exe && rm mpan-fe.exe
|
RUN cabextract mpan-fe.exe && rm mpan-fe.exe
|
||||||
|
|
||||||
FROM mindcollapse/malware-multi-scan-worker:latest
|
FROM mindcollapse/malware-multi-scan-scanner:latest
|
||||||
|
|
||||||
RUN apt-get update && apt-get install -y libc6-i386
|
RUN apt-get update && apt-get install -y libc6-i386
|
||||||
|
|
||||||
|
@ -2,17 +2,14 @@ using System;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
|
|
||||||
namespace MalwareMultiScan.Backends.Interfaces
|
namespace MalwareMultiScan.Backends.Interfaces
|
||||||
{
|
{
|
||||||
public interface IScanBackend
|
public interface IScanBackend
|
||||||
{
|
{
|
||||||
public string Id { get; }
|
public string Id { get; }
|
||||||
|
|
||||||
public Task<string[]> ScanAsync(string path, CancellationToken cancellationToken);
|
public Task<string[]> ScanAsync(string path, CancellationToken cancellationToken);
|
||||||
public Task<string[]> ScanAsync(Uri uri, CancellationToken cancellationToken);
|
public Task<string[]> ScanAsync(Uri uri, CancellationToken cancellationToken);
|
||||||
public Task<string[]> ScanAsync(IFormFile file, CancellationToken cancellationToken);
|
|
||||||
public Task<string[]> ScanAsync(Stream stream, CancellationToken cancellationToken);
|
public Task<string[]> ScanAsync(Stream stream, CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,12 +4,9 @@
|
|||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\MalwareMultiScan.Shared\MalwareMultiScan.Shared.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<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" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -4,6 +4,8 @@ namespace MalwareMultiScan.Backends.Messages
|
|||||||
{
|
{
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
public string Backend { get; set; }
|
public string Backend { get; set; }
|
||||||
|
|
||||||
|
public bool Succeeded { get; set; }
|
||||||
public string[] Threats { get; set; }
|
public string[] Threats { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,11 +2,10 @@ FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS builder
|
|||||||
|
|
||||||
WORKDIR /src
|
WORKDIR /src
|
||||||
|
|
||||||
COPY MalwareMultiScan.Worker /src/MalwareMultiScan.Worker
|
COPY MalwareMultiScan.Worker /src/MalwareMultiScan.Scanner
|
||||||
COPY MalwareMultiScan.Shared /src/MalwareMultiScan.Shared
|
|
||||||
COPY MalwareMultiScan.Backends /src/MalwareMultiScan.Backends
|
COPY MalwareMultiScan.Backends /src/MalwareMultiScan.Backends
|
||||||
|
|
||||||
RUN dotnet publish -c Release -r linux-x64 -o ./publish MalwareMultiScan.Worker/MalwareMultiScan.Worker.csproj
|
RUN dotnet publish -c Release -r linux-x64 -o ./publish MalwareMultiScan.Scanner/MalwareMultiScan.Scanner.csproj
|
||||||
|
|
||||||
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
|
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
|
||||||
|
|
||||||
@ -14,7 +13,4 @@ WORKDIR /worker
|
|||||||
|
|
||||||
COPY --from=builder /src/publish /worker
|
COPY --from=builder /src/publish /worker
|
||||||
|
|
||||||
ENV ASPNETCORE_ENVIRONMENT=Production
|
ENTRYPOINT ["/worker/MalwareMultiScan.Scanner"]
|
||||||
ENV ASPNETCORE_URLS=http://+:9901
|
|
||||||
|
|
||||||
ENTRYPOINT ["/worker/MalwareMultiScan.Worker"]
|
|
@ -11,10 +11,5 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\MalwareMultiScan.Backends\MalwareMultiScan.Backends.csproj" />
|
<ProjectReference Include="..\MalwareMultiScan.Backends\MalwareMultiScan.Backends.csproj" />
|
||||||
<ProjectReference Include="..\MalwareMultiScan.Shared\MalwareMultiScan.Shared.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<_ContentIncludedByDefault Remove="Properties\launchSettings.json" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -56,28 +56,35 @@ namespace MalwareMultiScan.Scanner.Services
|
|||||||
var cancellationTokenSource = new CancellationTokenSource(
|
var cancellationTokenSource = new CancellationTokenSource(
|
||||||
TimeSpan.FromSeconds(_configuration.GetValue<int>("MaxScanningTime")));
|
TimeSpan.FromSeconds(_configuration.GetValue<int>("MaxScanningTime")));
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var result = new ScanResultMessage
|
var result = new ScanResultMessage
|
||||||
{
|
{
|
||||||
Id = message.Id,
|
Id = message.Id,
|
||||||
Backend = _backend.Id,
|
Backend = _backend.Id
|
||||||
|
|
||||||
Threats = await _backend.ScanAsync(
|
|
||||||
message.Uri, cancellationTokenSource.Token)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result.Threats = await _backend.ScanAsync(
|
||||||
|
message.Uri, cancellationTokenSource.Token);
|
||||||
|
|
||||||
|
result.Succeeded = true;
|
||||||
|
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
$"Backend {_backend.Id} completed a scan of {message.Id} " +
|
$"Backend {_backend.Id} completed a scan of {message.Id} " +
|
||||||
$"with result '{string.Join(", ", result.Threats)}'");
|
$"with result '{string.Join(", ", result.Threats)}'");
|
||||||
|
|
||||||
await _bus.SendAsync(_configuration.GetValue<string>("ResultsSubscriptionId"), result);
|
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
|
result.Succeeded = false;
|
||||||
|
|
||||||
_logger.LogError(
|
_logger.LogError(
|
||||||
exception, "Scanning failed with exception");
|
exception, "Scanning failed with exception");
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
await _bus.SendAsync(
|
||||||
|
_configuration.GetValue<string>("ResultsSubscriptionId"), result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user