Skip to content

Instantly share code, notes, and snippets.

@rmtbb
Created January 20, 2025 00:46
Show Gist options
  • Save rmtbb/99df958361aed29c0fe6a9a5be46578e to your computer and use it in GitHub Desktop.
Save rmtbb/99df958361aed29c0fe6a9a5be46578e to your computer and use it in GitHub Desktop.
Folder Nuke: A Python script for overwriting file data and renaming files and folders to prevent data recovery before deletion.
"""
Folder Nuke: Secure Data Overwrite and Rename Script
This script is designed to securely overwrite and rename all files and folders within a specified directory.
Its purpose is to prevent the recovery of sensitive data by overwriting file contents with random data and
randomizing file and folder names.
Features:
- Overwrites file contents with random alphanumeric data.
- Renames files to random alphanumeric names while preserving their extensions.
- Renames folders, including the top-level folder, to random alphanumeric names.
- Processes files and folders recursively.
Usage Instructions:
1. Save this script as a Python file (e.g., folder_nuke.py).
2. Open a terminal or command prompt.
3. Run the script using the command:
python folder_nuke.py
4. When prompted, enter the path to the folder you wish to process.
5. Confirm the folder selection by typing "yes" when asked.
6. The script will overwrite and rename all files and folders within the specified directory.
Special Notes for Mac Users:
- If sanitizing a Mac computer, remember to clear iMessages.
- Move the Messages folder from `~/Library/Messages` to another location (e.g., Desktop) before running the script,
as it cannot process the Messages folder in its default location.
Disclaimer:
- This script irreversibly modifies files and folders. Ensure you have backups if needed.
- Use responsibly and only on directories where data loss is acceptable.
"""
import os
import random
import string
def generate_random_string(length=16):
"""Generate a random string of letters and digits."""
characters = string.ascii_letters + string.digits
return ''.join(random.choice(characters) for _ in range(length))
def overwrite_file_with_random_data(file_path):
"""Overwrite the content of a file with random data."""
try:
with open(file_path, 'wb') as file:
# Generate random data to fill the file
random_data = generate_random_string(random.randint(50, 200)).encode('utf-8')
file.write(random_data)
except Exception as e:
print(f"Error overwriting file {file_path}: {e}")
def rename_file_to_random(file_path):
"""Rename a file to a random name."""
try:
dir_name, original_name = os.path.split(file_path)
random_name = generate_random_string(12) + os.path.splitext(original_name)[1]
new_file_path = os.path.join(dir_name, random_name)
os.rename(file_path, new_file_path)
return new_file_path
except Exception as e:
print(f"Error renaming file {file_path}: {e}")
return file_path
def rename_folder_to_random(folder_path):
"""Rename a folder to a random name."""
try:
parent_dir, original_name = os.path.split(folder_path)
random_name = generate_random_string(12)
new_folder_path = os.path.join(parent_dir, random_name)
os.rename(folder_path, new_folder_path)
return new_folder_path
except Exception as e:
print(f"Error renaming folder {folder_path}: {e}")
return folder_path
def process_folder(folder_path):
"""Recursively process a folder: overwrite file content, rename files, and rename folders."""
# Process all files and subfolders first
for root, dirs, files in os.walk(folder_path, topdown=False):
for file in files:
file_path = os.path.join(root, file)
# Overwrite file content
overwrite_file_with_random_data(file_path)
# Rename the file
renamed_path = rename_file_to_random(file_path)
print(f"Processed file: {renamed_path}")
for dir in dirs:
dir_path = os.path.join(root, dir)
# Rename the folder
renamed_dir_path = rename_folder_to_random(dir_path)
print(f"Renamed folder: {renamed_dir_path}")
# Rename the top-level folder
new_folder_path = rename_folder_to_random(folder_path)
print(f"Renamed top-level folder to: {new_folder_path}")
if __name__ == "__main__":
# Prompt the user to enter the folder path
folder_to_process = input("Enter the path of the folder to process: ").strip()
if os.path.isdir(folder_to_process):
# Confirm the selection before proceeding
confirmation = input(f"You selected the folder: {folder_to_process}\nDo you want to proceed? (yes/no): ").strip().lower()
if confirmation == 'yes':
print("Starting the process...")
process_folder(folder_to_process)
print("Process completed.")
else:
print("Operation cancelled.")
else:
print("The specified path is not a valid folder.")
@rmtbb
Copy link
Author

