Created
January 20, 2025 00:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
How It Works
How to Use
Prerequisites
Steps
.py
file (e.g.,folder_nuke.py
).yes
when asked if you want to proceed.Example
If the folder
C:\SensitiveData
contains:After running the script, the contents of
C:\SensitiveData
will be transformed to something like:The files will be filled with random data, and the names of the files and folders will be randomized.
Important Notes
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:
Messages
folder to a different location, such as your Desktop.Messages
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.