百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

nginx 拦截非法字符,nginx禁止url访问特定字符,nginx过滤url

off999 2025-01-20 15:40 15 浏览 0 评论

## nginx 拦截非法字符,nginx禁止url访问特定字符,nginx过滤url访问

参考代码一

#前缀为无限,后面的精准屏蔽字符 123

if ($request_uri ~* "123") {

return 403 "I've recorded your ip Wait to check the water meter";

}


参考代码二


#前缀为index.php,后面的精准屏蔽字符 123

if ($request_uri ~* "/index.php\123") {

return 403 "I've recorded your ip Wait to check the water meter";

}

参考代码三

location / {

# 当访问的url中含有/%#时返回404

if ($request_uri ~* "/%#") {

return 404;

}

#当访问url的后面带有?的链接 返回404

if ($request_uri ~* ^/a/m/(.*)\.html\?(.*)$) {

return 404;

}

}

注释:

过滤链接中的?在nginx的配置文件中写入

location / {# 当访问的url中含有/%#时返回404if ($request_uri ~* "/%#") {return 404;}#

当访问url的后面带有?的链接 返回404if ($request_uri ~*^/a/m/(.*)\.html\?(.*)$) {return 404;

nginx 正则表达式

=开头表示精确匹配,如 A 中只匹配根目录结尾的请求,后面不能带任何字符串。

^~ 开头表示uri以某个常规字符串开头,不是正则匹配

~ 开头表示区分大小写的正则匹配;

~* 开头表示不区分大小写的正则匹配

.* .匹配任意字符,*匹配数量0到正无穷;

\. \转义,匹配.

/ 通用匹配, 如果没有其它匹配,任何请求都会匹配到


本站测试代码,小白一键复制(注:需要自行根据网站程序定义)

宝塔为例:/www/server/panel/vhost/nginx/你的网站域名.conf,复制代码点击保存

#前缀为无限,后面的是屏蔽字符

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语句过滤

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;

}

文件注入禁止

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|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;

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;

}

}

自动防护

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;

}

注:使用上也需要进行相应的调整

相关推荐

推荐一款Python的GUI可视化工具(python 可视化工具)

在Python基础语法学习完成后,进一步开发应用界面时,就需要涉及到GUI了,GUI全称是图形用户界面(GraphicalUserInterface,又称图形用户接口),采用图形方式显示的计算机操...

教你用Python绘制谷歌浏览器的3种图标

前两天在浏览matplotlib官方网站时,笔者无意中看到一个挺有意思的图片,就是用matplotlib制作的火狐浏览器的logo,也就是下面这个东东(网页地址是https://matplotlib....

小白学Python笔记:第二章 Python安装

Windows操作系统的python安装:Python提供Windows、Linux/UNIX、macOS及其他操作系统的安装包版本,结合自己的使用情况,此处仅记录windows操作系统的python...

Python程序开发之简单小程序实例(9)利用Canvas绘制图形和文字

Python程序开发之简单小程序实例(9)利用Canvas绘制图形和文字一、项目功能利用Tkinter组件中的Canvas绘制图形和文字。二、项目分析要在窗体中绘制图形和文字,需先导入Tkinter组...

一文吃透Python虚拟环境(python虚拟环境安装和配置)

摘要在Python开发中,虚拟环境是一种重要的工具,用于隔离不同项目的依赖关系和环境配置。本文将基于windows平台介绍四种常用的Python虚拟环境创建工具:venv、virtualenv、pip...

小白也可以玩的Python爬虫库,收藏一下

最近,微软开源了一个项目叫「playwright-python」,作为一个兴起项目,出现后受到了大家热烈的欢迎,那它到底是什么样的存在呢?今天为你介绍一下这个传说中的小白神器。Playwright是...

python环境安装+配置教程(python安装后怎么配置环境变量)

安装python双击以下软件:弹出一下窗口需选择一些特定的选项默认选项不需要更改,点击next勾选以上选项,点击install进度条安装完毕即可。到以下界面,证明安装成功。接下来安装库文件返回电脑桌面...

colorama,一个超好用的 Python 库!

大家好,今天为大家分享一个超好用的Python库-colorama。Github地址:https://github.com/tartley/coloramaPythoncolorama库是一...

python制作仪表盘图(python绘制仪表盘)

今天教大家用pyecharts画仪表盘仪表盘(Gauge)是一种拟物化的图表,刻度表示度量,指针表示维度,指针角度表示数值。仪表盘图表就像汽车的速度表一样,有一个圆形的表盘及相应的刻度,有一个指针...

总结90条写Python程序的建议(python写作)

  1.首先  建议1、理解Pythonic概念—-详见Python中的《Python之禅》  建议2、编写Pythonic代码  (1)避免不规范代码,比如只用大小写区分变量、使用容易...

[oeasy]python0137_相加运算_python之禅_import_this_显式转化

变量类型相加运算回忆上次内容上次讲了是从键盘输入变量input函数可以有提示字符串需要有具体的变量接收输入的字符串输入单个变量没有问题但是输入两个变量之后一相加就非常离谱添加图片注释,不超过1...

Python入门学习记录之一:变量(python中变量的规则)

写这个,主要是对自己学习python知识的一个总结,也是加深自己的印象。变量(英文:variable),也叫标识符。在python中,变量的命名规则有以下三点:>变量名只能包含字母、数字和下划线...

掌握Python的&quot;魔法&quot;:特殊方法与属性完全指南

在Python的世界里,以双下划线开头和结尾的"魔法成员"(如__init__、__str__)是面向对象编程的核心。它们赋予开发者定制类行为的超能力,让自定义对象像内置类型一样优雅工...

11个Python技巧 不Pythonic 实用大于纯粹

虽然Python有一套强大的设计哲学(体现在“Python之禅”中),但总有一些情况需要我们“打破规则”来解决特定问题。这触及了Python哲学中一个非常核心的理念:“实用主义胜于纯粹主义”...

Python 从入门到精通 第三课 诗意的Python之禅

导言:Python之禅,英文名是TheZenOfPython。最早由TimPeters在Python邮件列表中发表,它包含了影响Python编程语言设计的20条软件编写原则。它作为复活节彩蛋...

取消回复欢迎 发表评论: