SimpleWiper Suite – Wiping Files With C#

The SimpleWiper suite of tools includes a SimpleFileWiper application. This application will calculate the size of the file to be deleted. It will then overwrite the file in random 4096 byte chunks.

At the end of the file, the remaining size is calculated and the remaining bytes then overwritten. This is done without changing the size of the file.

The file is then renamed to a random value of the same length.

When these processes have completed, the file is erased from the storage medium.

Wiping a small file:

> .\SimpleFileWiper.exe C:\tools\test\smallfile.txt
[+] The end of/or entire file has been overwritten: (7 bytes of data)
[+] File overwritten successfully.
[+] File deleted after being overwritten.

Wiping a large file:


> .\SimpleFileWiper.exe C:\tools\test\largefile.mp4
Progress: 178,053 blocks (4096 bytes) of 178,054 blocks.
[+] The end of/or entire file has been overwritten: (3782 bytes of data)
[+] File overwritten successfully.
[+] File deleted after being overwritten.

Writing Data to File

Notice below, that two operations take place when wiping a larger file. The initial blocks (4096 chunks) of data are written to the file up to the last block of data. The last block of data size is calculated and then overwritten, preserving the file length.

            for (int i = 0; i <= totalBlocks; i++)
            {
                if (i == totalBlocks)
                {
                    var totalFileLength = fileLength - (4096 * totalBlocksWritten);
                    bytesBuffer = new byte[((int)totalFileLength)];
                    new Random().NextBytes(bytesBuffer);

                    streamWriter.BaseStream.Write(bytesBuffer, 0, bytesBuffer.Length);

                    Console.WriteLine($"[+] The end of/or entire file has " +
                        $"been overwritten: ({bytesBuffer.Length} bytes of data)");
                }
                else
                {
                    streamWriter.BaseStream.Write(bytesBuffer, 0, bytesBuffer.Length);

                    Console.SetCursorPosition(0, 0);
                    Console.WriteLine($"[+] Progress: {i:n0} blocks (4096 bytes) of " +
                        $"{totalBlocks:n0} blocks.");

                    totalBlocksWritten += 1;
                }
            }

The filename is then changed to a random double value and the path including the filename are returned.

        public static string ChangeFilename(string path)
        {
            var varRandomFileName = new Random().NextDouble();
            var directoryPath = Path.GetDirectoryName(path);
            var fileName = Path.GetFileName(path);

            File.Move(directoryPath + "\\" + fileName,
                directoryPath + "\\" + varRandomFileName);

            return directoryPath + "\\" + varRandomFileName;
        }

Finally, with the returned file path, the file is simply deleted.

        public static bool DeleteFileAfterWipe(string path)
        {
            try
            {
                File.Delete(path);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                ExitApplicationWithError();
            }

            return true;
        }

Download the source code from GitHub

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest article