Get a Pentest and security assessment of your IT network.

Cyber Security

Automated File Decryption

TL;DR

This guide shows you how to automatically decrypt files using command-line tools and scripting. It covers identifying the encryption method, setting up decryption software, and creating a script to process multiple files.

1. Identify the Encryption Method

Before you can decrypt anything, you need to know how it was encrypted. Common methods include:

  • GPG (GNU Privacy Guard): Often used for email and file encryption.
  • OpenSSL: A versatile toolkit for various cryptographic operations.
  • 7-Zip/RAR: Archive formats that can encrypt files.
  • VeraCrypt/BitLocker: Full disk or container encryption.

Look at the file extension (e.g., .gpg, .enc), ask the sender, or use a file identification tool like file on Linux/macOS:

file myfile.enc

This might tell you something like “GPG encrypted data” or “RAR archive, compressed”.

2. Install Decryption Software

Install the necessary software based on the encryption method identified in step 1.

  • GPG: On Debian/Ubuntu:
    sudo apt update && sudo apt install gnupg

    On macOS (using Homebrew):

    brew install gpg
  • OpenSSL: Usually pre-installed on Linux and macOS. If not:
    sudo apt install openssl

    (Debian/Ubuntu) or

    brew install openssl

    (macOS).

  • 7-Zip/RAR: Download from their respective websites or use your package manager.
  • VeraCrypt: Download from the VeraCrypt website.

3. Prepare Your Decryption Key/Password

You’ll need the key or password used to encrypt the files. Keep this secure! For GPG, you might have a private key stored in your keyring.

4. Create a Decryption Script (Example: GPG)

Let’s create a simple bash script to decrypt multiple .gpg files in a directory.

#!/bin/bash

# Directory containing the encrypted files
directory="./encrypted_files"

# Loop through all .gpg files in the directory
for file in "$directory"/*.gpg;
do
  if [ -f "$file" ]; then
    echo "Decrypting $file..."
    gpg --decrypt --output "${file%.gpg}" "$file"
    if [ $? -eq 0 ]; then
      echo "Successfully decrypted $file to ${file%.gpg}"
    else
      echo "Failed to decrypt $file"
    fi
  fi
done

echo "Decryption process complete."

Explanation:

  • #!/bin/bash: Specifies the interpreter for the script.
  • directory="./encrypted_files": Sets the directory containing encrypted files. Change this to your actual directory.
  • for file in "$directory"/*.gpg; do ... done: Loops through each .gpg file in the specified directory.
  • if [ -f "$file" ]; then ... fi: Checks if the file exists.
  • gpg --decrypt --output "${file%.gpg}" "$file": Decrypts the file using GPG and saves it with the same name but without the .gpg extension.
  • if [ $? -eq 0 ]; then ... else ... fi: Checks if the decryption was successful (exit code 0 means success).

To run this script:

  1. Save the script to a file, e.g., decrypt_gpg.sh.
  2. Make the script executable:
    chmod +x decrypt_gpg.sh
  3. Run the script:
    ./decrypt_gpg.sh

5. Adapt for Other Encryption Methods

Modify the decryption command in the script based on the encryption method.

  • OpenSSL: Use openssl enc -d -aes-256-cbc -in encrypted_file.enc -out decrypted_file.txt (replace with your specific cipher and key).
  • 7-Zip: Use 7z x encrypted_archive.zip (you’ll likely be prompted for the password).
  • RAR: Use unrar x encrypted_archive.rar (password prompt also applies here).

6. Error Handling and Security Considerations

  • Error Checking: Add more robust error handling to the script to deal with invalid files, incorrect passwords, etc.
  • Secure Key Storage: Never hardcode passwords or keys directly into the script! Use environment variables or a secure key management system.
  • Permissions: Ensure that only authorized users have access to the decryption scripts and keys.
  • Logging: Consider adding logging to track successful and failed decryption attempts.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation