Skip to content

Instantly share code, notes, and snippets.

@tavallaie
Last active May 21, 2024 01:54
Show Gist options
  • Save tavallaie/812ec1bb58e07678fa54db0324aab95c to your computer and use it in GitHub Desktop.
Save tavallaie/812ec1bb58e07678fa54db0324aab95c to your computer and use it in GitHub Desktop.
**Secure Data Erasure Script (Bash)** This Bash script securely erases data from a specified device with random data, ensuring irrecoverability. Use with caution.
#!/bin/bash
# Check if the script is run with superuser privileges
if [ "$EUID" -ne 0 ]; then
echo "Please run this script as root (sudo)."
exit 1
fi
# Prompt the user for the target devices
read -p "Enter the target devices (e.g., /dev/sdX /dev/sdY): " target_devices
# Iterate through the space-separated list of target devices
for device in $target_devices; do
# Check if the device exists
if [ ! -e "$device" ]; then
echo "Device $device does not exist."
continue
fi
# Overwrite with random data
echo "Erasing $device..."
dd if=/dev/urandom of="$device" bs=4M status=progress
echo "Finished erasing $device."
done
@thisisjab
Copy link

Good job!

@A4rmin
Copy link

A4rmin commented Dec 12, 2023

Great, this is very useful 👍

@ALiwoto
Copy link

ALiwoto commented Dec 12, 2023

What is that bs=4M in line 22?

@th3r0b0t
Copy link

th3r0b0t commented Dec 12, 2023

What is that bs=4M in line 22?

bs specifies how much bytes should be read and written from/to the device at a time (can be set separately by ibs and obs! bs override those).
You can check the man page of dd(1) or via man7.org
dd(1) deals with a device at byte level and therefore accessing with bigger chunk size, speeds up the process but also takes more RAM!

@antil0l
Copy link

antil0l commented Dec 12, 2023

it would be a good idea to use /dev/random since it has more entropy, the disadvantage being if there is not enough entropy you will have to wait

@antil0l
Copy link

antil0l commented Dec 12, 2023

also this command if you are really paranoid about it
openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero > /dev/sdx

@th3r0b0t
Copy link

it would be a good idea to use /dev/random since it has more entropy, the disadvantage being if there is not enough entropy you will have to wait

IMO, it doesn't matter if data is random or not, I would've use all zeros...

@parsapoorsh
Copy link

did you ever heard of dban or shredos?

@tavallaie
Copy link
Author

I didn't know them, thanks

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