接著你有一台全新的 server 了,這邊先教你怎麼先設定這台 Server 的安全性及相關權限

一開始由 SSH 登入去設定自己,登入先設定自己的使用者權限

使用者權限設定
你可能會大多數的時間都用 root 登入,但基於安全性考量,root 直接登入是有風險的,這代表別人也可以從自己的 client 用 SSH 使用 root 帳號登入,如果這時候 root 的密碼又超級簡單易懂又好破解,就很容易被控制啦!筆者是會再建一個 user 然後登入時改用這個 user

root@localhost:~# adduser admin

輸入密碼後,再次驗證密碼就完成,接下來輸入使用者相關身份,可以看到 /etc/passwd 下的最後一行就是你建立的使用者

root@localhost:~# cat /etc/passwd

安全性設定
接下來,讓 root 不要直接從 SSH 登入,那就是去改寫設定檔的定義

root@localhost:~# vi /etc/ssh/sshd_config

找到這行後,將 yes 改成 no

PermitLoging = no

# 讓可以登入的身份,只予許某個人
AllowUser admin

# 一般登入時,都會用 22 這個預設的 port 在這裡改掉換成別的 port
Port = 4022

改完後,重啟 ssh 的服務

root@localhost:~# sudo service ssh restart

然後重新登入看看是不是被擋掉了

root@localhost:~# exit
admin@localhost:~# ssh root@your.domain

登不進去的話就正常了,接著改用自己剛建立的使用者及對應的 port 登入

root@your.computer:~# ssh admin@your.domain -p 4022

出現詢問密碼就成功了!

防火牆
上述的例子,其實 22 port 還是開通的,要怎麼讓不相關的 port 關掉 Listen 畢免一些掃 port 的軟體或臭蟲抓到漏洞呢?這時候就可以用 iptables 的指令啦!

Linux 的好處是它已經內建相關防火牆的設定,只是都是打開的狀況,這時候就照著指令來一步一步完成吧!首先先來查看一下剛建的 iptables 目前狀態,使用 iptables -L 就可以查看。

root@localhost:~# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

看到 policy 是 ACCEPT 就得知目前防火牆的規則是都打開的喔!這時候就要來把它關掉,假設你也是使用 wordpress 的 Web Service 可以參考這份,或是上網 google 一下,廣大的資訊海馬上為你提供解答,若是 Web Service 我們首先就是把除了 80, 443 的 port 都關掉。另外也要保留上面 SSH 登入的 port 4022

iptables -F
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -j ACCEPT

iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW --dport 4022 -j ACCEPT
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
iptables -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
iptables -A INPUT -j REJECT
iptables -A FORWARD -j REJECT

或者你也可以編寫一份 firewall 的腳本,在系統啟動時自動讀入,或者 reload iptables 設定時,就直接吃這份腳本,筆者較偏好後者,畢免每次指令都要輸入半天的哩。

root@localhost:~# vi /etc/iptables.firewall.rules

基本的編寫內容就是上述的指令去掉 iptables 就行啦

*filter

#  Accept all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#  Allow all outbound traffic - you can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

#  Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

# Prevent DoS Attack
-A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT

#  Allow SSH connections
#  The -dport number should be the same port number you set in sshd_config
-A INPUT -p tcp -m state --state NEW --dport 4022 -j ACCEPT

#  Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

#  Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

#  Reject all other inbound - default deny unless explicitly allowed policy
-A INPUT -j REJECT
-A FORWARD -j REJECT

COMMIT

寫完後,將 iptables 重新載入這份腳本

root@localhost:~# iptables-restore < /etc/iptables.firewall.rules

另外一個安全設定是 Linode 在他的 Document 中有寫一個軟體 Fail2Ban 也可以試著裝看看

root@localhost:~# sudo apt-get install fail2ban

編寫 fail2ban 的阻擋設定

root@localhost:~# cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
root@localhost:~# vi /etc/fail2ban/jail.local

設定 ssh 的 port 為剛剛設定的 4022, 另外也把 ssh-ddos 打開來

[ssh]

enabled  = true
port     = 2022
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 6

[ssh-ddos]
enabled  = true

重新打開服務

root@localhost:~# sudo service fail2ban restart

這時候用 iptables -L 應該看見最下面幾行有 fail2ban 的阻擋

Chain fail2ban-ssh (0 references)
target     prot opt source               destination         

Chain fail2ban-ssh-ddos (0 references)
target     prot opt source               destination

其它的安全性設定可以參考 Linode 上的建議或是到鳥哥的私房菜來學習喲!

[advps-slideshow optset=”3″]

cloud

Write A Comment