首頁>技術>

1、執行安全

嚴禁使用root賬戶執行Nginx(首字母大寫代表軟體,首字母小寫代表指令),應該使用nginx使用者或者nobody執行Nginx。在Nginx配置中使用user來指定Nginx worker程序執行使用者以及使用者組。

user nobody nobody;

2、專案配置檔案

配置檔案禁止放在Web目錄中,因為一旦攻擊者對Web目錄擁有讀寫許可權後即可更改nginx.conf。

client_boby_temp_path /etc/shadow;# optional but more fun :)location /wat {alias /etc;}

當重啟Nginx時,Nginx會執行。

# strace -e trace=chmod,chown -f nginxchown("/etc/shadow",33,4294967295)=0+++exited with 0 +++

任何檔案或資料夾一旦被攻擊者寫入到上述配置檔案中,它的所有者都會被更改,攻擊者將擁有相應的許可權。

3、日誌配置

在線上伺服器中一定要將Nginx訪問日誌啟用,日誌不允許存放在Web目錄下,並且設定日誌操作許可權為root。Nginx中使用access_log來開啟並指定Nginx的訪問日誌記錄路徑,使用error_log來記錄錯誤日誌。

access_log logs/access.log combined;error_log logs/error.logerror;

使用log_format配置命令來配置Nginx日誌格式。log_format有一個預設的無須設定的combined日誌格式,相當於apache的combined日誌格式。

log_format combined '$remote_addr - $remote_user [$time_local] ''"$request" $status $body_bytes_sent '' "$http_referer" "$http_user_agent" ';

Nginx日誌格式允許包含的變數註釋如表1所列。

表1 Nginx日誌變數含義

4、目錄和檔案安全

凡允許“上傳或寫入”目錄的許可權,執行許可權必須設定成禁止訪問。在Nginx中使用deny all指令來實現。

禁止對目錄訪問並返回403 Forbidden,可以使用下面的配置。

location /dirdeny {deny all;return 403;}location ~ ^/upload/.*.(php|php 5)${deny all;return 403;}

5、隱藏版本號

為了防止Nginx的版本號指紋暴露,線上伺服器要對Nginx的資訊進行隱藏,通常可以透過修改配置檔案來實現。進入Nginx配置檔案的目錄,如/etc/nginx.conf,在http標籤里加上server_tokens off。

http{……server_tokens off; # 隱藏Nginx的版本號……}

同樣也可以對伺服器資訊進行混淆。可以採用編譯原始碼的方法來改變返回的伺服器的版本資訊,下載好Nginx的原始碼,直接在原始碼中修改src/http/ngx_http_header_filter_module.c檔案中ngx_http_server_string的值。

staticchar ngx_http_server_string[]="Server:nginx" CRLF;staticchar ngx_http_server_full_string[]="Server: " NGINX_VER CRLF;

還要修改src/core/nginx.h檔案中NGINX_VERSION和NGINX_VER的值。

# define NGINX_VERSION "1.7.0"# define NGINX_VER "nginx/" NGINX_VERSION

編輯php-fpm配置檔案中的配置,如fastcgi.conf或fcgi.conf,修改其中的版本號資訊。

fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

可以透過以上方式將伺服器資訊修改為其他字串標識,以達到隱藏版本號、迷惑部分攻擊者的效果。

6、防止目錄遍歷

location / {autoindex on;autoindex_localtime on;}

7、Nginx檔案型別錯誤解析漏洞

該漏洞導致只要使用者擁有上傳圖片許可權的Nginx+PHP伺服器,就有被入侵的可能。其實此漏洞並不是Nginx的漏洞,而是PHP PATH_INFO的漏洞。例如,使用者上傳了一張照片,訪問地址為http://www.ptpress.com.cn/Upl...,而test.jpg檔案內的內容實際上是PHP程式碼時,透過http://www.ptpress.com.cn/Upl...。下面的修復方法務必先經過測試,以確保修改配置不會對應用帶來影響。

(1)修改php.ini,設定cgi.fix_pathinfo=0,然後重啟php-cgi。此修改會影響到使用PATH_INFO偽靜態的應用。

(2)在Nginx的配置檔案中新增如下內容,該配置會影響類似http://www.ptpress.com.cn/sof...(v2.0為目錄)的訪問。

if($fastcgi_script_name~..*/.*php){return 403;}

(3)在CGI模組中對PHP的檔案是否存在進行校驗,可以避免該漏洞的發生。

location ~ .php$ {if($request_filename~*(.*).php) {set $php_url$1;}if(!-e $php_url.php) {return 403;}fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root $fastcgi_script_name;include fastcgi_params;}

(4)對於儲存圖片的location{…},應只允許純靜態訪問,不允許PHP指令碼執行。

location ~ *^/upload/.*.(php|php 5)${deny all;}

8、IP訪問限制

Nginx與Apache一樣,也可以透過IP對訪問者進行限制。

deny 10.10.1.0/24; # 禁止該IP段進行訪問

allow 127.0.0.1; # 允許該IP進行訪問

deny all; # 禁止所有IP進行訪問

同時,可以使用GEO白名單方式來進行IP訪問限制,具體配置如下。配置ip.config檔案。

default 0;       //預設情況key=default,value=1127.0.0.1 1;10.0.0.0/8 1;//key=10.0.0.0, value=0 192.168.1.0/24 1;配置nginx.conf檔案。geo $remote_addr #ip_whitelist {include ip.conf;}location /console {proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_For;# 白名單配置if($ip_whitelist=1) {proxy_pass http://10.10.1.5:8080;break;}return 403;}

15
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 圖神經網路2021的頂級應用