コード例 #1
0
    def each(self, target):
        tmpdir = tempdir()
        password_candidates = self.password_candidates.split("\n")

        with open(target, "rb") as myfile:

            document = OfficeFile(myfile)

            for password in password_candidates:
                password = password.strip()
                try:
                    document.load_key(password=password)
                    out_file = tmpdir + os.path.sep + "decrypted_" + os.path.basename(
                        target)
                    with open(out_file, "wb") as output:
                        document.decrypt(output)
                    if os.path.isfile(out_file):
                        self.add_extracted_file(out_file)
                    break
                except:
                    pass
            else:
                self.log(
                    'error',
                    'Could not extract {} (password not known)'.format(target))

        return True
コード例 #2
0
    def test_file_handle_open(self):
        """Check that file handles are open after is_encrypted()."""
        for suffix in "doc", "ppt", "xls":
            path = join(DATA_DIR, "plain." + suffix)

            with open(path, "rb") as file_handle:
                ofile = OfficeFile(file_handle)

                # do something with ofile
                self.assertEqual(ofile.is_encrypted(), False)

                # check that file handle is still open
                self.assertFalse(file_handle.closed)

                # destroy OfficeFile, calls destructor
                del ofile

                # check that file handle is still open
                self.assertFalse(file_handle.closed)

            # just for completeness:
            # check that file handle is now closed
            self.assertTrue(file_handle.closed)
コード例 #3
0
 def decrypt(encrypted_stream: IO[bytes], password: str) -> IO[bytes]:
     try:
         office_file = OfficeFile(encrypted_stream)
         decrypted_stream = io.BytesIO()
         office_file.load_key(password=password)
         office_file.decrypt(decrypted_stream)
         decrypted_stream.seek(0)
         return decrypted_stream
     except Exception as e:
         # xlsx exception message: 'The file could not be decrypted with this password'
         # xls exception message:  'Failed to verify password'
         if "password" in str(e):
             raise IncorrectPassword()
         else:
             raise RuntimeError(
                 UNEXPECTED_DECRYPTION_EXCEPTION_MESSAGE.format(
                     getattr(encrypted_stream, "name", None), str(e)))
コード例 #4
0
def _is_ole_encrypted(_file):
    return OfficeFile(_file).is_encrypted()