As penetration testers, we often gain access to highly sensitive data such as user lists, credentials (i.e., downloading the NTDS.dit file for offline password cracking), and enumeration data that can contain critical information about the organization's network infrastructure, and Active Directory (AD) environment, etc. Therefore, it is essential to encrypt this data or use encrypted data connections such as SSH, SFTP, and HTTPS. However, sometimes these options are not available to us, and a different approach is required.
β οΈ Note: Unless specifically requested by a client, we do not recommend exfiltrating data such as Personally Identifiable Information (PII), financial data (i.e., credit card numbers), trade secrets, etc., from a client environment. Instead, if attempting to test Data Loss Prevention (DLP) controls/egress filtering protections, create a file with dummy data that mimics the data that the client is trying to protect.
Therefore, encrypting the data or files before a transfer is often necessary to prevent the data from being read if intercepted in transit.
Data leakage during a penetration test could have severe consequences for the penetration tester, their company, and the client. As information security professionals, we must act professionally and responsibly and take all measures to protect any data we encounter during an assessment.
File Encryption on Windows
Many different methods can be used to encrypt files and information on Windows systems. One of the simplest methods is the Invoke-AESEncryption.ps1 PowerShell script. This script is small and provides encryption of files and strings.
Invoke-AESEncryption.ps1 Script
Download or create the script:
# The script can be downloaded or created manually# Save as Invoke-AESEncryption.ps1
OpenSSL is frequently included in Linux distributions, with sysadmins using it to generate security certificates, among other tasks. OpenSSL can be used to send files "nc style" to encrypt files.
OpenSSL Encryption
Encrypting /etc/passwd with openssl:
Decrypt passwd.enc with openssl:
OpenSSL Advanced Options
Different cipher algorithms:
Base64 encoding with encryption:
Using password from file:
GPG Encryption
Symmetric encryption with GPG:
Generate GPG key pair:
Encrypt for specific recipient:
Decrypt file:
Archive Encryption
Create encrypted tar archive:
Extract encrypted tar archive:
Using 7-Zip on Linux:
Advanced Protection Methods
Steganography
Hide data in images using steghide:
Hide data using LSB (Least Significant Bit):
Split and Encrypt
Split large files before encryption:
Reassemble and decrypt:
Secure Transfer Protocols
HTTPS File Transfer
Upload via HTTPS with curl:
Download via HTTPS with wget:
SFTP (SSH File Transfer Protocol)
Upload encrypted file via SFTP:
Batch SFTP operations:
SCP over SSH
Upload encrypted file via SCP:
SCP with compression:
Best Practices for Protected File Transfers
Password Security
Use strong, unique passwords for each engagement
Minimum 16 characters with mixed case, numbers, and symbols
Never reuse passwords across different clients
Store passwords securely in a password manager
Use different passwords for each encrypted file
Key Management
Generate strong encryption keys using cryptographically secure methods
Use key derivation functions (like PBKDF2) with high iteration counts
Rotate encryption keys regularly
Securely delete keys after use
Never hardcode keys in scripts or documentation
File Handling
Encrypt before transfer whenever possible
Verify file integrity after transfer using checksums
Securely delete original files after encryption
Use secure deletion tools (like shred on Linux)
Document encryption methods used for each file
Network Security
Prefer encrypted transport protocols (HTTPS, SFTP, SSH)
# Encrypt a file
Invoke-AESEncryption -Mode Encrypt -Key "test123" -Path .\scan-results.txt
# Output: File encrypted to C:\htb\scan-results.txt.aes
# List files to verify
ls
# Python example for LSB steganography
from PIL import Image
import numpy as np
def hide_data_in_image(image_path, data, output_path):
image = Image.open(image_path)
image_array = np.array(image)
# Convert data to binary
binary_data = ''.join(format(ord(char), '08b') for char in data)
# Hide data in LSB of image pixels
data_index = 0
for i in range(image_array.shape[0]):
for j in range(image_array.shape[1]):
for k in range(image_array.shape[2]):
if data_index < len(binary_data):
image_array[i][j][k] = (image_array[i][j][k] & 0xFE) | int(binary_data[data_index])
data_index += 1
# Save modified image
modified_image = Image.fromarray(image_array)
modified_image.save(output_path)
# Usage
hide_data_in_image('cover.png', 'secret message', 'stego.png')
# Split file into 1MB chunks
split -b 1M large_file.txt chunk_
# Encrypt each chunk
for file in chunk_*; do
openssl enc -aes256 -iter 100000 -pbkdf2 -in "$file" -out "$file.enc"
rm "$file" # Remove original chunk
done
# Decrypt each chunk
for file in chunk_*.enc; do
openssl enc -d -aes256 -iter 100000 -pbkdf2 -in "$file" -out "${file%.enc}"
done
# Reassemble file
cat chunk_* > large_file_restored.txt
# Clean up chunks
rm chunk_*
curl -X POST -F "file=@encrypted_file.enc" https://secure-server.com/upload
# Original file size
ls -la original_file.txt
# Encrypted file size (will be larger)
ls -la original_file.txt.enc
# Decrypted file size (should match original)
ls -la decrypted_file.txt
# Create checksum before encryption
sha256sum original_file.txt > original.sha256
# Verify after decryption
sha256sum -c original.sha256