#3
Post
by GeoffVass » 29 Jun 2023 02:10
Generally speaking, you can't rely on the idea that writing new data to a file will replace the existing data. A 'file' consists of some metadata such as the name, and a linked list of pointers to clusters on the disk which the file system uses to construct the contents. NTFS is a transactional database, in essence, so if you try to overwrite a file with the echo command, most likely the list of clusters used by the file previously will be untouched and the file's data will be created with a new list of clusters. Then the original clusters will be marked as available for the file system to use later. In a transactional database, you don't ever overwrite physical blocks that have data in them; you write new blocks. That way the database can roll back if there is any corruption. So in the scenario where you 'echo' out to the same file name, if you could look at the contents of clusters at a low level you'd still be able to see the contents of the original file.
To add to the complexity, NTFS actually stores very small files in the Master File Table itself, because the overhead of maintaining the links to clusters outside the MFT is more than just putting the data in the MFT. And generally you can't write to the MFT because the file system is in complete control of that. The very small files in your example would be stored in the MFT.
SDELETE from Sysinternals handles all this by opening the file and following the cluster chain so it overwrites the specific clusters in use by the file, and it also has the ability to wipe the space occupied in the MFT.
So the answer is that you can't do this from batch, the process needs very sophisticated access to file system APIs.
Additionally, depending on how long the file exists, it might have been captured by a shadow copy, so deleting/modifying the original file won't take the data off the disk unless you remove all the shadow copies first.
But in reality, if your concern is attackers who can examine the drive at a low level, use Bitlocker, and/or have drives disposed of with degaussing.