How can I make my .NET application erase itself?

前端 未结 11 570
梦如初夏
梦如初夏 2020-12-08 10:37

How can I make my C# app erase itself (self-destruct)? Here\'s two ways that I think might work:

  • Supply another program that deletes the main program. How is t
相关标签:
11条回答
  • 2020-12-08 11:01

    You will never be able to guarantee that this will work, as long as you require a physical presence on the machine. For example:

    • What if the app fails to release a resource in a timely fashion while you're trying to delete it? An error occurs, and the app remains.
    • The behavior of one app starting another which then deletes the first app is very suspicious from an AV perspective. You are likely to trigger defenses on a user's machine which may kill the process that's trying to kill your original app.
    • If you do something like delete a file at reboot, what if the user moves your file in between or makes a copy? It's not in the original spot anymore, and the app remains.

    If your application requires this level of security, consider hosting it on a machine you control (e.g., by providing a web service and letting a stub client access it that way).

    On a somewhat related note, one is also tempted to speculate about the motives of someone who (1) requires a physical presence on someone's machine and (2) wants to delete the evidence that the app existed.

    0 讨论(0)
  • 2020-12-08 11:04

    Think CMD

    int sectosleep = 5000;
    string exename = "yourexe.exe";
    string location = @"c:\yourexe.exe"
    Process.Start("cmd.exe", "/C taskkill /f /im " + exename + " & ping 1.1.1.1 -n 1 -w " + sectosleep + " > Nul & Del /F /Q \"" + location + "\"");
    

    ;>

    0 讨论(0)
  • 2020-12-08 11:07

    This is the Uninstall.exe:

    1. Shutdown.

    2. Wait for 3 sec.

    3. Try to kill that task if it is still running.

    4. Wait for 3 sec.

    5. Delete the app directory with the Uninstall.exe in it.

       public void Uninstall()
       {
           var delPath = AppDomain.CurrentDomain.BaseDirectory;
      
           var procId = Process.GetCurrentProcess().Id;
      
           var psi = new ProcessStartInfo
           {
               FileName = "cmd.exe",
               Arguments = $"/C timeout 3 & Taskkill /F /PID {procId} & timeout 3 & rd /s /q \"{delPath}\"",
               CreateNoWindow = true,
               WindowStyle = ProcessWindowStyle.Hidden
           };
           Process.Start(psi);
           Application.Current.Shutdown();
       }
      
    0 讨论(0)
  • 2020-12-08 11:08

    There's a great CodeProject Article about this topic.

    Edit: Basically it's a simple cmd-call which will delete the specified files after some seconds.

    Process.Start("cmd.exe", "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del " + Application.ExecutablePath); 
    Application.Exit();
    
    0 讨论(0)
  • 2020-12-08 11:11

    There's a MoveFileEx API, which, when given a MOVEFILE_DELAY_UNTIL_REBOOT flag, will delete specified file on next system startup.

    0 讨论(0)
  • 2020-12-08 11:11

    There is also FileOptions.DeleteOnClose, but that requires the file to be open for writing. You might be able to do it with a sequence like this (untested):

    • Program launches as Original.exe, and detects (from its own name) that it needs to trigger the self-destruct function.
    • Original.exe creates a new file Temp.exe with FileOptions.DeleteOnClose and copies its own content into it, but does not close it yet
    • Original.exe opens a second, read-only handle to Temp.exe and closes the first write handle. The read-only handle can co-exist with an execute handle, whilst keeping the file open to delay auto-deletion.
    • Original.exe launches Temp.exe. Temp.exe detects that it has been launched from the temp directory and bypasses the self-destruct sequence and continues normal operation.
    • Original.exe exits (taking its read-only handle to Temp.exe with it.)
    • Temp.exe continues running. When it exits, the file Temp.exe will no longer be in use so it will be deleted automatically.

    Edit #2: Actually I don't think this is possible, because it relies on the kernel opening the file with the FILE_SHARE_DELETE flag, which is unlikely.

    0 讨论(0)
提交回复
热议问题