了解最新公司动态及行业资讯
很少有人会立即为新安装的服务器设置保护,但我们生活的世界使它成为必需品。那么为什么这么多人要等到最后才设置保护呢?我也做过同样的事情,通常归结为想要直接去做有趣的事情。希望本文向您展示了保护服务器比您想象的要容易得多,并且当攻击开始时从堡垒往下看会很有趣。
这篇文章是为 12.04.2 LTS 写的,但是你可以在其他 Linux 发行版上做类似的事情。
我从哪里开始?
如果服务器已经有公共 IP,您将需要立即锁定 root 访问权限。实际上,您需要完全锁定 SSH 访问权限,以便只有您可以进入。添加一个新用户并将这个新用户添加到一个管理组(在 /etc/ 中预先设置了 sudo 访问权限)。
## 添加用户组 admin(译者注)
$ sudo addgroup admin
Adding group 'admin' (GID 1001)
Done.
## 添加用户 spenserj(译者注)
$ sudo adduser spenserj
Adding user `spenserj' ...
Adding new group `spenserj' (1002) ...
Adding new user `spenserj' (1001) with group `spenserj' ...
Creating home directory `/home/spenserj' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for spenserj
Enter the new value, or press ENTER for the default
Full Name []: Spenser Jones
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y
## 追加用户 spenserj 到用户组 admin 中(译者注)
$ sudo usermod -a -G admin spenserj
您还需要在您的计算机上创建一个私钥并在服务器上禁用密码验证。
(公钥也要加到服务器.sshd文件中,详见文末参考链接第三步。译者注)
## 本地主机用户主目录下新建 .ssh 文件,并将公钥追加到 authorized_keys(译者注)
$ mkdir ~/.ssh
$ echo "ssh-rsa [your public key]" > ~/.ssh/authorized_keys
PermitRootLogin no
PermitEmptyPasswords no
PasswordAuthentication no
AllowUsers spenserj
重新加载 SSH 以应用更改,然后尝试登录新会话以确保一切正常。如果您无法登录,您仍然可以使用原始会话来解决问题。
## 重启 ssh(译者注)
$ sudo service ssh restart
ssh stop/waiting
ssh start/running, process 1599
更新服务器
现在只有你可以访问服务器服务器运维,不用担心黑客潜入,又可以正常呼吸了。您的服务器很可能有一些更新,所以现在开始运行它们。
## 服务器更新(译者注)
$ sudo apt-get update
...
Hit http://ca.archive.ubuntu.com precise-updates/universe Translation-en_CA
Hit http://ca.archive.ubuntu.com precise-updates/universe Translation-en
Hit http://ca.archive.ubuntu.com precise-backports/main Translation-en
Hit http://ca.archive.ubuntu.com precise-backports/multiverse Translation-en
Hit http://ca.archive.ubuntu.com precise-backports/restricted Translation-en
Hit http://ca.archive.ubuntu.com precise-backports/universe Translation-en
Fetched 3,285 kB in 5s (573 kB/s)
Reading package lists... Done
## 服务器升级(译者注)
$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages have been kept back:
linux-headers-generic-lts-quantal linux-image-generic-lts-quantal
The following packages will be upgraded:
accountsservice apport apt apt-transport-https apt-utils aptitude bash ...
73 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
Need to get 61.0 MB of archives.
After this operation, 151 kB of additional disk space will be used.
Do you want to continue [Y/n]? Y
...
Setting up libisc83 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up libdns81 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up libisccc80 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up libisccfg82 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up libbind9-80 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up liblwres80 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up bind9-host (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up dnsutils (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up iptables (1.4.12-1ubuntu5) ...
...
安装防火墙
现在正在运行最新的软件?这很好。继续构建防火墙,只允许你现在需要的东西。您可以在之后添加一个例外,几分钟的额外工作不会让您崩溃。它是预装在的,所以先给它设置一些规则。
$ sudo mkdir /etc/iptables
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT DROP [0:0]
# Accept any related or established connections
-I INPUT 1 -m state --state RELATED,ESTABLISHED -j ACCEPT
-I OUTPUT 1 -m state --state RELATED,ESTABLISHED -j ACCEPT
# Allow all traffic on the loopback interface
-A INPUT -i lo -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
# Allow outbound DHCP request - Some hosts (Linode) automatically assign the primary IP
#-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
通过 -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//if-pre-up.d/。这将在启动服务器时自动加载规则。
#!/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
想当黑客的人
在安全性方面我最喜欢的工具之一,因为它会监控您的日志文件并暂时禁止滥用资源的用户,例如强制 SSH 连接或破坏您的网络服务器。
## 安装 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
...
安装了默认配置(/etc//jail.conf),但我们想在 /etc//jail.local 中进行更改,所以将其复制到那里。
## 拷贝配置(译者注)
sudo cp /etc/fail2ban/jail.{conf,local}
配置
将线路更改为您的 IP 并决定阻止多长时间(默认为 10 分钟)。您还需要设置一个,我通常将其设置为我的电子邮件地址并输入@.de。 .de 是一个跟踪并自动报告黑客试图滥用其连接的 IP 的系统。
[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
虽然默认设置就足够了,但还有一些其他设置需要检查,因此请快速查看该部分的文件。
允许您对恶意活动做出反应,但默认为禁止,我们希望它禁止并发送电子邮件。幸运的是服务器运维,有一个预配置的可以完成这项工作。
# 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 部分中配置的,有许多实例被预加载和禁用。由于到目前为止您只启用了 SSH 访问,我们将只启用 SSH 和 SSH-DDoS 监狱,但您需要为安装在该服务器上的每个可公开访问的服务添加一个新的监狱。
[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
应用更改
现在我们已经配置好了,您将重新加载它并确保它向其中添加了适当的规则。
$ sudo service fail2ban restart
* Restarting authentication failure monitor fail2ban
...done.
$ sudo iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
fail2ban-ssh-ddos tcp -- anywhere anywhere multiport dports ssh
fail2ban-ssh tcp -- anywhere anywhere multiport dports ssh
...
Chain fail2ban-ssh (1 references)
target prot opt source destination
RETURN all -- anywhere anywhere
Chain fail2ban-ssh-ddos (1 references)
target prot opt source destination
RETURN all -- anywhere anywhere
您可以随时使用 sudo -L 列出您的规则,然后是当前禁止 IP 的列表。目前正在处理两个恶意IP:
DROP all -- 204.50.33.22 anywhere
DROP all -- 195.128.126.114 anywhere
控制世界
现在您的服务器已锁定并准备就绪,这并不是您安全之旅的终点。密切关注更新(首先在非生产环境中进行测试),始终关闭不需要的端口,定期检查日志,并了解服务器内外发生的情况。
跟踪
有一些很好的评论,如果您对不同的视角和更高的安全性感兴趣,我建议您阅读它们。本文旨在作为初学者的安全指南,结束本文并不意味着您的服务器是无懈可击的。使用本指南快速锁定新服务器并根据您的独特情况在其上进行构建。您可能想探索 IPV6 安全性、更改 SSH 端口(通过隐藏)、内核安全性(和 )、跟踪系统更改并进行全面审核(如果您的服务器曾经不安全,或者已经在线很长时间)。服务器有数百个入口点,您安装的每个应用程序都可能引入另一个漏洞,但使用正确的工具,您可以放心上床。
参考链接:阮一峰Linux服务器的初步配置过程