Automatically detecting files placed in my Downloads directory in Gentoo Linux and scanning them for viruses
February 20, 2016 9 Comments
I have been using Linux for almost a decade and have never been unduly concerned about viruses on my machines running Linux. However, I do receive files from people who use Windows and Mac OS, and some of those files might contain Windows or Mac OS viruses, so, as a matter of courtesy and assistance to others, it would make some sense to scan those files before passing them on. Furthermore, as I use some Windows applications under WINE, it would also make sense to scan received files for Windows viruses if I am going to use those files with a Windows application running under WINE.
External files could get into my Gentoo Linux installations via pen drives, memory cards, optical discs, e-mails, my Dropbox directory and downloads from Web sites. In this post I am going to concentrate on the last of these. All the various e-mail account providers I use already scan e-mails for viruses on their e-mail servers before I even download e-mail into the e-mail client on my laptop (standard practice these days), so e-mail is not a particular worry.
I have had ClamAV and its GUI, ClamTk, installed for a long time. Whilst ClamTk can be used to schedule a daily update of virus signatures and a daily scan of one’s home directory by ClamAV, I normally run ClamTk and ClamAV ad hoc. However, I can see some benefit in launching ClamAV automatically when I download a file from the Internet, so I decided to do the following …
Automatically scan a file downloaded via a Web browser
I use Firefox to browse the Web, and had configured it to download files to the directory
/home/fitzcarraldo/Downloads/. I decided to monitor automatically the
Downloads directory for the addition of any file. As I use the ext4 file system, the method I opted to use is inotify, specifically the inotifywait command which is available once you install the package sys-fs/inotify-tools.
It is surprisingly easy to create a shell script to detect files downloaded into a directory. The following script, running continuously in a terminal, would detect any files created in my
/home/fitzcarraldo/Downloads directory, scan the new files with ClamAV and display a report in the terminal window:
#!/bin/bash echo DIR=$HOME/Downloads inotifywait -q -m -e create --format '%w%f' $DIR | while read FILE do date echo "File $FILE has been detected. Scanning it for viruses now ..." clamscan $FILE echo done
A usable script would need to be a bit more sophisticated than the one shown above, because an existing file in the directory could be overwritten by one with the same name, or opened and amended. Furthermore, the script above would need a permanently open terminal window. Therefore I created a script to run in the background and use a GUI dialogue tool to pop up a window with the virus scanner’s report when the script detects a new or changed file in the Downloads directory. As this laptop has KDE 4 installed I opted to use KDialog to display the pop-up window, but I could instead have used Zenity. The final script is shown below.
#!/bin/bash DIR=$HOME/Downloads # Get rid of old log file rm $HOME/virus-scan.log 2> /dev/null inotifywait -q -m -e close_write,moved_to --format '%w%f' $DIR | while read FILE do # Have to check file length is nonzero otherwise commands may be repeated if [ -s $FILE ]; then date > $HOME/virus-scan.log clamscan $FILE >> $HOME/virus-scan.log kdialog --title "Virus scan of $FILE" --msgbox "$(cat $HOME/virus-scan.log)" fi done
Now when I download a file in Firefox, a window pops up, displaying a message similar to the following:
Virus scan of /home/fitzcarraldo/Downloads/eicar_com.zip – KDialog
Fri 19 Feb 23:42:02 GMT 2016
/home/fitzcarraldo/Downloads/eicar_com.zip: Eicar-Test-Signature FOUND
———– SCAN SUMMARY ———–
Known viruses: 4259980
Engine version: 0.98.7
Scanned directories: 0
Scanned files: 1
Infected files: 1
Data scanned: 0.00 MB
Data read: 0.00 MB (ratio 0.00:1)
Time: 4.595 sec (0 m 4 s)
Notice in the above message that ClamAV detected a virus in a file
eicar_com.zip that I downloaded from the European Expert Group for IT Security Web site (originally ‘European Institute for Computer Antivirus Research’). In fact the executable
eicar.com does not contain a real virus; it was designed to contain a known signature that virus scanner creators and users can use in checking anti-virus software. You can find out more about the virus test files on the EICAR Web site.
Of course, if I use applications other than Firefox to download files, I need to make sure they download the files into the applicable directory so that the script can detect and scan the files:
So, now I have a shell script that pops up a window informing me whether or not any file I put in
$HOME/Downloads/ contains a virus. But I would like the script to be launched automatically when I login to the Desktop Environment. Therefore, as I use KDE 4, I selected ‘System Settings’ > ‘Startup and Shutdown’ and, in the ‘Autostart’ pane, clicked on ‘Add Script…’ and entered the path to my shell script (I left ‘create as symlink’ ticked). Now, every time I use KDE, any file placed (automatically or manually) into
$HOME/Downloads/ is scanned for viruses automatically and a window pops up giving the result.
As my laptop is not always connected to the Internet, I prefer to update the ClamAV virus signatures database manually, which I do either using the ClamTk GUI or via the command line using the