SpringBoot 整合 clamav 文件病毒检测

本文详细介绍ClamAV防病毒软件的安装步骤及配置过程,包括用户组与用户的创建、目录与文件的设置、配置文件的修改等。此外,还介绍了如何通过SpringBoot进行ClamAV客户端的初始化连接。

 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;
	}
}

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SunForYou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值