Last active
September 10, 2016 13:05
-
-
Save JLChnToZ/b8983f681b5faf64ab3df74b1ea90576 to your computer and use it in GitHub Desktop.
Simple script which will self destruct after executing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using System.Diagnostics; | |
| using System.Reflection; | |
| namespace SelfDestruct { | |
| public class SelfDestructor { | |
| const string batScript = "@echo off\r\n" + | |
| ":trydelete\r\n" + | |
| "echo \"%~f1\">\"%~f1\"\r\n" + | |
| "del \"%~f1\"\r\n" + | |
| "@timeout /t 0\r\n" + | |
| "if exist \"%~f1\" goto trydelete\r\n" + | |
| "attrib -h \"%~f0\"\r\n" + | |
| "del \"%~f0\""; | |
| static Process batProcess; | |
| static FileInfo tempFile; | |
| static FileSystemWatcher tempFileWatcher; | |
| static bool isRecreatingTempFile, isSpawningProcess, adminPermission; | |
| public static void SelfDestruct(bool adminPermission = false) { | |
| if((batProcess != null && !batProcess.HasExited) || isSpawningProcess) | |
| return; | |
| CreateBatchFile(); | |
| SelfDestructor.adminPermission = adminPermission; | |
| SpawnProcess(); | |
| } | |
| private static void CreateBatchFile() { | |
| bool recreateState = isRecreatingTempFile; | |
| isRecreatingTempFile = true; | |
| if(tempFile != null) | |
| try { | |
| if(tempFile.Exists) tempFile.Delete(); | |
| } catch { } | |
| tempFile = new FileInfo(Path.GetTempFileName()); | |
| using(var stream = tempFile.OpenWrite()) | |
| using(var writer = new StreamWriter(stream)) | |
| writer.Write(batScript); | |
| if(!tempFile.Name.EndsWith(".bat", StringComparison.OrdinalIgnoreCase)) | |
| tempFile.MoveTo(tempFile.Name + ".bat"); | |
| tempFile.Attributes |= FileAttributes.Hidden | FileAttributes.Temporary; | |
| isRecreatingTempFile = recreateState; | |
| RegisterWatcher(); | |
| } | |
| private static void RegisterWatcher() { | |
| if(tempFile == null) CreateBatchFile(); | |
| if(tempFileWatcher != null) { | |
| tempFileWatcher.Filter = tempFile.Name; | |
| tempFileWatcher.Path = tempFile.DirectoryName; | |
| return; | |
| } | |
| tempFileWatcher = new FileSystemWatcher(tempFile.DirectoryName, tempFile.Name) { | |
| NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size | |
| }; | |
| tempFileWatcher.Changed += OnChnagedFile; | |
| tempFileWatcher.Renamed += OnChnagedFile; | |
| tempFileWatcher.Deleted += OnChnagedFile; | |
| tempFileWatcher.EnableRaisingEvents = true; | |
| } | |
| private static void SpawnProcess() { | |
| isSpawningProcess = true; | |
| if(batProcess != null && !batProcess.HasExited) { | |
| batProcess.Exited -= OnDeleterExited; | |
| batProcess.Kill(); | |
| } | |
| batProcess = new Process(); | |
| batProcess.EnableRaisingEvents = true; | |
| batProcess.Exited += OnDeleterExited; | |
| ProcessStartInfo startInfo = batProcess.StartInfo; | |
| startInfo.FileName = "cmd.exe"; | |
| startInfo.Arguments = string.Format("/c \"\"{0}\" \"{1}\"\"", tempFile.FullName, Assembly.GetExecutingAssembly().Location); | |
| startInfo.CreateNoWindow = true; | |
| startInfo.UseShellExecute = false; | |
| startInfo.WindowStyle = ProcessWindowStyle.Hidden; | |
| startInfo.Verb = adminPermission ? "runas" : ""; | |
| batProcess.Start(); | |
| batProcess.PriorityClass = ProcessPriorityClass.Idle; | |
| isSpawningProcess = false; | |
| } | |
| private static void OnDeleterExited(object sender, EventArgs e) { | |
| if(!isSpawningProcess && batProcess.HasExited) | |
| SpawnProcess(); | |
| } | |
| private static void OnChnagedFile(object sender, FileSystemEventArgs e) { | |
| if(isRecreatingTempFile) return; | |
| if(tempFile == null || e.Name == tempFile.Name) { | |
| bool hasChanged = tempFile == null || !tempFile.Exists; | |
| if(!hasChanged) | |
| try { | |
| if(tempFile.Length == batScript.Length) | |
| using(var reader = tempFile.OpenText()) | |
| hasChanged = !string.Equals(reader.ReadToEnd(), batScript, StringComparison.OrdinalIgnoreCase); | |
| else | |
| hasChanged = true; | |
| } catch { | |
| hasChanged = true; | |
| } | |
| isRecreatingTempFile = true; | |
| if(hasChanged) CreateBatchFile(); | |
| } | |
| if(batProcess == null || batProcess.StartInfo.Arguments.IndexOf(tempFile.FullName, StringComparison.OrdinalIgnoreCase) < 0) | |
| SpawnProcess(); | |
| isRecreatingTempFile = false; | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demostration