Cover image for How To Setup ClamAV As a Service To Scan Files Before Uploading To A Server In A .Net Application

How To Setup ClamAV As a Service To Scan Files Before Uploading To A Server In A .Net Application

Profile image for Chandu Maram
Chandu Maram Full Stack Angular Developer
Jul 14, 2022 ‧ 2 min read
Series (1 Parts): .Net

ClamAV, or Clam Antivirus as it is also known, is an open-source antivirus solution developed for UNIX. In fact, it is the only open-source antivirus solution available. Naturally, we would like it to be available for Windows systems as well.

Download And Install ClamAV for Windows:

 
1) Download Latest version ClamAV (https://www.clamav.net/downloads)
  2) Install ClamAV 
      (The best thing to do is to let the installer install to the default location, which is C:/clamav)

Required Changes:

1) Open a command prompt/powerShell as Administator

2) Change directory to c:\clamav\ or wherever you installed clamav.

3) Copy files
    a) clamd.conf.sample and
    b) freshclam.conf.sample
        from from conf_examples folder to ClamAV and
        rename newly copies files to
    a) clamd.conf and
    b) freshclam.conf

OR
In command prompt:
copy .\conf_examples\freshclam.conf.sample .\freshclam.conf
copy .\conf_examples\clamd.conf.sample .\clamd.conf

4) Remove the word 'Example' from both files clamd.conf and freshclam.conf

Update Anti-virus Library:
1) Run .\freshclam.exe 

Install ClamD As a Service:
1) Run clamd.exe --install
2) Start ClamAV ClamD Service

Implementing ClamAV Antivirus scan files in .NET application

1) Install nClam package from NuGetPackage in Visual Studio.
2) Read file from File upload and convert to byte array.
3) Scan file byte array against ClamClient and validate ClamScanResult object.

ClamClient scan result returns with ClamScanResult enum values which tell you if your scan was clean or a  virus detected. Here is code:

KeyValuePair<bool, string> statusMsg = new KeyValuePair<bool, string>(false, "");
Byte[] bytes = byteArray;
 // Scan with Clam Av server  
var clam = new ClamClient(IPAddress.Parse("127.0.0.1"), 3310);
var scanResult = await clam.SendAndScanFileAsync(bytes);
switch (scanResult.Result)
{
  case ClamScanResults.Clean:
  statusMsg = new KeyValuePair<bool, string>(true, "Clean");
  break;
  case ClamScanResults.VirusDetected:  
  statusMsg = new KeyValuePair<bool, string>(true, "Virus detected.");
  break; 
  case ClamScanResults.Erroe:
  case ClamScanResults.Unkonwn:
  default:    
  statusMsg = new KeyValuePair<bool, string>(true, "Clean"); 
  break; 
}

return statusMsg;

Conclusion:
I wish I had more time to fill in the gaps and provide more detail, but I just don't. Even this amount of information took way too long to gather in the first place! It should provide a good foundation nevertheless.

Posted on Jul 14, 2022 by:
Profile image for Chandu Maram
Chandu Maram
Full Stack Angular Developer
AngularTypeScriptCSSC#.NETHTML5

Comments

Profile image for Chandu Maram

Full Stack Angular Developer

AngularTypeScriptCSSC#.NETHTML5
475
Reputation
10
Following
24
Followers