views:

1033

answers:

8

I just read about zip bombs, i.e. zip files that contain very large amount of highly compressible data (00000000000000000...).

When opened they fill the server's disk.

How can I detect a zip file is a zip bomb before unzipping it?

UPDATE Can you tell me how is this done in Python or Java?

+3  A: 

If the ZIP decompressor you use can provide the data on original and compressed size you can use that data. Otherwise start unzipping and monitor the output size - if it grows too much cut it loose.

sharptooth
+5  A: 

Check a zip header first :)

FractalizeR
A: 

Make sure you are not using your system drive for temp storage. I am not sure if a virusscanner will check it if it encounters it.

Also you can look at the information inside the zip file and retrieve a list of the content. How to do this depends on the utility used to extract the file, so you need to provide more information here

Heiko Hatzfeld
A: 

Download files only from trusted sources.

Waleed Eissa
Might not protect against stupidity as it does against malice.
kaizer.se
Never assume malice, when stupidity is a simpler explanation.
voyager
By stupidity, do you refer to creating a zip bomb by accident? :) Personally, I mostly download from www.download.com and www.microsoft.com, I'm not so worried about zip bombs unless microsoft decides to play a practical joke on us, not very likely IMHO
Waleed Eissa
Microsoft doesn't have to "decide" anything. If one of their servers gets compromised, the attacker can have it start serving up zip bombs (or anything else) in place of its normal "trusted" content. Then you will be in trouble :^)
Jeremy Friesner
@Jeremy, are you trying to tell me that you check the headers of every zip file you download? Actually I never heard about zip bombs before I read this question but I'm not so worried about them as long as they don't delete or damage any data on my computer.
Waleed Eissa
Waleed, no I don't do that. My point was that assuming Microsoft.com==safe is just that, an assumption. Instead of having to make assumptions, a better solution would be an unzip unpacker that is aware of the possibility of "zip bombs" and takes steps to deal with them as gracefully as possible (e.g. by telling you in advance what the unzipped size of the archive will be... so you can know in advance if your 42KB .zip file is going to unpack to 300GB)
Jeremy Friesner
+11  A: 

Try this in Python:

import zipfile
z = zipfile.ZipFile('c:/a_zip_file')
print 'total files size=', sum(e.file_size for e in z.infolist())
z.close()
Nick D
At least with gzip I think the uncompressed size might not be in the header (so it might work with zip, but not with .tar.gz)
tonfa
@tonfa, thanks for mentioning that zipfile doesn't handle gnu zip format.
Nick D
IIRC, Zip standard (and let's face it, if you want to cause a DoS, you are necessarily going to follow standards) allows certain sizes to be elided from the central directory and entry headers.
Tom Hawtin - tackline
The most famous zip bomb will pass this test because the first level is not very big. You need to check ZIP depth (ZIP inside ZIP) also.
ZZ Coder
@ZZ Coder, hmm that's true. Tom Hawtin - tackline's solution is better in case you decompress all levels at once.
Nick D
+4  A: 

Reading over the description on Wikipedia -

Deny any compressed files that contain compressed files.
     Use ZipFile.entries() to retrieve a list of files, then ZipEntry.getName() to find the file extension.
Deny any compressed files that contain files over a set size, or the size can not be determined at startup.
     While iterating over the files use ZipEntry.getSize() to retrieve the file size.

mlk
+1  A: 

Don't allow the upload process to write enough data to fill up the disk, ie solve the problem, not just one possible cause of the problem.

Pete Kirkham
+8  A: 

Zip is, erm, an "interesting" format. A robust solution is to stream the data out, and stop when you have had enough. In Java, use ZipInputStream rather than ZipFile. The latter also requires you to store the data in a temporary file, which is also not the greatest of ideas.

Tom Hawtin - tackline