How to Install ClamAV on CentOS 7

àì夳堔傛蜴生んèń 2022-05-14 10:22 314阅读 0赞

ClamAV or Clam Antivirus is an open source antivirus tool for UNIX. It was built specially for scanning emails at mail gateways but these days it is used for securing various types of systems and applications. ClamAV provides a number of utilities including a flexible multi-threaded daemon, a command line scanner and advanced tool for automatic database updates. ClamAV is used widely for securing Linux servers, mail gateways. It is also used along with CPanel to secure the file system.

A few notable features of ClamAV are:

  • It is opensource, POSIX compliant, portable software licensed under GNU general public licence.
  • It provides fast scanning and supports on access scanning of a file.
  • It claims to detect over 1 million viruses, worms and Trojans, including Microsoft Office macro viruses, mobile malware, and other threats.
  • Capable of scanning within various types of archives and compressed files.
  • Supports Portable Executable files, ELF and Mach-O files as well.
  • Supports almost all mail formats and special files and formats.
  • Advanced database updater with support for scripted updates, digital signatures and DNS based database version queries.

Requirements

ClamAV does not require any special hardware, you will only need a server with CentOS 7.x installed. You will need root or sudo access to your system. If you are logged in as non root user, you can either execute sudo -i to switch to root account or you can use sudo command at the start of all the administrative command.

Installing ClamAV

Before installing any package, it is recommended to update the system and packages using the following command.

  1. yum -y update

After updating the system we will need to add the EPEL repository into our system as ClamAV is not available in the default repository of CentOS 7. To add EPEL repository run the following command.

  1. yum -y install epel-release
  2. yum -y update
  3. yum clean all

Now run the following command to install ClamAV antivirus.

  1. yum -y install clamav-server clamav-data clamav-update clamav-filesystem clamav clamav-scanner-systemd clamav-devel clamav-lib clamav-server-systemd

Before running Clam antivirus you will need to configure few things.

As we are working on CentOS, it comes comes with SELinux. SELinux may deny access to few files, which will generate errors and warning. You can either disable SELinux or modify it’s configuration so that ClamAV can access all the files on the disk.

To disable SELinux run the following command.

  1. setenforce 0

To modify its configuration to allow ClamAV, run the following command.

  1. setsebool -P antivirus_can_scan_system 1
  2. setsebool -P clamd_use_jit 1

To check the status of SELinux configuration for antivirus, run the following command.

  1. getsebool -a | grep antivirus

You will get following output.

  1. [root@ip-172-31-16-226 ~]# getsebool -a | grep antivirus
  2. antivirus_can_scan_system --> on
  3. antivirus_use_jit --> off

Once we have adjusted SELinux, we will now need to modify the scanner configuration files of ClamAV. By default a configuration file is created at /etc/clamd.d/scan.conf, hence you will only need to modify the configuration. Before we can use the configuration, we must need to remove or comment out the Example word from the configuration. You can run the following command to remove the example word directly.

  1. cp /etc/clamd.d/scan.conf /etc/clamd.d/scan.conf.backup
  2. sed -i -e "s/^Example/#Example/" /etc/clamd.d/scan.conf

If the above command results in an error then open the file in your favourite editor and remove the text Example from the file.

You also have the option of which user will run ClamAV. You can either run ClamAV with the root user or any other user like clamscan etc. By default ClamAV creates a user in your system which we can use to run the scanner service. To find the user, you can run the following command.

  1. cat /etc/passwd | grep clam

You will likely see the following output.

  1. [root@ip-172-31-7-222 ~]# cat /etc/passwd | grep clam
  2. clamupdate:x:995:992:Clamav database update user:/var/lib/clamav:/sbin/nologin
  3. clamscan:x:994:991:Clamav scanner user:/:/sbin/nologin

We can use the above clamscan user to run the ClamAV scanner. Open the scanner configuration file in your favourite editor. In this tutorial we will be using nano editor, if you don’t have nano installed, you can run yum -y install nano command to install nano editor in your computer.

  1. nano /etc/clamd.d/scan.conf

Scroll down to find the following lines.

  1. # Run as another user (clamd must be started by root for this option to work)
  2. # Default: don't drop privileges
  3. User clamscan

