ossec 自定义解码实例--- 防火墙自动响应阻止邮件服务器攻击 -------- Neo 2011-10-28
最近公司邮件服务器总遭遇外网攻击,目的是要获取服务器内用户账号信息(账号和密码),一但枚举成功,他们就会拿获取到的用户信息登陆邮件服务器,并例用这个账号大规模的向外转发垃圾邮件。
以上状况如果不能及时发现,导至的后果还是挺麻烦的。比如:对于本服务器来说:系统负载和硬盘空间会被填满。更糟糕的是你的邮件服务器可能会被“梭子鱼反垃圾邮件”等工具加入黑名单,这将导致你企业的正常邮件无法发送。
在邮件服务器的LOG中你会看到类似的信息 (/var/log/message)
Oct 27 19:40:18 dns ipop3d[17030]: Login failed user=windows auth=windows host=221x255x188x40.ap221.ftth.ucom.ne.jp [221.255.188.40]
Oct 27 19:40:14 dns ipop3d[17029]: Login failed user=windows auth=windows host=221x255x188x40.ap221.ftth.ucom.ne.jp [221.255.188.40]
Oct 27 19:40:11 dns ipop3d[17028]: Login failed user=windows auth=windows host=221x255x188x40.ap221.ftth.ucom.ne.jp [221.255.188.40]
Oct 27 19:40:08 dns ipop3d[17027]: Login failed user=windows auth=windows host=221x255x188x40.ap221.ftth.ucom.ne.jp [221.255.188.40]
Oct 18 01:18:45 dns ipop3d[13099]: Login failed user=smtp auth=smtp host=[78.97.86.105]
Oct 18 01:18:40 dns ipop3d[13098]: Login failed user=jerry auth=jerry host=[78.97.86.105]
Oct 18 01:18:36 dns ipop3d[13097]: Login failed user=merry auth=merry host=[78.97.86.105]
Oct 18 01:18:31 dns ipop3d[13096]: Login failed user=brou auth=brou host=[78.97.86.105]
Oct 18 01:18:27 dns ipop3d[13095]: Login failed user=ben auth=ben host=[78.97.86.105]
Oct 18 01:18:22 dns ipop3d[13094]: Login failed user=root auth=root host=[78.97.86.105]
Oct 18 01:18:18 dns ipop3d[13093]: Login failed user=log auth=log host=[78.97.86.105]
安装OSSEC入侵检测程序以后默认可以正常工作,但有些LOG,OSSEC程序无法无法正常解码,所以无法抵挡攻击。
以下说明怎么添加解码。
一,运行OSSEC测试程序,并将需要解码的LOG粘贴进去。
/var/ossec/bin/ossec-logtest
Oct 28 06:27:30 dns ipop3d[18533]: Login excessive login failures user=mikael auth=mikael host=[58.64.134.123]' (粘贴进去)
会看到如下信息
**Phase 1: Completed pre-decoding.
full event: 'Oct 28 06:27:30 dns ipop3d[18533]: Login excessive login failures user=mikael auth=mikael host=[58.64.134.123]'
hostname: 'dns'
program_name: 'ipop3d' (添加解码时这个是关键)
log: 'Login excessive login failures user=mikael auth=mikael host=[58.64.134.123]'
**Phase 2: Completed decoding.
No decoder matched. (没有找到匹配的解码)
**Phase 3: Completed filtering (rules).
Rule id: '2501' (解码后用到的规则)
Level: '5'
Description: 'User authentication failure.'
**Alert to be generated.
/var/ossec/bin/ossec-logtest -f (用-f 选项查看使用到的详细规则)
Trying rule: 2501 - User authentication failure.
*Rule 2501 matched.
*Trying child rules.
Trying rule: 40111 - Multiple authentication failures. (执行动态响应时用到的规则)
**Phase 3: Completed filtering (rules).
Rule id: '2501'
Level: '5'
Description: 'User authentication failure.'
**Alert to be generated.
要想让OSSECC可以动态响应,关键的一步是要将log中的IP解码出来。
二,添加解码
编辑/var/ossec/etc/decoders.xml 文件 , 在末尾添加如下信息。
<decoder>
<program_name>^ipop3d</program_name> 指定程序的名称 ^ipop3d 这步是关键
</decoder>
<decoder> 定义 log中不带DNS的日志解码
<parent>poptest</parent>
<prematch>host=[</prematch>
<regex offset="after_prematch">^(\d+.\d+.\d+.\d+)]</regex>
<order>srcip</order> 指定只在LOG中提取IP地址
</decoder>
<decoder> 定义 log中带DNS的日志解码
<parent>poptest</parent>
<prematch>host=\S+ [</prematch> "\S+"在这里表示 排除“host= 和[” 这间的字符 (也就是DNS字段)
<regex offset="after_prematch">^(\d+.\d+.\d+.\d+)]</regex> 提取IP信息
<order>srcip</order> srcip 指定只在LOG中提取IP地址
</decoder>
保存文件。
三,再次测试解码
./var/ossec/bin/ossec-logtest
Oct 28 06:27:30 dns ipop3d[18533]: Login excessive login failures user=mikael auth=mikael host=[58.64.134.123]' (不带域名的LOG)
Oct 27 19:40:18 dns ipop3d[17030]: Login failed user=windows auth=windows host=221x255x188x40.ap221.ftth.ucom.ne.jp [221.255.188.40] (带域名的LOG)
(测试的时候一次输入一条log)
会看到如下信息
**Phase 1: Completed pre-decoding.
full event: 'Oct 28 06:27:30 dns ipop3d[18533]: Login excessive login failures user=mikael auth=mikael host=[58.64.134.123]'
hostname: 'dns'
program_name: 'ipop3d' (添加解码时这个是关键)
log: 'Login excessive login failures user=mikael auth=mikael host=[58.64.134.123]'
**Phase 2: Completed decoding.
decoder:'poptest' (再这里已经可以看到我们定义的解码poptest)
srcip: ' 58.64.134.123' (解码后提取到的IP地址)
**Phase 2: Completed decoding.
decoder:'poptest'
srcip: ' 221.255.188.40' (解码后提取到的IP地址)
**Phase 3: Completed filtering (rules).
Rule id: '2501' (解码后用到的规则)
Level: '5'
Description: 'User authentication failure.'
**Alert to be generated.
测试结果正常, 重启OSSECC服务
# /var/ossec/bin/ossec-control restart
四,查看OSSEC 的 firewall log 你会发现类似如下的信息
一 10月 24 12:56:48 CST 2011 /var/ossec/active-response/bin/host-deny.sh delete - 114.44.108.128 1319431578.181327 3104
一 10月 24 12:56:48 CST 2011 /var/ossec/active-response/bin/firewall-drop.sh delete - 114.44.108.128 1319431578.181327 3104
四 10月 27 14:08:57 CST 2011 /var/ossec/active-response/bin/firewall-drop.sh add - 210.33.40.103 1319695737.13970420 3104
四 10月 27 14:08:57 CST 2011 /var/ossec/active-response/bin/host-deny.sh add - 210.33.40.103 1319695737.13970420 3104
四 10月 27 14:19:27 CST 2011 /var/ossec/active-response/bin/host-deny.sh delete - 210.33.40.103 1319695737.13970420 3104
四 10月 27 14:19:27 CST 2011 /var/ossec/active-response/bin/firewall-drop.sh delete - 210.33.40.103 1319695737.13970420 3104
五 10月 28 16:36:07 CST 2011 /var/ossec/active-response/bin/firewall-drop.sh add - 10.13.64.10 1319790967.286147 40111
五 10月 28 16:36:07 CST 2011 /var/ossec/active-response/bin/host-deny.sh add - 10.13.64.10 1319790967.286147 40111
五 10月 28 16:46:37 CST 2011 /var/ossec/active-response/bin/host-deny.sh delete - 10.13.64.10 1319790967.286147 40111
五 10月 28 16:46:37 CST 2011 /var/ossec/active-response/bin/firewall-drop.sh delete - 10.13.64.10 1319790967.286147 40111
当然要在类似攻击发生以后才能看到上边的信息。
当连续收到10条攻击信息,就会触发以下规则,这条规则默认可以使攻击者10分钟内不能访问这台服务器。
<rule level="10" frequency="10" timeframe="160">
<if_matched_group>authentication_failed</if_matched_group>
<description>Multiple authentication failures.</description>
<group>authentication_failures,</group>
</rule>
完成。