Thinkphp 缓存RCE

 5.0.0<=ThinkPHP5<=5.0.10 

 

漏洞利用条件:

1.基于tp5开发的代码中使用了Cache::set 进行缓存

2.在利用版本范围内

3.runtime目录可以访问

 

扩展:

1.不可访问,可以考虑包含。前提要找到包含处。//Tp有一个Cache一个包含

2.利用缓存,做一个长期的shell动态生成。

 

漏洞原因:缓存名,可以预测,缓存内容用户可控。

 

造成漏洞代码

<?php

namespace appindexcontroller;

use thinkCache;

class Index

{

public function index()

{

Cache::set("name",input("get.username"));

return 'Cache success';

}

}

 

use thinkCache; 跟到thinkCache.php

thinkCache.php 文件内容

use thinkcacheDriver;

set 方法

缓存写入次数

return self::init()->set($name, $value, $expire);

 

自动缓存

 

init 方法里

 

 

Config::get('cache.type')

 

 

默认为File类型

程序获取cache type 以后

 

 

这样就加载了 hinkcachedriverFile.php

 

 

return self::init()->set($name, $value, $expire);

通过Init() 调用file 下的set方法

if (is_null($expire)) {
$expire = $this->options['expire'];

}

 

那么$expire=0

 

目录名以及文件名:

protected function getCacheKey($name, $auto = false)
{
$name = md5($name);
if ($this->options['cache_subdir']) {
//
使用子目录
$name = substr($name, 0, 2) . DS . substr($name, 2); //
目录名取md5$name)的前2位,文件名为后30
}
if ($this->options['prefix']) {
$name = $this->options['prefix'] . DS . $name;
}
$filename = $this->options['path'] . $name . '.php';
$dir = dirname($filename);

if ($auto && !is_dir($dir)) {
mkdir($dir, 0755, true);
}
return $filename;
}

 

$filename

找到set 的缓存名 就可以了 进行md5加密

2 为目录名,后30 为文件名

 

Set 只是对value做了一个序列化的操作,并没有做过滤。

dump一下 serialize 前后变化

 

 

文件的写入,这里为tp5.0.15 版本,作者对$data 进行了位置修改,并在$data 前,做了

exit()来避免用户输入的数据,被代码执行。 并且在$expire 这处,做了一个%012d强制的类型转换

 

tp5.0.10 之前,这段代码是这样的

 

 

写入的数据,之前没有exit,并且对$data 没有做过滤

导致利用%0a换行,进行代码执行。

我们访问一下文件

成功可以代码执行

原文地址:https://www.cnblogs.com/0xdd/p/11031529.html