{"id":1161,"date":"2012-08-28T08:49:55","date_gmt":"2012-08-28T00:49:55","guid":{"rendered":"http:\/\/rmohan.com\/?p=1161"},"modified":"2012-08-28T08:49:55","modified_gmt":"2012-08-28T00:49:55","slug":"clamav-virus-scanning","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=1161","title":{"rendered":"ClamAV Virus Scanning"},"content":{"rendered":"<p>Thankfully Linux isn\u2019t a platform which has a significant problem with Viruses, however it is always better to be safe than sorry. Luckily ClamAV is an excellent free anti-virus solution for Linux servers. However, at least on RedHat Enterprise 5 (RHEL5) the default install doesn\u2019t offer any automated scanning and alerting. So here is what I\u2019ve done:<\/p>\n<p>The following steps assume you are using RHEL5, but should apply to other Linux distributions as well.<\/p>\n<h2>First, you\u2019ll want to install ClamAV:<\/h2>\n<div>\n<div id=\"highlighter_570195\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>yum <\/code><code>install<\/code> <code>clamav clamav-db clamd<\/code><\/div>\n<div><code>\/etc\/init<\/code><code>.d<\/code><code>\/clamd<\/code> <code>start<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>On RHEL5 at least this automatically sets up a daily cron job that uses freshclam to update the virus definitions, so that\u2019s good.<\/p>\n<p>Next I recommend removing the test virus files, although you can save this until after you test the rest of the setup:<\/p>\n<div>\n<div id=\"highlighter_355469\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>rm<\/code> <code>-rf <\/code><code>\/usr\/share\/doc\/clamav-0<\/code><code>.95.3<\/code><code>\/test\/<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Now we want to setup our automation. I have a daily cron job that scans the entire server which can take several minutes, and then an hourly cron job that only scans files which were created or modified within the last hour. This should provide rapid notification of any infection without bogging your server down for 5 minutes every hour. The hourly scans run in a couple of seconds.<\/p>\n<p>Each scanning script then checks the scan logs to see if there were any infected files found, and if so immediately sends you a notification e-mail (you could set this address to your mobile phone\u2019s SMS account if you wanted).<\/p>\n<h2>The Daily Scan:<\/h2>\n<div>\n<div id=\"highlighter_591374\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>emacs <\/code><code>\/etc\/cron<\/code><code>.daily<\/code><code>\/clamscan_daily<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Paste in:<\/p>\n<div>\n<div id=\"highlighter_71115\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<div>12<\/div>\n<div>13<\/div>\n<div>14<\/div>\n<div>15<\/div>\n<div>16<\/div>\n<div>17<\/div>\n<div>18<\/div>\n<div>19<\/div>\n<div>20<\/div>\n<div>21<\/div>\n<div>22<\/div>\n<div>23<\/div>\n<div>24<\/div>\n<div>25<\/div>\n<div>26<\/div>\n<div>27<\/div>\n<div>28<\/div>\n<div>29<\/div>\n<\/td>\n<td>\n<div>\n<div><code>#!\/bin\/bash<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code># email subject<\/code><\/div>\n<div><code>SUBJECT=<\/code><code>\"VIRUS DETECTED ON `hostname`!!!\"<\/code><\/div>\n<div><code># Email To ?<\/code><\/div>\n<div><code>EMAIL=<\/code><code>\"me@domain.com\"<\/code><\/div>\n<div><code># Log location<\/code><\/div>\n<div><code>LOG=<\/code><code>\/var\/log\/clamav\/scan<\/code><code>.log<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>check_scan () {<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code># Check the last set of results. If there are any \"Infected\" counts that aren't zero, we have a problem.<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code> <code>[ `<\/code><code>tail<\/code> <code>-n 12 ${LOG}\u00a0 | <\/code><code>grep<\/code> <code>Infected | <\/code><code>grep<\/code> <code>-<\/code><code>v<\/code> <code>0 | <\/code><code>wc<\/code> <code>-l` != 0 ]<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>then<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>EMAILMESSAGE=`mktemp <\/code><code>\/tmp\/virus-alert<\/code><code>.XXXXX`<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>echo<\/code> <code>\"To: ${EMAIL}\"<\/code> <code>&gt;&gt;\u00a0 ${EMAILMESSAGE}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>echo<\/code> <code>\"From: alert@domain.com\"<\/code> <code>&gt;&gt;\u00a0 ${EMAILMESSAGE}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>echo<\/code> <code>\"Subject: ${SUBJECT}\"<\/code> <code>&gt;&gt;\u00a0 ${EMAILMESSAGE}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>echo<\/code> <code>\"Importance: High\"<\/code> <code>&gt;&gt; ${EMAILMESSAGE}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>echo<\/code> <code>\"X-Priority: 1\"<\/code> <code>&gt;&gt; ${EMAILMESSAGE}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>echo<\/code> <code>\"`tail -n 50 ${LOG}`\"<\/code> <code>&gt;&gt; ${EMAILMESSAGE}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>sendmail -t &lt; ${EMAILMESSAGE}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>fi<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>}<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>clamscan -r \/ --exclude-<\/code><code>dir<\/code><code>=<\/code><code>\/sys\/<\/code> <code>--quiet --infected --log=${LOG}<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>check_scan<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<div>\n<div id=\"highlighter_85900\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>chmod<\/code> <code>+x <\/code><code>\/etc\/cron<\/code><code>.daily<\/code><code>\/clamscan_daily<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>The Hourly Scan:<\/h2>\n<div>\n<div id=\"highlighter_867027\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>emacs <\/code><code>\/etc\/cron<\/code><code>.hourly<\/code><code>\/clamscan_hourly<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Paste in:<\/p>\n<div>\n<div id=\"highlighter_602010\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<div>12<\/div>\n<div>13<\/div>\n<div>14<\/div>\n<div>15<\/div>\n<div>16<\/div>\n<div>17<\/div>\n<div>18<\/div>\n<div>19<\/div>\n<div>20<\/div>\n<div>21<\/div>\n<div>22<\/div>\n<div>23<\/div>\n<div>24<\/div>\n<div>25<\/div>\n<div>26<\/div>\n<div>27<\/div>\n<div>28<\/div>\n<div>29<\/div>\n<div>30<\/div>\n<div>31<\/div>\n<\/td>\n<td>\n<div>\n<div><code>#!\/bin\/bash<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code># email subject<\/code><\/div>\n<div><code>SUBJECT=<\/code><code>\"VIRUS DETECTED ON `hostname`!!!\"<\/code><\/div>\n<div><code># Email To ?<\/code><\/div>\n<div><code>EMAIL=<\/code><code>\"me@domain.com\"<\/code><\/div>\n<div><code># Log location<\/code><\/div>\n<div><code>LOG=<\/code><code>\/var\/log\/clamav\/scan<\/code><code>.log<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>check_scan () {<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code># Check the last set of results. If there are any \"Infected\" counts that aren't zero, we have a problem.<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code> <code>[ `<\/code><code>tail<\/code> <code>-n 12 ${LOG}\u00a0 | <\/code><code>grep<\/code> <code>Infected | <\/code><code>grep<\/code> <code>-<\/code><code>v<\/code> <code>0 | <\/code><code>wc<\/code> <code>-l` != 0 ]<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>then<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>EMAILMESSAGE=`mktemp <\/code><code>\/tmp\/virus-alert<\/code><code>.XXXXX`<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>echo<\/code> <code>\"To: ${EMAIL}\"<\/code> <code>&gt;&gt;\u00a0 ${EMAILMESSAGE}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>echo<\/code> <code>\"From: alert@domain.com\"<\/code> <code>&gt;&gt;\u00a0 ${EMAILMESSAGE}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>echo<\/code> <code>\"Subject: ${SUBJECT}\"<\/code> <code>&gt;&gt;\u00a0 ${EMAILMESSAGE}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>echo<\/code> <code>\"Importance: High\"<\/code> <code>&gt;&gt; ${EMAILMESSAGE}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>echo<\/code> <code>\"X-Priority: 1\"<\/code> <code>&gt;&gt; ${EMAILMESSAGE}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>echo<\/code> <code>\"`tail -n 50 ${LOG}`\"<\/code> <code>&gt;&gt; ${EMAILMESSAGE}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>sendmail -t &lt; ${EMAILMESSAGE}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>fi<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>}<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>find<\/code> <code>\/ -not -wholename <\/code><code>'\/sys\/*'<\/code> <code>-and -not -wholename <\/code><code>'\/proc\/*'<\/code> <code>-mmin -61 -<\/code><code>type<\/code> <code>f -print0 | <\/code><code>xargs<\/code> <code>-0 -r clamscan --exclude-<\/code><code>dir<\/code><code>=<\/code><code>\/proc\/<\/code> <code>--exclude-<\/code><code>dir<\/code><code>=<\/code><code>\/sys\/<\/code> <code>--quiet --infected --log=${LOG}<\/code><\/div>\n<div><code>check_scan<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>find<\/code> <code>\/ -not -wholename <\/code><code>'\/sys\/*'<\/code> <code>-and -not -wholename <\/code><code>'\/proc\/*'<\/code> <code>-cmin -61 -<\/code><code>type<\/code> <code>f -print0 | <\/code><code>xargs<\/code> <code>-0 -r clamscan --exclude-<\/code><code>dir<\/code><code>=<\/code><code>\/proc\/<\/code> <code>--exclude-<\/code><code>dir<\/code><code>=<\/code><code>\/sys\/<\/code> <code>--quiet --infected --log=${LOG}<\/code><\/div>\n<div><code>check_scan<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<div>\n<div id=\"highlighter_719263\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>chmod<\/code> <code>+x <\/code><code>\/etc\/cron<\/code><code>.hourly<\/code><code>\/clamscan_hourly<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>Protected System<\/h2>\n<p>You should now have a well protected system with low impact to system performance and rapid alerting. Anti-Virus is only one piece of protecting a server, but hopefully this makes it easy to implement for everyone.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thankfully Linux isn\u2019t a platform which has a significant problem with Viruses, however it is always better to be safe than sorry. Luckily ClamAV is an excellent free anti-virus solution for Linux servers. However, at least on RedHat Enterprise 5 (RHEL5) the default install doesn\u2019t offer any automated scanning and alerting. So here is what [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1161"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1161"}],"version-history":[{"count":2,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1161\/revisions"}],"predecessor-version":[{"id":1163,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1161\/revisions\/1163"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}