漏洞:Client ReDos From Regex Injection
漏洞描述:
掃描漏洞如下:
代碼:
// In IE6, the hash fragment and search params are incorrect if the ??? // fragment contains `?`. ??? getSearch: function() { ????? var match = this.location.href.replace(/#.*/, '').match(/\?.+/); ????? return match ? match[0] : ''; ??? }, |
// Update the hash location, either replacing the current entry, or adding ??? // a new one to the browser history. ??? _updateHash: function(location, fragment, replace) { ????? if (replace) { ??????? var href = location.href.replace(/(javascript:|#).*$/, ''); ??????? location.replace(href + '#' + fragment); ????? } else { ??????? // Some browsers require that `hash` contains a leading #. ??????? location.hash = '#' + fragment; ????? } ??? } |
ReDoS(Regularexpression Denial of Service)正則表達式拒絕服務攻擊。開發人員使用了正則表達式來對用戶輸入的數據進行有效性校驗,當編寫校驗的正則表達式存在缺陷或者不嚴謹時, 攻擊者可以構造特殊的字符串來大量消耗服務器的系統資源,造成服務器的服務中斷或停止。
每個惡意的正則表達式模式應該包含:使用重復分組構造、在重復組內會出現、重復、交替重疊。
有缺陷的正則表達式會包含如下部分。
(a+)+
([a-zA-Z]+)*
(a|aa)+
(a|a?)+
(.*a){x} | for x > 10
注意: 這里的a是個泛指。
一些實際業務場景中會用到的缺陷正則:
英文的個人名字 Java類名 Email格式驗證 多個郵箱地址驗證 復數驗證 模式匹配 使用python來進行測試有缺陷的正則示例 ? |
解決方案:
防范手段只能降低風險而不能百分百消除ReDoS這種威脅。
1.??降低正則表達式的復雜度, 盡量少用分組;
2.??嚴格限制用戶輸入的字符串長度(特定情況下)。
??? 對本次測試漏洞進行分析,似乎并不存在重復分組或嵌套分組的正則表達式,為了應對安全測試,建議解決方案如下:
方案1 | 使用{m, n}替代“*”、“+”等,限制匹配的字符數量,如: 將 var href = location.href.replace(/(javascript:|#).*$/, ''); 修改為 var href = location.href.replace(/(javascript:|#).{0, 1000}$/, ''); |
方案2 | 使用字符串截取函數slice()或substring(),替代replace()函數,如: 將 var href = location.href.replace(/(javascript:|#).*$/, ''); 修改為 var lochref = location.href; var idxnum = lochref.indexof(“javascript:”); if(idxnum == -1) idxnum = lochref.indexof(“#”); var href = idxnum == -1? lochref : lochref.slice(0, idxnum + 1); |
參考資料:
Regular expression Denial of Service - ReDoS?
淺析ReDoS的原理與實踐
Location 對象
正則表達式基礎
js字符串截取函數slice()、substring()、substr()
總結
以上是生活随笔為你收集整理的漏洞:Client ReDos From Regex Injection的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 民生网乐购分期信用卡额度一般是多少
- 下一篇: sharepoint的文件是怎样存放的及