-
Notifications
You must be signed in to change notification settings - Fork 116
/
scan_encrypted_zip.py
190 lines (160 loc) · 7.07 KB
/
scan_encrypted_zip.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import io
import os
import subprocess
import tempfile
import zlib
import pyzipper
from strelka import strelka
def crack_zip(
self,
data,
jtr_path,
tmp_dir,
password_file,
brute=False,
max_length=10,
scanner_timeout=150,
):
try:
with tempfile.NamedTemporaryFile(dir=tmp_dir, mode="wb") as tmp_data:
tmp_data.write(data)
tmp_data.flush()
(zip2john, stderr) = subprocess.Popen(
[jtr_path + "zip2john", tmp_data.name],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
).communicate()
if not zip2john:
self.flags.append("zip2john_error")
return
with tempfile.NamedTemporaryFile(dir=tmp_dir) as tmp_data:
tmp_data.write(zip2john)
tmp_data.flush()
(stdout, stderr) = subprocess.Popen(
[jtr_path + "john", "--show", tmp_data.name],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
).communicate()
if not stdout:
self.flags.append("jtr_show_error")
return
if b"0 password hashes cracked" in stdout:
with tempfile.NamedTemporaryFile(dir=tmp_dir) as tmp_data:
tmp_data.write(zip2john)
tmp_data.flush()
if os.path.isfile(password_file):
(stdout, stderr) = subprocess.Popen(
[jtr_path + "john", f"-w={password_file}", tmp_data.name],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
).communicate(timeout=scanner_timeout)
# ZipCrypto
if b"PKZIP" in stdout.split(b"\n")[0]:
if stdout.split(b"\n")[1]:
self.flags.append("cracked_by_wordlist")
return stdout.split(b"\n")[1].split()[0]
# WinZip AES
elif b"WinZip" in stdout.split(b"\n")[0]:
if stdout.split(b"\n")[2]:
self.flags.append("cracked_by_wordlist")
return stdout.split(b"\n")[2].split()[0]
if brute:
(stdout, stderr) = subprocess.Popen(
[
jtr_path + "john",
"--incremental=Alnum",
f"--max-length={max_length}",
f"--max-run-time={scanner_timeout}",
tmp_data.name,
],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
).communicate(timeout=scanner_timeout)
if stdout.split(b"\n")[1]:
self.flags.append("cracked_by_incremental")
return stdout.split(b"\n")[1].split()[0]
return ""
else:
return stdout.split(b":")[1]
except strelka.ScannerTimeout:
raise
except Exception as e:
self.flags.append(str(e))
return ""
class ScanEncryptedZip(strelka.Scanner):
"""Extracts passwords from encrypted ZIP archives.
Attributes:
passwords: List of passwords to use when bruteforcing encrypted files.
Options:
limit: Maximum number of files to extract.
Defaults to 1000.
password_file: Location of passwords file for zip archives.
Defaults to /etc/strelka/passwords.dat.
"""
def scan(self, data, file, options, expire_at):
jtr_path = options.get("jtr_path", "/jtr/")
tmp_directory = options.get("tmp_file_directory", "/tmp/")
file_limit = options.get("limit", 1000)
password_file = options.get("password_file", "/etc/strelka/passwords.dat")
log_extracted_pws = options.get("log_pws", False)
scanner_timeout = options.get("scanner_timeout", 150)
brute = options.get("brute_force", False)
max_length = options.get("max_length", 5)
self.event["total"] = {"files": 0, "extracted": 0}
with io.BytesIO(data) as zip_io:
try:
is_aes = False
with pyzipper.ZipFile(zip_io) as zip_obj:
file_list = zip_obj.filelist # .filelist
for file_list_item in file_list:
if not file_list_item.is_dir():
# Check for the AES compression type
if file_list_item.compress_type == 99:
is_aes = True
break
with (
pyzipper.AESZipFile(zip_io) if is_aes else pyzipper.ZipFile(zip_io)
) as zip_obj:
file_list = zip_obj.filelist # .filelist
for file_list_item in file_list:
if not file_list_item.is_dir():
self.event["total"]["files"] += 1
extracted_pw = crack_zip(
self,
data,
jtr_path,
tmp_directory,
brute=brute,
scanner_timeout=scanner_timeout,
max_length=max_length,
password_file=password_file,
)
if not extracted_pw:
self.flags.append("Could not extract password")
return
if log_extracted_pws:
self.event["cracked_password"] = extracted_pw
for file_item in file_list:
if not file_item.is_dir():
if self.event["total"]["extracted"] >= file_limit:
break
try:
extract_data = zip_obj.read(
file_item.filename, pwd=extracted_pw
)
if extract_data:
# Send extracted file back to Strelka
self.emit_file(
extract_data, name=file_item.filename
)
self.event["total"]["extracted"] += 1
except NotImplementedError:
self.flags.append("unsupported_compression")
except RuntimeError:
self.flags.append("runtime_error")
except ValueError:
self.flags.append("value_error")
except zlib.error:
self.flags.append("zlib_error")
except pyzipper.BadZipFile:
self.flags.append("bad_zip")