githubEdit

Self Deletion

Malware Suicide

Introduction

The name should be self explanatory, but anyways, malware deleting itself. Now what if our malware is caught by the blue team?, we really want to slow down their efforts to analyze it as much as possible. Thus comes self deletion, when we feel like we are being "observed" aka debugged or analyzed, we can just delete ourselves. What we do here is abuse the NTFS alternate data stream. As you must have probably faced this problem, whenever you try deleting something that is being used by another process, you get the "Access Denied" error which at times could be quite annoying.

To view files with alternate data stream (also referred as ADS below), we can type the dir /r command in cmd.

A more "stealthier" way to use the ADS to create files with reserved names like CON which is explained herearrow-up-right.

Explanation

The NTFS file system has an alternate data stream, which basically was created for compatibility purpose which you can look up. By default, every file system has a :$DATA file stream, although we can create ADS (alternate data stream) with any name we like. It is not possible to delete a file / process when it is being run, but there is a way to achieve what we want, and that is by renaming the file to some ADS and then deleting that ADS, which in turn deletes the main file, achieving our goal.

Self Deletion

First we need to get the handle to our file using the CreateFileW and then we need to rename our file using the SetFileInformationByHandle function.

We can see here that the alternate data stream file is created as soon as the SetFileInformationByHandle is called.

Now that we have set the ADS , we need to again get the handle to the file. Think of this as "refreshing" so that we have the handle to the file with updated details (this should allow us to delete it which wasn't possible earlier).

And we update the File_DISPOSITION_INFO structure and mark it as delete again using the SetFileInformationByHandle call, although this marks the file for deletion, the file isn't deleted until the handle isn't closed.

As soon as the rename succeeds, the actual file kms.exe has its size back to 0 because now the file has been renamed to an ADS.

After which all we have to do is to get the handle again, which allows us to mark the actual file for deletion and as soon as we close the handle to it, the file gets deleted.

PoC

Conclusion

That's it for now, I'll update this same blog again later to showcase a similar technique to delete the file in windows 11 since this trick only works for windows 10 and below. If you think I made a mistake, you can reach out to me on twitter. Thanks.

References

Last updated