什么是CRLF注入漏洞?
CRLF是\r\n的简称,也就是‘回车换行’。它们的URL编码是%0d和%0a。
那么它是怎么产生漏洞的呢?
先普及一个知识:在HTTP协议中,header之间是用一个CRLF分隔的。Body是用两个CRLF分隔的。浏览器就是根据这些CRLF解析header和Body并显示页面。如果我们能够控制header中的值,注入恶意的CRLF,那么就可以在其中注入一些恶意的cookie或者js执行代码。
如何解决
- 从用户输入控制:在所有用户输入的header或者自定义header中,清除不必要的CRLF
- 从服务器返回控制:在服务器端返回的header中,检查CRLF,清除CRLF。
Nginx例子
location / {
return 302 https://example.com$uri;
}
如果发送请求:
curl -v "http://127.0.0.1:8080/example/%0d%0abadheader:%20oops"
* Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /example/%0d%0abadheader:%20oops HTTP/1.1
> Host: 127.0.0.1:8081
> User-Agent: curl/7.74.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 302 Moved Temporarily
< Server: nginx/1.19.8
< Date: Wed, 22 Mar 2022 09:05:45 GMT
< Content-Type: text/html
< Content-Length: 145
< Connection: keep-alive
< Location: http://172.17.2.100/example/
< badheader: oops
<
<html>
<head><title>302 Found</title></head>
<body>
<center><h1>302 Found</h1></center>
<hr><center>nginx/1.19.8</center>
</body>
</html>
* Connection #0 to host 127.0.0.1 left intact
nginx中的request_uri 是请求的完整的uri带参数,且没有decode编码,所以不回产生此问题。
location / {
return 302 https://example.com$request_uri;
}
» 转载保留版权:猿码设计师 » 《Web应用如何解决CRLF注入漏洞》
» 本文链接地址:Web应用如何解决CRLF注入漏洞 - 猿码设计师
» 如果喜欢可以:关注《猿码设计师》公众号