Thursday, 31 March 2011
Common Unzip Pitfall
A few weeks ago, I investigated a customer report of a false positive. The issue was a Path Manipulation found in a web application that allows users to upload zip files. The program validates the uploaded filename, ensures it is alphanumeric and ends with “.zip”. The program then opens the zip file and unzips everything into a specific folder. Here is some representative code:
ZipInputStream zin = new ZipInputStream(...);
while((entry = zin.getNextEntry()) != null) {
// SCA reports Path Manipulation in the following line
File file = new File(dir, entry.getName());
}
It took me just 10 minutes to write a test program to prove to the customer the file name stored in ZipEntry is an unvalidated string and can contain anything, including "../". The reported Path Manipulation is a true positive because an attacker can cause a file to be written to an arbitrary location.
And then I realized he would not be the only developer who believes ZipEntry is “safe”. I did some simple tests with the following zip files and unzip implementations:
- Test case1: a zip file with a dot-dot-slash entry "../abc.def"
- Test case2: a zip file with Unicode dot-dot-slash entry "%C0%AE%C0%AE/abc.def"
- Test case3: a zip file with an absolute entry name "/abc.def"
- Test case4: a zip file with a special character between dot-dot ".%03./abc.def"
| Name* | Vulnerable? | Comment |
|---|---|---|
| 7zip 9.2 | No | It will strip “../” and continue to run without error |
| WinXP SP2 Built-in Uncompress | No | It will prompt error message and stop |
| WinRAR 3.71 | No | It will strip “../” and continue to run without error |
| Win32 unzip 5.42 | YES | |
| Linux unzip 5.52 | No | It will strip “../” and display a warning message |
| JDK 1.6.0_23 "jar" | YES | |
| Tomcat 5.0, 5.5, 6.0 | YES | |
| Tomcat 7.0.8 | No | It will throw exception |
For Tomcat, I created a WAR file with an entry “../ROOT/index.html” and successfully overwrote the ROOT home page in Tomcat 5.0, 5.5 and 6.0. However, Tomcat 7 is not vulnerable and will throw an exception. But even if you are running a vulnerable version of Tomcat, you probably don’t need to rush to upgrade to Tomcat 7 unless you are running a J2EE web hosting service. If anyone can deploy an application to your Tomcat server, you have a bigger problem than Path Manipulation.







