This creates a zip bomb with a programmable recursion depth. It's useful when you want to create a very deep zip archive for testing decompression software, virus scanners and so on. Recursion depth is limited by Python itself, so very high values are probably not going to work. The algorithm could be re-structured to avoid recursion, but I've never needed a nesting depth of more than 900!
Each zip file will get the name stack<n>.zip where <n> is the number of zip files inside each layer. The data file at the core is called needle.txt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import os, zipfile, StringIO def GetZipWith(fname, content): fp = StringIO.StringIO() zf = zipfile.ZipFile(fp, "a", zipfile.ZIP_DEFLATED, False) zf.writestr(fname, content) zf.close() return fp.getvalue() def CreateRecursiveZip(stack, fname, content): if stack: zipname = stack.pop() arcname, outer = CreateRecursiveZip(stack, fname, content) print "Adding '%s' to zipfile '%s' (%d bytes)" % (arcname, zipname, len(outer)) return zipname, GetZipWith(zipname, outer) else: print "Creating '%s'" % fname return fname, GetZipWith(fname, content) if __name__ == "__main__": depth = 900 stack = ["stack%d.zip" % i for i in xrange(1,depth)] arcname, out = CreateRecursiveZip(stack, "needle.txt", "my data") print "Writing zip bomb to '%s'" % arcname file(arcname, "wb").write(out)