Virus Scanner Component
Introduction
As part
of any project, we will allow users to upload some documents. We must scan these files for viruses and malware.
To that end, we will use ClamAV as our virus scanner. ClamAV has a
daemon process that can run on the OS in the background waiting for
requests to scan data. We will submit files that we receive from the
user to this daemon to scan. Based on the result, we will either reject
the file or persist it on our system.
ClamAV
ClamAV
is an open-source virus scanner. It provides a daemon process that can
be used to scan data. For our purposes, we will set up the ClamAV daemon
to listen for requests on a TCP socket. Using our Java API, we will
send request to scan data to that socket and inspect the results
returned by ClamAV.
Setting up ClamAV on Ubuntu
We
will use the default ClamAV that is packaged with Ubuntu. The advantage
of this is that it will be easier to update the scanner, than if we
built it from source code.
Setting Up ClamAV
To install ClamAV
sudo apt-get install clamav
|
This will install ClamAV, create a ClamAV user etc. This does not install the daemon process.
Setting Up ClamAV Daemon
To install the ClamAV Daemon
sudo apt-get install clamav-daemon
|
This will create the ClamAV Daemon process and start it up.
Updating virus definitions
By
default freshclam is installed as a daemon and the default update
frequency is 24 times a day. You can update this by modifying the file
/etc/clamav/freshclam.conf
Configuring ClamAV Daemon
The
default ClamAV Daemon configuration does not create the TCP socket that
we need. To set it up, we will need to add the following lines to
/etc/clamav/clamd.conf at the end
TCPSocket 3310
TCPAddr 127.0.0.1
StreamMaxLength 100M
|
Once this is done, you should restart the clamd service using the following line
sudo service clamav-daemon restart
|
Java Application Setup
To
communicate with the ClamAV daemon, ClamAV defines a protocol. We could
write Java code to use that protocol to make our request. Luckily there
is a Jenkins plugin that we can leverage that already has a Java API
for ClamAV.
Maven Import
Add the following maven import to your project pom file.
< dependency >
< groupId >org.jenkins-ci.plugins</ groupId >
< artifactId >clamav</ artifactId >
< version >0.2.1</ version >
</ dependency >
|
Usage
To actually perform a scan on a given set of data, you can use the following snippet of code
ClamAvScanner scanner =
new
ClamAvScanner(
"192.168.23.129"
,
3310
,
5000
);
boolean
isAvailable = scanner.ping();
ScanResult result = scanner.scan(
new
ByteArrayInputStream(
"X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*"
.getBytes()));
if
(result.getStatus() == ScanResult.Status.PASSED) {
System.out.println(
"No Virus"
);
}
else
{
System.out.println(
"Possible Virus detected"
);
}