Advertisement
Guest User

VeraCrypt Header Shredder

a guest
Nov 6th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import filedialog, messagebox, simpledialog
  4.  
  5. def overwrite_file_edges(file_path):
  6. """Overwrite the first and last 131072 bytes of the specified file after creating a backup."""
  7. overwrite_size = 131072 # Size of bytes to overwrite
  8. total_required_size = 262144 # Total size required to safely overwrite
  9.  
  10. if os.path.getsize(file_path) < total_required_size:
  11. messagebox.showerror("Error", "The file is smaller than 262144 bytes. Please select another file.")
  12. return False # Indicate that the operation was unsuccessful due to file size
  13.  
  14. try:
  15. # Create a backup of the first 131072 bytes
  16. with open(file_path, 'rb') as f:
  17. original_data_start = f.read(overwrite_size) # Read the first 131072 bytes
  18.  
  19. # Generate random bytes for overwriting
  20. random_data_start = os.urandom(overwrite_size)
  21. random_data_end = os.urandom(overwrite_size)
  22.  
  23. # Ask the user if they want to save the backup
  24. backup_file_path = filedialog.asksaveasfilename(defaultextension=".bak",
  25. filetypes=[("Backup Files", "*.bak"),
  26. ("All Files", "*.*")],
  27. title="Save Backup File")
  28.  
  29. if backup_file_path: # Check if the user selected a file path for the backup
  30. with open(backup_file_path, 'wb') as backup_file:
  31. backup_file.write(original_data_start) # Write the backup
  32.  
  33. # Warning message before overwriting
  34. response = simpledialog.askstring("WARNING",
  35. "YOU ARE ABOUT TO SHRED YOUR VOLUME HEADER.\n"
  36. "Type 'yes' to proceed:")
  37.  
  38. if response and response.lower() == "yes":
  39. # Overwrite the original file
  40. with open(file_path, 'r+b') as f:
  41. f.seek(0) # Move the file pointer to the beginning
  42. f.write(random_data_start) # Write random data to the start
  43.  
  44. f.seek(-overwrite_size, os.SEEK_END) # Move to the end minus 131072 bytes
  45. f.write(random_data_end) # Write random data to the end
  46.  
  47. messagebox.showinfo("Success", "Successfully overwritten the first and last 131072 bytes of the file.")
  48. return True # Indicate success
  49. else:
  50. messagebox.showinfo("Cancelled", "Overwrite operation cancelled.")
  51. return False # Indicate cancellation
  52.  
  53. except Exception as e:
  54. messagebox.showerror("Error", f"An error occurred: {str(e)}")
  55. return False # Indicate error
  56.  
  57. def browse_file():
  58. """Open a file dialog to select a file and overwrite its edges."""
  59. while True: # Loop until a valid file is processed
  60. file_path = filedialog.askopenfilename()
  61. if file_path:
  62. if overwrite_file_edges(file_path): # If successful overwrite, break the loop
  63. break # Exit the loop if file is processed successfully
  64. else:
  65. break # Exit if no file is selected
  66.  
  67. # Exit the application after processing
  68. root.quit()
  69.  
  70. if __name__ == "__main__":
  71. # Display initial messages
  72. print("VeraCrypt Header Shredder")
  73. print("\nYou will now select the container with the header to shred and the backup location for the original one.")
  74.  
  75. # Wait for user input before proceeding
  76. input("Press Enter to continue...")
  77.  
  78. # Set up a simple GUI window
  79. root = tk.Tk()
  80. root.title("VeraCrypt Header Shredder") # Set the window title
  81. root.withdraw() # Hide the root window since we only want the file dialog
  82.  
  83. # Open the file dialog
  84. browse_file()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement