服務器安全基礎指南

首先,VPS 服務器被黑怎麼辦?php

第一, 10秒以內登陸你的 VPS 提供商帳號,關機。
第二,切斷被黑機器的外網。
第三,在相同的 VPS 提供商重開一臺機器,內網登陸,備份重要的信息
第四,遷移數據
第五,報廢以前被黑的 VPS nginx

最近正好接觸到一次服務器因爲開啓redis但沒注意安全設置從而被提權的入侵的事件,如下是本身作的一些小總結。web

此文章適用於系統版本爲:Ubuntu 14.04 64redis

Nginx 配置:

升級到穩定版shell

sudo apt-get update && sudo apt-get upgrade

sudo apt-get upgrade nginx

Nginx 目前在 Ubuntu 14.04 中穩定版本號是 1.4.6 ubuntu

能夠經過在 VPS 上執行如下 curl 命令來查看信息:安全

curl -I http://localhost

返回大概是:服務器

HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)

這裏直接顯示了 Nginx 的版本號,能夠經過配置隱藏:框架

sudo vi /etc/nginx/nginx.conf

http 部分添加:less

http {
        ##
        # Basic Settings
        ##
        server_tokens off;
}

重啓 Nginx

sudo service nginx reload

這樣再次執行 curl 就能夠看到隱藏了 Nginx 的版本號了:

curl -I http://localhost
HTTP/1.1 200 OK
Server: nginx
...
X-Powered-By: PHP/5.5.9-1ubuntu4.14
...

注意到上面 X-Powered-By: PHP/5.5.9-1ubuntu4.14 這一行暴露了 PHP 的版本號,因此咱們來配置 php.ini 來隱藏之,即在 php.ini 文件中設置:

expose_php = Off

若是你的服務器的 php.ini 默認就隱藏了版本,那麼恭喜你。

SSH 配置:

配置 ssh ,好比關掉密碼認證式登陸等:

sudo vi /etc/ssh/sshd_config

打開以後配置:

# 不容許空密碼
PermitEmptyPasswords no
# 關閉密碼驗證登陸,前提是你已經使用了 ssh 密鑰驗證方式登陸
PasswordAuthentication no
# 若是你在服務器上手動添加了用戶並將用戶分配到 root 用戶組,能夠考慮禁止root用戶登陸
PermitRootLogin no

重啓 ssh

sudo service ssh restart

iptables 配置

iptables 提供了一種可靈活配置安全策略防火牆框架,具體的原理能夠看這篇文章:http://seanlook.com/2014/02/23/iptables-understand/

博主寫文章很認真,推薦一下

開始以前,能夠經過如下命令全新配置 iptables

iptables -F

注意上述命令會將你以前本身設置的過濾規則清空。

你能夠查看 iptables 的相關資料,一條一條添加安全規則,也能夠這樣:

sudo vi /etc/iptables.up.rules

添加內容:

*filter

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

# Allows all outbound traffic
# You could modify this to only allow certain traffic
 -A OUTPUT -j ACCEPT

# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
 -A INPUT -p tcp --dport 443 -j ACCEPT
 -A INPUT -p tcp --dport 80 -j ACCEPT
# Allows SSH connections for script kiddies
# THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE
 -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

# Now you should read up on iptables rules and consider whether ssh access
# for everyone is really desired. Most likely you will only allow access from certain IPs.

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

# log iptables denied calls (access via 'dmesg' command)
 -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

其中很是值得注意的是這三條:

# 443 一般是 ssl 的端口,若是 VPS 是做爲一臺web應用的服務器而且啓用 https,你須要這個
 -A INPUT -p tcp --dport 443 -j ACCEPT
 # 80 端口就很少說了
 -A INPUT -p tcp --dport 80 -j ACCEPT
 # 這是 ssh 默認的 22 端口,開放給本身登陸使用
 -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

注意這裏的 22 端口,你能夠修改成其餘的端口,而且關閉 22 端口。更有一種解決方案是:將生產機器 ssh 端口徹底關閉,用一臺同一子網下的跳板機,經過來登陸目標生產機器。

其餘的配置的話,你須要根據你的業務來具體配置相對應的安全策略。

寫好這些配置項以後,告訴 iptables 你的配置在哪:

sudo iptables-restore < /etc/iptables.up.rules

建立 shell 腳原本開機自啓動:

sudo vi /etc/network/if-up.d/iptables

添加內容:

#!/bin/sh
iptables-restore /etc/iptables.up.rules

給以可執行的權限:

chmod +x /etc/network/if-up.d/iptables

到這裏,iptables 的基礎設置就OK了。

配置 Fail2Ban

Fail2Ban 的工做原理是:經過監控系統的日誌文件,並根據檢測到的任何可疑的行爲自動觸發不一樣的防護動做,如將產生可疑行爲的目標 ip 鎖定等。

安裝:

sudo apt-get install fail2ban

打開配置文件,進行配置:

sudo vi /etc/fail2ban/jail.conf

寫入:

[DEFAULT]
ignoreip = 127.0.0.1/8
bantime  = 3600
maxretry = 3
destemail = email@yourdomain.com
action = %(action_mw)s

[ssh]

enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 3

到這裏,就是我我的從上次被黑當中總結的一些基本的服務器安全,而至於羣裏 @little 大神說的跳板機的方案,容我再消化消化再嘗試部署起來。

總結

1.服務一些基本的安全配置得跟上。
2.關注安全圈的新聞和動態,及時升級軟件和修復相關安全漏洞。
3.服務器上的服務,用哪一個就看哪一個,不用的堅定關掉。
4.服務器一旦被入侵,不急。關機,斷網,備份,遷移,捨棄。

記住:The less information you provide, the more secure you are .

相關文章
相關標籤/搜索