Page 1 of 1

Increase your program privileges

Posted: Sat Sep 09, 2006 7:03 pm
by newbie
Code updated For 5.20+
Hello,

Increasing it's own program privileges is needed for instance if you want to enumerate all running processes. With the search fucntion I found few other posts with a procedure like this, but mine checks (hopefully) all errors that can happens.

Code: Select all

Procedure.b IncreasePrivileges()
    Protected hToken, Buff
    Protected mLUID.LUID
    Protected mPriv.TOKEN_PRIVILEGES
    Protected mNewPriv.TOKEN_PRIVILEGES
    Protected CurrentProcess = GetCurrentProcess_()
    #TOKEN_ADJUST_PRIVILEGES = $20
	#TOKEN_QUERY = $8
    mPriv\PrivilegeCount = 1
    mPriv\Privileges[0]\Attributes = #SE_PRIVILEGE_ENABLED
    
    If CurrentProcess = 0
    	ProcedureReturn #False
    EndIf
    
    If OpenProcessToken_(CurrentProcess, #TOKEN_ADJUST_PRIVILEGES | #TOKEN_QUERY, @hToken) = 0
    	ProcedureReturn #False
    EndIf
    
    If LookupPrivilegeValue_(#Null, "SeDebugPrivilege", @mLUID) = 0
    	CloseHandle_(hToken) 
    	ProcedureReturn #False
    EndIf
        
    If IsBadWritePtr_(@mPriv\Privileges[0]\Luid, SizeOf(LUID)) <> 0
    	CloseHandle_(hToken) 
    	ProcedureReturn #False
    EndIf
    
    CopyMemory(@mLUID, @mPriv\Privileges[0]\Luid, SizeOf(LUID))
                 
    If AdjustTokenPrivileges_(hToken, #False, @mPriv, SizeOf(TOKEN_PRIVILEGES), @mNewPriv, @Buff) = 0
    	CloseHandle_(hToken) 
    	ProcedureReturn #False
    EndIf
    
    If GetLastError_() <> #ERROR_SUCCESS
    	CloseHandle_(hToken) 
    	ProcedureReturn #False
    EndIf
    
    ; If we get here, then it worked. Returns TRUE
    CloseHandle_(hToken) 
    ProcedureReturn #True
EndProcedure
Then you can use it like this :

Code: Select all

If IncreasePrivileges() = #False
    MessageBox_(0, "Could not retrieve all privileges.", "Error :", #MB_ICONEXCLAMATION)
EndIf
It's not really a "hot stuff", but it might help someone ;)

Posted: Wed Sep 13, 2006 9:24 am
by jpd
Hi newbie,

this is the first step required on the project

http://www.planet-source-code.com/vb/sc ... 9&lngWId=1

:-)

jpd