leonheart 发表于 2018-11-12 06:47:28

nginx安全加固

  nginx模块
  http_substitutions_filter_module
  modsecurity
  Tengine支持了动态加载模块
  增加modsecurity模块
  modsecurity倾向于过滤和阻止web危险
  http://waringid.blog.51cto.com/65148/1629905
  关闭服务器标记http{server_tokens off} 隐藏nginx版本
  自定义缓存
  server
  {
  client_body_buffer_size16K;
  client_header_buffer_size1k;
  client_max_body_size1m;
  large_client_header_buffers48k;
  }
  设置timeout设低来防御DOS***
  http
  {
  client_body_timeout   10;
  client_header_timeout30;
  keepalive_timeout   3030;
  send_timeout          10;
  }
  限制访问的方法
  server
  {
  if($request_method !~ ^(GET|HEAD|POST)$) {
  return404;
  }
  }
  实际中应该使用444
  日志切割
  kill-USR1 $(cat /var/run/nginx/nginx.pid)
  模块 ngx_http_access_module 允许限制某些IP地址的客户端访问
  location/ {
  deny192.168.1.1;
  allow 192.168.1.0/24;
  allow 10.1.1.0/16;
  allow 2001:0db8::/32;
  denyall;
  }
  在原来的编译参数的首行加入--add-module=/root/install/naxsi-core-0.51-1/naxsi_src
  nbs.rules文件
  增加IP限制功能
  模块ngx_http_limit_conn_module和来ngx_http_limit_req_module
  http {
  limit_req_zone$binary_remote_addr zone=one:10m rate=10r/s;
  limit_conn_zone$binary_remote_addr zone=two:10m;
  }
  server{
  limit_req zone=one burst=5;
  limit_conn two 15;
  }
  增加WAF模块
  增加fail2ban防止非法IP
  ngx_http_stub_status_module模块
  Nginx模块GeoIP匹配处理IP所在国家、城市
  if ( $geoip_country_code !~^(CN|US)$ ) {
  return 403;
  }
  自定义nginx版本号
  http://purplegrape.blog.51cto.com/1330104/1291871
  封杀各种user-agent
  if ($http_user_agent ~* "java|python|perl|ruby|curl|bash|echo|uname|base64|decode|md5sum|select|concat|httprequest|httpclient|nmap|scan" ) {
  return 403;
  }
  if ($http_user_agent ~* "" ) {
  return 403;
  }
  封杀特定的url
  location ~* \.(bak|save|sh|sql|mdb|svn|git|old)$ {
  rewrite ^/(.*)$$hostpermanent;
  }
  location /(admin|phpadmin|status){ deny all; }
  封杀特定的http方法和行为
  if ($request_method !~ ^(GET|POST|HEAD)$ ) {
  return 405;
  }
  if ($http_range ~ "\d{9,}") {
  return 444;
  }
  强制网站使用域名访问,可以逃过IP扫描
  if ( $host !~* 'abc.com' ) {
  return 403;
  }
  url 参数过滤敏感字
  if ($query_string ~* "union.*select.*\(") {
  rewrite ^/(.*)$$hostpermanent;
  }
  if ($query_string ~* "concat.*\(") {
  rewrite ^/(.*)$$hostpermanent;
  }
  强制要求referer
  if ($http_referer = "" ) {
  return 403;
  }
  老外的配置
  set $add 1;
  location /index.php {
  limit_except GET POST {
  deny all;
  }
  set $ban "";
  if ($http_referer = "" ) {set $ban $ban$add;}
  if ($request_method = POST ) {set $ban $ban$add;}
  if ($query_string = "action=login" ){set $ban $ban$add;}
  if ($ban = 111 ) {
  access_log /var/log/nginx/ban IP;
  return 404;
  }
  proxy_pass http://127.0.0.1:8000; #here is a patch
  }
  目录只读
  mount --bind /data /var/www/html
  mount -o remount,ro --bind /data /var/www/html
  极大程度上可以防止提权篡改

页: [1]
查看完整版本: nginx安全加固