If you are using the ClamAV user, then you will need to start the daemon as root. As we are logged in as root, we can keep using this user. If you want you can also switch to root user here.

Next you will need to define the server type in the configuration, scroll down to find the following lines.

  1. # The daemon can work in local mode, network mode or both.
  2. # Due to security reasons we recommend the local mode.

# Path to a local socket file the daemon will listen on. # Default: disabled (must be specified by a user) #LocalSocket /var/run/clamd.scan/clamd.sock

Uncomment the line #LocalSocket /var/run/clamd.scan/clamd.sock to

  1. LocalSocket /var/run/clamd.scan/clamd.sock

Save and exit from editor. But before we can run the scanner service, we will need to configure and run Freshclam.

Freshclam is used to update the database of virus definitions into the server. Freshclam configuration file is located at /etc/freshclam.conf. We will take a backup of the file and then we will comment out the Example line from the file.

  1. cp /etc/freshclam.conf /etc/freshclam.conf.bakup
  2. sed -i -e "s/^Example/#Example/" /etc/freshclam.conf

We can now run Freshclam using the following command.

  1. freshclam

You will get following output.

  1. [root@ip-172-31-7-222 ~]# freshclam
  2. ClamAV update process started at Tue Nov 1 08:44:10 2016
  3. main.cvd is up to date (version: 57, sigs: 4218790, f-level: 60, builder: amishhammer)
  4. Trying host database.clamav.net (27.96.54.66)...
  5. Downloading daily.cvd [100%]
  6. daily.cvd updated (version: 22463, sigs: 804704, f-level: 63, builder: neo)
  7. Downloading bytecode-279.cdiff [100%]
  8. Downloading bytecode-280.cdiff [100%]
  9. Downloading bytecode-281.cdiff [100%]
  10. Downloading bytecode-282.cdiff [100%]
  11. Downloading bytecode-283.cdiff [100%]
  12. bytecode.cld updated (version: 283, sigs: 53, f-level: 63, builder: neo)
  13. Database updated (5023547 signatures) from database.clamav.net (IP: 27.96.54.66)

To check if the database is up to date or not, run the same command freshclam again, if any update is available, it will update it or it will show you that everything is up to date.

  1. [root@ip-172-31-7-222 ~]# freshclam
  2. ClamAV update process started at Tue Nov 1 08:51:46 2016
  3. main.cvd is up to date (version: 57, sigs: 4218790, f-level: 60, builder: amishhammer)
  4. daily.cvd is up to date (version: 22463, sigs: 804704, f-level: 63, builder: neo)
  5. bytecode.cld is up to date (version: 283, sigs: 53, f-level: 63, builder: neo)

As freshclam updates the virus database, it is recommended to run it frequently to download the latest malware signatures. To run it automatically, you either create a cron job or you can create a systemd service file.

To create a cron job run the following command.

  1. crontab -e

This will open the cron job file in VI text editor, go to insert mode by pressing i and then enter the following line into the file.

  1. 00 01,13 * * * /usr/bin/freshclam --quiet

Now press Esc and then in command mode type :wq to save and exit from editor. The above cron job will execute the the freshclam command, twice a day, once at 1AM and then again at 1PM.

You can also create a systemd service if you do not want to use cron jobs. Create a new text file using your favourite editor.

  1. nano /usr/lib/systemd/system/freshclam.service

Now add the following lines into the file.

  1. [Unit]
  2. Description = freshclam scanner
  3. After = network.target[Service]
  4. Type = forking
  5. ExecStart = /usr/bin/freshclam -d -c 2
  6. Restart = on-failure
  7. PrivateTmp = true

[Install] WantedBy=multi-user.target

Now save and exit from the editor. To start the service and enable it to automatically start at boot time, run the following command.

  1. systemctl start freshclam
  2. systemctl enable freshclam

Now the systemd service will run freshclam twice a day to update the database. You can also check the status of our newly created freshclam service using the following command.

  1. systemctl status freshclam

You should see following output.

  1. [root@ip-172-31-7-222 ~]# systemctl status freshclam
  2. freshclam.service - freshclam scanner
  3. Loaded: loaded (/usr/lib/systemd/system/freshclam.service; enabled; vendor preset: disabled)
  4. Active: active (running) since Tue 2016-11-01 09:07:06 UTC; 1min 7s ago
  5. Main PID: 4488 (freshclam)
  6. CGroup: /system.slice/freshclam.service
  7. └─4488 /usr/bin/freshclam -d -c 2

