[03-11 13:43:54] 来源:http://www.67xuexi.com 服务器_操作系统教程 阅读:85736次
#-A OUTPUT -p udp --dport 67:68 --sport 67:68 -j ACCEPT
# Outbound DNS lookups
-A OUTPUT -o eth0 -p udp -m udp --dport 53 -j ACCEPT
# Outbound PING requests
-A OUTPUT -p icmp -j ACCEPT
# Outbound Network Time Protocol (NTP) request
-A OUTPUT -p udp --dport 123 --sport 123 -j ACCEPT
# SSH
-A INPUT -i eth0 -p tcp -m tcp --dport 22 -m state --state NEW -j ACCEPT
# Outbound HTTP
-A OUTPUT -o eth0 -p tcp -m tcp --dport 80 -m state --state NEW -j ACCEPT
-A OUTPUT -o eth0 -p tcp -m tcp --dport 443 -m state --state NEW -j ACCEPT
COMMIT
通过 iptables-apply 命令为规则集生效。如果你丢失连接,修补你的规则,在继续之前再试一下
$ sudo iptables-apply /etc/iptables/rules
Applying new ruleset.。。 done.
Can you establish NEW connections to the machine? (y/N) y
。。。 then my job is done. See you next time.
创建文件 /etc/network/if-pre-up.d/iptables,然后写入下面内容。当你启动服务器的时候,将自动载入你的iptables规则。
/etc/network/if-pre-up.d/iptables
#!/bin/sh
iptables-restore 《 /etc/iptables/rules
现在给它执行权限,执行文件,以确保它正常载入
$ sudo chmod +x /etc/network/if-pre-up.d/iptables
$ sudo /etc/network/if-pre-up.d/iptables
用 Fail2ban 处理潜在黑客
当谈到安全的时,Fail2ban 是我最喜欢的工具之一,它将监控你的日志文件,并且可以临时禁止那些正在滥用你资源,或者正在强制肆虐你的SSH连接,或者正在dos攻击你web服务器的用户。
Install Fail2ban
$ sudo apt-get install fail2ban
[sudo] password for sjones:
Reading package lists.。。 Done
Building dependency tree
Reading state information.。。 Done
The following extra packages will be installed:
gamin libgamin0 python-central python-gamin python-support whois
Suggested packages:
mailx
The following NEW packages will be installed:
fail2ban gamin libgamin0 python-central python-gamin python-support whois
0 upgraded, 7 newly installed, 0 to remove and 2 not upgraded.
Need to get 254 kB of archives.
After this operation, 1,381 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
。。。
虽然 Fail2ban 安装一个默认配置(/etc/fail2ban/jail.conf),但我们希望在 /etc/fail2ban/jail.local 写配置,所以把它拷贝到那儿。
sudo cp /etc/fail2ban/jail.{conf,local}
配置
把 ignoreip 行修改为你的ip,并且可以设置禁止恶意用户的时间量(默认是10分钟)。你也将希望设置一个destemail,这里我通常输入我自已的email地址,再在后面加上 ,fail2ban@blocklist.de。BlockList.de 是一个跟踪并且自动报告黑客IP的系统。
/etc/fail2ban/jail.local
[DEFAULT]
# “ignoreip” can be an IP address, a CIDR mask or a DNS host
ignoreip = 127.0.0.1/8
bantime = 600
maxretry = 3
# “backend” specifies the backend used to get files modification. Available
# options are “gamin”, “polling” and “auto”。
# yoh: For some reason Debian shipped python-gamin didn‘t work as expected
# This issue left ToDo, so polling is default backend for now
backend = auto
#
# Destination email address used solely for the interpolations in
# jail.{conf,local} configuration files.
destemail = root@localhost,fail2ban@blocklist.de
这有一些其他的你想检查的配置,尽管缺省配置已经相当不错了,所以,快速浏览这些,直到你读到Actions章节。
Actions
Actions 允许你对恶意行为作出反应,然而当我们想要它禁止和发邮件的时候,默认是禁用了 iptables。值得感谢的是,有一个预配置文件 action_wml,它恰恰是做这个的。
/etc/fail2ban/jail.local
# Choose default action. To change, just override value of ’action‘ with the
# interpolation to the chosen action shortcut (e.g. action_mw, action_mwl, etc) in jail.local
# globally (section [DEFAULT]) or per specific section
action = %(action_mwl)s
Jails 监控
为了让Fail2ban工作,需要了解要监控哪些东西。这些已在Jails部分的配置文件,并且这有一些预载入而未启用的例子。既然到目前为止,你仅仅在服务器上启用了SSH访问,那我们就只启用SSH和SSH-DDos 监控,然而你还是会想给安装在这台服务器上的公共访问服务增加新的监控。
/etc/fail2ban/jail.local
[ssh]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 6
[ssh-ddos]
enabled = true
port = ssh
filter = sshd-ddos
logpath = /var/log/auth.log
maxretry = 6
让变化生效
既然我们已经配置了Fail2ban,你将希望重新载入它,并且确保向iptables增加了合适的规则。
$ sudo service fail2ban restart