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 33 raise crypto.MaxCryptoNestingReached(crypto_nesting, filename)
34 34 decrypted_file = None
35 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 37 if decrypted_file is None:
38 38 raise crypto.WrongEncryptionPassword(input_file)
39 39 # might still be encrypted, so call this again recursively
... ... @@ -315,7 +315,7 @@ def check_msoffcrypto():
315 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 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 332 `dirname` or `prefix`. `suffix` will default to
333 333 suffix of input `filename`, `prefix` defaults to
334 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 337 :returns: a tuple with the name of the decrypted temporary file (type str) or `None`
338 338 and the correct password or 'None'
339 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 409  
410 410 if decrypt_file and correct_password:
411 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 416 else:
417 417 log.info('All passwords failed')
418 418  
... ...
oletools/msodde.py
... ... @@ -271,9 +271,9 @@ def process_args(cmd_line_args=None):
271 271 parser.add_argument("-p", "--password", type=str, action='append',
272 272 help='if encrypted office files are encountered, try '
273 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 275 default=None,
276   - help='save the decrypted file to this location.')
  276 + help='store the decrypted file to this folder.')
277 277 filter_group = parser.add_argument_group(
278 278 title='Filter which OpenXML field commands are returned',
279 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 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 917 **kwargs):
918 918 """
919 919 Process a file that might be encrypted.
... ... @@ -924,8 +924,8 @@ def process_maybe_encrypted(filepath, passwords=None, decrypted_filepath=None, c
924 924  
925 925 :param str filepath: path to file on disc.
926 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 929 :param int crypto_nesting: How many decryption layers were already used to
930 930 get the given file.
931 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 954 passwords = list(passwords) + crypto.DEFAULT_PASSWORDS
955 955 try:
956 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 958 if correct_password:
959 959 logger.info(f"The correct password is: {correct_password}")
960 960 if not decrypted_file:
961 961 logger.error('Decrypt failed, run with debug output to get details')
962 962 raise crypto.WrongEncryptionPassword(filepath)
963 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 965 crypto_nesting+1, **kwargs)
966 966 finally: # clean up
967 967 try: # (maybe file was not yet created)
... ... @@ -997,7 +997,7 @@ def main(cmd_line_args=None):
997 997 return_code = 1
998 998 try:
999 999 text = process_maybe_encrypted(
1000   - args.filepath, args.password, args.decrypted_filepath,
  1000 + args.filepath, args.password, args.decrypted_dir,
1001 1001 field_filter_mode=args.field_filter_mode)
1002 1002 return_code = 0
1003 1003 except Exception as exc:
... ...
oletools/olevba.py
... ... @@ -3473,14 +3473,14 @@ class VBA_Parser(object):
3473 3473 self.is_encrypted = crypto.is_encrypted(self.ole_file)
3474 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 3477 decrypted_file = None
3478 3478 correct_password = None
3479 3479 if self.detect_is_encrypted():
3480 3480 passwords = crypto.DEFAULT_PASSWORDS
3481 3481 if passwords_list and isinstance(passwords_list, list):
3482 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 3484 if correct_password:
3485 3485 log.info(f"The correct password is: {correct_password}")
3486 3486  
... ... @@ -4373,9 +4373,9 @@ def parse_args(cmd_line_args=None):
4373 4373 default=None,
4374 4374 help='if the file is a zip archive, open all files '
4375 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 4377 default=None,
4378   - help='save the decrypted file to this location.')
  4378 + help='store the decrypted file to this folder.')
4379 4379 parser.add_argument("-p", "--password", type=str, action='append',
4380 4380 default=[],
4381 4381 help='if encrypted office files are encountered, try '
... ... @@ -4557,9 +4557,9 @@ def process_file(filename, data, container, options, crypto_nesting=0):
4557 4557 try:
4558 4558 log.debug('Checking encryption passwords {}'.format(options.password))
4559 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 4563 if not decrypted_file:
4564 4564 log.error('Decrypt failed, run with debug output to get details')
4565 4565 raise crypto.WrongEncryptionPassword(filename)
... ...