What is ClamAV?
ClamAV (Clam AntiVirus) is an open-source antivirus engine designed for detecting malwares, viruses, and other malicious threats. It is widely used for scanning file servers, and web applications. ClamAV is known for its lightweight nature and ability to be integrated into various security systems.
Why integrate ClamAV into your web application?
Integrating ClamAV into a web application, provides several security benefits:- Protect uploaded files: If your application allows users to upload files, scanning them for viruses ensures that no malicious files enter your system.
- Regulatory compliance: Many industries require businesses to scan files for malware as part of cybersecurity compliance.
- Prevent system contamination: Detecting and removing infected files early prevents malware from spreading across your infrastructure.
- Increase user trust: Ensuring that files are clean enhances trust in your platform, especially in cloud-based environments like Upsun.
Using ClamAV with service mode vs one-time scan mode
There are two possible (and cumulative) policy approaches to file verification with ClamAV:- One-time scan mode: This mode of ClamAV allows the user to scan an entire file tree without distinction. A pro is that all files can be scanned, however this does take a considerably long amount of time to process all the verifiers - which some may consider to be a con.
- Service mode: This mode of ClamAV allows the user to scan specific files on demand. eg. the user uploads 3 new files and scans only these. A pro is that this process is very fast, due to the limitation of files to be processed, however a con is that it also requires communication between the application and the antivirus.
Setting up ClamAV
Add ClamAV binary
To use ClamAV on Upsun, you need to install the ClamAV binaries.Let’s use Upsun composable-image to add ClamAV to our application container:
Add ClamAV configuration
After adding the ClamAV binaries, you need to configure ClamAV (see the full ClamAV documentation here). This involves modifying two configuration files to add on the reposotory project, each corresponding to one of the two main commands: This includes updating the virus database (akafreshclam).
Create these 2 files, etc/freshclam.conf and etc/clamd.conf, with the following:
clamscan).
DatabaseDirectory and NotifyClamd to the mount points with write permissions.
Add mount endpoint for storage
Since ClamAV needs to write to the disk, we must add appropriate mount points with the necessary write permissions.PLATFORM_APP_DIR to define the application’s default path.
And let’s add execution permissions to the script.
To automate this, we will add the script call to the deployment hooks.
Deploying ClamAV on Upsun
We need to push these additions to the Upsun project. (If you don’t have one, create a project on Upsun.)- Disk : > 800Mb
- Memory : > 1.5Gb
container_profile parameters in your clamav settings:
Test the ClamAV integration on Upsun
After the deployment, test that everything works as expected by opening an SSH connection:Automating updates and scans
In order to automate the database updates and the scans, we will be grouping all the necessary commands into shell scripts to make their usage easier:Extra feature
You can use runtime-operation provided by Upsun to trigger a new scan:Declare runtime operation
Call runtime operation
Then, trigger this runtime operation from your development machine (or any other environment with access to the Upsun CLI).Conclusion
By following this guide, you have learned how to set up ClamAV on Upsun, utilizing features like Upsun composable-image and deployment hooks to automate updates and scans. This approach not only strengthens your infrastructure’s security but also enhances user trust, especially in cloud environments like Upsun. With proper configuration and automation scripts, ClamAV can be a powerful tool for maintaining system integrity while remaining lightweight and easy to integrate. Another ClamAV integration approach in service mode in this article.Project on our Github Upsun