ClamAv开源杀毒引擎详解

最近公司在弄文件交换系统,为了确保文件交换安全执行,需要添加文件扫描杀毒功能。系统要实现调用杀毒引擎,对文件查杀。和国内外比较著名、熟知的杀毒厂商(360、瑞星、金山、小红伞等)联系,均告知没有提供相应的接口供第三方调用。百度搜索中还发现有人使用金山制作杀毒u盘,具体过程就是使用安装后的金山的dll文件和病毒库,实现病毒查杀,但这都是10年前的知识了,而且现在金山都是云查杀,本地都没有病毒库。各种查找无果,就想有没有一些开源的杀毒引擎,百度一下,没想到还真有—-ClamAv。

1. ClamAv介绍
Clam AntiVirus(ClamAv)是免费而且开源代码的防病毒软件,软件与病毒库的更新皆有社群免费发布。ClamAv主要使用在由Linux、FreeBSD等Unix-like系统架构的邮件服务器上,提供电子邮件的病毒扫描服务,在Windows与Mac OSX 平台也有移植版。ClamAv官方网站:http://www.clamav.net/。源码下载(见下图):
这里写图片描述

ClamAv支持较多OS,针对相应的OS,选择安装程序(本人使用的是windows 32系统,见下图):
这里写图片描述

以上安装后,没有图形界面,只能通过命令行执行。由于使用的是windows系统,就想找个具有操作界面的程序,测试杀毒效果。Clamwin就是基于ClamAV实现的面向windows系统的开源图形操作杀毒程序。Clamwin官方网站:http://www.clamwin.com/,在官网中clamwin的介绍如下,是英文,比较好理解,就不翻译了。

ClamWin is a Free Antivirus program for Microsoft Windows 10 / 8 / 7 / Vista / XP / Me / 2000 / 98 and Windows Server 2012, 2008 and 2003.
ClamWin Free Antivirus is used by more than 600,000 users worldwide on a daily basis. It comes with an easy installer and open source code. You may download and use it absolutely free of charge. It features:
Ⅰ.High detection rates for viruses and spyware;
Ⅱ.Scanning Scheduler;
Ⅲ.Automatic downloads of regularly updated Virus Database.
Ⅳ.Standalone virus scanner and right-click menu integration to Microsoft Windows Explorer;
Ⅳ.Addin to Microsoft Outlook to remove virus-infected attachments automatically.

The latest version of Clamwin Free Antivirus is 0.99.1
Please note that ClamWin Free Antivirus does not include an on-access real-time scanner. You need to manually scan a file in order to detect a virus or spyware.
ClamWin Free Antivirus is based on ClamAV engine and uses GNU General Public License by the Free Software Foundation, and is free (as in freedom) software. To find out more about GNU GPL, please visit the following link: Philosophy of the GNU Project - Free Software Foundation.

Clamwin安装后界面如下:
这里写图片描述
使用clamwin和360杀毒对本人E盘分别进行杀毒,扫描结果两者有相同的扫描结果,也有不一致的,效果各有千秋吧。结果说明clamav引擎可以使用,开始研究源码,准备将clamav嵌入公司文件交换系统中。

2. 源码
使用vs2010打开源码,项目列表如下(涂抹是个人后建的项目,忽略掉):
这里写图片描述
整个解决方案中,libclamav是最关键的,libclamav是一个dll工程,其它工程均是引用该工程。clamav用到了openssl库,计算机需要安装openssl,否则项目无法编译成功。我使用的是windows7,openssl安装过程参照了以下两位博主的安装过程:
Window7下安装openssl完整版(亲测实现)
使用VS2010编译OpenSSL的过程记录
安装过程如果出错,可以多试几次,或百度解决(我就遇到了,具体问题不清楚了,不过问题很好解决,百度就有答案)。openssl我是安装在【C:\openssl】下,安装完毕后,右击libclamav项目【属性】,选择【配置属性】->【c/c++】->【常规】,在【附件包含目录】中添加“C:\openssl\include”目录即可。对于其它需要用到openssl库的工程,同样执行操作。展开libclamav项目,发现项目下面有libeasy32.lib和ssleasy32.lib,说明项目需要libeasy32.dll和ssleasy32.dll。在安装后的“C:\openssl\out32dll”下,找到libeasy32.dll、libeasy32.lib和ssleasy32.dll、ssleasy32.lib文件,将其拷贝到libclamav工程中libeasy32.lib显示的路径(在vs中点击libeasy.lib,在【属性】窗口中【项目文件】查看具体路径)。这样clamav就可以编译成功!
ClamAv病毒查杀主要包括两个过程:1.创建病毒扫描引擎;2.文件扫描。
①病毒扫描引擎创建
创建病毒扫描引擎代码步骤如下:

int cl_init(unsigned int options);//初始化函数,必须执行该函数,返回为CL_SUCCESS(值为0)表示成功
struct cl_engine *cl_engine_new(void);//创建一个新的扫描引擎,需要注意的是,文件扫描完成需要使用cl_engine_free()这个方法释放掉引擎,否则造成内存泄漏
int cl_load(const char *path, struct cl_engine *engine,
unsigned int *signo, unsigned int options);//加载病毒库(path为病毒库目录),加载成功返回CL_SUCCESS,本人病毒路径使用的是clamwin中的病毒库路径
int cl_engine_compile(struct cl_engine *engine);//引擎编译,为文件扫描准备

②文件扫描
文件扫描方法:

int cl_scanfile(const char *filename, const char **virname,unsigned long int *scanned, const struct cl_engine *engine,unsigned int options);//传入文件路径;病毒引擎,执行扫描,返回值:CL_VIRUS表示有病毒;CL_CLEAN表示无病毒;

目前病毒引擎已经调用成功,具体每个方法没有去深入理解。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章