Can an exe be made to delete itself?

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Can an exe be made to delete itself?

Post 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. :)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Can an exe be made to delete itself?

Post 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)
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.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

Ahh, I see. Launch a delete from the command prompt just before closing the program. Excellent, thank you. :D
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post 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?.
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Can an exe be made to delete itself?

Post 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.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Yep worked fine here on Vista 32. Tested only in admin mode; though I doubt whether that would make a difference.
I may look like a mule, but I'm not a complete ass.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post by ricardo »

Yes, work fine in Vista.
ARGENTINA WORLD CHAMPION
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

Ok thanks guys.
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

This may not work when the app resides on a network device?
wallgod
User
User
Posts: 48
Joined: Wed Oct 06, 2010 2:03 pm

Re: Can an exe be made to delete itself?

Post by wallgod »

This does not work on 7.
Procrastinators unite... tomorrow!
User avatar
TomS
Enthusiast
Enthusiast
Posts: 342
Joined: Sun Mar 18, 2007 2:26 pm
Location: Munich, Germany

Re: Can an exe be made to delete itself?

Post by TomS »

wallgod wrote:This does not work on 7.
Works here. Windows 7, 32b
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Can an exe be made to delete itself?

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
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?

Post 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.
BERESHEIT
User avatar
TomS
Enthusiast
Enthusiast
Posts: 342
Joined: Sun Mar 18, 2007 2:26 pm
Location: Munich, Germany

Re: Can an exe be made to delete itself?

Post 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
Post Reply