nginx服务器防sql注入/溢出攻击/spam及禁User-agents

简介:

在配置文件添加如下字段即可
server {
## 禁SQL注入 Block SQL injections
set $block_sql_injections 0;
if ($query_string ~ “union.*select.*\(“) {
set $block_sql_injections 1;
}
if ($query_string ~ “union.*all.*select.*”) {
set $block_sql_injections 1;
}
if ($query_string ~ “concat.*\(“) {
set $block_sql_injections 1;
}
if ($block_sql_injections = 1) {
return 444;
}

## 禁掉文件注入
set $block_file_injections 0;
if ($query_string ~ “[a-zA-Z0-9_]=http://”) {
set $block_file_injections 1;
}
if ($query_string ~ “[a-zA-Z0-9_]=(\.\.//?)+”) {
set $block_file_injections 1;
}
if ($query_string ~ “[a-zA-Z0-9_]=/([a-z0-9_.]//?)+”) {
set $block_file_injections 1;
}
if ($block_file_injections = 1) {
return 444;
}

###一句话木马

if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}

 

## 禁掉溢出攻击
set $block_common_exploits 0;
if ($query_string ~ “(<|%3C).*script.*(>|%3E)”) {
set $block_common_exploits 1;
}
if ($query_string ~ “GLOBALS(=|\[|\%[0-9A-Z]{0,2})”) {
set $block_common_exploits 1;
}
if ($query_string ~ “_REQUEST(=|\[|\%[0-9A-Z]{0,2})”) {
set $block_common_exploits 1;
}
if ($query_string ~ “proc/self/environ”) {
set $block_common_exploits 1;
}
if ($query_string ~ “mosConfig_[a-zA-Z_]{1,21}(=|\%3D)”) {
set $block_common_exploits 1;
}
if ($query_string ~ “base64_(en|de)code\(.*\)”) {
set $block_common_exploits 1;
}
if ($block_common_exploits = 1) {
return 444;
}

## 禁spam字段
set $block_spam 0;
if ($query_string ~ “\b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)\b”) {
set $block_spam 1;
}
if ($query_string ~ “\b(erections|hoodia|huronriveracres|impotence|levitra|libido)\b”) {
set $block_spam 1;
}
if ($query_string ~ “\b(ambien|blue\spill|cialis|cocaine|ejaculation|erectile)\b”) {
set $block_spam 1;
}
if ($query_string ~ “\b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)\b”) {
set $block_spam 1;
}
if ($block_spam = 1) {
return 444;
}

## 禁掉user-agents
set $block_user_agents 0;

# Don’t disable wget if you need it to run cron jobs!
#if ($http_user_agent ~ “Wget”) {
# set $block_user_agents 1;
#}

# Disable Akeeba Remote Control 2.5 and earlier
if ($http_user_agent ~ “Indy Library”) {
set $block_user_agents 1;
}

# Common bandwidth hoggers and hacking tools.
if ($http_user_agent ~ “libwww-perl”) {
set $block_user_agents 1;
}
if ($http_user_agent ~ “GetRight”) {
set $block_user_agents 1;
}
if ($http_user_agent ~ “GetWeb!”) {
set $block_user_agents 1;
}
if ($http_user_agent ~ “Go!Zilla”) {
set $block_user_agents 1;
}
if ($http_user_agent ~ “Download Demon”) {
set $block_user_agents 1;
}
if ($http_user_agent ~ “Go-Ahead-Got-It”) {
set $block_user_agents 1;
}
if ($http_user_agent ~ “TurnitinBot”) {
set $block_user_agents 1;
}
if ($http_user_agent ~ “GrabNet”) {
set $block_user_agents 1;
}
   if ($http_user_agent ~ "WebBench") {
       set $block_user_agents 1;
   }
   if ($http_user_agent ~ "ApacheBench") {
       set $block_user_agents 1;
   }
   if ($http_user_agent ~ ^$) {
       set $block_user_agents 1;
   }
   if ($http_user_agent ~ "Python-urllib") {
       set $block_user_agents 1;
   }
if ($block_user_agents = 1) {
return 444;
}
}

####sql####

if ($request_uri ~* "(cost\()|(concat\()") {
               return 444;
       }
       if ($request_uri ~* "[+|(%20)]union[+|(%20)]") {
               return 444;
       }
       if ($request_uri ~* "[+|(%20)]and[+|(%20)]") {
               return 444;
       }
       if ($request_uri ~* "[+|(%20)]select[+|(%20)]") {
               return 444;
       }

## Block SQL injections
   set $block_sql_injections 0;
   if ($query_string ~ "union.*select.*\(") {
       set $block_sql_injections 1;
   }
   if ($query_string ~ "union.*all.*select.*") {
       set $block_sql_injections 1;
   }
   if ($query_string ~ "concat.*\(") {
       set $block_sql_injections 1;
   }
   if ($block_sql_injections = 1) {
       return 403;
   }

   ## Block file injections
   set $block_file_injections 0;
   if ($query_string ~ "[a-zA-Z0-9_]=http://") {
       set $block_file_injections 1;
   }
   if ($query_string ~ "[a-zA-Z0-9_]=(\.\.//?)+") {
       set $block_file_injections 1;
   }
   if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") {
       set $block_file_injections 1;
   }
   if ($block_file_injections = 1) {
       return 403;
   }

   ## Block common exploits
   set $block_common_exploits 0;
   if ($query_string ~ "(<|%3C).*script.*(>|%3E)") {
       set $block_common_exploits 1;
   }
   if ($query_string ~ "GLOBALS(=|\[|\%[0-9A-Z]{0,2})") {
       set $block_common_exploits 1;
   }
   if ($query_string ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})") {
       set $block_common_exploits 1;
   }
   if ($query_string ~ "proc/self/environ") {
       set $block_common_exploits 1;
   }
   if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|\%3D)") {
       set $block_common_exploits 1;
   }
   if ($query_string ~ "base64_(en|de)code\(.*\)") {
       set $block_common_exploits 1;
   }
   if ($block_common_exploits = 1) {
       return 403;
   }

   ## Block spam
   set $block_spam 0;
   if ($query_string ~ "\b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)\b") {
       set $block_spam 1;
   }
   if ($query_string ~ "\b(erections|hoodia|huronriveracres|impotence|levitra|libido)\b") {
       set $block_spam 1;
   }
   if ($query_string ~ "\b(ambien|blue\spill|cialis|cocaine|ejaculation|erectile)\b") {
       set $block_spam 1;
   }
   if ($query_string ~ "\b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)\b") {
       set $block_spam 1;
   }
   if ($block_spam = 1) {
       return 403;
   }

   ## Block user agents
   set $block_user_agents 0;

   # Don't disable wget if you need it to run cron jobs!
   #if ($http_user_agent ~ "Wget") {
   #    set $block_user_agents 1;
   #}

   # Disable Akeeba Remote Control 2.5 and earlier
   if ($http_user_agent ~ "Indy Library") {
       set $block_user_agents 1;
   }

   # Common bandwidth hoggers and hacking tools.
   if ($http_user_agent ~ "libwww-perl") {
       set $block_user_agents 1;
   }
   if ($http_user_agent ~ "GetRight") {
       set $block_user_agents 1;
   }
   if ($http_user_agent ~ "GetWeb!") {
       set $block_user_agents 1;
   }
   if ($http_user_agent ~ "Go!Zilla") {
       set $block_user_agents 1;
   }
   if ($http_user_agent ~ "Download Demon") {
       set $block_user_agents 1;
   }
   if ($http_user_agent ~ "Go-Ahead-Got-It") {
       set $block_user_agents 1;
   }
   if ($http_user_agent ~ "TurnitinBot") {
       set $block_user_agents 1;
   }
   if ($http_user_agent ~ "GrabNet") {
       set $block_user_agents 1;
   }
   if ($http_user_agent ~ "WebBench") {
       set $block_user_agents 1;
   }
   if ($http_user_agent ~ "ApacheBench") {
       set $block_user_agents 1;
   }
   if ($http_user_agent ~ ^$) {
       set $block_user_agents 1;
   }
   if ($http_user_agent ~ "Python-urllib") {
       set $block_user_agents 1;
   }
   if ($block_user_agents = 1) {
       return 403;
   }
 

这些规则必须要测试 才能上线。根据实际情况应用!!!

本文转自 liang3391 51CTO博客,原文链接:http://blog.51cto.com/liang3391/974952

相关文章
|
7月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
392 18
|
7月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
650 17
|
8月前
|
缓存 负载均衡 JavaScript
Nginx:高性能Web服务器与反向代理利器
Nginx:高性能Web服务器与反向代理利器
384 110
|
8月前
|
缓存 负载均衡 前端开发
Nginx:高性能Web服务器的核心引擎
Nginx:高性能Web服务器的核心引擎
269 47
|
8月前
|
缓存 负载均衡 前端开发
Nginx:高性能的Web服务器与反向代理利器
Nginx:高性能的Web服务器与反向代理利器
412 99
|
8月前
|
负载均衡 前端开发 安全
Nginx:高性能的Web服务器与反向代理利器
Nginx:高性能的Web服务器与反向代理利器
322 98
|
8月前
|
缓存 负载均衡 前端开发
Nginx:高性能Web服务器的核心引擎
Nginx:高性能Web服务器的核心引擎
361 99
|
SQL 存储 关系型数据库
|
SQL 存储 关系型数据库
|
关系型数据库 MySQL 网络安全
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")

热门文章

最新文章