Here is a little tool for Windows that replaces the content of a file with zero-bytes, keeping the same filesize. It works. The goal is to be sure that the old bytes-content of the file is no more recoverable on disk.
Question: how to be 100% sure that it replaces the content in-place and that it doesn't allocate new sectors on disk to write the zero bytes? (thus leaving the old content somewhere else, still possibly accessible, which would be a security hole!)
Note: I know that freewares exist for this, but I would like to try a simple implementation first
Note2: I know that some more advanced wiping techniques exist (random content and/or multi-pass, etc.), but for me zeroing the content is enough if at least I know that the zeroing is done in-place, i.e. if the new zero bytes replace the old content at the same location on disk (and not moved to another sectors on disk!)
Could you review the following code?
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *f;
if (argc != 2) { fprintf (stderr, "Usage: %s <file>\n", argv[0]); exit(1); }
if ((f = fopen(argv[1], "rb+")) == NULL) { perror("Error opening the file"); exit(1); }
fseek(f, 0L, SEEK_END);
unsigned long size = ftell(f);
fseek(f, 0L, SEEK_SET);
char buf[1024] = {0};
unsigned long num = size / 1024;
int remaining = size % 1024;
for (unsigned long i = 0; i < num; i++)
fwrite(buf, 1, 1024, f);
fwrite(buf, 1, remaining, f);
fclose(f);
exit(0);
}
Securely deleting a file that has no special attributes is relatively straight-forward: the secure delete program simply overwrites the file with the secure delete pattern. What is more tricky is securely deleting Windows NT/2K compressed, encrypted and sparse files, and securely cleansing disk free spaces.
So I guess if the file has no special attributes and you overwrite it, it goes on the same disk location. Although using standard libraries invalidates any such assumptions and you should go with Win32 APIs. \$\endgroup\$