很少見有人馬上為一臺新安裝的服務(wù)器做安全措施,然而我們生活所在的這個社會使得這件事情是必要的。不過為什么仍舊這么多人把它拖在最后?我也做過相同的事,這通??梢詺w結(jié)為我們想要馬上去折騰那些有趣的東西。希望這篇文章將向大家展示,確保服務(wù)器安全沒有你想得那樣難。在攻擊開始后,俯瞰你的“堡壘”,也相當享受。
這篇文章為 Ubuntu 12.04.2 LTS 而寫,你也可以在任何其他 Linux 分發(fā)版上做相同的事情。
如果服務(wù)器已經(jīng)有了一個公有IP,你會希望立即鎖定 root 訪問。事實上,你得鎖定整個ssh訪問,并確保只有你可以訪問。增加一個新用戶,把它加入admin組(在/etc/sudoers預(yù)配置以擁有sudo訪問權(quán)限)。
$ sudo addgroup admin
Adding group ‘admin’ (GID 1001)
Done.
$ 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
$ sudo usermod -a -G admin spenserj
你也將希望在你電腦上創(chuàng)建一個私有key,并且在服務(wù)器上禁用討厭的密碼驗證。
$ mkdir ~/.ssh
$ echo “ssh-rsa [your public key]” 》 ~/.ssh/authorized_keys
/etc/ssh/sshd_config
PermitRootLogin no
PermitEmptyPasswords no
PasswordAuthentication no
AllowUsers spenserj
重新加載SSH,使用修改生效,之后嘗試在一個新會話中登陸來確保所有事情正常工作。如果你不能登陸,你將仍然擁有你的原始會話來做修改。
$ sudo service ssh restart
ssh stop/waiting
ssh start/running, process 1599
更新服務(wù)器
既然你是訪問服務(wù)器的唯一用戶,你就不用擔心黑客鬼鬼祟祟進入,再次正常呼吸。當有一些針對你服務(wù)器的更新時,正是修補的機會,所以動手吧,就現(xiàn)在。
$ sudo apt-get update
。。。
Hit precise-updates/universe Translation-en_CA
Hit precise-updates/universe Translation-en
Hit precise-backports/main Translation-en
Hit precise-backports/multiverse Translation-en
Hit precise-backports/restricted Translation-en
Hit 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) 。。。
。。。
安裝防火墻
安裝現(xiàn)在正最流行的防火墻軟件?好,行動吧。那就配置一個防火墻。之后你總是可以增加另一個異常,幾分鐘額外的工作并不會折騰死你。Iptables在Ubuntu里預(yù)裝了,所以去設(shè)置一些規(guī)則吧。
$ sudo mkdir /etc/iptables
/etc/iptables/rules
*filter
?。篒NPUT DROP [0:0]
:FORWARD DROP [0:0]
?。篛UTPUT 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
通過 iptables-apply 命令為規(guī)則集生效。如果你丟失連接,修補你的規(guī)則,在繼續(xù)之前再試一下
$ 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.
創(chuàng)建文件 /etc/network/if-pre-up.d/iptables,然后寫入下面內(nèi)容。當你啟動服務(wù)器的時候,將自動載入你的iptables規(guī)則。
/etc/network/if-pre-up.d/iptables
#!/bin/sh
iptables-restore 《 /etc/iptables/rules
現(xiàn)在給它執(zhí)行權(quán)限,執(zhí)行文件,以確保它正常載入
$ sudo chmod +x /etc/network/if-pre-up.d/iptables
$ sudo /etc/network/if-pre-up.d/iptables
用 Fail2ban 處理潛在黑客