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表示無病毒;

目前病毒引擎已經調用成功,具體每個方法沒有去深入理解。

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