2015年8月26日 星期三

[Linux] 駭人之心不可有,防人之心不可無,Ubuntu簡單防護

enter image description here
上禮拜拿到Softlayer的時候,因為是實體機,有朋友告知安全性的問題,都已經將防火牆架設列在todo list裡面,但是因為大家平常都各自有事情要忙,想說沒有重要資料就先擺著.沒想到今天一早就接到ibm大大的聯繫說有不明人士嘗試進入主機…orz 沒想到平常看人家笑話今天自己變成笑話,果然出來跑的總是要還,在網路上的遲早會被駭.
事情是這樣的:

一早IBM大大說接到這樣的信:
SoftLayer Security has received the following HACKING / MALICIOUS ACTIVITY complaint in reference to an IP hosted on your server. A copy of the complaint is listed below or attached to this ticket for your review. Please disable or remove this activity immediately as it is direct abuse of the network services and a violation of your TOS and AUP. Failure to resolve this issue in an expeditious manner could lead to service interruption for this server. Please update this ticket with resolution to this issue. We thank you in advance for your quick action and cooperation.
看了log發現:
Aug 25 05:45:56 server sshd[26941]: Invalid user zhangyan from xxx.xxx.xxx.xxx
Aug 25 05:45:57 server sshd[26941]: pam_unix(sshd:auth): authentication fai=
lure; logname=3D uid=3D0 euid=3D0 tty=3Dssh ruser=3D rhost=3Dxxx.xxx.xxx.xxx-=static.reverse.softlayer.com=20
靠北我們的server被當跳板,登入後台差點昏倒
enter image description here
整個流量大爆炸啊@@! 一天就破了百G
跟社群夥伴後確認不是自己人搞的就立馬展開除駭程序(小弟我資安能力0分,只能用很簡單的方法orz)
  1. 檢查不正常的程序
    先用ps aux看看有沒有可疑的使用者或程式
    enter image description here
    果然抓出一堆該死的guest,是當初開帳號設的orz
    一個一個把pid kill掉後先把帳號刪除.
  2. 找殘黨
    看網路說駭客通常會放一些hacker的script到/tmp資料夾,一找果然有…內容還滿不妙的.
    enter image description here
  3. 改帳號密碼,關閉不必要帳號
    這步比較簡單,就root帳號和要求使用者帳號密碼重設
  4. 不回應ping
    降低hacker在網路上亂槍打鳥找server的可能性
    sudo vim /etc/sysctl.conf
    在最後一行加上net.ipv4.icmp_echo_ignore_all=1
    最後要sysctl -p 來應用設定.
  5. 安裝Failtoban
    這個服務可以讓試圖進入主機太多次失敗的ip直接ban掉
    sudo apt-get install fail2ban
    安裝後就行了.
  6. SSH改port
    一般登入都是用22 port,換個port抵擋一些簡單的測試
    vim /etc/ssh/sshd_config
    把第一行Port 改成自己喜歡的數字,但是這招擋不了掃port,認真要試還是可以.修改後要記得sudo restart ssh來重設服務.
  7. 關閉密碼登入
    可以在上述檔案中加入PasswordAuthentication no來設定,一定要透過交換ssh key才能登入主機,安全性相對高很多.
  8. 關閉root登入
    在上述檔案中加入PermitRootLogin no禁止root登入,並且透過權限控管僅讓部分成員擁有sudo權限.
  9. 設定防火牆
    透過防火牆把一些不必要的port關掉.減少入侵管道,用的是uwf這個服務.
    apt-get install ufw

    安裝後可以透過
    sudo ufw allow 2222

    這樣的方式把允許的port開放出來,設完之後記得要sudo ufw enable
    可以使用ufw status 觀察port的使用狀況.

    如果有使用VPN或特定IP,可以使用
    ufw allow from 172.16.0.94 to any port 3306 來限制登入的IP位置
  10. 設定VPN
    IBM有提供VPN的設定,設定過程很複雜,有興趣可以參考
    http://knowledgelayer.softlayer.com/procedure/ssl-vpn-mac-os-x-1010
    http://knowledgelayer.softlayer.com/procedure/ssl-vpn-connections
  11. 利用Docker隔離服務和環境
    雖然docker也有安全性的問題,但是多少也增加了直接入侵的難度…吧?

    很難說做到這樣還會不會被攻擊,畢竟服務還是要開,只要有洞就有辦法鑽,只是多少降低hacker侵入的難度….每天監控網路流量報表才是真的.

參考資料:

最後還是要感謝IBM和Softlayer的贊助:
enter image description here
圖片來源:Softlayer


沒有留言:

張貼留言