SSTI

Velocity

SstiController.java
package com.example.controller;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.StringWriter;

@RestController
public class SstiController {

    @RequestMapping("/ssti/velocity")
    public String  Velocity(@RequestParam(name = "content") String content) {
        Velocity.init();
        VelocityContext velocityContext = new VelocityContext();

        velocityContext.put("username", "test");

        StringWriter stringWriter = new StringWriter();
        Velocity.evaluate(velocityContext, stringWriter, "test", content);
        return stringWriter.toString();
    }
}

启动计算器

修复代码

为了防止 SSTI 漏洞,我们应该对用户输入进行严格的输入验证和过滤,避免在模板引擎中直接使用用户输入。在使用 Velocity 模板引擎时,还可以配置 Velocity 引擎,禁止使用 Velocity 中危险的函数和类,从而增加应用程序的安全性。

FreeMarker

FreeMarker的攻击方式主要是利用模板本身的语法和功能。攻击者可以在FreeMarker模板文件中使用内置函数或变量来执行恶意代码,或者使用一些特殊的语法结构来绕过过滤或转义机制。

因此,攻击者通常会利用FreeMarker模板注入漏洞来上传或修改模板文件,以在后续的渲染中执行恶意代码。例如,攻击者可以上传一个包含恶意代码的FreeMarker模板文件,然后通过Web应用程序中的漏洞或访问控制不当等方式来触发渲染操作,从而导致恶意代码的执行。

这个示例中的FreeMarker模板文件存在注入漏洞。具体来说,它使用FreeMarker的内置方法freemarker.template.utility.Execute来执行系统命令,并将结果输出到页面中。攻击者可以利用这个漏洞,在ex()方法中注入恶意的命令来执行任意代码。

image-20230310161809365

最后更新于

这有帮助吗?