-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
prep_file.py
113 lines (89 loc) · 3.36 KB
/
prep_file.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Name: Prep File
Author: ytisf
Date of Creation: Unknown
Last Modified: April 05, 2024
Dev: K4YT3X
Last Modified: August 21, 2019
Licensed under the GNU General Public License Version 3 (GNU GPL v3),
available at: https://www.gnu.org/licenses/gpl-3.0.txt
(C) 2014-2019 ytisf
"""
# built-in imports
import sys
import time
import hashlib
import pathlib
import traceback
try:
import pyzipper
except ImportError as e:
print('Could not import "pyzipper". Did you install requirements?', file=sys.stderr)
print('You can always just get "pyzipper" by "pip install --user pyzipper"', file=sys.stderr)
raise e
COMPRESSION_PASSWORD = 'infected'
OUTPUT_FOLDER = pathlib.Path('OUTPUT')
def print_help():
""" print help message
print program help message and return None
"""
print(f'usage: {__file__} [INPUT_FILE]')
return
def prepare_file(file_path):
""" prep file from file path for submission
take file name, encrypt in ZIP with password 'infected', create MD5
and SHA1 sums and store all of that in a directory of it's own
Arguments:
file_path {pathlib.Path} -- path object of input file
"""
OUTPUT_FOLDER.mkdir(parents=True, exist_ok=True)
# create ZIP Archive
# we are using 7z because "zipfile" did not support adding a password
# Apparently "pyminizip" works just as well.
print('Info: Creating encrypted ZIP archive')
with pyzipper.AESZipFile(OUTPUT_FOLDER / f'{file_path.name}.zip', 'w', compression=pyzipper.ZIP_LZMA, encryption=pyzipper.WZ_AES) as zip_file:
zip_file.setpassword(COMPRESSION_PASSWORD.encode())
zip_file.write(file_path)
print('Info: Created ZIP archive')
# calculating file hashes
md5sum = hashlib.md5(open(OUTPUT_FOLDER / f'{file_path.name}.zip', 'rb').read()).hexdigest()
sha1sum = hashlib.sha1(open(OUTPUT_FOLDER / f'{file_path.name}.zip', 'rb').read()).hexdigest()
# writing file hashes and password to files
open(OUTPUT_FOLDER / f'{file_path.name}.md5', 'w').write(md5sum)
open(OUTPUT_FOLDER / f'{file_path.name}.sha', 'w').write(sha1sum)
open(OUTPUT_FOLDER / f'{file_path.name}.pass', 'w').write(COMPRESSION_PASSWORD)
# start timer
start_time = time.time()
# if this file is being imported
if __name__ != '__main__':
print('Error: This file cannot be imported', file=sys.stderr)
ImportError('File not importable')
# check if there's a right amount of arguments provided
if len(sys.argv) != 2:
print_help()
exit(1)
# convert input file path into file object
try:
input_file = pathlib.Path(sys.argv[1])
except Exception:
print('Error: input file format invalid', file=sys.stderr)
# input file validity check
if not input_file.is_file():
print_help()
print(f'Seems like {str(input_file)} is not a file', file=sys.stderr)
exit(1)
# zip file
try:
prepare_file(input_file)
except Exception:
print('Unexpected exception has been caught')
print('Compression has failed')
print('Please report the following error message to us so we can fix it')
traceback.print_exc()
exit(1)
print('Script finished')
print(f'Time taken: {round((time.time() - start_time), 5)} seconds')
print('Please don\'t forget to add details to "conf/maldb.db" and placing the folder in the appropriate directory')
print('Thanks for helping us to get this accessible to everyone')