[Delphi] 권한상승
- 09-04
- 2,066 회
- 0 건
[code]
function SetPrivilege(lpszPrivilege: string; Enabled: Boolean): Boolean;
var
TokenHandle: THandle;
TokenPriv, TokenPriv_Prev: TOKEN_PRIVILEGES;
ReturnLength: Cardinal;
begin
if OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, TokenHandle) then
begin
try
if LookupPrivilegeValue(nil, PChar(lpszPrivilege), TokenPriv.Privileges[0].Luid) then
begin
TokenPriv.PrivilegeCount := 1;
case Enabled of
True: TokenPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
False: TokenPriv.Privileges[0].Attributes := 0;
end;
ReturnLength := 0;
TokenPriv_Prev := TokenPriv;
AdjustTokenPrivileges(TokenHandle, False, TokenPriv, SizeOf(TokenPriv_Prev), TokenPriv_Prev, ReturnLength);
end;
finally
CloseHandle(TokenHandle);
end;
end;
Result := GetLastError = ERROR_SUCCESS;
if not Result then raise Exception.Create(SysErrorMessage(GetLastError));
end;
[/code]