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

本文章给大家介绍一个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; 

  
## 禁掉溢出攻击 
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|bluespill|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 ($block_user_agents = 1) { 
return 444; 

}

时间: 2024-08-02 23:13:19

nginx服务器防sql注入/溢出攻击/spam及禁User-agents的相关文章

PHP防SQL注入攻击

PHP防SQL注入攻击 收藏 没有太多的过滤,主要是针对php和mysql的组合. 一般性的防注入,只要使用php的 addslashes 函数就可以了. 以下是一段copy来的代码: PHP代码 $_POST = sql_injection($_POST); $_GET = sql_injection($_GET); function sql_injection($content) { if (!get_magic_quotes_gpc()) { if (is_array($content))

【荐】PDO防 SQL注入攻击 原理分析 以及 使用PDO的注意事项

我们都知道,只要合理正确使用PDO,可以基本上防止SQL注入的产生,本文主要回答以下几个问题: 为什么要使用PDO而不是mysql_connect? 为何PDO能防注入? 使用PDO防注入的时候应该特别注意什么? 一.为何要优先使用PDO? PHP手册上说得很清楚: Prepared statements and stored procedures Many of the more mature databases support the concept of prepared statemen

防sql注入攻击

这两天看了个防sql注入,觉得有必要总结一下: 首先需要做一些php的安全配置: 1 在php.ini 中把display_errors改成OFF display_errors = OFF 或在php文件前加入 error_reporting(0) 2 关闭一些“坏功能” 1)关闭magic quotes功能 在php.ini 把magic_quotes_gpc = OFF 避免和addslashes等重复转义 2)关闭register_globals = Off 在php.ini 把regis

Java 防SQL注入过滤器(拦截器)代码

原文出自:https://blog.csdn.net/seesun2012 前言 浅谈SQL注入: 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,达到一定的非法用途. 解决办法 1.配置WEB-INF/web.xml <web-app> <welcome-file-list> <welcome-file>index.html</welcome-file> </welco

防SQL注入的参数化查询

在做机房收费系统的时候,曾经利用过传递参数的形式来将值传递给SQL语句或者存储过程,因为这样可以通过参数化的查询来帮助抵御"SQL 注入"式攻击,这种攻击者会将命令插入SQL语句,从而危机服务器的安全. <span style="font-family:SimSun;font-size:18px;"><span style="font-family:SimSun;font-size:18px;">SqlParameter

.Net防sql注入的方法总结

#防sql注入的常用方法: 1.服务端对前端传过来的参数值进行类型验证: 2.服务端执行sql,使用参数化传值,而不要使用sql字符串拼接: 3.服务端对前端传过来的数据进行sql关键词过来与检测: #着重记录下服务端进行sql关键词检测: 1.sql关键词检测类: 1 public class SqlInjectHelper:System.Web.UI.Page 2 { 3 private static string StrKeyWord = "select|insert|delete|fro

C#语言Winform防SQl注入做用户登录的例子

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace OmyGod {     public partial

在Global.asax文件里实现通用防SQL注入漏洞程序

首先,创建一个SQLInjectionHelper类完成恶意代码的检查 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text.RegularExpressions; /// <summary> ///SQLInjectionHelper 的摘要说明 /// </summary> public class SQLInjec

自定义防SQL注入函数

/************************************************ *SQL防注入函数 *@time 2014年6月24日18:50:59 * */ public function safe_replace($string){ $string = str_replace('%20','',$string); $string = str_replace('%27','',$string); $string = str_replace('%2527','',$stri