🔗 使用c-icap进行代理内容实时防病毒检查
作者:Yuri Voinov
🔗 目录
对于Squid-3.0及更高版本,我们可以使用ICAP进行内容过滤或防病毒检查。此配置示例描述了如何使用squidclamav防病毒模块结合ClamAV防病毒服务进行实时病毒扫描。这与推荐的squidclamav配置略有不同,并已针对Squid-3.4及以上版本和最新配置更改进行了调整。
🔗 用法
这对于拦截代理和显式代理都很有用。通过适当的ClamAV配置,验证几乎不会带来任何明显的延迟,并且可以在可接受的延迟内完成。
🔗 构建c-icap服务器
下载最新的c-icap源代码(更改日志)。进行防病毒检查不需要BerkeleyDB支持。然后按照下面的说明进行配置和编译。
# 32 bit
./configure 'CXXFLAGS=-O2 -m32 -pipe' 'CFLAGS=-O2 -m32-pipe' --enable-large-files --without-bdb --prefix=/usr/local
# 64 bit
./configure 'CXXFLAGS=-O2 -m64 -pipe' 'CFLAGS=-O2 -m64 -pipe' --without-bdb --prefix=/usr/local
make/gmake
make/gmake install-strip
🔗 配置和运行c-icap服务器
像这样编辑c-icap.conf
PidFile /var/run/c-icap/c-icap.pid
CommandsSocket /var/run/c-icap/c-icap.ctl
StartServers 1
MaxServers 20
MaxRequestsPerChild 100
Port 1344
User squid
Group squid
ServerAdmin yourname@yourdomain
TmpDir /tmp
MaxMemObject 131072
DebugLevel 0
ModulesDir /usr/local/lib/c_icap
ServicesDir /usr/local/lib/c_icap
LoadMagicFile /usr/local/etc/c-icap.magic
acl all src 0.0.0.0/0.0.0.0
acl localhost src 127.0.0.1/255.255.255.255
acl PERMIT_REQUESTS type REQMOD RESPMOD
icap_access allow localhost PERMIT_REQUESTS
icap_access deny all
ServerLog /var/log/i-cap_server.log
AccessLog /var/log/i-cap_access.log
如有必要,请编辑路径并启动c-icap服务器。为您的操作系统添加启动脚本。
注意:在最新的squidclamav版本(从squidclamav版本6.14开始)中,*OPTIONS*方法已从扫描中排除。因此,无需允许访问它。
注意:TmpDir通常设置为/var/tmp(这是默认值)。更改它时要**非常**小心。当对象在内存中大于MaxMemObject时,TmpDir用于临时文件。并且在处理完成后,这些临时文件(CI_TMP_XXXX)会保留在TmpDir中。否则,高负载服务器上的/var文件系统的可用空间可能会用完,请安排TmpDir的维护。
注意:在某些情况下,您可以增加MaxMemObject以提高性能,但会增加一些RAM消耗。有时建议将此参数设置为您操作系统逻辑IO单元的最大值。
🔗 使用c-icap、ClamAV守护进程和Squidclamav进行防病毒检查
当然,这个安装需要更多的资源,尤其是在单台主机上安装时。但它也提供了更多的灵活性,并且在某些情况下可以提供更好的可扩展性。
🔗 构建、配置和运行ClamAV守护进程
ClamAV包含在许多存储库中,可以从它们那里获得。配置clamd时,选项要非常保守。默认值是一个很好的起点。我不建议使用SafeBrowsing,因为它存在性能和内存问题,也不建议使用DetectPUA,因为它会产生很多误报。还要注意防病毒数据库的更新——它们会非常频繁地发生。我每天更新24次。
注意:ClamAV守护进程(clamd)是一个内存消耗服务,在最小配置下使用约200-300兆字节(主要用于将AV数据库存储在内存中),在深度扫描大型存档时可能会更高。因此,您可以将其放在具有与代理快速网络互连的单独节点上(此选项仅在使用squidclamav时有效)。
注意:在clamd.conf中将StreamMaxLength参数设置为与squidclamav.conf中的maxsize相同的值非常重要。
即,取消注释并在clamd.conf中调整
StreamMaxLength 5M
🔗 构建和配置squidclamav
安装SquidClamav需要您已经按照上述说明安装了c-icap。您必须将c-icap的安装路径提供给configure命令,然后进行编译和安装。
# 32 bit
./configure 'CXXFLAGS=-O2 -m32 -pipe' 'CFLAGS=-O2 -m32 -pipe' --with-c-icap=/usr/local
# 64 bit
./configure 'CXXFLAGS=-O2 -m64 -pipe' 'CFLAGS=-O2 -m64 -pipe' --with-c-icap=/usr/local
make/gmake
make/gmake install-strip
这将把squidclamav.so库安装到c-icap的modules/services存储库中。然后将以下行添加到c-icap.conf文件中。
Service squidclamav squidclamav.so
然后像这样调整squidclamav.conf
maxsize 5000000
# When a virus is found then redirect the user to this URL
redirect http://<You_proxy_FQDN>:8080/cgi-bin/clwarn.cgi.xx_XX
# Path to the clamd socket, use clamd_local if you use Unix socket or if clamd
# is listening on an Inet socket, comment clamd_local and set the clamd_ip and
# clamd_port to the corresponding value.
clamd_local /tmp/clamd.socket
#clamd_ip 127.0.0.1
#clamd_port 3310
# Set the timeout for clamd connection. Default is 1 second, this is a good
# value but if you have slow service you can increase up to 3.
timeout 3
# Force SquidClamav to log all virus detection or squiguard block redirection
# to the c-icap log file.
logredir 1
# Enable / disable DNS lookup of client ip address. Default is enabled '1' to
# preserve backward compatibility but you must desactivate this feature if you
# don't use trustclient with hostname in the regexp or if you don't have a DNS
# on your network. Disabling it will also speed up squidclamav.
dnslookup 0
# Enable / Disable Clamav Safe Browsing feature. You must have enabled the
# corresponding behavior in clamd by enabling SafeBrowsing into freshclam.conf
# Enabling it will first make a safe browsing request to clamd and then the
# virus scan request.
safebrowsing 0
#
# Here is some defaut regex pattern to have a high speed proxy on system
# with low resources.
#
# Abort AV scan, but not chained program
abort \.google\.*
abort \.youtube\.com
abort \.googlevideo\.com
abort \.ytimg\.com
abort \.yimg\.com
# Do not scan images
abort ^.*\.([j|J][p|P][?:[e|E]?[g|G]|gif|png|bmp|ico|svg|web[p|m])
abortcontent ^image\/.*$
# Do not scan text files
abort ^.*\.((cs|d?|m?|p?|r?|s?|w?|x?|z?)h?t?m?(l?)|php[3|5]?|rss|atom|vr(t|ml)|(c|x|j)s[s|t|px]?)
abortcontent ^text\/.*$
abortcontent ^application\/x-javascript$
abortcontent ^application\/javascript$
abortcontent ^application\/json$
# Do not scan fonts
abort ^.*\.(ttf|eot|woff2?)
abortcontent ^font\/.*$
abortcontent ^application\/x-woff$
abortcontent ^application\/font-woff2?$
abortcontent ^application\/x-font-ttf$
# Do not scan (streamed) videos and audios
abort ^.*\.(flv|f4f|mp(3|4))
abortcontent ^video\/.*$
abortcontent ^audio\/.*$
abortcontent ^application\/mp4$
# Do not scan flash files
abort ^.*\.swfx?
abortcontent ^application\/x-shockwave-flash$
# Do not scan sequence of framed Microsoft Media Server (MMS) data packets
abortcontent ^.*application\/x-mms-framed.*$
# White list some sites
# Abort both AV and chained program
whitelist clamav\.net
whitelist securiteinfo\.com
whitelist sanesecurity\.com
whitelist clamav\.bofhland\.org
whitelist threatcenter\.crdf\.fr
whitelist \.avast\.*
whitelist \.gdatasoftware\.com
whitelist \.emsisoft\.*
whitelist \.chilisecurity\.*
whitelist pcpitstop\.com
whitelist \.unthreat\.com
whitelist \.preventon\.com
whitelist lavasoft\.com
whitelist \.norton\.com
whitelist \.symantec\.com
whitelist \.symantecliveupdate\.com
whitelist \.kaspersky\.*
whitelist \.drweb\.*
whitelist \.mcafee\.com
whitelist \.fsecure\.com
whitelist \.f-secure\.com
whitelist \.esetnod32\.*
whitelist \.eset\.*
whitelist \.escanav\.com
whitelist \.360totalsecurity\.com
whitelist \.bitdefender\.com
whitelist pckeeper\.com
whitelist \.mysecuritycenter\.com
whitelist \.avira\.com
whitelist \.pandasecurity\.com
whitelist \.vipreantivirus\.com
whitelist \.quickheal\.com
whitelist \.trustport\.*
whitelist \.trustport-ru\.*
whitelist \.sophos\.com
whitelist \.spamfighter\.com
whitelist \.webroot\.com
whitelist \.k7computing\.com
whitelist \.charityantivirus\.com
whitelist \.avg\.com
whitelist \.trendmicro\.*
whitelist \.zonealarm\.com
whitelist \.comodo\.com
#
whitelist update\.microsoft\.com
whitelist update\.microsoft\.com\.akadns\.net
whitelist download\.windowsupdate\.com
whitelist download\.microsoft\.com
whitelist update\.microsoft\.com
#
whitelist \.oracle\.com
#
whitelist \.shallalist\.de
whitelist opencsw\.org
# See also 'trustuser' and 'trustclient' configuration directives
#
并重启c-icap服务器。最后,不要忘记将clwarn.cgi.xx_XX(其中xx_XX匹配您的语言)放到您的Web服务器cgi-bin目录下。
由于白名单可能很大,为了减少维护和简化管理,您可以创建一个单独的文件来包含白名单正则表达式,并像这样配置squidclamav。
# White list some sites
# Abort both AV and chained program
whitelist /usr/local/etc/squidclamav_whitelist
其中squidclamav_whitelist包含
clamav\.net
securiteinfo\.com
sanesecurity\.com
clamav\.bofhland\.org
threatcenter\.crdf\.fr
...
注意:您可能希望使用I-CAP模板进行重定向,而不是squidclamav重定向。在这种情况下,您必须根据自己的需求自定义c-icap模板。
🔗 Squid 配置文件
Squid-3.4及更早版本需要使用**–enable-icap-client**选项进行构建。较新版本默认启用此选项。
像这样粘贴配置文件
# -------------------------------------
# Adaptation parameters
# -------------------------------------
icap_enable on
icap_send_client_ip on
icap_send_client_username on
icap_client_username_header X-Authenticated-User
icap_preview_enable on
icap_preview_size 1024
icap_service service_avi_req reqmod_precache icap://:1344/squidclamav bypass=on
adaptation_access service_avi_req allow all
icap_service service_avi_resp respmod_precache icap://:1344/squidclamav bypass=off
adaptation_access service_avi_resp allow all
启用了IPv6的操作系统可能会将localhost解析为双栈启用的::1地址。如果您在连接到仅IPv4的ICAP服务时遇到问题,只需将上面的**localhost**替换为**127.0.0.1**。
🔗 使用c-icap和病毒检查模块进行防病毒检查
与eCAP类似,您可以使用libclamav执行防病毒检查。这不需要守护进程,并且节省了运行clamd所需的500兆字节(平均)的内存。这对于单层设置可能很有用。
I-CAP模块提供了两个子模块:使用ClamAV守护进程,以及仅使用libclamav。
🔗 构建c-icap模块
下载最新的模块,然后根据您的ClamAV和c-icap构建类型(32位或64位)进行配置和构建。
# 32 bit GCC
./configure 'CFLAGS=-O3 -m32 -pipe' 'CPPFLAGS=-I/usr/local/clamav/include' 'LDFLAGS=-L/usr/local/lib -L/usr/local/clamav/lib'
# 64 bit GCC
./configure 'CFLAGS=-O3 -m64 -pipe' 'CPPFLAGS=-I/usr/local/clamav/include' 'LDFLAGS=-L/usr/local/lib -L/usr/local/clamav/lib/'
gmake
gmake install-strip
要构建clamav_mod子模块(使用libclamav),您可能需要用最新的修复程序修补您的c-icap安装。它使用OpenSSL头文件依赖项,并且您可能会在模块构建时遇到问题。如果您的系统有较旧的OpenSSL版本(例如0.9.8),则可以解决此问题。要做到这一点,只需将旧的OpenSSL头文件路径添加到CPPFLAGS变量中。
🔗 配置c-icap模块
在clamav_mod.conf中添加非默认参数
clamav_mod.TmpDir /var/tmp
clamav_mod.MaxFilesInArchive 1000
clamav_mod.MaxScanSize 5M
clamav_mod.HeuristicScanPrecedence on
clamav_mod.OLE2BlockMacros on
在virus_scan.conf中添加非默认参数
virus_scan.ScanFileTypes TEXT DATA EXECUTABLE ARCHIVE DOCUMENT
virus_scan.SendPercentData 5
virus_scan.PassOnError on
virus_scan.MaxObjectSize 5M
virus_scan.DefaultEngine clamav
Include clamav_mod.conf
在c-icap.conf的末尾添加以下行
Include virus_scan.conf
注意:您还必须在ClamAV安装目录中创建一个指向clamd.conf中配置的ClamAV防病毒数据库目录的符号链接,例如
# ln -s /var/lib/clamav /usr/local/clamav/share/clamav
最后重启c-icap服务以应用更改。
🔗 Squid配置文件
Squid-3.4需要使用**–enable-icap-client**选项进行构建。较新版本默认启用此选项。
像这样粘贴配置文件
icap_enable on
icap_service service_avi_req reqmod_precache icap://:1344/virus_scan bypass=off
adaptation_access service_avi_req allow all
icap_service service_avi_resp respmod_precache icap://:1344/virus_scan bypass=on
adaptation_access service_avi_resp allow all
使用squidclamav时,您必须使用Squid ACL和adaptation_access指令绕过白名单网站。您还可以自定义virus_scan模块模板以适应您的语言等。
另外请注意:没有clamd,您将拥有相同的300-500兆字节的加载AV数据库到一个c-icap进程中使用libclamav。
🔗 测试您的安装
将您的客户端机器指向代理,然后访问EICAR测试病毒,并确保您被重定向到警告页面。
对于真正大型的安装,您可以将所有检查基础设施组件放在单独的节点上——即代理、c-icap服务器、ClamAV。就这些了,伙计们!
🔗 DNSBL过滤支持
如果出于谨慎考虑,您还可以为c-icap兼容的设置启用DNSBL URL检查支持。
为此,您需要下载并安装c-icap模块
# 32 bit GCC
./configure 'CFLAGS=-O3 -m32 -pipe' 'CPPFLAGS=-I/usr/local/clamav/include' 'LDFLAGS=-L/usr/local/lib -L/usr/local/clamav/lib'
# 64 bit GCC
./configure 'CFLAGS=-O3 -m64 -pipe' 'CPPFLAGS=-I/usr/local/clamav/include' 'LDFLAGS=-L/usr/local/lib -L/usr/local/clamav/lib'
gmake
gmake install-strip
然后将此添加到您的c-icap.conf文件中
Module common dnsbl_tables.so
Include srv_url_check.conf
像这样调整srv_url_check.conf
Service url_check srv_url_check.so
url_check.LookupTableDB whitelist domain hash:/usr/local/etc/domain.whitelist "Whitelist"
url_check.LookupTableDB blackuribl domain dnsbl:black.uribl.com
url_check.Profile default pass whitelist
url_check.Profile default block blackuribl
url_check.Profile default pass ALL
url_check.DefaultAction pass AddXHeader "X-Next-Services"
注意:出于性能原因,使用白名单是一个好主意。这是一个纯文本文件,包含二级域名。此域之外的所有主机名都将通过。设置DNS缓存也是一个提高性能的好主意。
并将此添加到您的squid.conf
# DNSBL service
# Requires to enable "Module common dnsbl_tables.so" in c-icap.conf,
# and install and configure c-icap modules!
icap_service service_dnsbl_req reqmod_precache icap://:1344/url_check bypass=on
adaptation_access service_dnsbl_req allow all
最后,您必须重启c-icap服务并重启您的squid。基本上就是这样。
注意:在ClamAV防病毒服务**之前**添加DNSBL ICAP服务。
当使用squidclamav AV服务时,最好在请求上创建适应链,如下所示
icap_enable on
icap_send_client_ip on
icap_send_client_username on
icap_client_username_header X-Client-Username
icap_preview_enable on
icap_preview_size 1024
icap_service_failure_limit -1
# DNSBL service
# Requires to enable "Module common dnsbl_tables.so" in c-icap.conf
icap_service service_dnsbl_req reqmod_precache icap://:1344/url_check bypass=on routing=on
# ClamAV service
icap_service service_avi_req reqmod_precache icap://:1344/squidclamav bypass=on
adaptation_service_chain svcRequest service_dnsbl_req service_avi_req
adaptation_access svcRequest allow all
icap_service service_avi_resp respmod_precache icap://:1344/squidclamav
adaptation_access service_avi_resp allow all
使用DNSBL时,建议在c-icap主机上设置DNS缓存以提高性能。
🔗 性能和调优
实际上,使用clamd和squidclamav的配置是最快的。事实上,squidclamav使用INSTREAM执行AV检查是最好的方法。您可能只需要根据负载调整c-icap服务中的工作进程数量。您将只有两个瓶颈——您的代理服务器与c-icap的交互以及c-icap与防病毒服务的交互。您需要将这些交互的延迟减少到最小。
在某些情况下,将所有服务放在单台主机上并不是一个好主意。高负载设置必须在不同层之间分离。
-
不要做额外的工作——尽可能使用白名单。
-
避免过载——尤其是在所有服务都安装在单台主机上的情况下。
-
尽可能减少内存消耗。不要设置高的clamd系统限制——这会增加延迟和内存消耗,并可能导致系统在高峰时段崩溃。
-
使用链来正确适应和自定义顺序,并进行正确的访问——以免因不必要的工作而使各个阶段过载。
在所有情况下,c-icap工作进程在扫描期间都会产生高CPU负载。您必须尽可能地最小化扫描。不要扫描所有数据类型。不要扫描受信任的网站。当然,也不要尝试扫描YouTube视频。
在某些Solaris设置上,使用libmtmalloc进行c-icap进程可以获得性能提升。只需在配置时将* -lmtmalloc*添加到CFLAGS和CXXFLAGS。这还可以减少多核CPU盒上的内存锁定争用。此解决方案还可以解决clamd的内存消耗问题。
带有自定义数据库(SecuriteInfo等)或最新版本(0.102.x)的Clamd使用700兆字节或更多的RAM。在这种情况下,最好使用独立的服务器。
🔗 多层设置
由于最新的ClamAV会占用大量内存(高达1 GB RAM),多层设置可能更好。
要实现这一点,请记住
c-icap应保留在squid的层;因为squid与c-icap通过TCP的连接是不可靠的。
squidclamav将通过TCP与clamd通信;只需修改squidclamav.conf并重启c-icap。
clamd_ip your_clamav_tier_ip
clamd_port 3310
在squidclamav.conf中注释掉clamd_local。
在ClamAV层,在clamd.conf中取消注释此参数并重启守护进程。
TCPSocket 3310
不要忘记在ClamAV层的防火墙上打开TCP端口3310。
🔗 C-ICAP监控
要监控C-ICAP的一些运行时统计信息,您可以使用此处所述的解决方案,并进行一些补充和修正。
您可以使用CLI和Web界面通过内置信息服务来监控C-ICAP。
要使用CLI,请使用此命令(或将其添加为shell别名)
/usr/local/bin/c-icap-client -s "info?view=text" -i localhost -p 1344 -req use-any-url
或作为shell别名
alias icap_stat='c-icap-client -s '\''info?view=text'\'' -i localhost -p 1344 -req use-any-url'
结果将如下所示
ICAP server:localhost, ip:127.0.0.1, port:1344
Running Servers Statistics
===========================
Children number: 3
Free Servers: 27
Used Servers: 3
Started Processes: 5
Closed Processes: 2
Crashed Processes: 2
Closing Processes: 0
Child pids: 24689 15427 4947
Closing children pids:
Semaphores in use
sysv:accept/4
sysv:children-queue/5
Shared mem blocks in use
sysv:kids-queue/30 13 kbs
General Statistics
==================
REQUESTS : 44501
REQMODS : 39336
RESPMODS : 5071
OPTIONS : 94
FAILED REQUESTS : 5
ALLOW 204 : 44245
BYTES IN : 25625 Kbs 486 bytes
BYTES OUT : 5679 Kbs 536 bytes
HTTP BYTES IN : 16232 Kbs 612 bytes
HTTP BYTES OUT : 212 Kbs 887 bytes
BODY BYTES IN : 2621 Kbs 532 bytes
BODY BYTES OUT : 192 Kbs 288 bytes
要通过Web获取相同的信息,只需将这些行添加到squid.conf并重新配置。
# ICAP info service. URL: http://icap.info
acl infoaccess dstdomain icap.info
icap_service service_info reqmod_precache 1 icap://:1344/info
adaptation_service_set class_info service_info
adaptation_access class_info allow infoaccess
adaptation_access class_info deny all
使用上面的URL访问统计信息页面。
这里还有用于C-ICAP监控(性能相关/运行时统计)的Munin插件。
🔗 故障排除
升级c-icap服务器时,您通常还需要(在大多数情况下)重新构建squidclamav,以避免可能的API不兼容。
如果c-icap永久重启,请增加c-icap.conf中的DebugLevel并首先检查ServerLog。请注意,DebugLevel 0是生产值,在调整过程中可能会掩盖任何问题。
🔗 c-icap/eCAP共存
要在同一向量点对同一事务应用多个适配服务,必须使用adaptation_service_chain。适配顺序通常对于适配逻辑或性能至关重要,但Squid支持任何顺序的链式服务。Squid的适配链代码甚至不知道ICAP和eCAP之间的区别!例如,一个包含ICAP服务、然后是eCAP服务、然后是另一个ICAP服务的adaptation_service_chain是支持的。
当您在一个Squid实例中同时需要c-icap和eCAP时,您必须记住:适配服务/链定义和adaptation_access ACL的顺序很重要。适配逻辑定义了适配服务的默认偏好顺序,在adaptation_access指令中定义了哪些服务或链可以用于正在考虑的事务。在某些情况下,适配动作链可能是互斥的。因此,在进行适配配置时要小心。彻底测试适配逻辑。
注意:最简单的情况是将具有相同访问方案的适配进行链式处理。当链式适配的访问方案不同时,请使用adaptation_access以正确的顺序实现所需的适配目标。
⚠️ Disclaimer: Any example presented here is provided "as-is" with no support
or guarantee of suitability. If you have any further questions about
these examples please email the squid-users mailing list.
类别: ConfigExample
导航:网站搜索、网站页面、类别、🔼 向上