找回密码
 立即注册
首页 业界区 安全 精准定位文件包含漏洞:代码审计中的实战思维 ...

精准定位文件包含漏洞:代码审计中的实战思维

别萧玉 4 天前
前言

最近看到由有分析梦想 CMS 的,然后也去搭建了一个环境看了一看,发现了一个文件包含漏洞的点,很有意思,下面是详细的复现和分析,以后代码审计又多了一中挖掘文件包含漏洞的新思路。
环境搭建

下载https://gitee.com/iteachyou/dreamer_cms
然后各种配置都可以看到
JDK:Jdk8IDE:Spring Tool Suite 4(STS)或 IntelliJ IDEA
DB:Mysql 5.7,Windows配置安装Mysql5.7,请参考:
https://www.iteachyou.cc/article/a1db138b4a89402ab50f3499edeb30c2
Redis:3.2+,Windows配置安装Redis教程,请参考:
https://www.iteachyou.cc/article/4b0a638f65fa4fb1b9644cf461dba602
修改一下配置文件中的 resource-path 和 mysql 的连接
漏洞寻找过程

[img=720,347.7857142857143]https://www.yijinglab.com/guide-img/d9634e2f-3b66-42e7-8279-c0877cdd70e5/1b5f9f04-8a04-4cf0-a4f1-d4ce1e4a735e.png[/img]

进入后台后发现可以编辑模板文件
然后这时候我们就需要注意常见的可以触发漏洞的点了
首先就是 xss
加入我们的 xss 代码
[img=720,291.85714285714283]https://www.yijinglab.com/guide-img/d9634e2f-3b66-42e7-8279-c0877cdd70e5/d03afb3b-7b3e-4d4a-bf7b-788b4a949ea5.png[/img]

然后访问首页
1.png

成功 xss,当然我们真实使用的话可以利用我们的 xss 平台
这里推荐与一个
https://xssaq.com/
[img=720,384.42857142857144]https://www.yijinglab.com/guide-img/d9634e2f-3b66-42e7-8279-c0877cdd70e5/e2e2a350-d513-49b3-96fe-ba98319d884f.png[/img]

点击配置代码后会有很多 payload,随便复制一个
这里我们简单弹个 cookie
[img=720,289.92857142857144]https://www.yijinglab.com/guide-img/d9634e2f-3b66-42e7-8279-c0877cdd70e5/1026dc5d-5fd3-44e7-a7cc-68aa35923f99.png[/img]

[img=720,427.5]https://www.yijinglab.com/guide-img/d9634e2f-3b66-42e7-8279-c0877cdd70e5/529b26a7-e824-4939-914f-4b0747a7471a.png[/img]

xss 的话还是国外使用比较多
当然这样并不能达到我们 getshell 的目的
【----帮助网安学习,以下所有学习资料免费领!加vx:YJ-2021-1,备注 “博客园” 获取!】
 ① 网安学习成长路径思维导图
 ② 60+网安经典常用工具包
 ③ 100+SRC漏洞分析报告
 ④ 150+网安攻防实战技术电子书
 ⑤ 最权威CISSP 认证考试指南+题库
 ⑥ 超1800页CTF实战技巧手册
 ⑦ 最新网安大厂面试题合集(含答案)
 ⑧ APP客户端安全检测指南(安卓+IOS)
漏洞深入利用

这里就不得不提到我们的一些标签了
一般常见的我们可以尝试 include 标签
这里我们先复现一下
[img=720,343.2857142857143]https://www.yijinglab.com/guide-img/d9634e2f-3b66-42e7-8279-c0877cdd70e5/06bdf4bc-5dd3-42cd-8b99-b413dd803c89.png[/img]

这个标签是 cms 的专属的标签
然后我们根据目录关系创建一个文件尝试一下
[img=720,557.9810725552051]https://www.yijinglab.com/guide-img/d9634e2f-3b66-42e7-8279-c0877cdd70e5/81629973-26df-4c49-90c3-baa09a5ee734.png[/img]

然后我们访问首页
[img=720,374.7857142857143]https://www.yijinglab.com/guide-img/d9634e2f-3b66-42e7-8279-c0877cdd70e5/e5b829ca-63f9-4ad7-9ef3-8a1c43fb79ac.png[/img]

可以看到成功了
调试分析

我们调试分析一下,抓个包看看路由
[img=720,600.4040404040404]https://www.yijinglab.com/guide-img/d9634e2f-3b66-42e7-8279-c0877cdd70e5/4c239b5d-9950-44cb-b32a-bc2f99a9bcf8.png[/img]

