A Rust implementation that allows a running Windows executable to delete itself from disk using Windows API calls. This technique works around file locking mechanisms by leveraging Alternate Data Streams (ADS) and file disposition flags.
- Windows 10, Windows 11 (pre-24H2): Uses traditional
FILE_DISPOSITION_INFOmethod - Windows 11 24H2 and later: Uses new
FILE_DISPOSITION_INFO_EXmethod withFILE_DISPOSITION_FLAG_POSIX_SEMANTICSto comply with enhanced security restrictions (tested on 25H2) - Cross-compilation: Supports building from macOS/Linux to Windows targets
The code uses Windows API bindings to achieve self-deletion through a multi-step process:
- Retrieves the path of the current executable using
GetModuleFileNameW - Creates a file handle with
DELETEandSYNCHRONIZEaccess rights - Renames the file to an Alternate Data Stream (e.g.,
:backtomato) usingSetFileInformationByHandlewithFILE_RENAME_INFO - This breaks the file locking while keeping the executable running
For Windows 11 24H2 and later:
- Uses
FILE_DISPOSITION_INFO_EXstructure - Applies
FILE_DISPOSITION_FLAG_DELETE | FILE_DISPOSITION_FLAG_POSIX_SEMANTICSflags - Required due to security enhancements in Windows 24H2
For older Windows versions:
- Uses traditional
FILE_DISPOSITION_INFOstructure - Sets
DeleteFile = TRUEfor immediate deletion
The implementation includes automatic detection and fallback:
- Attempts the new Windows 24H2 method first
- Falls back to traditional method if the new API is unavailable
- Handles both scenarios gracefully without user intervention