Skip to content

Instantly share code, notes, and snippets.

@CharlesAverill
Created October 1, 2021 05:30
Show Gist options
  • Save CharlesAverill/862e8cb776cb1db1ef43823d22fbaaaa to your computer and use it in GitHub Desktop.
Save CharlesAverill/862e8cb776cb1db1ef43823d22fbaaaa to your computer and use it in GitHub Desktop.
/*
* Author: Charles Averill
* WARNING: Don't run this on anything important
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/statvfs.h>
#include <sys/random.h>
int main(int argc, char *argv[]){
// Check arguments
if(argc != 2){
printf("Usage: hyperase DRIVE\n");
return 1;
}
char *fn = argv[1];
// Warnings
printf("\033[31mWARNING: THIS PROGRAM WILL ATTEMPT TO OVERWRITE EVERY BYTE ON THE PROVIDED PARTITION %s\n", fn);
printf("THIS OPERATION COULD BE COMPLETELY IRREVERSIBLE\n");
// Confirmation
char correct_confirmation[300] = "\0";
strcat(correct_confirmation, "I WOULD LIKE TO ERASE ");
strcat(correct_confirmation, fn);
strcat(correct_confirmation, "\n\0");
printf("IF YOU WOULD LIKE TO COMPLETELY ERASE THIS PARTITION, ENTER \"I WOULD LIKE TO ERASE {partition_name}\": ");
printf("\033[0m");
fflush(stdout);
char confirmation[300] = "\0";
read(0, confirmation, 300);
if(strcmp(confirmation, correct_confirmation)){
printf("Exiting\n");
return 1;
}
// Delay
printf("You have 5 seconds to interrupt this script\n");
for(int i = 0; i < 5; i++){
printf("%d\n", 5 - i);
sleep(1);
}
// Open partition
FILE *fp = fopen(fn, "r+b");
if(fp == NULL){
perror("Error opening partition");
return 1;
}
// Get partition size
fseek(fp, 0, SEEK_END);
long long int size = ftell(fp);
printf("%lld\n", size);
// Erase
size_t blksize = size > 512 ? 512 : size;
long long int n_blks = size / blksize;
char rnd[blksize];
for(int i = 1; i < 51; i++){
rewind(fp);
printf("Erase #%d\n", i);
for(int x = 0; x < n_blks; x++){
// Fill each block with random bytes
if(getrandom(rnd, blksize, GRND_RANDOM) == -1){
perror("getrandom error");
return 1;
}
if(fwrite(rnd, 1, blksize, fp) == -1){
perror("write error");
return 1;
}
}
}
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment