.net Core Cli to Scan Uploaded Files
This article volition demonstrate virtually the what is the easiest mode to protect our files/documents from viruses before they got uploaded to a storage location. Usually, without performing anti-virus scanning or other content checking against the uploaded file, there might exist a chance that attackers could target other users of the application by uploading malware to the server.
In today's article, we will perform filtering and content checking on whatsoever files which are uploaded to the server. Files should be thoroughly scanned and validated confronting an anti-virus scanner with upwards-to-date virus signatures before existence made available to other users. Any files flagged as malicious should be discarded or deleted immediately.
Prerequisites
I've been using the .Cyberspace 5.0 template to perform this operation and hither I'chiliad sharing the link to install the SDK,
Create a Web API projection with a .Net 5.0 Template.
Step 2
Install the Parcel which will be used for the virus when uploading whatsoever document to the API endpoint.
For more than details almost nclam & Clam Av, yous can cheque here.
Step iii
There are two possible ways in which we can use this Clam AV Server in our projection,
- Running the Clam Av Server under the Docker image
- Using their own Clam Av Ip accost to browse our files (If clam server is already installed in our machine)
Running the Mollusk Av Server under the Docker image
Setup the Mollusk server - in local using Docker
- Click here to install the docker.
- Use the below commands in command prompt to setup the clam Av server locally
- docker run -d -p 3310:3310 mkodockx/docker-clamav:alpine
- docker run -d -p 3310:3310 mk0x/docker-clamav
- docker ps
Once this setup is washed, let'south create an endpoint and the necessary configuration to scan our files under that mollusk Av server by docker epitome. Add the Mollusk Av port configuration in the appsettings.json file.
appsettings.json
- {
- "Logging" : {
- "LogLevel" : {
- "Default" : "Information" ,
- "Microsoft" : "Warning" ,
- "Microsoft.Hosting.Lifetime" : "Information"
- }
- },
- "AllowedHosts" : "*" ,
- "ClamAVServer" : {
- "URL" : "localhost" ,
- "Port" : "3310"
- }
- }
Create an endpoint and read the file from the file upload endpoint and convert the file to a byte array. ClamClient scan result returns with ClamScanResult enum values which tells you that your browse was make clean or a virus was detected.
Here is some sample lawmaking,
FileController.cs
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Logging;
- using nClam;
- using Organization;
- using Organisation.Collections.Generic;
- using Organization.IO;
- using Organization.Linq;
- using System.Net;
- using Organisation.Threading.Tasks;
- namespace ScanFiles_AntiVirus.Controllers
- {
- [Route("api/[controller]" )]
- [ApiController]
- public grade FileUploadController : ControllerBase
- {
- private readonly IConfiguration _configuration;
- public FileUploadController(
- IConfiguration configuration)
- {
- _configuration = configuration;
- }
- [HttpPost]
- public async Task<IActionResult> UploadFile(IFormFile file)
- {
- if (file == nix || file.Length == 0)
- return Content( "file not selected" );
- var ms =new MemoryStream();
- file.OpenReadStream().CopyTo(ms);
- byte[] fileBytes = ms.ToArray();
- string Result = cord.Empty;
- attempt
- {
- var mollusk =new ClamClient( this ._configuration[ "ClamAVServer:URL" ],
- Catechumen.ToInt32(this ._configuration[ "ClamAVServer:Port" ]));
- var scanResult = await clam.SendAndScanFileAsync(fileBytes);
- Result = scanResult.Resultswitch
- {
- ClamScanResults.Clean =>"Clean" ,
- ClamScanResults.VirusDetected =>"Virus Detected" ,
- ClamScanResults.Mistake =>"Error in File" ,
- ClamScanResults.Unknown =>"Unknown File" ,
- _ =>"No instance available"
- };
- }
- catch (Exception ex)
- {
- throw ex;
- }
- render Ok(Result);
- }
- }
- }
Run and Test with Valid File,
Test with Anti-malware File
As a POC, the EICAR file was uploaded. This is a sample file used to test the response of anti-virus software. You can download a sample file from this Site. You may demand to pause your antivirus protection on your device to perform this activity.
Using their ain Clam Av Ip address to scan our files
To browse the documents without docker image and if the clam av server is already installed in our machine we need to change a bit of code in the API level instead of going with docker port.
Hither is the following lawmaking,
FileController.cs
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Logging;
- using nClam;
- using Organisation;
- using Arrangement.Collections.Generic;
- using Arrangement.IO;
- using System.Linq;
- using System.Net;
- using System.Threading.Tasks;
- namespace ScanFiles_AntiVirus.Controllers
- {
- [Route("api/[controller]" )]
- [ApiController]
- public class FileUploadController : ControllerBase
- {
- private readonly IConfiguration _configuration;
- public FileUploadController(
- IConfiguration configuration)
- {
- _configuration = configuration;
- }
- [HttpPost]
- public async Job<IActionResult> UploadFile(IFormFile file)
- {
- if (file == null || file.Length == 0)
- return Content( "file non selected" );
- var ms =new MemoryStream();
- file.OpenReadStream().CopyTo(ms);
- byte[] fileBytes = ms.ToArray();
- string Effect = string.Empty;
- effort
- {
- var clam = new ClamClient(IPAddress.Parse( "127.0.0.ane" ), 3310);
- var scanResult = await mollusk.SendAndScanFileAsync(fileBytes);
- Result = scanResult.Resultswitch
- {
- ClamScanResults.Clean =>"Make clean" ,
- ClamScanResults.VirusDetected =>"Virus Detected" ,
- ClamScanResults.Error =>"Error in File" ,
- ClamScanResults.Unknown =>"Unknown File" ,
- _ =>"No case bachelor"
- };
- }
- take hold of (Exception ex)
- {
- throw ex;
- }
- render Ok(Result);
- }
- }
- }
Conclusion
Thank you for reading, delight let me know your questions, thoughts, or feedback in the comments section. I appreciate your feedback and encouragement.
keep learning....!
reyesmamrainy1941.blogspot.com
Source: https://www.c-sharpcorner.com/article/antivirus-protection-scan-on-document-upload-in-net/
Post a Comment for ".net Core Cli to Scan Uploaded Files"