Tested in Windows 7 64bit, and Windows XP Pro.
Elevates a process to SYSTEM... test it by compiling the previous code, and running it from this script?
*** if you run it as is - the Calculator should open - check Task Manager to make sure it's running as SYSTEM ***
Code: Select all
#UNTRUSTED_INTEGRITY_SID = "S-1-16-0"
#LOW_INTEGRITY_SID = "S-1-16-4096"
#MEDIUM_INTEGRITY_SID = "S-1-16-8192"
#MEDIUM_PLUS_INTEGRITY_SID = "S-1-16-8448"
#HIGH_INTEGRITY_SID = "S-1-16-12288"
#SYSTEM_INTEGRITY_SID = "S-1-16-16384"
#PROTECTED_PROCESS_MANDATORY_SID = "S-1-16-20480"
#SECURITY_MANDATORY_UNTRUSTED_RID = $00000000
#SECURITY_MANDATORY_LOW_RID = $00001000
#SECURITY_MANDATORY_MEDIUM_RID = $00002000
#SECURITY_MANDATORY_MEDIUM_PLUS_RID = $00002100
#SECURITY_MANDATORY_HIGH_RID = $00003000
#SECURITY_MANDATORY_SYSTEM_RID = $00004000
#SECURITY_MANDATORY_PROTECTED_PROCESS_RID = $00005000
#SE_GROUP_INTEGRITY = $00000020
Enumeration
#TokenUser = 1
#TokenGroups
#TokenPrivileges
#TokenOwner
#TokenPrimaryGroup
#TokenDefaultDacl
#TokenSource
#TokenType
#TokenImpersonationLevel
#TokenStatistics
#TokenRestrictedSids
#TokenSessionId
#TokenGroupsAndPrivileges
#TokenSessionReference
#TokenSandBoxInert
#TokenAuditPolicy
#TokenOrigin
#TokenElevationType
#TokenLinkedToken
#TokenElevation
#TokenHasRestrictions
#TokenAccessInformation
#TokenVirtualizationAllowed
#TokenVirtualizationEnabled
#TokenIntegrityLevel
#TokenUIAccess
#TokenMandatoryPolicy
#TokenLogonSid
#MaxTokenInfoClass
EndEnumeration
Enumeration
#SE_UNKNOWN_OBJECT_TYPE
#SE_FILE_OBJECT
#SE_SERVICE
#SE_PRINTER
#SE_REGISTRY_KEY
#SE_LMSHARE
#SE_KERNEL_OBJECT
#SE_WINDOW_OBJECT
#SE_DS_OBJECT
#SE_DS_OBJECT_ALL
#SE_PROVIDER_DEFINED_OBJECT
EndEnumeration
Structure TOKEN_MANDATORY_LABEL
Label.SID_AND_ATTRIBUTES
EndStructure
Prototype.b protoConvertStringSidToSid(StringSid.s, *Sid)
Global ConvertStringSidToSid.protoConvertStringSidToSid
Prototype.i protoCreateProcessWithToken(hToken.i, dwLogonFlags.l, lpApplicationName.s, lpCommandLine.s, dwCreationFlags.l, lpEnvironment.i, lpCurrentDirectory.s, lpStartupInfo.i, lpProcessInfo.i)
Global CreateProcessWithToken.protoCreateProcessWithToken
Prototype.b protoCreateEnvironmentBlock(lpEnvironment, hToken, bInherit.b)
Global CreateEnvironmentBlock.protoCreateEnvironmentBlock
Prototype.b protoDestroyEnvironmentBlock(lpEnvironment)
Global DestroyEnvironmentBlock.protoDestroyEnvironmentBlock
Procedure.i GetWindowsVersion()
Result.i = 0
Select OSVersion()
Case #PB_OS_Windows_2000 : Result = 50
Case #PB_OS_Windows_XP : Result = 51
Case #PB_OS_Windows_Server_2003 : Result = 52
Case #PB_OS_Windows_Vista : Result = 60
Case #PB_OS_Windows_7 : Result = 61
EndSelect
ProcedureReturn Result
EndProcedure
Procedure.b AdjustCurrentProcessPrivilege()
Result.b = #False
If OpenProcessToken_(GetCurrentProcess_(), #TOKEN_ADJUST_PRIVILEGES | #TOKEN_QUERY, @TokenHandle)
lpLuid.LUID
If LookupPrivilegeValue_(#Null, #SE_DEBUG_NAME, @lpLuid)
NewState.TOKEN_PRIVILEGES
With NewState
\PrivilegeCount = 1
\Privileges[0]\Luid\LowPart = lpLuid\LowPart
\Privileges[0]\Luid\HighPart = lpLuid\HighPart
\Privileges[0]\Attributes = #SE_PRIVILEGE_ENABLED
EndWith
Result = AdjustTokenPrivileges_(TokenHandle, #False, @NewState, SizeOf(TOKEN_PRIVILEGES), @PreviousState.TOKEN_PRIVILEGES, @ReturnLength)
EndIf
CloseHandle_(TokenHandle)
EndIf
ProcedureReturn Result
EndProcedure
Procedure.s GetProcessIntegrityLevel(ProcessId)
Result.s = ""
ProcessHandle = OpenProcess_(#MAXIMUM_ALLOWED, #False, ProcessId)
If ProcessHandle <> 0
If OpenProcessToken_(ProcessHandle, #MAXIMUM_ALLOWED, @TokenHandle)
GetTokenInformation_(TokenHandle, #TokenIntegrityLevel, #Null, 0, @ReturnLength)
*SIDAndAttributes.SID_AND_ATTRIBUTES
*SIDAndAttributes = AllocateMemory(ReturnLength)
If *SIDAndAttributes <> #Null
If GetTokenInformation_(TokenHandle, #TokenIntegrityLevel, *SIDAndAttributes, ReturnLength, @ReturnLength)
If IsValidSid_(*SIDAndAttributes\Sid)
Select PeekL(GetSidSubAuthority_(*SIDAndAttributes\Sid, PeekL(GetSidSubAuthorityCount_(*SIDAndAttributes\Sid)) - 1))
Case #SECURITY_MANDATORY_UNTRUSTED_RID
Result = "UntrustedIntegrityLevel"
Case #SECURITY_MANDATORY_LOW_RID
Result = "LowIntegrityLevel"
Case #SECURITY_MANDATORY_MEDIUM_RID
Result = "MediumIntegrityLevel"
Case #SECURITY_MANDATORY_MEDIUM_PLUS_RID
Result = "MediumPlusIntegrityLevel"
Case #SECURITY_MANDATORY_HIGH_RID
Result = "HighIntegrityLevel"
Case #SECURITY_MANDATORY_SYSTEM_RID
Result = "SystemIntegrityLevel"
Case #SECURITY_MANDATORY_PROTECTED_PROCESS_RID
Result = "ProtectedProcessLevel"
EndSelect
EndIf
EndIf
FreeMemory(*SIDAndAttributes)
EndIf
CloseHandle_(TokenHandle)
EndIf
CloseHandle_(ProcessHandle)
EndIf
ProcedureReturn Result
EndProcedure
Procedure.s GetTokenUserName(ProcessId)
Result.s = ""
ProcessHandle = OpenProcess_(#MAXIMUM_ALLOWED, #False, ProcessId)
If ProcessHandle <> 0
If OpenProcessToken_(ProcessHandle, #MAXIMUM_ALLOWED, @TokenHandle)
GetTokenInformation_(TokenHandle, #TokenUser, #Null, 0, @ReturnLength)
*SIDAndAttributes.SID_AND_ATTRIBUTES
*SIDAndAttributes = AllocateMemory(ReturnLength)
If *SIDAndAttributes <> #Null
If GetTokenInformation_(TokenHandle, #TokenUser, *SIDAndAttributes, ReturnLength, @ReturnLength)
*Name = AllocateMemory(#MAX_PATH)
*Domain = AllocateMemory(#MAX_PATH)
If *Name <> #Null And *Domain <> #Null
If LookupAccountSid_(#Null, *SIDAndAttributes\Sid, *Name, @ReturnLength, *Domain, @ReturnLength, @peUse)
Result = PeekS(*Name)
EndIf
FreeMemory(*Name)
FreeMemory(*Domain)
EndIf
EndIf
FreeMemory(*SIDAndAttributes)
EndIf
CloseHandle_(TokenHandle)
EndIf
CloseHandle_(ProcessHandle)
EndIf
ProcedureReturn Result
EndProcedure
Procedure GetWinlogonProcessId()
Result = 0
hSnapshot = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0)
If hSnapshot <> #INVALID_HANDLE_VALUE
ProcEntry.PROCESSENTRY32
ProcEntry\dwSize = SizeOf(PROCESSENTRY32)
If Process32First_(hSnapshot, @ProcEntry)
Repeat
If LCase(PeekS(@ProcEntry\szExeFile, 260)) = "winlogon.exe"
If GetWindowsVersion() >= 60
IntegrityLevel.s = GetProcessIntegrityLevel(ProcEntry\th32ProcessID)
If IntegrityLevel = "SystemIntegrityLevel"
Result = ProcEntry\th32ProcessID
Break
EndIf
Else
If GetTokenUserName(ProcEntry\th32ProcessID) = "SYSTEM"
Result = ProcEntry\th32ProcessID
Break
EndIf
EndIf
EndIf
Until Not Process32Next_(hSnapshot, @ProcEntry)
EndIf
CloseHandle_(hSnapshot)
EndIf
ProcedureReturn Result
EndProcedure
Procedure.b RunProcessAsSystem(lpApplicationName.s, IntegrityLevel.s)
Result = #False
ProcessHandle = OpenProcess_(#MAXIMUM_ALLOWED, #False, GetWinlogonProcessId())
If ProcessHandle <> 0
If OpenProcessToken_(ProcessHandle, #MAXIMUM_ALLOWED, @TokenHandle)
#SecurityImpersonation = 2
#TokenPrimary = 1
If DuplicateTokenEx_(TokenHandle, #MAXIMUM_ALLOWED, #Null, #SecurityImpersonation, #TokenPrimary, @ImpersonateToken)
If Not GetTokenInformation_(ImpersonateToken, #TokenIntegrityLevel, #Null, 0, @ReturnLength) And GetLastError_() = #ERROR_INSUFFICIENT_BUFFER
*MandatoryLabel.TOKEN_MANDATORY_LABEL
*MandatoryLabel = AllocateMemory(ReturnLength)
If *MandatoryLabel <> #Null
If GetTokenInformation_(ImpersonateToken, #TokenIntegrityLevel, *MandatoryLabel, ReturnLength, @ReturnLength)
Select IntegrityLevel
Case "UntrustedIntegrityLevel"
PIntegrityLevel.s = #UNTRUSTED_INTEGRITY_SID
Case "LowIntegrityLevel"
PIntegrityLevel.s = #LOW_INTEGRITY_SID
Case "MediumIntegrityLevel"
PIntegrityLevel.s = #MEDIUM_INTEGRITY_SID
Case "MediumPlusIntegrityLevel"
PIntegrityLevel.s = #MEDIUM_PLUS_INTEGRITY_SID
Case "HighIntegrityLevel"
PIntegrityLevel.s = #HIGH_INTEGRITY_SID
Case "SystemIntegrityLevel"
PIntegrityLevel.s = #SYSTEM_INTEGRITY_SID
Case "ProtectedProcessLevel"
PIntegrityLevel.s = #PROTECTED_PROCESS_MANDATORY_SID
EndSelect
advapi32 = OpenLibrary(#PB_Any, "advapi32.dll")
If IsLibrary(advapi32)
ConvertStringSidToSid = GetFunction(advapi32, "ConvertStringSidToSidW")
CreateProcessWithToken = GetFunction(advapi32, "CreateProcessWithTokenW")
If ConvertStringSidToSid(PIntegrityLevel, @*Sid)
*MandatoryLabel\Label\Sid = *Sid
*MandatoryLabel\Label\Attributes = #SE_GROUP_INTEGRITY
If SetTokenInformation_(ImpersonateToken, #TokenIntegrityLevel, *MandatoryLabel, SizeOf(TOKEN_MANDATORY_LABEL) + GetLengthSid_(*Sid))
userenv = OpenLibrary(#PB_Any, "userenv.dll")
If IsLibrary(userenv)
CreateEnvironmentBlock = GetFunction(userenv, "CreateEnvironmentBlock")
DestroyEnvironmentBlock = GetFunction(userenv, "DestroyEnvironmentBlock")
If CreateEnvironmentBlock(@pEnvironment, #Null, #False)
#CREATE_UNICODE_ENVIRONMENT = $400
dwCreationFlag = #NORMAL_PRIORITY_CLASS | #CREATE_NEW_CONSOLE | #CREATE_UNICODE_ENVIRONMENT
si.STARTUPINFO
ZeroMemory_(@si, SizeOf(STARTUPINFO))
si\cb = SizeOf(STARTUPINFO)
si\lpDesktop = @"WinSta0\Default"
pi.PROCESS_INFORMATION
ZeroMemory_(@pi, SizeOf(PROCESS_INFORMATION))
Result = CreateProcessWithToken(ImpersonateToken, 0, lpApplicationName, lpCommandLine.s, dwCreationFlag, pEnvironment, lpCurrentDirectory.s, @si, @pi)
SetLastError_(0)
DestroyEnvironmentBlock(pEnvironment)
EndIf
CloseLibrary(userenv)
EndIf
EndIf
EndIf
CloseLibrary(advapi32)
EndIf
EndIf
FreeMemory(*MandatoryLabel)
EndIf
EndIf
CloseHandle_(ImpersonateToken)
EndIf
CloseHandle_(TokenHandle)
EndIf
CloseHandle_(ProcessHandle)
EndIf
ProcedureReturn Result
EndProcedure
Procedure.b RunProcessAsSystemXP(lpApplicationName.s)
Result = #False
ProcessHandle = OpenProcess_(#MAXIMUM_ALLOWED, #False, GetWinlogonProcessId())
If ProcessHandle <> 0
If OpenProcessToken_(ProcessHandle, #MAXIMUM_ALLOWED, @TokenHandle)
#DACL_SECURITY_INFORMATION = $4
If GetSecurityInfo_(TokenHandle, #SE_KERNEL_OBJECT, #DACL_SECURITY_INFORMATION, #Null, #Null, @ppDacl, #Null, @PSD) = 0
If SetSecurityInfo_(TokenHandle, #SE_KERNEL_OBJECT, #DACL_SECURITY_INFORMATION, #Null, #Null, #Null,#Null) = 0
If OpenProcessToken_(ProcessHandle, #MAXIMUM_ALLOWED, @TokenHandle2)
#SecurityImpersonation = 2
#TokenPrimary = 1
If DuplicateTokenEx_(TokenHandle2, #MAXIMUM_ALLOWED, #Null, #SecurityImpersonation, #TokenPrimary, @ImpersonateToken)
If ImpersonateLoggedOnUser_(ImpersonateToken)
userenv = OpenLibrary(#PB_Any, "userenv.dll")
If IsLibrary(userenv)
CreateEnvironmentBlock = GetFunction(userenv, "CreateEnvironmentBlock")
DestroyEnvironmentBlock = GetFunction(userenv, "DestroyEnvironmentBlock")
If CreateEnvironmentBlock(@pEnvironment, #Null, #False)
#CREATE_UNICODE_ENVIRONMENT = $400
dwCreationFlag = #NORMAL_PRIORITY_CLASS | #CREATE_NEW_CONSOLE | #CREATE_UNICODE_ENVIRONMENT
si.STARTUPINFO
ZeroMemory_(@si, SizeOf(STARTUPINFO))
si\cb = SizeOf(STARTUPINFO)
si\lpDesktop = @"WinSta0\Default"
pi.PROCESS_INFORMATION
ZeroMemory_(@pi, SizeOf(PROCESS_INFORMATION))
Result = CreateProcessAsUser_(ImpersonateToken, lpApplicationName, lpCommandLine.s, #Null, #Null, #False, dwCreationFlag, pEnvironment, lpCurrentDirectory.s, @si, @pi)
SetLastError_(0)
DestroyEnvironmentBlock(pEnvironment)
EndIf
CloseLibrary(userenv)
EndIf
RevertToSelf_()
EndIf
CloseHandle_(ImpersonateToken)
EndIf
CloseHandle_(TokenHandle2)
EndIf
SetSecurityInfo_(TokenHandle, #SE_KERNEL_OBJECT, #DACL_SECURITY_INFORMATION, #Null, #Null, ppDacl, #Null)
EndIf
LocalFree_(ppDacl)
LocalFree_(PSD)
EndIf
CloseHandle_(TokenHandle)
EndIf
CloseHandle_(ProcessHandle)
EndIf
ProcedureReturn Result
EndProcedure
AdjustCurrentProcessPrivilege()
If GetWindowsVersion() >= 60
RunProcessAsSystem("C:\Windows\System32\calc.exe", "SystemIntegrityLevel")
Else
RunProcessAsSystemXP("C:\Windows\System32\calc.exe")
EndIf