「配枪朱丽叶。」

RootのCTF学习笔记。

FBCTF2019/BUUCTF-RCEService

题目给的表单可以接收JSON格式的命令。比如{"cmd":"ls"}
https://s2.ax1x.com/2020/01/03/lU4dFP.png

接下来如果输入{"cmd":"cat index.php"}的话什么都不会返回。
这是因为后端中有匹配规则:

<?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的最常用方法之一是使用多行输入,因为preg_match仅尝试匹配第一行。
查看源代码:putenv('PATH=/home/rceservice/jail');,jail应用于当前环境,
又根据题目描述的提示--“只允许执行ls命令”,即jail包含了执行ls的二进制文件,
所以最后还要注意填写cat的路径:/bin/cat
https://s2.ax1x.com/2020/01/03/lUIR2T.png

https://s2.ax1x.com/2020/01/03/lUIWxU.png