公司各業務網站大多用到Nginx,花了點時間整理了一下Nginx服務器安全加固的各類tips。
默認配置文件和Nginx端口
/usr/local/nginx/conf/-Nginx配置文件目錄,/usr/local/nginx/conf/nginx.conf是主配置文件
/usr/local/nginx/html/-默認網站文件位置
/usr/local/nginx/logs/-默認日志文件位置
Nginx HTTP默認端口:TCP 80
Nginx HTTPS默認端口:TCP 443
可以使用以下命令來測試Nginx配置文件准確性。
/usr/local/nginx/sbin/nginx -t
將會輸出:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is OK
configuration file /usr/local/nginx/conf/nginx.conf test is successful
執行以下命令來重新加載配置文件:
/usr/local/nginx/sbin/nginx -s reload
執行以下命令來停止服務器:
/usr/local/nginx/sbin/nginx -s stop
通過分區掛在允許最少特權
服務器上的網頁/html/php文件單獨分區。例如,新建一個分區/dev/sda5(第一邏輯分區),並且掛載在/nginx。確保/nginx是以noexec,nodev and nosetuid的權限掛載。
例:
LABAL=/nginx/nginx ext3 defaults,nosuid,noexec,nodev 1 2
注意:需要使用fdisk和mkfs.ext3命令創建一個新分區。
配置/etc/sysctl.conf強化Linux安全
可以通過編輯/etc/sysctl.conf來控制和配置Linux內核、網絡設置。
#Avoid a smurf attack
net.ipv4.icmp_echo_ignore_broadcasts=1
#Turn on protection for bad icmp error messages
net.ipv4.icmp_ignore_bogus_error_responses=1
#Turn on syncookies for SYN flood attack protection
net.ipv4.tcp_syncookies=1
#Turn on and log spoofed,source routed,and redirect packets
net.ipv4.conf.all.log_martians=1
net.ipv4.conf.default.log_martians=1
#No source routed packets here
net.ipv4.conf.all.accept_source_route=0
net.ipv4.conf.default.accept_source_route=0
#Make sure no one can alter the routing tables
net.ipv4.conf.all.accept_redirects=0
net.ipv4.conf.default.accept_redirects=0
net.ipv4.conf.all.secure_redirects=0
net.ipv4.conf.default.secure_redirects=0
#Don't act as a router
net.ipv4.ip_forward=0
net.ipv4.conf.send_redirects=0
net.ipv4.conf.default.send_redirects=0
#Turn on execshild
kernel.exec-shield=1
kernel.randomize_va_space=1
刪除所有不需要的Nginx模塊
需要直接通過編譯Nginx源代碼使模塊數量最少化。通過限制只允許web服務器訪問模塊把風險降到最低。
可以只配置安裝nginx所需要的模塊。例如,禁用SSL和autoindex模塊可以執行以下命令:
./configure -without-http_autoindex_module -without-http_ssl_module
make
make install
通過以下命令來查看當編輯nginx服務器時哪個模塊能開啟或關閉:
./configure -help | less
禁用你用不到的nginx模塊。
使用mod_security(只適合后端Apache服務器)
mod_security為Apache提供一個應用程序集的防火牆,為后續Apache Web服務器安裝mod_security,這會阻止很多注入式攻擊。
安裝SELinux策略以強化Nginx Web服務器
默認的SELinux不會保護Nginx Web服務器,但是你可以安裝和編譯保護軟件。
1.安裝編譯SELnux所需要環境支持
yum -y install selinux-policy-targeted selinux-policy-devel
2.下載SELinux策略以強化Nginx Web服務器。
cd /opt
wget http://downloads.sourseforge.net/project/selinuxnginx/se-ngix_1_0_10.tar.gz?use_mirror=nchc’
3.解壓文件
tar -zxvf se-ngix_1_0_10.tar.gz
4.編譯文件
cd se-ngix_1_0_10/nginx
make
rm tmp/nginx.mod.fc tmp/nginx.mod
5.安裝生成的nginx.pp SELinux模塊:
/usr/sbin/semodule -i nginx.pp
控制緩沖區溢出攻擊
編輯nginx.conf,為所有客戶端設置緩沖區的大小限制。
vi /usr/local/nginx/conf/nginx.conf
編輯和設置所有客戶端緩沖區的大小限制如下:
##Start: Size Limits & Buffer Overflows ## //server上下文
client_body_buffer_size 1K;
client_header_buffer_size 1k;
client_max_body_size 1k;
large_client_header_buffers 2 1k;
##END:Size Limits & Buffer Overflows ##
控制並發連接
可以使用NginxHttpLimitZone模塊來限制指定的會話或者一個IP地址的特殊情況下的並發連接。編輯nginx.conf:
### Directive describes the zone,in which the session states are stored i.e. stored in slimits. ###
### 1m can handle 32000 sessions with 32 bytes/session,set to 5m x 32000 session###
limit_zone slimits $binary_remote_addr 5m;
### Control maximum number of simultaneous connections for one session i.e.###
### restricts the amount of connections from a single ip address ###
limit_conn slimits 5;
上面標示線至每個遠程IP地址的客戶端同時打開連接不能超過5個。
只允許我們的域名的訪問
如果機器人只是隨機掃描服務器的所有域名,可以允許配置的虛擬域或反向代理請求,以拒絕這個請求。
##Only requests to our Host are allowed i.e. xxx.in,images.xxx.in and www.xxx.in
if($host !~ ^(xxx.in|www.xxx.in|images.xxx.in)$){
retrun 444;
}
##
限制可用的請求方法
GET和POST是最常用的方法。Web服務器的方法被定義在RFC 2616。如果Web服務器不要求啟用所有可用的方法,它們應該被禁用。下面的指令將過濾只允許GET,HEAD和POST方法:
##Only allow these request methods##
if($request_method !~ ^(GET|HEAD|POST)$){
retrun 444;
}
##Do not accept DELETE,SEARCH and other methods##
如何拒絕一些User-Agents?
##Block download agents ##
if ($http_user_agent ~* LWP::Simple|BBBike|wget){
retrun 403;
}
##
組織Soso和有道的機器人:
##Block some robots ##
if ($http_user_agent ~* Sosospider|Yodaobot){
retrun 403;
}
目錄限制
可以對指定的目錄設置訪問權限。所有的網站目錄應該一一配置,只允許必須的目錄訪問權限。
可以通過IP地址來限制訪問目錄/admin/:
location /docs/ {
##block one workstation
deny 192.168.1.1;
## allow anyone in 192.168.1.0/24
allow 192.168.1.0/24;
##drop rest of the world
deny all;
}
通過密碼保護目錄
首先創建密碼文件並增加“user”用戶:
mkdir /usr/local/nginx/conf/.htpasswd/
htpasswd -c /usr/local/nginx/conf/.htpasswd/passwd user
編輯nginx.conf,加入需要保護的目錄:
###Password Protect /personal-images/ and /delta/ directories###
location ~ /(personal-images/./delta/.){
auth_basic "Restricted";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd/passwd;
}
密碼文件生成后,也可以用以下的命令來增加允許訪問的用戶:
htpasswd -s /usr/local/nginx/conf/.htpasswd/passwd userName
Nginx SSL配置
HTTP是一個純文本協議,它是開放的被動監測。應使用SSL來加密你的用戶內容。
創建SSL證書
執行以下命令:
cd /usr/local/nginx/conf
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
編輯nginx.conf並更新:
server{
server_name example.com;
listen 443;
ssl on;
ssl_certificate /user/local/nginx/conf/server.crt;
ssl_certificate_key /usr/local/nginx/conf/server.key;
access_log /usr/local/nginx/logs/ssl.access.log;
error_log /usr/local/nginx/logs/ssl.error.log;
}
重啟nginx:
/usr/local/nginx/sbin/nginx -s reload
在防火牆級限制每個IP的連接數
網絡服務器必須監視連接和每秒連接限制。PF和Iptables都能夠在進入nginx服務器之前阻止最終用戶的訪問。