Nov 01 09:07:06 ip-172-31-7-222 systemd[1]: Starting freshclam scanner... Nov 01 09:07:06 ip-172-31-7-222 systemd[1]: Started freshclam scanner. Nov 01 09:07:06 ip-172-31-7-222 freshclam[4488]: freshclam daemon 0.99.2 (OS: linux-gnu, ARCH: x86_64, CPU: x86_64) Nov 01 09:07:06 ip-172-31-7-222 freshclam[4488]: ClamAV update process started at Tue Nov 1 09:07:06 2016 Nov 01 09:07:06 ip-172-31-7-222 freshclam[4488]: main.cvd is up to date (version: 57, sigs: 4218790, f-level: 60, builder: amishhammer) Nov 01 09:07:06 ip-172-31-7-222 freshclam[4488]: daily.cvd is up to date (version: 22463, sigs: 804704, f-level: 63, builder: neo) Nov 01 09:07:06 ip-172-31-7-222 freshclam[4488]: bytecode.cld is up to date (version: 283, sigs: 53, f-level: 63, builder: neo) Nov 01 09:07:06 ip-172-31-7-222 freshclam[4488]: -------------------------------------- Hint: Some lines were ellipsized, use -l to show in full.

Now as we have our virus definitions updated, we can proceed further to run the scanner service of ClamAV. To start the scanner service run the following command.

  1. systemctl start clamd@scan

To automatically start the service at boot time, run the following command.

  1. systemctl enable clamd@scan

To check the status of scanner service, run the following command.

  1. systemctl status clamd@scan

It should produce the following output when running normally.

  1. [root@ip-172-31-7-222 ~]# systemctl status clamd@scan
  2. clamd@scan.service - Generic clamav scanner daemon
  3. Loaded: loaded (/usr/lib/systemd/system/clamd@scan.service; disabled; vendor preset: disabled)
  4. Active: active (running) since Tue 2016-11-01 11:20:36 UTC; 12s ago
  5. Main PID: 4956 (clamd)
  6. CGroup: /system.slice/system-clamd.slice/clamd@scan.service
  7. └─4956 /usr/sbin/clamd -c /etc/clamd.d/scan.conf --foreground=yes

Nov 01 11:20:45 ip-172-31-7-222 clamd[4956]: HTML support enabled. Nov 01 11:20:45 ip-172-31-7-222 clamd[4956]: XMLDOCS support enabled. Nov 01 11:20:45 ip-172-31-7-222 clamd[4956]: HWP3 support enabled. Nov 01 11:20:45 ip-172-31-7-222 clamd[4956]: Self checking every 600 seconds. Nov 01 11:20:45 ip-172-31-7-222 clamd[4956]: PDF support enabled. Nov 01 11:20:45 ip-172-31-7-222 clamd[4956]: SWF support enabled. Nov 01 11:20:45 ip-172-31-7-222 clamd[4956]: HTML support enabled. Nov 01 11:20:45 ip-172-31-7-222 clamd[4956]: XMLDOCS support enabled. Nov 01 11:20:45 ip-172-31-7-222 clamd[4956]: HWP3 support enabled. Nov 01 11:20:45 ip-172-31-7-222 clamd[4956]: Self checking every 600 seconds.

You can also use clamconf utility to check the configuration files. This utility will display the configuration of config file clam.conf and freshclam.conf. It will display information about software settings, database, platform, and build information. To use this utility run the following command.

  1. clamconf

