1.5 <= Apache Commons Text <= 1.9
如图所示,在IDEA中新建一个Maven项目,项目配置如下
创建Maven项目后,在pom.xml文件加入如下内容
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.8</version></dependency>
如图所示,在src/main/java目录中创建一个JavaTest类
将JavaTest类内容修改为如下所示
import org.apache.commons.text.StringSubstitutor;
public class JavaTest { public static void main(String[] args) { StringSubstitutor interpolator = StringSubstitutor.createInterpolator(); String poc = interpolator.replace("${script:js:java.lang.Runtime.getRuntime().exec(\"calc\")}"); }}
如图所示,在Runtime.java文件的第346行exec函数设置断点
点击Debug ‘JavaTest’开始调试应用
如图所示,在StringSubstiutor#replace中,调用substitute函数处理source值
跟进到StringSubstiutor#substitute,如图所示,在StringSubstiutor#substitute中,将source值去除${、}后通过resolveVariable函数进行处理。
跟进resolveVariable函数,如图所示,在StringSubstiutor#resolveVariable中调用resolver.lookup函数处理variableName值
跟进resolver.lookup函数,如图所示,在resolver.lookup函数中,首先将variableName值script:js:java.lang.Runtime.getRuntime().exec("calc")去除了script:
再根据variableName值前缀为script调用ScriptStringLookup.lookup解析name值
跟进ScriptStringLookup.lookup函数,在ScriptStringLookup.lookup函数中,根据传入的参数值前缀为js实例化js渲染引擎对象,并调用该对象的eval函数处理去除js:的传入参数值java.lang.Runtime.getRuntime().exec("calc")
最终触发命令执行
将Apache Commons Text版本升级至1.10