隐藏Nginx软件版本号信息
Nginx 每个版本都有特定的漏洞,通过隐藏版本可以增加被入侵的难度
只需要在Nginx配置文件nginx.conf 的http 标签段内加入“server_tokens off; ”
当然更有效的方法就是更改源码隐藏的Nginx软件名及版本号
修改的第一个文件为/nginx-1.10.3/src/core/nginx.h 如下
[root@2kb.com]# sed -n '13,17p' nginx.h #define NGINX_VERSION "2.10.10" #此处修改版本号 #define NGINX_VER "OWS/" NGINX_VERSION #一起修改
修改第二个文件是nginx-1.13.3/src/http/ngx_http_header_filter_module.c
通过sed替换:
sed -i 's#Server: nginx#Server: OWS#g' ngx_http_header_filter_module.c
修改第三个配置文件是:nginx-1.13.3/src/http/ngx_http_special_response.c
[root@2kb.com]# sed -n '21,31p' ngx_http_special_response.c static u_char ngx_http_error_full_tail[] = "<hr><center>" OWS "</center>" CRLF #<==将此处修改为“OWS” "</body>" CRLF "</html>" CRLF;
第二步修改完后需要重新编译
--prefix=/application/nginx-1.3.3 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
最后可以通过在命令行curl 命令查看效果
更改Nginx服务的默认用户
1.useradd nginx -s /sbin/nologin -M #首先这里先创建一个nginx用户 2.修改配置文件nginx.conf user nginx nginx;
Nginx日志修改
参考配置操作
(1)编辑 nginx.conf 配置文件
将 error_log 前的“#”去掉,记录错误日志
将 access_log 前的“#”去掉,记录访问日志
(2)设置 access_log,修改配置文件如下:
log_format formatname '$remote_addr - $remote_user [$time_local] ' ' "$request" $status $body_bytes_sent "$http_referer" ' ' "$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log formantname; #formatname 设置配置文件格式的名称
注意
查看 nginx.conf 配置文件中,error_log、access_log 前的“#”是否去掉
限制 IP 访问
对网站或敏感目录的访问 IP 进行限制
参考配置操作
(1)修改配置文件
#vi /usr/local/nginx/conf/nginx.conf
具体设置如下:
location / { deny 192.168.1.1; #拒绝 IP allow 192.168.1.0/24; #允许 IP allow 10.1.1.0/16; #允许 IP deny all; #拒绝其他所有 IP }
重新启动 nginx 服务
修改配置文件
#vi /usr/local/nginx/conf/nginx.conf
具体设置如下:
client_body_timeout 10; #设置客户端请求主体读取超时时间 client_header_timeout 10; #设置客户端请求头读取超时时间 keepalive_timeout 5 5; #第一个参数指定客户端连接保持活动的超时时间,第二个参数是可选的,它指定了消息头保持活动的有效时间 send_timeout 10; #指定响应客户端的超时时间
修改配置文件
#vi /usr/local/nginx/conf/nginx.conf 具体设置如下: location ~* ^.+.(gif|jpg|png|swf|flv|rar|zip)$ { valid_referers none blocked server_names *.2kb.com http://localhost 2kb.com; if ($invalid_referer) { rewrite ^/ [img]http://www.2kb.com/images/default/logo.gif[/img]; # return 403; } }
修改 src/http/ngx_http_special_response.c,自定义错误信息
## messages with just a carriage return. static char ngx_http_error_400_page[] = CRLF; static char ngx_http_error_404_page[] = CRLF; static char ngx_http_error_413_page[] = CRLF; static char ngx_http_error_502_page[] = CRLF; static char ngx_http_error_504_page[] = CRLF;
防止缓存溢出
[root@2kb.com]# vim /usr/local/nginx/conf/nginx.conf http { client_body_buffer_size 8K; #主体缓存空间 client_max_body_size 16k; #主体最大缓存 client_header_buffer_size 1k; #默认请求包头信息的缓存 large_client_header_buffers 4 4k; #大请求包头信息的缓存个数,每个 缓存的容量 ...}
[root@2kb.com]# vim /usr/local/nginx/conf/nginx.conf server { ... if ($request_method !~ ^(GET|POST)$ ) {return 404;} #访问类型不是 GET或乾POST 时,返回 404错误 }
SSRF(服务端请求伪造)漏洞常出现在反向代理的配置中,反向代理如下:
proxy_pass http ://www.2kb.com
如果攻击者可以操控IP, 将其修改成内网IP地址即可造成SSRF漏洞。
设置alias目录的别名
location /2kbfile { alias /2kbcom/; }
使用HTTPS认证
HTTP认证默认使用crypt,哈希不安全,此处省略500字
减少使用"if"语句
"if"是重写模块的一部分,尽量不要使用。
“if”声明重写模块评估指令强制性的部分。也就是说,Nginx的配置一般来说是声明式的。
在某些情况下,由于用户的需求,他们试图在一些非重写指令内使用“if”,这导致我们现在遇到的情况。大多数情况下都能正常工作。
所以如果可以不用"if"就完全不用
对Nginx后台限制IP访问
使用域名限制访问
如果机器人只是随机扫描服务器的所有域名,那拒绝这个请求。你必须允许配置的虚拟域或反向代理请求。你不必使用IP地址来拒绝。
if ($host !~ ^(2kb.com|www.2kb.com|images.2kb.com)$ ) { return 404; #尽量返回404,让攻击者更难猜测错误 }
限制User-Agents访问
通过User-Agents,限制扫描器,机器人,垃圾蜘蛛,垃圾邮件发送者等
## Block download agents ## if ($http_user_agent ~* LWP::Simple|BBBike|wget) { return 403; }
如果业务在国内,国外流量带来不了客户,可以参考www.2kb.com的配置,阻止国外的蜘蛛Scrapy,MJ12bot访问
if ($http_user_agent ~* Scrapy|MJ12bot) { return 403; }
Nginx目录限制
可以限制访问目录/admin/:
location /admin/ { deny all; }
Nginx与PHP的安全配置
如下编辑/etc/php.ini文件:
# Disallow dangerous functions disable_functions = phpinfo, system, mail, exec ## Try to limit resources ## # Maximum execution time of each script, in seconds max_execution_time = 30 # Maximum amount of time each script may spend parsing request data max_input_time = 60 # Maximum amount of memory a script may consume (8MB) memory_limit = 8M # Maximum size of POST data that PHP will accept. post_max_size = 8M # Whether to allow HTTP file uploads. file_uploads = Off # Maximum allowed size for uploaded files. upload_max_filesize = 2M # Do not expose PHP error messages to external users display_errors = Off # Turn on safe mode safe_mode = On # Only allow access to executables in isolated directory safe_mode_exec_dir = php-required-executables-path # Limit external access to PHP environment safe_mode_allowed_env_vars = PHP_ # Restrict PHP information leakage expose_php = Off # Log all errors log_errors = On # Do not register globals for input data register_globals = Off # Minimize allowable PHP post size post_max_size = 1K # Ensure PHP redirects appropriately cgi.force_redirect = 0 # Disallow uploading unless necessary # Enable SQL safe mode sql.safe_mode = On # Avoid Opening remote files allow_url_fopen = Off
此处省略说明...
限制相同IP的并发数
同一用户正常操作下,很少会产生大量的并发,相同用户产生大量并发,基本上是异常
使用Nginx HttpLimitZone模块来限制指定的会话或者一个IP地址的特殊情况下的并发连接。
配置nginx.conf:
### Directive describes the zone, in which the session states are stored i.e. store 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 6;
先到这里,后台继续更新,有问题可以留言帮你解决。
2KB项目(www.2kb.com,源码交易平台),提供担保交易、源码交易、虚拟商品、在家创业、在线创业、任务交易、网站设计、软件设计、网络兼职、站长交易、域名交易、链接买卖、网站交易、广告买卖、站长培训、建站美工等服务