2

老版本的Spring应用该如何应对CVE-2022-22965漏洞? - 程序猿DD

 2 years ago
source link: https://www.cnblogs.com/didispace/p/16092002.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

昨天,在发布了《Spring官宣承认网传大漏洞,并提供解决方案》之后。群里就有几个小伙伴问了这样的问题:我们的Spring版本比较老,该怎么办?这是一个好问题,所以DD今天单独拿出来说说。

这次的RCE漏洞宣布之后,官方给出的主要解决方案是升级版本,但只有Spring 5.2、5.3和Spring Boot 2.5、2.6提供了对应的升级版本。

那么对于一些还在用Spring 5.0、5.1甚至Spring 4.x、或者Spring Boot 1.x和Spring 2.4及以下版本的用户该怎么办呢?

第一种方法

官方给出过一种通过扩展RequestMappingHandlerAdapter来实现的方法。同时也给出了一个Spring Boot下使用Spring MVC的实现方案,如果是WebFlux的话略做修改即可。但如果不是Spring Boot的话,则Bean的初始化方式还要再改改。

@SpringBootApplication
public class MyApp {

 public static void main(String[] args) {
  SpringApplication.run(CarApp.class, args);
 }


 @Bean
 public WebMvcRegistrations mvcRegistrations() {
  return new WebMvcRegistrations() {
   @Override
   public RequestMappingHandlerAdapter getRequestMappingHandlerAdapter() {
    return new ExtendedRequestMappingHandlerAdapter();
   }
  };
 }


 private static class ExtendedRequestMappingHandlerAdapter extends RequestMappingHandlerAdapter {

  @Override
  protected InitBinderDataBinderFactory createDataBinderFactory(List<InvocableHandlerMethod> methods) {

   return new ServletRequestDataBinderFactory(methods, getWebBindingInitializer()) {

    @Override
    protected ServletRequestDataBinder createBinderInstance(
      Object target, String name, NativeWebRequest request) throws Exception {
     
     ServletRequestDataBinder binder = super.createBinderInstance(target, name, request);
     String[] fields = binder.getDisallowedFields();
     List<String> fieldList = new ArrayList<>(fields != null ? Arrays.asList(fields) : Collections.emptyList());
     fieldList.addAll(Arrays.asList("class.*", "Class.*", "*.class.*", "*.Class.*"));
     binder.setDisallowedFields(fieldList.toArray(new String[] {}));
     return binder;
    }
   };
  }
 }
}

这种需要我们去修改代码,其实我觉得还是有点麻烦的。如果对Spring机制不太熟悉的话,可能还会遇到不少麻烦。下面讲讲另外的便捷方法,也是我对老项目推荐的方法。

第二种方法

下面要讲的方法主要是规避的思路。什么是规避呢?就是针对该漏洞的利用条件去做一些调整。

比如,这次漏洞的条件是这些:

  • JDK 9 +
  • 使用Apache Tomcat部署
  • 使用WAR方式打包
  • 依赖spring-webmvc或spring-webflux

那么我就可以选择规避其中的1个条件就能防止漏洞的利用了,比如:

  • 降级到JDK 8
  • 使用Undertow来部署
  • 如果是Spring Boot的早期项的话,还能调整打包方式,采用JAR的方式打包和运行来规避。

另外,DD有注意到,这次漏洞之后Tomcat的版本也更新了,所以当你用WAR部署的情况下,可以直接下载最新的Tomcat版本来规避也是一种不错的选择。

好了,今天的分享就到这里,解决群友(点击加群)的疑问是一方面,另一方面也是给大家讲讲解决问题时候的一种思考方式。有时候碰到硬茬,我们不一定要硬刚,换个方向解决可能性价比更高。如果您觉得今天的分享还不错,欢迎点赞、在看、转发到朋友圈。

欢迎关注我的公众号:程序猿DD。第一时间了解前沿行业消息、分享深度技术干货、获取优质学习资源


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK