Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import tkinter as tk
- from tkinter import filedialog, messagebox, simpledialog
- def overwrite_file_edges(file_path):
- """Overwrite the first and last 131072 bytes of the specified file after creating a backup."""
- overwrite_size = 131072 # Size of bytes to overwrite
- total_required_size = 262144 # Total size required to safely overwrite
- if os.path.getsize(file_path) < total_required_size:
- messagebox.showerror("Error", "The file is smaller than 262144 bytes. Please select another file.")
- return False # Indicate that the operation was unsuccessful due to file size
- try:
- # Create a backup of the first 131072 bytes
- with open(file_path, 'rb') as f:
- original_data_start = f.read(overwrite_size) # Read the first 131072 bytes
- # Generate random bytes for overwriting
- random_data_start = os.urandom(overwrite_size)
- random_data_end = os.urandom(overwrite_size)
- # Ask the user if they want to save the backup
- backup_file_path = filedialog.asksaveasfilename(defaultextension=".bak",
- filetypes=[("Backup Files", "*.bak"),
- ("All Files", "*.*")],
- title="Save Backup File")
- if backup_file_path: # Check if the user selected a file path for the backup
- with open(backup_file_path, 'wb') as backup_file:
- backup_file.write(original_data_start) # Write the backup
- # Warning message before overwriting
- response = simpledialog.askstring("WARNING",
- "YOU ARE ABOUT TO SHRED YOUR VOLUME HEADER.\n"
- "Type 'yes' to proceed:")
- if response and response.lower() == "yes":
- # Overwrite the original file
- with open(file_path, 'r+b') as f:
- f.seek(0) # Move the file pointer to the beginning
- f.write(random_data_start) # Write random data to the start
- f.seek(-overwrite_size, os.SEEK_END) # Move to the end minus 131072 bytes
- f.write(random_data_end) # Write random data to the end
- messagebox.showinfo("Success", "Successfully overwritten the first and last 131072 bytes of the file.")
- return True # Indicate success
- else:
- messagebox.showinfo("Cancelled", "Overwrite operation cancelled.")
- return False # Indicate cancellation
- except Exception as e:
- messagebox.showerror("Error", f"An error occurred: {str(e)}")
- return False # Indicate error
- def browse_file():
- """Open a file dialog to select a file and overwrite its edges."""
- while True: # Loop until a valid file is processed
- file_path = filedialog.askopenfilename()
- if file_path:
- if overwrite_file_edges(file_path): # If successful overwrite, break the loop
- break # Exit the loop if file is processed successfully
- else:
- break # Exit if no file is selected
- # Exit the application after processing
- root.quit()
- if __name__ == "__main__":
- # Display initial messages
- print("VeraCrypt Header Shredder")
- print("\nYou will now select the container with the header to shred and the backup location for the original one.")
- # Wait for user input before proceeding
- input("Press Enter to continue...")
- # Set up a simple GUI window
- root = tk.Tk()
- root.title("VeraCrypt Header Shredder") # Set the window title
- root.withdraw() # Hide the root window since we only want the file dialog
- # Open the file dialog
- browse_file()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement