web攻击之八:溢出攻击(nginx服务器防sql注入/溢出攻击/spam及禁User-agents)

时间:2022-07-07 00:19:24

一、什么是溢出攻击

首先, 溢出,通俗的讲就是意外数据的重新写入,就像装满了水的水桶,继续装水就会溢出,而溢出攻击就是,攻击者可以控制溢出的代码,如果程序的对象是内核级别的,如dll、sys文件等,就可以直接操控系统内核了

其次,分类:按对象名加以区分:IIS溢出、SQL溢出等,就是按对象名来加以区分,按特点区分:远程溢出、本地溢出

最后,溢出的基本原理:一是内存溢出;二是缓冲区溢出

1、内存溢出

内存溢出,是程序使用了不可靠的方式存取/复制内存缓冲区,或者是编辑设置的内存缓冲区太靠近数据结构等,进而导致内存缓冲区溢出,而溢出的字符就会取代后面的数据。例如,c语言不检查数组边界,不检查数据类型的可靠性,而c语言与机器内核代码接近,能直接访问内存和寄存器。

2、缓冲区溢出

缓冲区是用户为程序运行时在计算机中申请的一段连续的内存,它保存了给定类型的数据,而缓冲区溢出就是通过向程序的缓冲区中写入超过其长度的内容,造成缓冲区的溢出,从而破坏程序的堆栈,使程序转而执行其他的命令,以达到攻击的目的。

3、内存、缓冲区、堆、栈的概念与联系

这部分留着以后单独阐述

nginx防御方法

本文章给大家介绍一个nginx服务器防sql注入/溢出攻击/spam及禁User-agents实例代码,有需要了解的朋友可进入参考。

在配置文件添加如下字段即可

server {
## 禁SQL注入 Block SQL injections
set $block_sql_injections ;
if ($query_string ~ "union.*select.*(") {
set $block_sql_injections ;
}
if ($query_string ~ "union.*all.*select.*") {
set $block_sql_injections ;
}
if ($query_string ~ "concat.*(") {
set $block_sql_injections ;
}
if ($block_sql_injections = ) {
return ;
} ## 禁掉文件注入
set $block_file_injections ;
if ($query_string ~ "[a-zA-Z0-9_]=http://") {
set $block_file_injections ;
}
if ($query_string ~ "[a-zA-Z0-9_]=(..//?)+") {
set $block_file_injections ;
}
if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") {
set $block_file_injections ;
}
if ($block_file_injections = ) {
return ;
} ## 禁掉溢出攻击
set $block_common_exploits ;
if ($query_string ~ "(<|%3C).*script.*(>|%3E)") {
set $block_common_exploits ;
}
if ($query_string ~ "GLOBALS(=|[|%[0-9A-Z]{0,2})") {
set $block_common_exploits ;
}
if ($query_string ~ "_REQUEST(=|[|%[0-9A-Z]{0,2})") {
set $block_common_exploits ;
}
if ($query_string ~ "proc/self/environ") {
set $block_common_exploits ;
}
if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|%3D)") {
set $block_common_exploits ;
}
if ($query_string ~ "base64_(en|de)code(.*)") {
set $block_common_exploits ;
}
if ($block_common_exploits = ) {
return ;
} ## 禁spam字段
set $block_spam ;
if ($query_string ~ "b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)b") {
set $block_spam ;
}
if ($query_string ~ "b(erections|hoodia|huronriveracres|impotence|levitra|libido)b") {
set $block_spam ;
}
if ($query_string ~ "b(ambien|bluespill|cialis|cocaine|ejaculation|erectile)b") {
set $block_spam ;
}
if ($query_string ~ "b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)b") {
set $block_spam ;
}
if ($block_spam = ) {
return ;
} ## 禁掉user-agents
set $block_user_agents ; # Don’t disable wget if you need it to run cron jobs!
#if ($http_user_agent ~ "Wget") {
# set $block_user_agents ;
#} # Disable Akeeba Remote Control 2.5 and earlier
if ($http_user_agent ~ "Indy Library") {
set $block_user_agents ;
} # Common bandwidth hoggers and hacking tools.
if ($http_user_agent ~ "libwww-perl") {
set $block_user_agents ;
}
if ($http_user_agent ~ "GetRight") {
set $block_user_agents ;
}
if ($http_user_agent ~ "GetWeb!") {
set $block_user_agents ;
}
if ($http_user_agent ~ "Go!Zilla") {
set $block_user_agents ;
}
if ($http_user_agent ~ "Download Demon") {
set $block_user_agents ;
}
if ($http_user_agent ~ "Go-Ahead-Got-It") {
set $block_user_agents ;
}
if ($http_user_agent ~ "TurnitinBot") {
set $block_user_agents ;
}
if ($http_user_agent ~ "GrabNet") {
set $block_user_agents ;
} if ($block_user_agents = ) {
return ;
}
}