Page 1 of 2

Can an exe be made to delete itself?

Posted: Sat Jan 19, 2008 6:37 am
by Mistrel
Is there any way an exe can load itself into memory and delete the executable that ran it? I don't think this is possible but I thought I'd ask. :)

Re: Can an exe be made to delete itself?

Posted: Sat Jan 19, 2008 7:41 am
by PB
NOTE: See the "bug-fixed" version (by myself) later in this thread. Use this one at your own risk!

Code: Select all

; Delete running exe, by PB. Works under all versions of Windows.

MessageBox_(0,"This exe will self-destruct when you click OK","Test",0)
RunProgram(GetEnvironmentVariable("comspec"),"/c del "+Chr(34)+ProgramFilename()+Chr(34),"",2)

Posted: Sat Jan 19, 2008 8:23 am
by Mistrel
Ahh, I see. Launch a delete from the command prompt just before closing the program. Excellent, thank you. :D

Posted: Sat Jan 19, 2008 8:43 am
by netmaestro

Code: Select all

Procedure DeleteMyself()
  scriptname$ = "UNINST"
  extension$  = ".vbs"
  append=0
  testfilename$ = scriptname$+RSet(Str(append),2,"0")+extension$
  While FileSize(testfilename$) <> -1
    append+1
    testfilename$ = scriptname$+RSet(Str(append),2,"0")+extension$
  Wend
  If CreateFile(0, testfilename$)
    WriteStringN(0, "' Wait for calling exe to close, then delete it ")
    WriteStringN(0, "Wscript.Sleep 1000")
    WriteStringN(0, "Dim fso, Filename")
    WriteStringN(0, "Set fso = CreateObject("+Chr(34)+"Scripting.FileSystemObject"+Chr(34)+")")
    WriteStringN(0, "Set Filename = fso.GetFile("+Chr(34)+ProgramFilename()+Chr(34)+")")
    WriteStringN(0, "Filename.Delete") 
    WriteStringN(0, "' ")                                         
    WriteStringN(0, "' Now delete this script ")
    WriteStringN(0, "Dim objFSO")
    WriteStringN(0, "Set objFSO = CreateObject("+Chr(34)+"Scripting.FileSystemObject"+Chr(34)+")")
    WriteStringN(0, "objFSO.DeleteFile WScript.ScriptFullName") 
    WriteStringN(0, "Set objFSO = Nothing")                                    
    CloseFile(0) 
    RunProgram(testfilename$)
    End
  EndIf
  ProcedureReturn 0
EndProcedure

DeleteMyself()
Compile to exe, run it and watch it disappear.

Posted: Sat Jan 19, 2008 10:13 am
by superadnim
nice but it wont run on my system due to security issues, no unauthorized vbs can run in here... I'm guessing the same would occur on many other systems. If that's not the case, a firewall might even popup claiming that the application is attempting a suspicious action which for many end-users is a turn off (believe me when I say this, they don't really know what a vbs is thus they tend to think anything, I'm not generalizing here but the average user will indeed think bad about this sort of operations).

I made a program that communicates with another application and it was flagged as a virus on many databases... even thought it does nothing bad/wrong nor it behaves like one. It's really annoying when people flag your stuff as malicious even though it isn't and even worse is the fact that those companies maintaining the databases don't even confirm the reports, they simply flag applications as malicious and then they review them when they feel like it (or they get tons of complaints)... they shouldn't do that to begin with :(


In this case, perhaps a bat would pass more lightly?.

Re: Can an exe be made to delete itself?

Posted: Thu Mar 26, 2009 6:55 am
by idle
PB wrote:

Code: Select all

; Delete running exe, by PB. Works under all versions of Windows.

MessageBox_(0,"This exe will self-destruct when you click OK","Test",0)
RunProgram(GetEnvironmentVariable("comspec"),"/c del "+Chr(34)+ProgramFilename()+Chr(34),"",2)
Can anyone confirm if this works on Vista.

Posted: Thu Mar 26, 2009 10:42 am
by srod
Yep worked fine here on Vista 32. Tested only in admin mode; though I doubt whether that would make a difference.

Posted: Thu Mar 26, 2009 1:34 pm
by ricardo
Yes, work fine in Vista.

Posted: Thu Mar 26, 2009 10:56 pm
by idle
Ok thanks guys.

Posted: Fri Mar 27, 2009 12:13 pm
by Edwin Knoppert
This may not work when the app resides on a network device?

Re: Can an exe be made to delete itself?

Posted: Mon Oct 25, 2010 5:37 pm
by wallgod
This does not work on 7.

Re: Can an exe be made to delete itself?

Posted: Mon Oct 25, 2010 6:10 pm
by TomS
wallgod wrote:This does not work on 7.
Works here. Windows 7, 32b

Re: Can an exe be made to delete itself?

Posted: Mon Oct 25, 2010 6:25 pm
by Trond
PB wrote:

Code: Select all

; Delete running exe, by PB. Works under all versions of Windows.

MessageBox_(0,"This exe will self-destruct when you click OK","Test",0)
RunProgram(GetEnvironmentVariable("comspec"),"/c del "+Chr(34)+ProgramFilename()+Chr(34),"",2)
This is not reliable on any os. It relies on pure timing luck.

Re: Can an exe be made to delete itself?

Posted: Mon Oct 25, 2010 6:41 pm
by netmaestro
Trond, could you elaborate please? How does timing luck affect it? Since PB posted that quite some time ago I've used it and never saw it fail.

Re: Can an exe be made to delete itself?

Posted: Mon Oct 25, 2010 6:46 pm
by TomS
netmaestro wrote:Trond, could you elaborate please? How does timing luck affect it? Since PB posted that quite some time ago I've used it and never saw it fail.
I guess if cmd has a high priority (for any reason), the call could be executed before the program ends (like it's actually the case)

Code: Select all

t(0)       call cmd
t(1)   delete     End <- theoretically simultanious