protect process from closing with taskmanager

Share your advanced PureBasic knowledge/code with the community.
codeprof
User
User
Posts: 65
Joined: Sun Sep 16, 2012 12:13 pm

protect process from closing with taskmanager

Post by codeprof »

This prevents access by other processes. So you cannot terminate the process with the task manager. Of course with administrator rights you can still do it.

Code: Select all

; Licence: Public domain
; Autor: codeprof

#OWNER_SECURITY_INFORMATION    =   $00000001
#GROUP_SECURITY_INFORMATION    =   $00000002
#DACL_SECURITY_INFORMATION     =   $00000004
#SACL_SECURITY_INFORMATION     =   $00000008
#LABEL_SECURITY_INFORMATION    =   $00000010
 
#PROTECTED_DACL_SECURITY_INFORMATION   =  $80000000
#PROTECTED_SACL_SECURITY_INFORMATION   =  $40000000
#UNPROTECTED_DACL_SECURITY_INFORMATION =  $20000000
#UNPROTECTED_SACL_SECURITY_INFORMATION =  $10000000
 
*pACL.ACL
cbACL = 1024;
 
; Initialize a security descriptor.
*pSD.SECURITY_DESCRIPTOR = AllocateMemory(#SECURITY_DESCRIPTOR_MIN_LENGTH)
InitializeSecurityDescriptor_(*pSD, #SECURITY_DESCRIPTOR_REVISION)
*pACL = AllocateMemory(cbACL);
InitializeAcl_(*pACL, cbACL, #ACL_REVISION2)
SetSecurityDescriptorDacl_(*pSD, #True, *pACL, #False)
 
;SetFileSecurity_("C:\TEST.TXT",#DACL_SECURITY_INFORMATION, *pSD) ; <-- remove all rights from a certain file
SetKernelObjectSecurity_(GetCurrentProcess_(), #DACL_SECURITY_INFORMATION, *pSD) ; <-- now you cannot close the process with the task manager



OpenWindow(1,0,0,500,300,"Try to close me with task manager")
ButtonGadget(0,0,0,100,20,"close")
Repeat
Until WaitWindowEvent() = #PB_Event_Gadget And EventGadget() = 0
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: protect process from closing with taskmanager

Post by jassing »

cool -- but only works on x86; when I run it as a 64bit app, I can kill it w/o issue. I noticed when I ran it as 32bit app, the user name was blank in task manager, but as a 64bit app, the user name was there.
PureGuy
Enthusiast
Enthusiast
Posts: 102
Joined: Mon Aug 30, 2010 11:51 am

Re: protect process from closing with taskmanager

Post by PureGuy »

Thanks, codeprof.

@jassing it works here as x64 exe on Windows 7.
Post Reply