The sample output of the command is:

  1. [root@ip-172-31-7-222 ~]# clamconf
  2. Checking configuration files in /etcConfig file: clamd.conf
  3. ERROR: Please edit the example config file /etc/clamav/clamd.confConfig file: freshclam.conf
  4. ---------------------------
  5. StatsHostID disabled
  6. StatsEnabled disabled
  7. StatsTimeout disabled
  8. LogFileMaxSize = "1048576"
  9. LogTime disabled
  10. LogSyslog = "yes"
  11. LogFacility = "LOG_LOCAL6"
  12. LogVerbose disabled
  13. LogRotate disabled
  14. PidFile disabled
  15. DatabaseDirectory = "/var/lib/clamav"
  16. Foreground disabled
  17. Debug disabled
  18. AllowSupplementaryGroups disabled
  19. UpdateLogFile disabled
  20. DatabaseOwner = "clamupdate"
  21. Checks = "12"
  22. DNSDatabaseInfo = "current.cvd.clamav.net"
  23. DatabaseMirror = "database.clamav.net"
  24. PrivateMirror disabled
  25. MaxAttempts = "3"
  26. ScriptedUpdates = "yes"
  27. TestDatabases = "yes"
  28. CompressLocalDatabase disabled
  29. ExtraDatabase disabled
  30. DatabaseCustomURL disabled
  31. HTTPProxyServer disabled
  32. HTTPProxyPort disabled
  33. HTTPProxyUsername disabled
  34. HTTPProxyPassword disabled
  35. HTTPUserAgent disabled
  36. NotifyClamd = "/etc/clamd.conf"
  37. OnUpdateExecute disabled
  38. OnErrorExecute disabled
  39. OnOutdatedExecute disabled
  40. LocalIPAddress disabled
  41. ConnectTimeout = "30"
  42. ReceiveTimeout = "30"
  43. SubmitDetectionStats disabled
  44. DetectionStatsCountry disabled
  45. DetectionStatsHostID disabled
  46. SafeBrowsing disabled
  47. Bytecode = "yes"clamav-milter.conf not foundSoftware settings
  48. -----------------
  49. Version: 0.99.2
  50. Optional features supported: MEMPOOL IPv6 AUTOIT_EA06 BZIP2 LIBXML2 PCRE ICONV JITDatabase information
  51. --------------------
  52. Database directory: /var/lib/clamav
  53. main.cvd: version 57, sigs: 4218790, built on Wed Mar 16 23:17:06 2016
  54. bytecode.cld: version 283, sigs: 53, built on Thu Jun 23 15:01:37 2016
  55. daily.cld: version 22464, sigs: 804704, built on Tue Nov 1 10:03:49 2016
  56. Total number of signatures: 5023547Platform information
  57. --------------------
  58. uname: Linux 3.10.0-327.10.1.el7.x86_64 #1 SMP Tue Feb 16 17:03:50 UTC 2016 x86_64
  59. OS: linux-gnu, ARCH: x86_64, CPU: x86_64
  60. zlib version: 1.2.7 (1.2.7), compile flags: a9
  61. Triple: x86_64-redhat-linux-gnu
  62. CPU: i686, Little-endian
  63. platform id: 0x0a2152520804080503040805

Build information ----------------- GNU C: 4.8.5 20150623 (Red Hat 4.8.5-4)(4.8.5) GNU C++: 4.8.5 20150623 (Red Hat 4.8.5-4) (4.8.5) CPPFLAGS: CFLAGS: -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic -Wall -W -Wmissing-prototypes -Wmissing-declarations -std=gnu99 -fno-strict-aliasing -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE CXXFLAGS: -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic -std=gnu++98 LDFLAGS: -Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,--as-needed sizeof(void*) = 8 Engine flevel: 82, dconf: 82

To scan the files in a directory, you can use clamscan utility. Clamscan is the command line scanner utility in antivirus. To scan your home directory run the following command.

  1. clamscan --infected --remove --recursive /home /root

The above command will scan the directory /home and /root. --infected option will print the list of infected files, --remove options will remove the virus detected and --recursive option will enable clamscan to scan all the sub-directories and files. You will find the following output.

  1. [root@ip-172-31-7-222 ~]# clamscan --infected --remove --recursive /home /root

----------- SCAN SUMMARY ----------- Known viruses: 5018129 Engine version: 0.99.2 Scanned directories: 7 Scanned files: 12 Infected files: 0 Data scanned: 0.00 MB Data read: 0.00 MB (ratio 0.00:1) Time: 10.117 sec (0 m 10 s)

To verify that ClamAV is working correctly, you can use the EICAR Standard Anti-Virus Test File. It is simply a signature of a virus but not an actual virus. Download the file using following command.

  1. wget http://www.eicar.org/download/eicar_com.zip

Now scan the current directory using the following command.

  1. clamscan --infected --remove --recursive .

