应用于ASP文件上传漏洞的0×00截断***
kingbase

2011/8/1

中国人的简单叙述
这篇文章翻译自:0×00 vs ASP file uploads,原作者是:Brett Moore,Security-Assessment.com公司的,本文写于04年,原文在这里:http:// www.security-assessment.com/Whitepapers/0×00_vs_ASP_File_Uploads.pdf,我之所以翻译它是因为一直以来我对这个0×00有疑惑,感觉有时能用有时又不能用,看了这篇文章,有点明白了。

说白了一句话:只有form post时有用,而且只针对文件名变量,作用于asp的CreateTextFile等方法。
简介
Poison NULL byte技术还没有在ASP中广泛应用,but as with other languages the NULL byte can cause problems when ASP passes data to objects.

用ASP写的上传,有个共性的问题:空字节可以被插入到文件名,这样文件名可以被添加任意扩展名,而写入文件的时候,空字节以后的部分都会被忽略掉。

这样,我们就可以绕过一些针对扩展名的检查,even if one is appended by the application.

这很类似于Perl/PHP,不同在于这个空字节是怎么发到ASP程序的。

这种情况发生于:数据在没有检查空字节的情况下,比较、验证后,又直接被传入FileSystemObject时。

本文将讨论Poison空字节***如何作用于ASP的上传脚本。

Scope
本文是基于以下两点研究:upload时的POST(multipart/form-data)、Scripting.FileSystemObject对象。

本文中,我们集中关注于CreateTextFile方法,它被用于对文件写入。可能其他的object function也有这样的漏洞。

%00或者空字节在URL或者通常的form post中发不出去,因为服务器虽然会认为这是字符串的结果但是并不会在文件名变量中存储它的值。

而当文件名通过multipart/form-data的形式发送时,空字节将会保存在文件名变量中,这会影响对FileSystemObject的调用。

文件上传
文件上传通常是用输入对象类型file和编码类型multipart/form-data。

content type application/x-www-form-urlencoded不太适合发送大量的二进制数据或者非ASCII字符,multipart/form-data则比较适合。

一个multipart/form-data消息包含了好几个部分,The parts are sent to the processing agent in the same order the corresponding controls appear in the document stream.

<form method=post enctype=”multipart/form-data” action=upload.asp>

        Your Picture:<BR><input><BR><BR>

        <input value=”Upload”>

</form>

当form submit时,数据会被安排成multipart/form-data格式,这会把post的数据进行编码,这些数据中当然也包含空字节。

文件保存
上传成功后,文件会保存在一个地方,下面的代码就是通常的文件上传处理代码:

Sub Save() Sub Save(filename)

         Dim objFSO, objFSOFile

        path=server.MapPath(“/uploads/”)

         Set objFSO = Server.CreateObject(“Scripting.FileSystemObject”)

         Set objFSOFile = objFSO.CreateTextFile(path + “\” + filename)

        objFSOFile.Write <file contents>

        objFSOFile.Close

End Sub

当文件名参数传到CreateTextFile函数,文件名中可能会包含空字节。这会使得创建的文件名被截断,空字节后被忽略。

Set objFSOFile = objFSO.CreateTextFile(path + “\” + filename)

空字节
空字节的插入,可以使用手工,也可以使用16进制编辑器或者web代理程序,它的插入需要修改multipart post数据。

Multipart Form Post

POST /upload.asp HTTP/1.0

Content-Type: multipart/form-data; boundary=—————————

7d4cb161b009c

Host: localhost

Content-Length: 359

Pragma: no-cache

Cookie: ASPSESSIONIDSAADRCRS=LAKNNAKAGMIBJCOOLBIFEHIK

—————————–7d4cb161b009c

Content-Disposition: form-data;; filename=”c:\nc.exe .bmp”

Content-Type: text/plain

Proof Of Upload Test File

brett.moore@security-assessment.com

—————————–7d4cb161b009c

Content-Disposition: form-data;

Upload

—————————–7d4cb161b009c

注意,上面的文件名参数已经被更改了: