Commit 5034a62de51ad7dfbfa5d3f292029c4c7949022d

Authored by Federico Fantini
1 parent ccf99d1a

changed decrypted_filepath name to decrypted_dir

oletools/crypto.py
@@ -33,7 +33,7 @@ potentially encrypted files:: @@ -33,7 +33,7 @@ potentially encrypted files::
33 raise crypto.MaxCryptoNestingReached(crypto_nesting, filename) 33 raise crypto.MaxCryptoNestingReached(crypto_nesting, filename)
34 decrypted_file = None 34 decrypted_file = None
35 try: 35 try:
36 - decrypted_file, correct_password = crypto.decrypt(input_file, passwords, decrypted_filepath) 36 + decrypted_file, correct_password = crypto.decrypt(input_file, passwords, decrypted_dir)
37 if decrypted_file is None: 37 if decrypted_file is None:
38 raise crypto.WrongEncryptionPassword(input_file) 38 raise crypto.WrongEncryptionPassword(input_file)
39 # might still be encrypted, so call this again recursively 39 # might still be encrypted, so call this again recursively
@@ -315,7 +315,7 @@ def check_msoffcrypto(): @@ -315,7 +315,7 @@ def check_msoffcrypto():
315 return msoffcrypto is not None 315 return msoffcrypto is not None
316 316
317 317
318 -def decrypt(filename, passwords=None, decrypted_filepath=None, **temp_file_args): 318 +def decrypt(filename, passwords=None, decrypted_dir=None, **temp_file_args):
319 """ 319 """
320 Try to decrypt an encrypted file 320 Try to decrypt an encrypted file
321 321
@@ -332,8 +332,8 @@ def decrypt(filename, passwords=None, decrypted_filepath=None, **temp_file_args) @@ -332,8 +332,8 @@ def decrypt(filename, passwords=None, decrypted_filepath=None, **temp_file_args)
332 `dirname` or `prefix`. `suffix` will default to 332 `dirname` or `prefix`. `suffix` will default to
333 suffix of input `filename`, `prefix` defaults to 333 suffix of input `filename`, `prefix` defaults to
334 `oletools-decrypt-`; `text` will be ignored 334 `oletools-decrypt-`; `text` will be ignored
335 - :param decrypted_filepath: filepath of the decrypted file in case you want to  
336 - preserve it 335 + :param decrypted_dir: folder to store the decrypted file in case you want
  336 + to preserve it
337 :returns: a tuple with the name of the decrypted temporary file (type str) or `None` 337 :returns: a tuple with the name of the decrypted temporary file (type str) or `None`
338 and the correct password or 'None' 338 and the correct password or 'None'
339 :raises: :py:class:`ImportError` if :py:mod:`msoffcrypto-tools` not found 339 :raises: :py:class:`ImportError` if :py:mod:`msoffcrypto-tools` not found
@@ -409,10 +409,10 @@ def decrypt(filename, passwords=None, decrypted_filepath=None, **temp_file_args) @@ -409,10 +409,10 @@ def decrypt(filename, passwords=None, decrypted_filepath=None, **temp_file_args)
409 409
410 if decrypt_file and correct_password: 410 if decrypt_file and correct_password:
411 log.debug(f'Successfully decrypted the file with password: {correct_password}') 411 log.debug(f'Successfully decrypted the file with password: {correct_password}')
412 - if decrypted_filepath:  
413 - if os.path.isdir(decrypted_filepath) and os.access(decrypted_filepath, os.W_OK):  
414 - log.info(f"Saving decrypted file in: {decrypted_filepath}")  
415 - shutil.copy(decrypt_file, decrypted_filepath) 412 + if decrypted_dir:
  413 + if os.path.isdir(decrypted_dir) and os.access(decrypted_dir, os.W_OK):
  414 + log.info(f"Saving decrypted file in: {decrypted_dir}")
  415 + shutil.copy(decrypt_file, decrypted_dir)
