准备工作:linux下安装clamav成功,启动clamav并打开本地socket监听"/tmp/clamav.socket"
clamav开源工程目录:/usr/local/
修改配置文件:/usr/local/clamav/etc/clamd.conf
# Path to a local socket file the daemon will listen on.
# Default: disabled (must be specified by a user)
LocalSocket /tmp/clamd.socket
保存配置文件后,执行命令:
[root@ext sbin]# /usr/local/clamav/sbin/clamd &
[1] 25102
[root@ext sbin]# LibClamAV Warning: **************************************************
LibClamAV Warning: *** The virus database is older than 7 days! ***
LibClamAV Warning: *** Please update it as soon as possible. ***
LibClamAV Warning: **************************************************
[root@ext sbin]#
查看服务监听:
netstat -an|grep cla
unix [ ACC ] STREAM LISTENING /tmp/clamd.socket
该程序实现方式是通过读取文件内容,然后将文件内容以流的方式发送至clamav服务,通过对clamav服务的返回结果,来判断该文件是否存在病毒;
流程:
①、创建与clamav.socket的连接;
②、发送字符串 "zINSTREAM"到clamav,表示开始对数据流进行检查;
③、开始读取文件内容,然后讲文件内容以流的方式发送至clamav,发送内容 <= 10M ;(为了提高效率定为 10M ,可以根据自己实际情况调整该值大小)
④、数据发送完成后,发送 个字节的空字节至clamav,通过该方式通知clamav该次数据发送完成,可以开始返回检测结果;
⑤、读取clamav返回内容,其中包含内容 “stream: OK”时,表示该次检测的流中没有检测到病毒;否则,将会返回该病毒信息;
代码示例:
#define MAX_DATA_LEN 8000
#define MAX_FILE_LEN 1024*1024*10
#define START_CHECK_VIRUS "zINSTREAM" int virus_check(const char *szFileName){
int sock,fd;
int tmperror = ;
int len;
int revl = ;
unsigned long int todo = MAX_FILE_LEN;
char rbuf[];
uint32_t buf[MAX_DATA_LEN/sizeof(uint32_t)];
sock = CreateConnectLocaleSocket("/tmp/clamd.socket", &tmperror);
if (sock < ){
zlog_debug(cat,"CreateConnectLocaleSocket error");
return -;
}
fd = open (szFileName, O_RDONLY|);
if (fd < ){
zlog_debug(cat,"open [%s] err!",szFileName);
return -;
}
if(sendln(sock,START_CHECK_VIRUS, )){
zlog_debug(cat,"sendln zINSTREAM erro \n");
close(sock);
return -;
} while((len = read(fd, &buf[], sizeof(buf) - sizeof(uint32_t))) > ){
if((unsigned int)len > todo)
len = todo;
buf[] = htonl(len);
if(sendln(sock, (const char *)buf, len+sizeof(uint32_t))){
close(fd);
close(sock);
return -;
}
todo -= len;
if(!todo){
len = ;
break;
}
}
close(fd); *buf=;
sendln(sock, (const char *)buf, ); memset(rbuf,0x00,sizeof(rbuf));
revl = recv(sock, rbuf, sizeof(rbuf), ); close(sock);
return parse_Check_results(rbuf,revl);
} int parse_Check_results(const char *bufdata,int bufLen){
zlog_debug(cat,"parse_Check_results = [%s] ----------",bufdata);
if(!strcmp(bufdata,"stream: OK")){
return ;
}
else{
zlog_error(cat, "parse_Check_results err, info = [%s]",bufdata);
}
return -;
} int sendln(int sockd, const char *line, unsigned int len){
while(len)
{
int sent = send(sockd, line, len, );
if(sent <= )
{
if(sent && errno == EINTR)
{
continue;
}
return ;
}
line += sent;
len -= sent;
}
return ;
} int CreateConnectLocaleSocket(char *pszHostString, int *pnResCode){
int nConnectFd = ;
struct sockaddr_un struInAddr;//���� socket
if((nConnectFd=socket(AF_UNIX,SOCK_STREAM,))==-){
zlog_error(cat, "CreateConnectLocaleSocket err, errno = [%d] strerror(errno) = [%s]",
errno,strerror(errno));
*pnResCode=errno;
return -;
}
struInAddr.sun_family = AF_UNIX;
strcpy(struInAddr.sun_path, pszHostString);
if(connect(nConnectFd,(struct sockaddr *)&struInAddr,sizeof(struct sockaddr_un))==-)
{
zlog_error(cat, "CreateConnectLocaleSocket connect err, errno = [%d] strerror(errno) = [%s]",
errno,strerror(errno));
close(nConnectFd);
*pnResCode=errno;
return -;
}
return nConnectFd;
}
转载请注明出处