13、web 中间件加固-Nginx 加固

1.隐藏版本信息

避免被针对版本直接使用漏洞

修改 nginx.conf 文件

在 http 模块中添加信息:server_tokens off;

2.限制目录权限

某些目录为运维页面,不要公开访问

编辑 nginx.conf 文件

在 server 标签内添加内容:

location ~ /attachments/.*\.(php|php5)?$ {
deny all;
}
location ~ /(attachments|upload)/.*\.(php|php5)?$ {
dent all;
}

3.限制 IP 地址访问

敏感目录使用白名单访问

修改 nginx.conf 文件

在 server 中添加

location /upload {
allow 192.168.1.0/24;
allow 10.1.1.1/32
deny all;
}

4.禁止浏览目录内容

防止通过浏览器直接查看目录内容

编辑 nginx.conf

在 http 模块下添加:autoindex off;

5.错误页面重定向

编辑 nginx.conf

在 server 模块下加入

error_page 404 /404.html;
location = /404.html {
root /usr/local/nginx/html;
}

6.日志配置

修改日志格式,便于审计

编辑 nginx.conf

在 http 模块内启用标签 main 的 log_format 格式

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

在 server 标签内调用:access_log logs/host.access.log main

7.限制 http 请求方法

只允许常用的 get 和 post 方法,减少漏洞

编辑 httpd.conf

在 server 模块中加入判断信息

除了 GET 和 POST 方法,页面都显示 403
if ($request_method !~* GET|POST) {
return 403;
}

8.限制并发和速度(需要经过测试,根据实际信息进行修改)

编辑 nginx.conf

在 http 模块中声明:limit_req_zone $binary_remote_addr zone=allips:10m rate=20r/s;

添加在 server 的location 中

location / {
limit_req zone=allips burst=5 nodelay;;
limit_rate 20k;
}
location /download {
limit_rate_after 10m;
limit_rate 128k;
}

9.防盗链

防止通过其他途径使用本网站资源

location ~* \.(jpg|jpeg|png|gif|bmp|swf|rar|zip|doc|xls|pdf|gz|bz2|mp3|mp4|flv)$
valid_referers none blocked 192.168.0.1 *.baidu.com; #允许的
if ($invalid_referer) {
rewrite ^/ https://site.com/403.jpg;
#return 403;
}
root /usr/share/nginx/img;
}

10.nginx 降权

防止高权限运行 nginx 进程

编辑 nginx.conf

在 http 模块下修改:user nobody;

11.解析漏洞

防止非法后缀被服务器识别

1)将 php.ini 文件中的 cgi.fix_pathinfo 的值设为 0

2)将 /etc/php5/fpm/pool.d/www.conf 中 security.limit_ectensions 后面的值设为 .php

 

posted @ 2024-04-13 16:02  落落的学习  阅读(78)  评论(0编辑  收藏  举报