rmtbb commented Jan 20, 2025

Folder Nuke

Overview

Folder Nuke is a Python script designed to securely overwrite and rename all files and folders within a specified directory. By doing so, it helps to ensure that the original data cannot be easily recovered using recovery tools, which is often possible if files are deleted without being overwritten.

This tool is particularly useful for securely preparing sensitive data for deletion, preventing unauthorized recovery of confidential or private information.

Features

  1. Data Overwriting:
    • Replaces the contents of each file with random alphanumeric data.
  2. File Renaming:
    • Changes the filenames to random alphanumeric strings.
  3. Folder Renaming:
    • Renames all subfolders and the top-level folder to random alphanumeric strings.
  4. Recursive Operation:
    • Processes all files and subfolders within the specified folder.
  5. Confirmation Prompt:
    • Ensures the user confirms the folder selection before starting the operation to prevent accidental changes.

How It Works

  1. The script traverses the specified folder and identifies all files and subfolders.
  2. For each file:
    • The content is overwritten with random data.
    • The file is renamed to a randomly generated name.
  3. For each folder:
    • After processing its contents, it is renamed to a random name.
  4. The top-level folder is renamed once all files and subfolders have been processed.

How to Use

Prerequisites

  • Python 3.x installed on your system.

Steps

  1. Save the script as a .py file (e.g., folder_nuke.py).
  2. Open a terminal or command prompt.
  3. Run the script by entering:
    python folder_nuke.py
  4. When prompted, enter the path of the folder you wish to process.
  5. Confirm your selection by typing yes when asked if you want to proceed.
  6. The script will recursively process all files and folders in the specified directory.

Example

If the folder C:\SensitiveData contains:

C:\SensitiveData
├── file1.txt
├── file2.docx
└── Subfolder
    ├── file3.png
    └── file4.pdf

After running the script, the contents of C:\SensitiveData will be transformed to something like:

C:\aBc123XyZ456
├── Xy1Z4aBc789.txt
├── 9X7Bc2YZa34.docx
└── 7XaBc9Z1Y4X
    ├── 34Z7X1aBc89.png
    └── 9aBc1X7YZ34.pdf

The files will be filled with random data, and the names of the files and folders will be randomized.

Important Notes

  • Irreversible Changes: This script irreversibly modifies file contents and names. Ensure you have backups if necessary.
  • Permissions: Ensure you have the required permissions to modify and rename the files and folders in the specified directory.
  • Use Cases: This tool is intended for securely preparing data for deletion. After running the script, you can delete the folder with confidence that its contents cannot be easily recovered.

Special Considerations for Mac Users

If you are sanitizing a Mac computer before giving it away or returning it, make sure to clear your iMessages as well. iMessages data is stored in a specific folder that cannot be processed by this script in its normal location. Follow these steps to handle the Messages folder:

  1. Navigate to the Messages folder by opening Finder and going to:
    ~/Library/Messages
    
  2. Move (do not copy) the Messages folder to a different location, such as your Desktop.
  3. Run the script on the relocated Messages folder.
  4. After the process is complete, delete the processed folder.

This ensures that sensitive data from iMessages is also securely overwritten and cannot be recovered.

Disclaimer

Use this script responsibly. Ensure you fully understand the consequences of running it, as it will irreversibly alter the files and folders in the specified directory. The author is not responsible for any data loss caused by improper use of this script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment