Java:Spring Boot 框架漏洞

outman
10
2025-06-29

//靶场:https://github.com/whgojp/JavaSecLab

Spel

Spring 框架的表达式语言,可以在运行的时候查询数据,调用方法 --> 调用 getRuntime,exec() 就会造成 RCE 漏洞

同类的还有 struts2 的 ognl,不过很少用了


漏洞测试:

POC(弹计算器):T(java.lang.Runtime).getRuntime().exec('calc.exe')

EvaluationContext 类中有 forReadOnlyDataBinding() 方法,可以限制 Spel 功能,只读


//漏洞代码
ExpressionParser parser = new SpelExpressionParser();
    
EvaluationContext evaluationContext = new StandardEvaluationContext();
//安全代码
ExpressionParser parser = new SpelExpressionParser();
    
EvaluationContext simpleContext = SimpleEvaluationContext.forReadOnlyDataBinding().build();

SSTI 模板注入

MVC模式

  1. 用户输入 ---> Controller(控制器)

  2. Controller 根据请求指令和类型 ---> Model(业务模型)

  3. Model 进行逻辑处理,数据库操作等 ---> Viewer(视图层)

  4. Viewer 经过模板渲染 ---> 用户

模板

模板可以当作是被固定好的格式,需要开发人员和用户来填补信息的文件,这种方法可以视图与逻辑分离,更清晰,更安全


thymeleaf模版注入:

RCE:para=__$%7bnew%20java.util.Scanner(T(java.lang.Runtime).getRuntime().exec(%27id%27).getInputStream()).next()%7d__::.x

白名单路径校验 + 控制器返回为 void,方法参数设置为 HttpServletResponse 后,Spring MVC 会跳过视图解析过程

//漏洞代码
public String vul1(@RequestParam String para, Model model) {
    // 用户输入直接拼接到模板路径,可能导致SSTI(服务器端模板注入)漏洞
    return "vul/ssti/" + para;
}

public void vul2(@PathVariable String path) {
    log.info("SSTI注入:"+path);
}
//安全代码
public String safe1(String para, Model model) {
    List<String> white_list = new ArrayList<>(Arrays.asList("vul", "ssti"));
    if (white_list.contains(para)){
        return "vul/ssti" + para;
    } else{
        return "common/401";
    }
}
@GetMapping("/safe2/{path}")
public void safe2(@PathVariable String path, HttpServletResponse response) {
    log.info("SSTI注入:"+path);
}

Spring Boot框架漏洞

Swagger 泄露

Swagger:一个框架,集合了所有 API 接口,本意是方便开发人员理解和使用这些 API,攻击人员进入到 Swagger,就可以扩大自己的攻击面

  1. 1. 发现 Swagger(tool): https://gitee.com/team-man/spring-scan

  2. 2. 利用 Swagger,通过接口找到漏洞(手工+tool):login 试试有没有弱口令;上传接口试试文件上传;试试进入一些平时进不去的,没准是未授权访问 //apifox

Actuator 泄露

Actuator:监控管理 Spring,暴露应用的运行信息,健康状态,配置信息等

  1. 1. 发现 Actuator(tool): https://gitee.com/team-man/spring-scan

  2. 2. 利用 Actuator ,发现其他泄露,如 Heapdump ,druid,jolokia

  3. 3. 扫描器提取 Heapdump //工具:https://github.com/DeEpinGh0st/JDumpSpiderGUI