msoffcrypto-tool

repository·master·Indexed 20 days ago

https://github.com/nolze/msoffcrypto-tool

A Python tool and library for decrypting and encrypting Microsoft Office files using passwords, intermediate keys, or private keys. It supports legacy formats (doc97, ppt97, xls97) and modern Office Open XML formats (OOXML). The package includes a CLI for decryption and experimental OOXML encryption, as well as a Python API featuring the OfficeFile class for programmatic decryption and integrity verification.

Tokens
2.2K
Snippets
8
Records
13
Agent score
69%

What's inside msoffcrypto-tool

  1. Overview of the msoffcrypto.format package submodules

    master

    The msoffcrypto.format package provides specialized modules for handling different Microsoft Office file formats. Developers can use these submodules to interact with specific encryption structures for legacy and modern Office documents.

    Supported format submodules include:

    • msoffcrypto.format.doc97: For legacy Word documents.
    • msoffcrypto.format.ppt97: For legacy PowerPoint presentations.
    • msoffcrypto.format.xls97: For legacy Excel spreadsheets.
    • msoffcrypto.format.ooxml: For modern Office Open XML formats (e.g., .docx, .xlsx, .pptx).
    • msoffcrypto.format.base: Contains base classes and fundamental logic.
    • msoffcrypto.format.common: Contains shared utilities used across different format modules.
  2. Overview of the msoffcrypto package

    master

    The msoffcrypto package is used for handling Microsoft Office encrypted files. It is organized into several subpackages that handle specific aspects of the decryption process:

    • msoffcrypto.exceptions: Contains exception classes for error handling.
    • msoffcrypto.format: Handles the parsing and structure of different Office file formats.
    • msoffcrypto.method: Manages the cryptographic methods used for encryption/decryption.

    The main msoffcrypto module serves as the primary entry point for interacting with the tool.

  3. Understand the msoffcrypto.method package structure

    master
    The msoffcrypto.method package provides the underlying cryptographic implementations used to decrypt Microsoft Office files. It is organized into specialized modules based on the encryption standard or algorithm used by the file. Developers working with low-level decryption logic should select the module that matches the file's encryption type.
  4. Encrypt MS Office files (OOXML only, experimental) via Python library

    master

    Note: The encryption feature is experimental. It only supports OOXML formats via the msoffcrypto.format.ooxml.OOXMLFile class.

    Basic Encryption:

    from msoffcrypto.format.ooxml import OOXMLFile
    
    plain = open("plain.docx", "rb")
    file = OOXMLFile(plain)
    
    with open("encrypted.docx", "wb") as f:
        file.encrypt("Passw0rd", f)
    
    plain.close()

    In-memory Encryption:

    from msoffcrypto.format.ooxml import OOXMLFile
    import io
    
    encrypted = io.BytesIO()
    
    with open("plain.xlsx", "rb") as f:
        file = OOXMLFile(f)
        file.encrypt("Passw0rd", encrypted)
    
    # 'encrypted' now contains an OLE container with an encrypted stream
  5. Decrypt MS Office files using the Python library

    master

    Use the msoffcrypto.OfficeFile class to handle decryption in your Python code. You must first load the key using load_key() before calling decrypt().

    Basic Decryption:

    import msoffcrypto
    
    encrypted = open("encrypted.docx", "rb")
    file = msoffcrypto.OfficeFile(encrypted)
    file.load_key(password="Passw0rd")
    
    with open("decrypted.docx", "wb") as f:
        file.decrypt(f)
    
    encrypted.close()

    In-memory Decryption (e.g., for Pandas):

    import msoffcrypto
    import io
    import pandas as pd
    
    decrypted = io.BytesIO()
    
    with open("encrypted.xlsx", "rb") as f:
        file = msoffcrypto.OfficeFile(f)
        file.load_key(password="Passw0rd")
        file.decrypt(decrypted)
    
    df = pd.read_excel(decrypted)
    print(df)
    import msoffcrypto
    
    encrypted = open("encrypted.docx", "rb")
    file = msoffcrypto.OfficeFile(encrypted)
    file.load_key(password="Passw0rd")
    
    with open("decrypted.docx", "wb") as f:
        file.decrypt(f)
  6. Advanced key loading and integrity checks in msoffcrypto

    master

    The load_key and decrypt methods support advanced authentication and verification options:

    • Verify password before decryption: Use verify_password=True in load_key(). This is only meaningful for ECMA-376 Agile/Standard Encryption.
    • Use a private key: Pass a file object of a PEM private key to load_key(private_key=...).
    • Use an intermediate key (secretKey): Pass the hex-decoded key to load_key(secret_key=...).
    • Verify data integrity: Use verify_integrity=True in decrypt(). This is only meaningful for ECMA-376 Agile Encryption and checks the HMAC of the data payload.
    # Verify password before decryption
    file.load_key(password="Passw0rd", verify_password=True)
    
    # Use private key
    file.load_key(private_key=open("priv.pem", "rb"))
    
    # Use intermediate key (secretKey)
    import binascii
    file.load_key(secret_key=binascii.unhexlify("AE8C36E68B4BB9EA46E5544A5FDB6693875B2FDE1507CBC65C8BCF99E25C2562"))
    
    # Check HMAC integrity before decryption
    file.decrypt(open("decrypted.docx", "wb"), verify_integrity=True)
  7. Available encryption modules in msoffcrypto.method

    master

    The following modules are available within the msoffcrypto.method package for handling different encryption standards:

    • msoffcrypto.method.ecma376_agile: Implements the ECMA-376 Agile encryption standard.
    • msoffcrypto.method.ecma376_extensible: Implements the ECMA-376 Extensible encryption standard.
    • msoffcrypto.method.ecma376_standard: Implements the ECMA-376 Standard encryption standard.
    • msoffcrypto.method.rc4: Implements the RC4 encryption algorithm.
    • msoffcrypto.method.rc4_cryptoapi: Implements RC4 using the Windows CryptoAPI.
    • msoffcrypto.method.xor_obfuscation: Implements XOR-based obfuscation.
  8. Encrypt MS Office files (OOXML only, experimental) via CLI

    master

    Note: The encryption feature is currently experimental. Use it at your own risk. It only supports OOXML formats.

    To password-protect a document, use the -e flag along with the -p flag.

    msoffcrypto-tool -e -p Passw0rd plain.docx encrypted.docx
  9. Decrypt MS Office files using the CLI

    master

    Use the msoffcrypto-tool command to decrypt files. You can provide the password directly using the -p flag or omit it to be prompted for input.

    Decryption Command: msoffcrypto-tool <encrypted_file> <decrypted_file> -p <password>

    Check if a file is encrypted: Use the -t flag with --test and -v (verbose) to check encryption status. The command returns exit code 1 if the file is encrypted and 0 if it is not.

    # Decrypt with password
    msoffcrypto-tool encrypted.docx decrypted.docx -p Passw0rd
    
    # Decrypt with password prompt
    msoffcrypto-tool encrypted.docx decrypted.docx -p
    
    # Test if file is encrypted
    msoffcrypto-tool document.doc --test -v
  10. msoffcrypto-tool CLI reference: Arguments and Flags

    master

    The following flags and arguments are available for the msoffcrypto-tool command line interface:

    FlagArgumentDescription
    -p, --password[TEXT]The password text. If used without a value, it prompts for input.
    -t, --test(flag)Tests if the input file is encrypted.
    -e(flag)Enables encryption mode (only works with OOXML files).
    -v(flag)Enables verbose output (DEBUG level logging).
    infile(positional)The input file to process.
    outfile(positional)The output file. If omitted, stdout is used.