Last active
August 29, 2015 14:03
-
-
Save sirtony/6651b493f02b8e751230 to your computer and use it in GitHub Desktop.
Calculates the theoretical uncompressed size of a zip bomb.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Call with `python.exe bomb_size.py --noc` | |
#to suppress the thousands separator. | |
from sys import argv | |
suppressComma = len( argv ) >= 2 and argv[1] == "--noc" | |
def humanSize( size, places = 2, lower = False ): | |
units = ( "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ) | |
index = 0 | |
newSize = size | |
while newSize >= 1024 and index < len( units ) - 1: | |
newSize /= 1024 | |
index += 1 | |
unit = units[index].lower() if lower is True else units[index] | |
return "{0:,}{1}".format( newSize, unit ) if not suppressComma else "{0}{1}".format( newSize, unit ) | |
#You may edit the values of 'levels', 'zipsPerLevel', and 'payloadSize' | |
#to anything you want. Variable names self-explanatory. | |
levels = 100000 | |
zipsPerLevel = 100000 | |
payloadSize = 500 * 1024 * 1024 #This is the size, in bytes, of the junk file. | |
#Initial value = 500.0MB | |
payloadCount = zipsPerLevel ** levels | |
size = payloadCount * payloadSize | |
digits = len( str( size ) ) | |
human = humanSize( size ) | |
bytes = "{:,}".format( size ) if not suppressComma else str( size ) | |
print( "Digits (of size in bytes): %u" % digits ) | |
if len( human ) > 100: | |
with open( "size.txt", "w" ) as file: | |
file.write( human ) | |
file.flush() | |
print( "Full size written to size.txt" ) | |
else: | |
print( "Size: %s" % human ) | |
if digits > 100: | |
with open( "bytes.txt", "w" ) as file: | |
file.write( bytes ) | |
file.flush() | |
print( "Full size in bytes written to bytes.txt." ) | |
else: | |
print( "Size in bytes: %s" % bytes ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment