Write random data to random sectors of Hard Drive

Solution 1:

you can use a shellscript in combination with dd.

e.g.

 while (true); do
   number=$((($RANDOM * 32768 + $RANDOM)))
   dd if=/dev/urandom of=/dev/sdx1 seek=$number count=1
 done

You only have to modify the number which is generatet from $RANDOM to fit to your Blocks.

EDIT 2016: Please note, the old solution was incorrect. Because we want to overwrite a byte at a random position in the output stream, we have to use seek instead of skip, as mentioned in the comments.

Solution 2:

My system isn't broken and writing from /dev/random is about 650Kb/s. Writing from /dev/urandom is 7Kb/s.

I've worked this issue for myself and for a purely bash solution you have to be realistic about what your goal is because unless your goal is to learn how to do it in bash because the point is doing it ALL in bash, the objective of wiping a disk securely is better accomplished in other ways though bash is useful in the solution.

evildead's bash algorythm to randomly select where dd will write to the drive works however, /dev/urandom is significantly slower than /dev/random and I think it is less secure to pepper the drive with chunks of random data and I think it will be harder to retrieve data from a wiped drive if you quickly do two passes writing one's then zeros to it.

For myself I wrote ones to the drive with:

tr '\0' '\377' < /dev/zero | pv > /dev/sdz

then I wrote zeros to the drive with:

pv < /dev/zero > /dev/sdz

Note the use of pv. It's a great program though you have to install it on most systems. Easy to find, doesn't seem to come with a man page but there is one on-line. It shows the progress of data passing through a pipe and with some programs you can even set it to stop after a certain amount of data is passed.

THEN purely because I was irritated that no bash solution would write random numbers as fast as either of those writes above would do, I wrote a small C program called svrandom to generate random numbers QUICKLY which I called from bash as follows:

while :; do ./svrandom; done  | pv > /dev/sdz

On my system that fills drive /dev/sdz with random numbers as fast as writing /dev/zero does. Goal of wiping drive with random numbers achieved, and bash was used in the solution.

/* svrandom
a small program from shadowvision.com September 2015 to generate strings of random numbers.
the default setting of 10000000 gives about 453 megs, adjust as 
needed */ 

#include <stdio.h>
#include <stdlib.h>
int main()
{
int r,a,b;
 /* adjust following number as needed to suit your string length */
 for(a=0;a<10000000;a++)
 {
 for(b=0;b<5;b++)
 {
 r=rand();
 printf("%d",r);
 }

 }
 return(0);
}

In the source I mention that you can control the length of the random number string by adjusting a number. If you call it from a while loop you wont have to although there may be a carriage return at the end of each loop, but if you want one continuous string from just the program you will have to change the number.

snip! looking around I found this solution on https://superuser.com/questions/19326/how-to-wipe-free-disk-space-in-linux

openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero |pv > /dev/sd{x}

and like the guy there said, I can't believe how fast this is. It's writing to the disk faster than I thought the disks maximum write speed was.

It looks to me that this command is using openssl to encrypt a string of zeros from /dev/zero using /dev/urandom as the seed but im not 100% sure. my solution with the C program to write random numbers writes to my system at 82mbps, this openssl solution is writing at 100mbps. wow!