TECHNICAL TUTORIAL

ClamAV Setup and .NET Scanning.

Install ClamAV, update signatures and invoke a local scan from C#.

ClamAV Linux C#

ClamAV installation and scanning

1. Install on Ubuntu

sudo apt update
sudo apt install clamav clamav-freshclam

Ensure the freshclam updater is running; avoid launching a second updater while its service holds the database lock.

2. Scan a folder

clamscan --recursive --infected /srv/uploads

Exit codes: 0 = clean, 1 = virus detected, 2 = error. Treat errors as failed scans, not clean results.

3. C# wrapper

using System.Diagnostics;

public static async Task<int> ScanAsync(string safePath, CancellationToken ct)
{
    var psi = new ProcessStartInfo("clamscan")
    {
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        UseShellExecute = false
    };
    psi.ArgumentList.Add("--infected");
    psi.ArgumentList.Add("--no-summary");
    psi.ArgumentList.Add(safePath);
    using var process = Process.Start(psi) ?? throw new InvalidOperationException("Cannot start ClamAV");
    var stdout = process.StandardOutput.ReadToEndAsync(ct);
    var stderr = process.StandardError.ReadToEndAsync(ct);
    await process.WaitForExitAsync(ct);
    await Task.WhenAll(stdout, stderr);
    return process.ExitCode;
}

Only scan validated server-owned paths, impose file-size and execution limits, and quarantine uploads until a successful scan. For high volume, prefer a configured clamd service rather than spawning a new process per file.