nginx 攔截非法字符,nginx禁止url訪問特定字符,nginx過濾url訪問
## nginx 攔截非法字符,nginx禁止url訪問特定字符,nginx過濾url訪問
**參考代碼一**
```php
#前綴為無限,后面的精準(zhǔn)屏蔽字符 123
#資源寶分享:www.httple.net
if? ($request_uri ~* "123") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
```
**參考代碼二**
```php
?#前綴為index.php,后面的精準(zhǔn)屏蔽字符 123
?#資源寶分享:www.httple.net
? if? ($request_uri? ~* "/index.php\123") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
```
**參考代碼三**
```php
location / {
? ? ? ? # 當(dāng)訪問的url中含有/%#時(shí)返回404
? ? ? ? if ($request_uri ~* "/%#") {
? ? ? ? ? ? return 404;
? ? ? ? }
? ? ? ? #當(dāng)訪問url的后面帶有?的鏈接 返回404
? ? ? ? if ($request_uri ~*? ^/a/m/(.*)\.html\?(.*)$) {
? ? ? ? ? ? ?return 404;
? ? ? ? }
?}
```
**注釋**
```php
過濾鏈接中的?在nginx的配置文件中寫入
location / {# 當(dāng)訪問的url中含有/%#時(shí)返回404if ($request_uri ~* "/%#") {return 404;}#
當(dāng)訪問url的后面帶有?的鏈接 返回404if ($request_uri ~*^/a/m/(.*)\.html\?(.*)$) {return 404;
nginx 正則表達(dá)式
=開頭表示精確匹配,如 A 中只匹配根目錄結(jié)尾的請求,后面不能帶任何字符串。
^~ 開頭表示uri以某個(gè)常規(guī)字符串開頭,不是正則匹配
~ 開頭表示區(qū)分大小寫的正則匹配;
~* 開頭表示不區(qū)分大小寫的正則匹配
.*? ?.匹配任意字符,*匹配數(shù)量0到正無窮;
\.? ?\轉(zhuǎn)義,匹配.?
/ 通用匹配, 如果沒有其它匹配,任何請求都會匹配到
```
**本站測試代碼,小白一鍵復(fù)制(注:需要自行根據(jù)網(wǎng)站程序定義)**
寶塔為例:/www/server/panel/vhost/nginx/你的網(wǎng)站域名.conf,復(fù)制代碼點(diǎn)擊保存
```php
? ? #前綴為無限,后面的是屏蔽字符
if? ($request_uri ~* "../../../../../../") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為無限,后面的是屏蔽字符
if? ($request_uri ~* ";") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為無限,后面的是屏蔽字符
if? ($request_uri ~* "&") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為無限,后面的是屏蔽字符
if? ($request_uri ~* "base64") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為index.php屏蔽,后面的是屏蔽字符
? if? ($request_uri? ~* "/index.php\&") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為index.php屏蔽,后面的是屏蔽字符
? if? ($request_uri? ~* "/index.php\?") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為index.php屏蔽,后面的是屏蔽字符
? if? ($request_uri? ~* "/index.php\;") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為index.php屏蔽,后面的是屏蔽字符
? if? ($request_uri? ~* "/index.php\=") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為index.php屏蔽,后面的是屏蔽字符
? if? ($request_uri? ~* "/index.php\=") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為index.php屏蔽,后面的是屏蔽字符
? if? ($request_uri? ~* "/index.php\_") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為index.php屏蔽,后面的是屏蔽字符
? if? ($request_uri? ~* "/index.php\./") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為index.php屏蔽,后面的是屏蔽字符
? if? ($request_uri? ~* "/index.php\:") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為index.php屏蔽,后面的是屏蔽字符
? if? ($request_uri? ~* "/index.php\base64") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為index.php屏蔽,后面的是屏蔽字符
? if? ($request_uri? ~* "/index.php\%") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為index.php屏蔽,后面的是屏蔽字符
? if? ($request_uri? ~* "/index.php\wp-content") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
? ? #前綴為index.php屏蔽,后面的是屏蔽字符
? if? ($request_uri? ~* "/index.php\;amp") {
return 403? ?"I've recorded your ip Wait to check the water meter";
}
```
## 附帶:nginx ql語句過濾、文件注入禁止、溢出攻擊過濾、spam字段過濾、user-agents頭過濾
**sql語句過濾**
```php
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;
? ? ? ? ?}
```
**文件注入禁止**
```php
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;
}
```
**溢出攻擊過濾**
```php
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字段過濾**
```php
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頭過濾**
```php
set $block_user_agents 0;
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 ~ “l(fā)ibwww-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;
}
}
```
**自動防護(hù)**
```php
if ($request_uri ~* \.(htm|do)\?(.*)$) {
? ? ? ? ? ?set $req $2;
? ? ? ? }
? ? ? ? if ($req ~* "(cost\()|(concat\()") {
? ? ? ? ? ? ? ? return 503;
? ? ? ? }
? ? ? ? if ($req ~* "union[+|(%20)]") {
? ? ? ? ? ? ? ? return 503;
? ? ? ? }
? ? ? ? if ($req ~* "and[+|(%20)]") {
? ? ? ? ? ? ? ? return 503;
? ? ? ? }
? ? ? ? if ($req ~* "select[+|(%20)]") {
? ? ? ? ? ? ? ? return 503;
? ? ? ? }
```
注:使用上也需要進(jìn)行相應(yīng)的調(diào)整