您的位置:首页 > 大数据 > 人工智能

防暴力破解SSH/FTP/SMTP用户密码----fail2ban操作实务

2011-11-30 16:49 906 查看
防暴力破解SSH/FTP/SMTP用户密码----fail2ban操作实务

在debian下以邮件服务器为例进行说明



一、邮件服务器现状

1、邮件服务器mail.warn 日志中每天大概有9000 次尝试登陆邮件服务器25 端口的失败记

录。如:

Nov 29 00:00:01 mailserver postfix/smtpd[4434]: warning: unknown[14.145.101.1]: SASL

LOGIN authentication failed: UGFzc3dvcmQ6

2、以上记录虽然只是正常的wanring,服务器也正常,但对服务器的安全造成很大潜在威

胁。

二、解决方法

在不影响正常收发邮件的情况下,采用fail2ban 配合iptables 来动态自动屏蔽恶意用

户ip,来排除暴力登陆的安全隐患。

三、Fail2ban简介

Fail2ban 扫描系统日志文件,例如/var/log/auth or /var/log/apache/error_log 并

从中找出多次尝试登录失败的IP 地址,并将该IP 地址加入防火墙的拒绝访问列表中。

Fail2ban 对于解决暴力破解、非法扫描能起到比较好的效果,它是一个基于防火墙链

添加新规则构成,并发送e-mail 通知系统管理员。Fail2ban 不仅可以使用自动识别可能的

暴力入侵,而且可按照快速且简易的用户自定义规则去分析,因为fail2ban 的原理是调

用iptables 实时阻挡外界的攻击,按照你的要求在一段时间内找出符合条件的日志,然后

动作,所以你的系统里必选装有iptables,以及Python 的支持。

四、fail2ban适用范围

针对SSH、FTP、SMTP 等协议字典暴力破解用户名密码。

五、操作步骤

1、安装

# apt-get install fail2ban

2、配置最大尝试登陆次数maxretry 和冻结违反规则ip 的时间bantime

# cat -n /etc/fail2ban/jail.local

18 [DEFAULT]

19

20 # "ignoreip" can be an IP address, a CIDR mask or a DNS host

21 ignoreip = 127.0.0.1 192.168.0.120

22 bantime = 60

23 maxretry = 2

3、配置postfix 的jail

# cat -n /etc/fail2ban/jail.local

207 [postfix]

208

209 #enabled = false

210 enabled = true

211 port = smtp (即默认的25 端口)

212 filter = postfix

213 #logpath = /var/log/mail.log

214 logpath = /var/log/mail.warn

4、配置正则过滤规则

# cat -n /etc/fail2ban/filter.d/postfix.conf

18 failregex = warning: unknown\[<HOST>\]: SASL LOGIN authentication failed:

UGFzc3dvcmQ6

5、测试上步编写的正则表达式是否正确

# fail2ban-regex /var/log/mail.warn /etc/fail2ban/filter.d/postfix.conf

Success, the total number of match is 18

6、启动fail2ban

# /etc/init.d/fail2ban start

7、观察fail2ban 的log

# tail -f /var/log/fail2ban.log

2011-11-30 11:38:35,250 fail2ban.filter : INFO Added logfile =

/var/log/mail.warn

2011-11-30 11:38:35,251 fail2ban.filter : INFO Set maxRetry = 2

2011-11-30 11:38:35,253 fail2ban.filter : INFO Set findtime = 600

2011-11-30 11:38:35,254 fail2ban.actions: INFO Set banTime = 60

2011-11-30 11:38:35,334 fail2ban.jail : INFO Jail 'ssh' started

2011-11-30 11:38:35,373 fail2ban.jail : INFO Jail 'postfix' started

2011-11-30 12:12:15,827 fail2ban.actions: WARNING [postfix] Ban 192.168.206.245

2011-11-30 12:13:15,881 fail2ban.actions: WARNING [postfix] Unban 192.168.206.245

2011-11-30 12:13:35,949 fail2ban.actions: WARNING [postfix] Ban 192.168.206.245

2011-11-30 12:14:36,009 fail2ban.actions: WARNING [postfix] Unban 192.168.206.245

#watch -n 1 fail2ban-client status postfix

Status for the jail: POSTFIX

|- filter

| |- File list: /var/log/maillog

| |- Currently failed: 2

| `- Total failed: 22

`- action

|- Currently banned: 0

| `- IP list:

`- Total banned: 0

8、测试

尝试连续错误输入密码2 次后,观察防火墙的规则中是否自动加入了被屏蔽的测试ip 地址

# iptables -L -v -n

Chain INPUT (policy ACCEPT 15545 packets, 9277K bytes)

pkts bytes target prot opt in out source destination

50 2389 fail2ban-postfix tcp -- * * 0.0.0.0/0

0.0.0.0/0 multiport dports 25,465

2266 168K fail2ban-ssh tcp -- * * 0.0.0.0/0

0.0.0.0/0 multiport dports 22

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 7899 packets, 1897K bytes)

pkts bytes target prot opt in out source destination

Chain fail2ban-postfix (1 references)

pkts bytes target prot opt in out source destination

35 1637 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0

Chain fail2ban-ssh (1 references)

pkts bytes target prot opt in out source destination

2266 168K RETURN all -- * * 0.0.0.0/0 0.0.0.0/0

六、限制

1、fail2ban 扫描系统日志并且日志有更改来发掘恶意用户,如果日志输出采用了buffer,

则会影响fail2ban 的行为。

2、很难评估fail2ban 的反应时间,因为fail2ban 扫描日志的周期是1s,如在1s 内尝试

登陆失败的次数为10 次,而设置maxretry 为5 次,那么只有在下一秒钟也就是第11 次登

陆失败的时候才能禁止掉该用户。

不过就实际情况而言,每秒钟尝试登陆的次数99%在2-3 次,并且系统mail 日志没有采用

输出缓存,故以上2 条限制对mail.warn 没太大影响。

七、无法在内网测试,需在线上观察的事项

1、cpu、load 的使用情况。

2、是否会出现其他无法预测的问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: