forked from ytisf/theZoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprep_file.py
84 lines (69 loc) · 2.54 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
#!/usr/bin/python
import os
import sys
import hashlib
try:
import pyminizip
except ImportError:
sys.stderr.write("Could not import 'pyminizip'. Did you install requirements?\n")
sys.stderr.write("You can always just get 'pyminizip' by 'pip install --user pyminizip'.\n")
sys.exit(1)
OUTPUT_FOLDER = "OUTPUT"
def _help():
"""
hmmmm. nope.
:return:
"""
print("Please run with '%s filename'." % sys.argv[0])
return
def _Do(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.
:param file_path: str
:return: Bool
"""
if not os.path.isfile(file_path):
_help()
sys.stderr.write("Seems like '%s' is not a file.\n" % file_path)
return False
try:
os.mkdir(OUTPUT_FOLDER)
except OSError:
sys.stderr.write("Folder exists. Please remove it before continuing.\n")
return False
if "\\" in file_path:
filename = file_path.split("\\")[:-1]
elif "/" in file_path:
filename = file_path.split("/")[:-1]
else:
filename = file_path
# Create ZIP Archive:
# We used 7z because 'zipfile' did not support adding a password. Apparently 'pyminizip' works just as well.
try:
pyminizip.compress(file_path, OUTPUT_FOLDER, "%s.zip" % filename, "infected", 9)
except Exception as e:
sys.stderr.write("Unknown error occurred. Please report this to us so that we can fix this.\n")
sys.stderr.write(str(e))
return False
compressed_path = '%s/%s.zip' % (OUTPUT_FOLDER, filename)
sys.stdout.write("[+]\tCreated ZIP Archive.\n")
md5sum = hashlib.md5(open(compressed_path, 'rb').read()).hexdigest()
sha1sum = hashlib.sha1(open(compressed_path, 'rb').read()).hexdigest()
open("%s/%s.md5" % (OUTPUT_FOLDER, filename), 'w').write(md5sum)
open("%s/%s.sha" % (OUTPUT_FOLDER, filename), 'w').write(sha1sum)
open("%s/%s.pass" % (OUTPUT_FOLDER, filename), 'w').write("infected")
return True
if __name__ == "__main__":
if len(sys.argv) != 2:
_help()
sys.exit(1)
stt = _Do(sys.argv[1])
if stt:
sys.stdout.write("Please don't forget to add details to 'conf/maldb.db' "
"and placing the folder in the appropriate directory.\n")
sys.stdout.write("Thanks for helping us get this accessible to everyone.\n")
sys.stdout.write("\n")
sys.exit(0)
else:
sys.exit(1)