定位到代码
  1. @Log(operType = OperatorType.UPDATE, module = "模板管理", content = "修改模板")
  2. @PostMapping("save")
  3. @RequiresPermissions("5n6ta53y")
  4. public String save(TemplateVo template) throws IOException, CmsException {
  5.     String fileName = template.getPath() + File.separator + template.getFile();
  6.     File templateFile = new File(fileName);
  7.     /**
  8.      * 查询当前模版目录,判断是否为模版目录,如不是,则报错
  9.      */
  10.     Theme currentTheme = themeService.getCurrentTheme();
  11.     String resourceDir = fileConfiguration.getResourceDir();
  12.     String themePath = resourceDir + File.separator + "templates" + File.separator + currentTheme.getThemePath() + File.separator;
  13.     themePath = themePath.replaceAll("\\*", "/");
  14.     File themeDir = new File(themePath);
  15.     // 检查当前编辑文件是否有权限
  16.     if(!templateFile.getCanonicalPath().startsWith(themeDir.getCanonicalPath())) {
  17.         throw new TemplatePermissionDeniedException(StateCodeEnum.HTTP_FORBIDDEN.getCode(), StateCodeEnum.HTTP_FORBIDDEN.getDescription(), "您没有操作权限!");
  18.     }
  19.     if(!templateFile.exists()) {
  20.         throw new TemplateNotFoundException(StateCodeEnum.HTTP_NOTFOUND.getCode(), StateCodeEnum.HTTP_NOTFOUND.getDescription(), "模板文件不存在!");
  21.     }
  22.    
  23.     String filePath = template.getPath() + File.separator + template.getFile();
  24.     filePath = filePath.replaceAll("\\*", "/");
  25.     File file = new File(filePath);
  26.     FileUtils.writeStringToFile(file, template.getContent(), "UTF-8");
  27.     return "redirect:/admin/templates/toIndex";
  28. }
复制代码
这里就是就简单的写一下模板,然后重点关注解析的部分
[img=720,752.5056433408578]https://www.yijinglab.com/guide-img/d9634e2f-3b66-42e7-8279-c0877cdd70e5/56a93a2f-aacc-4219-a01a-5d3b19e6f81c.png[/img]

这里有各种各样的标签
看到我们的 Include 标签
  1. @Tag(beginTag="{dreamer-cms:include /}",endTag="{/dreamer-cms:include}",regexp="(\\{dreamer-cms:include[ \\t]+.*/\\})|(\\{dreamer-cms:include[ \\t]+.*\\}\\{/dreamer-cms:include\\})", attributes={
  2.     @Attribute(name = "file",regex = "[ \t]+file=["\'].*?["\']"),
  3. })
复制代码
格式
我们看一下解析过程
首先是解析我们的模板
[img=720,316.2857142857143]https://www.yijinglab.com/guide-img/d9634e2f-3b66-42e7-8279-c0877cdd70e5/ac74d25d-929a-4861-b73e-3667b7e4cfc8.png[/img]

识别我们的模板内容后开始解析各种标签,然后是我们的 include
  1. public String parse(String html) {
  2.     Tag annotations = IncludeTag.class.getAnnotation(Tag.class);
  3.     Attribute[] attributes = annotations.attributes();
  4.     List<String> all = RegexUtil.parseAll(html, annotations.regexp(), 0);
  5.     if(StringUtil.isBlank(all)) {
  6.         return html;
  7.     }
  8.     String newHtml = html;
  9.    
  10.     String resourceDir = fileConfiguration.getResourceDir() + "templates/";
  11.     Theme currentTheme = themeService.getCurrentTheme();
  12.     String templatePath = currentTheme.getThemePath() + "/";
  13.     String basePath = resourceDir + templatePath;
  14.     for (int i = 0; i < all.size(); i++) {
  15.         Map<String,Object> entity = new HashMap<String,Object>();
  16.         String includeTag = all.get(i);
  17.         for (Attribute attribute : attributes) {
  18.             String condition = RegexUtil.parseFirst(includeTag, attribute.regex(), 0);
  19.             if(StringUtil.isBlank(condition)) {
  20.                 continue;
  21.             }
  22.             String key = condition.split("=")[0];
  23.             String value = condition.split("=")[1];
  24.             key = key.trim();
  25.             value = value.replace(""", "").replace("\'", "");
  26.             entity.put(key, value);
  27.         }
  28.         
  29.         if(entity.keySet() != null && entity.keySet().size() > 0) {
  30.             String path = basePath + entity.get("file").toString();
  31.             File includeFile = new File(path);
  32.             String includeHtml;
  33.             try {
  34.                 includeHtml = FileUtils.readFileToString(includeFile, "UTF-8");
  35.                 newHtml = newHtml.replaceFirst(annotations.regexp(), includeHtml);
  36.             } catch (IOException e) {
  37.                 e.printStackTrace();
  38.             }
  39.         }
  40.     }
  41.     return newHtml;
  42. }
复制代码
[img=720,534.2142857142857]https://www.yijinglab.com/guide-img/d9634e2f-3b66-42e7-8279-c0877cdd70e5/be261a33-a92c-4c14-98c2-87a786782269.png[/img]

可以目录穿越读取我们任意文件的内容
更多网安技能的在线实操练习,请点击这里>>
  

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

16 小时前

举报

热心回复!
您需要登录后才可以回帖 登录 | 立即注册