1
1
import hashlib
2
2
import logging
3
3
import os
4
+ import re
4
5
import shutil
5
6
import subprocess
6
7
from pathlib import Path
7
8
from zipfile import BadZipfile , ZipFile
8
9
9
- try :
10
- import re2 as re
11
- except ImportError :
12
- import re
13
-
14
10
from lib .common .constants import OPT_MULTI_PASSWORD
15
11
from lib .common .exceptions import CuckooPackageError
16
12
from lib .common .hashing import hash_file
@@ -61,7 +57,7 @@ def extract_archive(seven_zip_path, archive_path, extract_path, password="infect
61
57
stdout = subprocess .PIPE ,
62
58
)
63
59
stdoutput , stderr = p .stdout , p .stderr
64
- log .debug (f" { p .stdout } { p .stderr } " )
60
+ log .debug ("%s %s" , p .stdout , p .stderr )
65
61
66
62
if try_multiple_passwords :
67
63
passwords = password .split (":" )
@@ -85,9 +81,9 @@ def extract_archive(seven_zip_path, archive_path, extract_path, password="infect
85
81
stdout = subprocess .PIPE ,
86
82
)
87
83
stdoutput , stderr = p .stdout , p .stderr
88
- log .debug (f" { p .stdout } { p .stderr } " )
84
+ log .debug ("%s - %s" , p .stdout , p .stderr )
89
85
if b"Wrong password" in stderr :
90
- log .debug (f "The provided password '{ pword } ' was incorrect" )
86
+ log .debug ("The provided password '%s ' was incorrect" , str ( pword ) )
91
87
continue
92
88
else :
93
89
# We did it!
@@ -196,15 +192,15 @@ def extract_zip(zip_path, extract_path, password=b"infected", recursion_depth=1,
196
192
raise CuckooPackageError ("Invalid Zip file" ) from e
197
193
except RuntimeError as e :
198
194
if "Bad password for file" in repr (e ):
199
- log .debug (f "Password '{ pword } ' was unsuccessful in extracting the archive." )
195
+ log .debug ("Password '%s ' was unsuccessful in extracting the archive." , str ( pword ) )
200
196
password_fail = True
201
197
continue
202
198
else :
203
199
# Try twice, just for kicks
204
200
try :
205
201
archive .extractall (path = extract_path , pwd = pword )
206
202
except RuntimeError as e :
207
- raise CuckooPackageError (f "Unable to extract Zip file: { e } " ) from e
203
+ raise CuckooPackageError ("Unable to extract Zip file: %s" , str ( e ) ) from e
208
204
finally :
209
205
if recursion_depth < 4 :
210
206
# Extract nested archives.
@@ -228,7 +224,7 @@ def extract_zip(zip_path, extract_path, password=b"infected", recursion_depth=1,
228
224
log .error ("Error extracting nested Zip file %s with details: %s" , name , run_err )
229
225
230
226
if password_fail :
231
- raise CuckooPackageError (f "Unable to extract password-protected Zip file with the password(s): { passwords } " )
227
+ raise CuckooPackageError ("Unable to extract password-protected Zip file with the password(s): %s" , str ( passwords ) )
232
228
233
229
234
230
def is_overwritten (zip_path ):
@@ -265,7 +261,7 @@ def winrar_extractor(winrar_binary, extract_path, archive_path):
265
261
stdout = subprocess .PIPE ,
266
262
)
267
263
# stdoutput, stderr = p.stdout, p.stderr
268
- log .debug (p .stdout + p .stderr )
264
+ log .debug ("%s - %s" , p .stdout , p .stderr )
269
265
270
266
return os .listdir (extract_path )
271
267
@@ -290,11 +286,11 @@ def upload_extracted_files(root, files_at_root):
290
286
for entry in files_at_root :
291
287
try :
292
288
file_path = os .path .join (root , entry )
293
- log .info ("Uploading {0} to host" . format (file_path ))
289
+ log .info ("Uploading %s to host" , str (file_path ))
294
290
filename = f"files/{ hash_file (hashlib .sha256 , file_path )} "
295
291
upload_to_host (file_path , filename , metadata = Path (entry ).name , duplicated = False )
296
292
except Exception as e :
297
- log .warning (f "Couldn't upload file { Path (entry ).name } to host { e } " )
293
+ log .warning ("Couldn't upload file %s to host %s" , str ( Path (entry ).name ), str ( e ) )
298
294
299
295
300
296
def attempt_multiple_passwords (options : dict , password : str ) -> bool :
0 commit comments