
Can an exe be made to delete itself?
Can an exe be made to delete itself?
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?
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)
Last edited by PB on Tue Oct 26, 2010 12:58 am, edited 1 time in total.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
"PureBasic won't be object oriented, period" - Fred.
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
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()
BERESHEIT
-
- Enthusiast
- Posts: 480
- Joined: Thu Jul 27, 2006 4:06 am
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?.
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?
Can anyone confirm if this works on Vista.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)
-
- Addict
- Posts: 1073
- Joined: Fri Apr 25, 2003 11:13 pm
- Location: Netherlands
- Contact:
Re: Can an exe be made to delete itself?
This does not work on 7.
Procrastinators unite... tomorrow!
Re: Can an exe be made to delete itself?
Works here. Windows 7, 32bwallgod wrote:This does not work on 7.
Re: Can an exe be made to delete itself?
This is not reliable on any os. It relies on pure timing luck.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)
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Can an exe be made to delete itself?
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.
BERESHEIT
Re: Can an exe be made to delete itself?
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)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.
Code: Select all
t(0) call cmd
t(1) delete End <- theoretically simultanious