baidu/openrasp

关于Dom4j Hook类方法的优化建议

xl1605368195 opened this issue · 2 comments

问题描述:

image

这里会匹配org.dom4j.io.SAXReader#read包括下面的8个,仅方法参数不一样

  1. public Document read(File file) throws DocumentException
  2. public Document read(URL url) throws DocumentException
  3. public Document read(String systemId) throws DocumentException
  4. public Document read(InputStream in) throws DocumentException
  5. public Document read(Reader reader) throws DocumentException
  6. public Document read(InputStream in, String systemId) throws DocumentException
  7. public Document read(Reader reader, String systemId) throws DocumentException
  8. public Document read(InputSource in) throws DocumentException

查看dom4j源码可知,前面的1~7个方法都会调用第8个方法(下面的红色框),列举其中一个的调用关系如下(其他6个都会调用方法8)
image

反编译hook后的类字节码,1-8方法均被插桩:
image

优化:

由于1-7方法都会调用方法8,并且真正处理xml的逻辑在方法8中,仅hook方法8即可,这样可以减少字节码、避免插桩代码2次调用。
(下图为方法8代码)
image

优化后代码:

@Override
protected void hookMethod(CtClass ctClass) throws IOException, CannotCompileException, NotFoundException {
    String src = getInvokeStaticSrc(DisableDom4jXxeEntity.class, "setFeature", "$0", Object.class);
    // insertBefore(ctClass, "read", null, src);
    // 仅匹配指定方法
    insertBefore(ctClass, "read", "(Lorg/xml/sax/InputSource;)Lorg/dom4j/Document;" , src);
}

源码位置:DisableDom4jXxeEntity.java#L44

如果错误,欢迎指正~~

厉害了!

@xl1605368195 你好,我看了下dom4j 1.X、2.X的代码,你这个修改应该没啥问题,可以提交个补丁