clamav安装
clamav-0.105.1.linux.x86_64.rpm
添加用户组和用户
groupadd clamav
useradd -g clamav clamav
创建日志目录、病毒库目录和套接字目录
mkdir -p /usr/local/clamav/logs
mkdir -p /usr/local/clamav/update
mkdir -p /usr/local/clamav/socket
创建日志文件
touch /usr/local/clamav/logs/clamd.log
touch /usr/local/clamav/logs/freshclam.log
文件授权
chown clamav:clamav /usr/local/clamav/logs/clamd.log
chown clamav:clamav /usr/local/clamav/logs/freshclam.log
chown clamav:clamav /usr/local/clamav/logs
chown clamav:clamav /usr/local/clamav/update
chown clamav:clamav /usr/local/clamav/socket
修改配置文件
cp /usr/local/clamav/etc/clamd.conf.sample /usr/local/clamav/etc/clamd.conf
cp /usr/local/clamav/etc/freshclam.conf.sample /usr/local/clamav/etc/freshclam.conf
vim /usr/local/clamav/etc/clamd.conf
LogFile /usr/local/clamav/logs/clamd.log
PidFile /usr/local/clamav/update/clamd.pid
DatabaseDirectory /usr/local/clamav/update
LocalSocket /usr/local/clamav/socket/clamd.socket
# TCP port address.
# Default: no
TCPSocket 3310
# TCP address.
# By default we bind to INADDR_ANY, probably not wise.
# Enable the following to provide some degree of protection
# from the outside world. This option can be specified multiple
# times if you want to listen on multiple IPs. IPv6 is now supported.
# Default: no
TCPAddr 192.168.0.1
# Maximum length the queue of pending connections may grow to.
# Default: 200
MaxConnectionQueueLength 30
# Close the connection when the data size limit is exceeded.
# The value should match your MTA's limit for a maximum attachment size.
# Default: 100M
StreamMaxLength 3000M
# Limit port range.
# Default: 1024
StreamMinPort 1024
# Default: 2048
StreamMaxPort 32000
vim /usr/local/clamav/etc/freshclam.conf
DatabaseDirectory /usr/local/clamav/update
UpdateLogFile /usr/local/clamav/logs/freshclam.log
PidFile /usr/local/clamav/update/freshclam.pid
DatabaseMirror database.clamav.net
cp /usr/local/clamav/etc/*.conf /usr/local/etc/
vim /etc/ld.so.conf
/usr/local/clamav/lib64
ldconfig
更新病毒库
/usr/local/clamav/bin/freshclam
建立软链接
ln -s /usr/local/clamav/bin/clamscan /usr/local/bin/clamscan
ln -s /usr/local/clamav/bin/freshclam /usr/local/bin/freshclam
启动服务
/usr/local/clamav/sbin/clamd
SpringBoot 初始化连接
<dependency>
<groupId>xyz.capybara</groupId>
<artifactId>clamav-client</artifactId>
<version>2.1.2</version>
</dependency>
package com.zzlc.saiams.config.clamav;
import java.io.File;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import cn.hutool.core.io.IoUtil;
import xyz.capybara.clamav.ClamavClient;
import xyz.capybara.clamav.ClamavException;
import xyz.capybara.clamav.Platform;
import xyz.capybara.clamav.commands.scan.result.ScanResult;
@Configuration
public class ClamavConnection {
private static Logger logger = LoggerFactory.getLogger(ClamavConnection.class);
private final static String clamav_uri = "clamav.uri";
private static ClamavClient client;
@Autowired
Environment env;
@Bean(name = "clamavClient")
public ClamavClient getClamavConnection() {
try {
String uri = env.getProperty(clamav_uri);//clamav.uri: 192.168.0.1:3310
String host = uri.split(":")[0];
int port = Integer.parseInt(uri.split(":")[1]);
client = new ClamavClient(host, port, Platform.UNIX);
logger.info("----->>> connect clamav " + host+":"+port + " success <<<-----");
logger.info(client.version());
return client;
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}
/**
* @param file
* @return 0 安全 1有病毒
*/
public static int scan(InputStream file) throws ClamavException {
try {
ScanResult scanResult = client.scan(file);
if(scanResult instanceof ScanResult.OK) {
return 0;
}else if(scanResult instanceof ScanResult.VirusFound) {
Map<String, Collection<String>> foundViruses = ((ScanResult.VirusFound) scanResult).getFoundViruses();
logger.info(foundViruses.toString());
return 1;
}
} catch (ClamavException e) {
logger.error(e.getMessage());
throw e;
}
return 0;
}
public static int scan(File file) {
try {
ScanResult scanResult = client.scan(file.toPath());
if(scanResult instanceof ScanResult.OK) {
return 0;
}else if(scanResult instanceof ScanResult.VirusFound) {
Map<String, Collection<String>> foundViruses = ((ScanResult.VirusFound) scanResult).getFoundViruses();
logger.info(foundViruses.toString());
return 1;
}
} catch (ClamavException e) {
logger.error(e.getMessage());
return scan(IoUtil.toStream(file));
}
return 0;
}
}