You should get following output.

  1. [root@ip-172-31-7-222 ~]# clamscan --infected --remove --recursive .
  2. ./eicar_com.zip: Eicar-Test-Signature FOUND
  3. ./eicar_com.zip: Removed.

----------- SCAN SUMMARY ----------- Known viruses: 5018129 Engine version: 0.99.2 Scanned directories: 4 Scanned files: 8 Infected files: 1 Data scanned: 0.00 MB Data read: 0.00 MB (ratio 0.00:1) Time: 10.758 sec (0 m 10 s)

To scan the whole system, you can run the following command.

  1. clamscan --infected --recursive --exclude-dir="^/sys" /

The above command will scan the whole server but excluding the /sys directory. /sys directory contains few unreachable files, hence scanning them will print unnecessary warnings.

The above commands can be used to scan the system, although be careful with --remove option, you can simply run the command without this option and then check for the files containing virus. Once you are sure of the files are containing virus, you can remove them manually or simply run the command again with --remove option.

Conclusion

以上病毒库签名文件总结如下:






















































































签名文件后缀名 签名类型 签名格式
.hdb 基于md5 hash的签名 HashString:FileSize:MalwareName
.hsb 基于sha1和sha256 hash的签名 HashString:FileSize:MalwareName
.mdb 基于PE section hash的签名 PESectionSize:PESectionHash:MalwareName
.db Body-based的基本签名 MalwareName=HexSignature
.ndb Body-based的扩展签名 MalwareName:TargetType:Offset:HexSignature[:MinFL:[MaxFL]]
.ldb Body-based的逻辑签名 SignatureName;TargetDescriptionBlock;LogicalExpression;Subsig0; Subsig1;Subsig2;…
.idb PE文件的图标签名 ICONNAME:GROUP1:GROUP2:ICON_HASH
.ndb PE文件版本信息元数据签名 my_test_vi_sig:1:VI:paste_your_hex_sig_here
.crb, .crtdb 信任和撤销的证书签名 Name;Trusted;Subject;Serial;Pubkey;Exponent;CodeSign;TimeSign;CertSign; NotBefore;Comment[;minFL[;maxFL]]
.cdb 容器元数据签名 VirusName:ContainerType:ContainerSize:FileNameREGEX:FileSizeInContainer:FileSizeReal:IsEncrypted:FilePos:Res1:Res2[:MinFL[:MaxFL]]
.zmd(zip)和.rmd(rar) 只基于ZIP/RAR元数据的签名 virname:encrypted:filename:normal size:csize:crc32:cmethod:fileno:max depth
.fp(md5)和.sfp(sha1和sha256) 白名单数据库 HashString:FileSize:MalwareName
.pdb 钓鱼url/host签名 R[Filter]:RealURL:DisplayedURL[:FuncLevelSpec]或H[Filter]:DisplayedHostname[:FuncLevelSpec]
.gdb 钓鱼URL hash签名 S:P:HostPrefix[:FuncLevelSpec]或S:F:Sha256hash[:FuncLevelSpec]或S1:P:HostPrefix[:FuncLevelSpec]或S1:F:Sha256hash[:FuncLevelSpec]或S2:P:HostPrefix[:FuncLevelSpec]或S2:F:Sha256hash[:FuncLevelSpec]或S:W:Sha256hash[:FuncLevelSpec]
*.wdb 钓鱼URL白名单签名 X:RealURL:DisplayedURL[:FuncLevelSpec]或M:RealHostname:DisplayedHostname[:FuncLevelSpec]
  1. sigtool查看cvd的信息 sigtool -i main.cvd
  2. sigtool解压cvd的信息 sigtool -u main.cvd
  3. sigtool生成指纹的方法: sigtool md5 test.exe > test.hdb
  4. sigtool生成指纹的方法: sigtool sha1 test.exe > test.hsb
  5. sigtool生成指纹的方法: sigtool sha256 test.exe >test.hsb
  6. sigtool生成指纹的方法: sigtool mdb test.exe > test.mdb

遗留问题:

  1. 但是sigtool关于如何重新打包,研究许久 尚未解决。

参考: https://blog.csdn.net/zourzh123/article/details/45719757

发表评论

表情:
评论列表 (有 0 条评论,314人围观)

还没有评论,来说两句吧...

相关阅读