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

前言

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

环境搭建

下载gitee.com/iteachyou/d...

然后各种配置都可以看到

JDK:Jdk8IDE:Spring Tool Suite 4(STS)或 IntelliJ IDEA

DB:Mysql 5.7,Windows配置安装Mysql5.7,请参考:

www.iteachyou.cc/article/a1d...

Redis:3.2+,Windows配置安装Redis教程,请参考:

www.iteachyou.cc/article/4b0...

修改一下配置文件中的 resource-path 和 mysql 的连接

漏洞寻找过程

进入后台后发现可以编辑模板文件

然后这时候我们就需要注意常见的可以触发漏洞的点了

首先就是 xss

加入我们的 xss 代码

然后访问首页

成功 xss,当然我们真实使用的话可以利用我们的 xss 平台

这里推荐与一个

xssaq.com/

点击配置代码后会有很多 payload,随便复制一个

这里我们简单弹个 cookie

xss 的话还是国外使用比较多

当然这样并不能达到我们 getshell 的目的

漏洞深入利用

这里就不得不提到我们的一些标签了

一般常见的我们可以尝试 include 标签

这里我们先复现一下

这个标签是 cms 的专属的标签

然后我们根据目录关系创建一个文件尝试一下

然后我们访问首页

可以看到成功了

调试分析

我们调试分析一下,抓个包看看路由

定位到代码

python 复制代码
@Log(operType = OperatorType.UPDATE, module = "模板管理", content = "修改模板")@PostMapping("save")@RequiresPermissions("5n6ta53y")public String save(TemplateVo template) throws IOException, CmsException {    String fileName = template.getPath() + File.separator + template.getFile();    File templateFile = new File(fileName);    /**     * 查询当前模版目录,判断是否为模版目录,如不是,则报错     */    Theme currentTheme = themeService.getCurrentTheme();    String resourceDir = fileConfiguration.getResourceDir();    String themePath = resourceDir + File.separator + "templates" + File.separator + currentTheme.getThemePath() + File.separator;    themePath = themePath.replaceAll("\\*", "/");    File themeDir = new File(themePath);    // 检查当前编辑文件是否有权限    if(!templateFile.getCanonicalPath().startsWith(themeDir.getCanonicalPath())) {        throw new TemplatePermissionDeniedException(StateCodeEnum.HTTP_FORBIDDEN.getCode(), StateCodeEnum.HTTP_FORBIDDEN.getDescription(), "您没有操作权限!");    }    if(!templateFile.exists()) {        throw new TemplateNotFoundException(StateCodeEnum.HTTP_NOTFOUND.getCode(), StateCodeEnum.HTTP_NOTFOUND.getDescription(), "模板文件不存在!");    }        String filePath = template.getPath() + File.separator + template.getFile();    filePath = filePath.replaceAll("\\*", "/");    File file = new File(filePath);    FileUtils.writeStringToFile(file, template.getContent(), "UTF-8");    return "redirect:/admin/templates/toIndex";}​

这里就是就简单的写一下模板,然后重点关注解析的部分

这里有各种各样的标签

看到我们的 Include 标签

swift 复制代码
@Tag(beginTag="{dreamer-cms:include /}",endTag="{/dreamer-cms:include}",regexp="(\\{dreamer-cms:include[ \\t]+.*/\\})|(\\{dreamer-cms:include[ \\t]+.*\\}\\{/dreamer-cms:include\\})", attributes={    @Attribute(name = "file",regex = "[ \t]+file=[\"\'].*?[\"\']"),})

格式

我们看一下解析过程

首先是解析我们的模板

识别我们的模板内容后开始解析各种标签,然后是我们的 include

scss 复制代码
public String parse(String html) {    Tag annotations = IncludeTag.class.getAnnotation(Tag.class);    Attribute[] attributes = annotations.attributes();    List<String> all = RegexUtil.parseAll(html, annotations.regexp(), 0);    if(StringUtil.isBlank(all)) {        return html;    }    String newHtml = html;        String resourceDir = fileConfiguration.getResourceDir() + "templates/";    Theme currentTheme = themeService.getCurrentTheme();    String templatePath = currentTheme.getThemePath() + "/";    String basePath = resourceDir + templatePath;    for (int i = 0; i < all.size(); i++) {        Map<String,Object> entity = new HashMap<String,Object>();        String includeTag = all.get(i);        for (Attribute attribute : attributes) {            String condition = RegexUtil.parseFirst(includeTag, attribute.regex(), 0);            if(StringUtil.isBlank(condition)) {                continue;            }            String key = condition.split("=")[0];            String value = condition.split("=")[1];            key = key.trim();            value = value.replace("\"", "").replace("\'", "");            entity.put(key, value);        }                if(entity.keySet() != null && entity.keySet().size() > 0) {            String path = basePath + entity.get("file").toString();            File includeFile = new File(path);            String includeHtml;            try {                includeHtml = FileUtils.readFileToString(includeFile, "UTF-8");                newHtml = newHtml.replaceFirst(annotations.regexp(), includeHtml);            } catch (IOException e) {                e.printStackTrace();            }        }    }    return newHtml;}

可以目录穿越读取我们任意文件的内容

相关推荐
豆豆2 天前
2026企业建站核心流程指南
cms·网站建设·网站制作·低代码平台·建站·网站管理系统·国产化改造
一次就好6026 天前
硬核干货:如何在运行的 PuTTY 中提取明文密码?从命令行到内存取证
linux·黑客
豆豆7 天前
2026年建设网站的十个步骤
大数据·cms·网站建设·网站制作·低代码平台·建站·网站设计
豆豆8 天前
主流的企业建站方式,sass云建站和自助建站系统怎么选择?
前端·css·低代码·cms·sass·低代码平台·站群
豆豆10 天前
2026年企业怎么做网站建设?
低代码·cms·网站建设·低代码平台·建站·创建网站·sso单点登录
hzp66613 天前
GhostCache 的新型缓存侧信道攻击
人工智能·黑客·网络攻击·ghostcache
AC赳赳老秦13 天前
使用PbootCMS制作网站如何免费做好防护
前端·数据库·黑客·网站建设·网站制作·防挂马·网站防黑
豆豆14 天前
常用的可实现站群管理的 CMS 系统有哪些?
cms·低代码平台·站群·高校网站·sso单点登录·站群cms·政务网站
豆豆15 天前
支持企业/政府/高校网站站群的cms内容管理系统有哪些
java·开发语言·cms·低代码平台·工单系统·sso单点登录·站群cms
豆豆17 天前
哪些cms网站内容管理系统支持lucene或Elasticsearch的全站全文检索功能
elasticsearch·全文检索·cms·lucene·低代码平台·单点登录·工单系统