如何更改此代码以使其容易受到 CRLF 注入的影响?

Posted

技术标签:

【中文标题】如何更改此代码以使其容易受到 CRLF 注入的影响?【英文标题】:How can i change this code to be vulnerable of CRLF injection? 【发布时间】:2020-12-19 10:37:38 【问题描述】:

我创建了一个简单的spring web项目,项目中只有一个Controller,其代码为:

package com.example.sbtest.controller;

import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MainController 
  @RequestMapping("/crlf")
  @ResponseBody
  public void CRLFInjectVuln(HttpServletResponse response, @RequestParam("id") String id) 
    response.addHeader("test", id);
  

然后我尝试使用 curl 触发 CRLF 注入漏洞,如下所示:

curl -vvvv "http://127.0.0.1:8080/crlf" --data "id=x\r\nLocation:%20https://www.google.com"
curl -vvvv "http://127.0.0.1:8080/crlf" --data "id=x%0d%0aLocation:%20https://www.google.com"

但是通过 CRLF 返回的所有这些都被替换为空格:

HTTP/1.1 200 
Cookies:   Location: https://www.google.com
Content-Length: 0
Date: Mon, 31 Aug 2020 09:27:58 GMT


那么,我怎样才能编写一段易受 CRLF 注入攻击的代码呢? 可能是因为Tomcat或Spring过滤了我的输入参数,所以这不起作用?但是我一路追踪代码,没有得到任何看起来像过滤器的代码。


我的 pom.xml 文件是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>sbtest</artifactId>
    <version>0.1</version>
    <name>sbtest</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

【参考方案1】:

会不会是Tomcat或者Spring过滤了我的输入参数,所以这不起作用?

根据https://security.stackexchange.com/questions/172908/vulnerable-crlf-environment,对于某些版本的Tomcat,答案是肯定的。

事实上,可能对于所有版本的 Tomcat,因为 CVE-2012-3544 已修复1;即 6.0.36 之后的 Tomcat 6.x 和 7.0.29 之后的 7.x。

我一路追踪代码,没有得到任何看起来像过滤器的代码。

我做了一些代码挖掘。当 Tomcat 输出堆栈将响应标头值写入输出流时,它会将 ASCII 控制字符(TAB 除外)和 DEL 转换为空格。这是在AjpMessage 类的public void appendBytes(MessageBytes mb) 方法中完成的。 (可能还有其他地方。)

如何更改此代码以防 CRLF 注入?

我怀疑2你不能。至少 Tomcat 不会。


1 - CVE 指的是特定的 CRLF 注入攻击,该攻击是由 Tomcat 错误造成的,而不是由于 webapp 对响应标头的不安全使用。您尝试的注入将无法利用该漏洞。

2 - 我没有检查是否可以通过标头名称进行注入,但很难理解为什么普通的 webapp 需要使用请求参数值作为(或在)响应标头名称。

【讨论】:

感谢您的帖子。但是当我尝试在public void appendBytes(MessageBytes mb) 中断时,IDE 甚至没有停止。然后我尝试使用 IDE 的“强制介入”来查找过滤器代码。最后,我停在了private void write(MessageBytes mb)package org.apache.coyote.http11,班级Http11OutputBuffer 而且通过header name注入CRLF也是不可能的,同样的处理方式。 是的......好吧,就像我说的“可能还有其他地方”。【参考方案2】:

例如,Tomcat (9.0.37) 使用 Http11Processor 类来准备调用 Http11OutputBuffer 的响应(参见它的 prepareResponse() 方法)——参见 sendHeader() 方法的实现。它调用私有 write() 方法,用空格替换换行符。 Undertow 和 Jetty 也有自己的方法来清理标题。

【讨论】:

以上是关于如何更改此代码以使其容易受到 CRLF 注入的影响?的主要内容,如果未能解决你的问题,请参考以下文章

以下代码片段是不是容易受到 Rails 5 中 SQL 注入的影响?

这个 Python 代码是不是容易受到 SQL 注入的影响? (SQLite3)

Unity Web 游戏是不是容易受到跨站点脚本的影响

参数化的 java 持久性查询是不是容易受到 sql 注入的影响?

如何修复我的代码以使其自动化?

如何防止sql注入oracle apex