416 else: 416 else:
417 log.info('All passwords failed') 417 log.info('All passwords failed')
418 418
oletools/msodde.py
@@ -271,9 +271,9 @@ def process_args(cmd_line_args=None): @@ -271,9 +271,9 @@ def process_args(cmd_line_args=None):
271 parser.add_argument("-p", "--password", type=str, action='append', 271 parser.add_argument("-p", "--password", type=str, action='append',
272 help='if encrypted office files are encountered, try ' 272 help='if encrypted office files are encountered, try '
273 'decryption with this password. May be repeated.') 273 'decryption with this password. May be repeated.')
274 - parser.add_argument("--decrypted_filepath", dest='decrypted_filepath', type=str, 274 + parser.add_argument("--decrypted_dir", dest='decrypted_dir', type=str,
275 default=None, 275 default=None,
276 - help='save the decrypted file to this location.') 276 + help='store the decrypted file to this folder.')
277 filter_group = parser.add_argument_group( 277 filter_group = parser.add_argument_group(
278 title='Filter which OpenXML field commands are returned', 278 title='Filter which OpenXML field commands are returned',
279 description='Only applies to OpenXML (e.g. docx) and rtf, not to OLE ' 279 description='Only applies to OpenXML (e.g. docx) and rtf, not to OLE '
@@ -913,7 +913,7 @@ def process_file(filepath, field_filter_mode=None): @@ -913,7 +913,7 @@ def process_file(filepath, field_filter_mode=None):
913 # === MAIN ================================================================= 913 # === MAIN =================================================================
914 914
915 915
916 -def process_maybe_encrypted(filepath, passwords=None, decrypted_filepath=None, crypto_nesting=0, 916 +def process_maybe_encrypted(filepath, passwords=None, decrypted_dir=None, crypto_nesting=0,
917 **kwargs): 917 **kwargs):
918 """ 918 """
919 Process a file that might be encrypted. 919 Process a file that might be encrypted.
@@ -924,8 +924,8 @@ def process_maybe_encrypted(filepath, passwords=None, decrypted_filepath=None, c @@ -924,8 +924,8 @@ def process_maybe_encrypted(filepath, passwords=None, decrypted_filepath=None, c
924 924
925 :param str filepath: path to file on disc. 925 :param str filepath: path to file on disc.
926 :param passwords: list of passwords (str) to try for decryption or None 926 :param passwords: list of passwords (str) to try for decryption or None
927 - :param decrypted_filepath: filepath of the decrypted file in case you want to  
928 - preserve it 927 + :param decrypted_dir: folder to store the decrypted file in case you want
  928 + to preserve it
929 :param int crypto_nesting: How many decryption layers were already used to 929 :param int crypto_nesting: How many decryption layers were already used to
930 get the given file. 930 get the given file.
931 :param kwargs: same as :py:func:`process_file` 931 :param kwargs: same as :py:func:`process_file`
@@ -954,14 +954,14 @@ def process_maybe_encrypted(filepath, passwords=None, decrypted_filepath=None, c @@ -954,14 +954,14 @@ def process_maybe_encrypted(filepath, passwords=None, decrypted_filepath=None, c
954 passwords = list(passwords) + crypto.DEFAULT_PASSWORDS 954 passwords = list(passwords) + crypto.DEFAULT_PASSWORDS
955 try: 955 try:
956 logger.debug('Trying to decrypt file') 956 logger.debug('Trying to decrypt file')
957 - decrypted_file, correct_password = crypto.decrypt(filepath, passwords, decrypted_filepath) 957 + decrypted_file, correct_password = crypto.decrypt(filepath, passwords, decrypted_dir)
958 if correct_password: 958 if correct_password:
959 logger.info(f"The correct password is: {correct_password}") 959 logger.info(f"The correct password is: {correct_password}")
960 if not decrypted_file: 960 if not decrypted_file:
961 logger.error('Decrypt failed, run with debug output to get details') 961 logger.error('Decrypt failed, run with debug output to get details')
962 raise crypto.WrongEncryptionPassword(filepath) 962 raise crypto.WrongEncryptionPassword(filepath)
963 logger.info('Analyze decrypted file') 963 logger.info('Analyze decrypted file')
964 - result = process_maybe_encrypted(decrypted_file, passwords, decrypted_filepath, 964 + result = process_maybe_encrypted(decrypted_file, passwords, decrypted_dir,
965 crypto_nesting+1, **kwargs) 965 crypto_nesting+1, **kwargs)
966 finally: # clean up 966 finally: # clean up
967 try: # (maybe file was not yet created) 967 try: # (maybe file was not yet created)
@@ -997,7 +997,7 @@ def main(cmd_line_args=None): @@ -997,7 +997,7 @@ def main(cmd_line_args=None):
997 return_code = 1 997 return_code = 1
998 try: 998 try:
999 text = process_maybe_encrypted( 999 text = process_maybe_encrypted(
1000 - args.filepath, args.password, args.decrypted_filepath, 1000 + args.filepath, args.password, args.decrypted_dir,
1001 field_filter_mode=args.field_filter_mode) 1001 field_filter_mode=args.field_filter_mode)
1002 return_code = 0 1002 return_code = 0
1003 except Exception as exc: 1003 except Exception as exc:
oletools/olevba.py
@@ -3473,14 +3473,14 @@ class VBA_Parser(object): @@ -3473,14 +3473,14 @@ class VBA_Parser(object):
3473 self.is_encrypted = crypto.is_encrypted(self.ole_file) 3473 self.is_encrypted = crypto.is_encrypted(self.ole_file)
3474 return self.is_encrypted 3474 return self.is_encrypted
3475 3475
3476 - def decrypt_file(self, passwords_list=None, decrypted_filepath=None): 3476 + def decrypt_file(self, passwords_list=None, decrypted_dir=None):
3477 decrypted_file = None 3477 decrypted_file = None
3478 correct_password = None 3478 correct_password = None
3479 if self.detect_is_encrypted(): 3479 if self.detect_is_encrypted():
3480 passwords = crypto.DEFAULT_PASSWORDS 3480 passwords = crypto.DEFAULT_PASSWORDS
3481 if passwords_list and isinstance(passwords_list, list): 3481 if passwords_list and isinstance(passwords_list, list):
3482 passwords.extend(passwords_list) 3482 passwords.extend(passwords_list)
3483 - decrypted_file, correct_password = crypto.decrypt(self.filename, passwords, decrypted_filepath) 3483 + decrypted_file, correct_password = crypto.decrypt(self.filename, passwords, decrypted_dir)
3484 if correct_password: 3484 if correct_password:
3485 log.info(f"The correct password is: {correct_password}") 3485 log.info(f"The correct password is: {correct_password}")
3486 3486
@@ -4373,9 +4373,9 @@ def parse_args(cmd_line_args=None): @@ -4373,9 +4373,9 @@ def parse_args(cmd_line_args=None):
4373 default=None, 4373 default=None,
4374 help='if the file is a zip archive, open all files ' 4374 help='if the file is a zip archive, open all files '
4375 'from it, using the provided password.') 4375 'from it, using the provided password.')
4376 - parser.add_argument("--decrypted_filepath", dest='decrypted_filepath', type=str, 4376 + parser.add_argument("--decrypted_dir", dest='decrypted_dir', type=str,
4377 default=None, 4377 default=None,
4378 - help='save the decrypted file to this location.') 4378 + help='store the decrypted file to this folder.')
4379 parser.add_argument("-p", "--password", type=str, action='append', 4379 parser.add_argument("-p", "--password", type=str, action='append',
4380 default=[], 4380 default=[],
4381 help='if encrypted office files are encountered, try ' 4381 help='if encrypted office files are encountered, try '
@@ -4557,9 +4557,9 @@ def process_file(filename, data, container, options, crypto_nesting=0): @@ -4557,9 +4557,9 @@ def process_file(filename, data, container, options, crypto_nesting=0):
4557 try: 4557 try:
4558 log.debug('Checking encryption passwords {}'.format(options.password)) 4558 log.debug('Checking encryption passwords {}'.format(options.password))
4559 passwords = options.password + crypto.DEFAULT_PASSWORDS 4559 passwords = options.password + crypto.DEFAULT_PASSWORDS
4560 - log.debug('Checking decrypted filepath {}'.format(options.decrypted_filepath)) 4560 + log.debug('Checking decrypted filepath {}'.format(options.decrypted_dir))
4561 4561
4562 - decrypted_file, correct_password = crypto.decrypt(filename, passwords, options.decrypted_filepath) 4562 + decrypted_file, correct_password = crypto.decrypt(filename, passwords, options.decrypted_dir)
4563 if not decrypted_file: 4563 if not decrypted_file:
4564 log.error('Decrypt failed, run with debug output to get details') 4564 log.error('Decrypt failed, run with debug output to get details')
4565 raise crypto.WrongEncryptionPassword(filename) 4565 raise crypto.WrongEncryptionPassword(filename)