防御原理:
对于简单的情况,比如包含单引号' , 分号;, <, >, 等字符可通过rewrite直接重订向到404页面来避免。
用rewrite有个前提需要知道,一般用rewrite进行正则匹配只能匹配到网页的URI,也就是url中?前部分,?以后部分是请求参数。
问号后面的请求参数,在nginx用$query_string表 示,不能在rewrite中匹配到,需要用if判断
例如,对于参数中带有单引号的'进行匹配然后定向到错误页面,
/plus/list.php?tid=19&mid=1124'
rewrite ^.*([;'<>]).* /error.html break;
直接写这样的一条重写肯定不会正确匹配,因为rewrite参数只会匹配请求的uri,也就是/plus/list.php部分。
需要使用$query_string 借助if进行判断,如果查询串种包含特殊字符,返回404。
if ( $query_string ~* ".*[;'<>].*" ){
return 404;
}
SQL注入攻击一般问号后面的请求参数,在nginx用$query_string表示
if ($request_uri ~* "(cost()|(concat()") {
return 404;
}
if ($request_uri ~* "[+|(%20)]union[+|(%20)]") {
return 404;
}
if ($request_uri ~* "[+|(%20)]and[+|(%20)]") {
return 404;
}
if ($request_uri ~* "[+|(%20)]select[+|(%20)]") {
return 404;
}
if ( $query_string ~* ".*[;'<>].*" ){
return 404;
}
防范sql注入攻击
在虚拟主机server段中加入如下配置:
if ($request_uri ~* (.*)(insert|select|delete|update|count|*|%|master|truncate|declare|'|;|and|or|(|)|exec)(.*)$ ) { rewrite ^(.*) 11 redirect; }
当然我们也可以返回404错误:
if ($request_uri ~* (.*)(insert|select|delete|update|count|*|%|master|truncate|declare|'|;|and|or|(|)|exec)(.*)$ ) { return 404; }
其它一些方法
在配置文件添加如下字段即可
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;
}
************************************************************************************************************************
以下是我所用的常规配置文件:
server {
listen 80;
location / {
root html;
index index.html index.htm;
proxy_redirect default ;
proxy_set_header Host $host;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
if ($request_method !~* GET|POST) { return 444; }
if ($query_string ~* (\$|'|--|[+|(%20)]union[+|(%20)]|[+|(%20)]insert[+|(%20)]|[+|(%20)]drop[+|(%20)]|[+|(%20)]truncate[+|(%20)]|[+|(%20)]update[+|(%20)]|[+|(%20)]from[+|(%20)]|[+|(%20)]grant[+|(%20)]|[+|(%20)]exec[+|(%20)]|[+|(%20)]where[+|(%20)]|[+|(%20)]select[+|(%20)]|[+|(%20)]and[+|(%20)]|[+|(%20)]or[+|(%20)]|[+|(%20)]count[+|(%20)]|[+|(%20)]exec[+|(%20)]|[+|(%20)]chr[+|(%20)]|[+|(%20)]mid[+|(%20)]|[+|(%20)]like[+|(%20)]|[+|(%20)]iframe[+|(%20)]|[\<|%3c]script[\>|%3e]|javascript|alert|webscan|dbappsecurity|style|confirm\(|innerhtml|innertext)(.*)$) { return 555; }
if ($uri ~* (/~).*) { return 501; }
if ($uri ~* (\\x.)) { return 501; }
if ($query_string ~* "[;'<>].*") { return 509; }
if ($request_uri ~ " ") { return 509; }
if ($request_uri ~ (\/\.+)) { return 509; }
if ($request_uri ~ (\.+\/)) { return 509; }
#if ($uri ~* (insert|select|delete|update|count|master|truncate|declare|exec|\*|\')(.*)$ ) { return 503; }
if ($request_uri ~* "(cost\()|(concat\()") { return 504; }
if ($request_uri ~* "[+|(%20)]union[+|(%20)]") { return 504; }
if ($request_uri ~* "[+|(%20)]and[+|(%20)]") { return 504; }
if ($request_uri ~* "[+|(%20)]select[+|(%20)]") { return 504; }
if ($request_uri ~* "[+|(%20)]or[+|(%20)]") { return 504; }
if ($request_uri ~* "[+|(%20)]delete[+|(%20)]") { return 504; }
if ($request_uri ~* "[+|(%20)]update[+|(%20)]") { return 504; }
if ($request_uri ~* "[+|(%20)]insert[+|(%20)]") { return 504; }
if ($query_string ~ "(<|%3C).*script.*(>|%3E)") { return 505; }
if ($query_string ~ "GLOBALS(=|\[|\%[0-9A-Z]{0,2})") { return 505; }
if ($query_string ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})") { return 505; }
if ($query_string ~ "proc/self/environ") { return 505; }
if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|\%3D)") { return 505; }
if ($query_string ~ "base64_(en|de)code\(.*\)") { return 505; }
if ($query_string ~ "[a-zA-Z0-9_]=http://") { return 506; }
if ($query_string ~ "[a-zA-Z0-9_]=(\.\.//?)+") { return 506; }
if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") { return 506; }
if ($query_string ~ "b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)b") { return 507; }
if ($query_string ~ "b(erections|hoodia|huronriveracres|impotence|levitra|libido)b") {return 507; }
if ($query_string ~ "b(ambien|bluespill|cialis|cocaine|ejaculation|erectile)b") { return 507; }
if ($query_string ~ "b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)b") { return 507; }
if ($http_user_agent ~* YisouSpider|ApacheBench|WebBench|Jmeter|JoeDog|Havij|GetRight|TurnitinBot|GrabNet|masscan|mail2000|github|wget|curl|Java|python) { return 508; }
if ($http_user_agent ~* "Go-Ahead-Got-It") { return 508; }
if ($http_user_agent ~* "GetWeb!") { return 508; }
if ($http_user_agent ~* "Go!Zilla") { return 508; }
if ($http_user_agent ~* "Download Demon") { return 508; }
if ($http_user_agent ~* "Indy Library") { return 508; }
if ($http_user_agent ~* "libwww-perl") { return 508; }
if ($http_user_agent ~* "Nmap Scripting Engine") { return 508; }
if ($http_user_agent ~* "~17ce.com") { return 508; }
if ($http_user_agent ~* "WebBench*") { return 508; }
if ($http_user_agent ~* "spider") { return 508; }
if ($http_referer ~* 17ce.com) { return 509; }
if ($http_referer ~* WebBench*") { return 509; }
}
[课程]FART 脱壳王!加量不加价!FART作者讲授!