資源簡介
C#提升進程或程序權限,使得進程擁有更高的權限(Windows提供了35中權限),如管理員具有的相關權限,甚至可以擁有管理員所不具備的權限。

代碼片段和文件信息
using?System;
using?System.Runtime.InteropServices;
namespace?PrivilegeOperate
{
????class?PrivilegeAPI
????{
????????[DllImport(“kernel32.dll“?CharSet?=?CharSet.Auto?SetLastError?=?true)]
????????public?static?extern?IntPtr?GetCurrentProcess();
????????[DllImport(“Advapi32.dll“?CharSet?=?CharSet.Auto?SetLastError?=?true)]
????????[return:?MarshalAs(UnmanagedType.Bool)]
????????public?static?extern?bool?OpenProcessToken(IntPtr?ProcessHandle?uint?DesiredAccesss?out?IntPtr?TokenHandle);
????????[DllImport(“kernel32.dll“?CharSet?=?CharSet.Auto?SetLastError?=?true)]
????????[return:?MarshalAs(UnmanagedType.Bool)]
????????public?static?extern?bool?CloseHandle(IntPtr?hobject);
????????[DllImport(“advapi32.dll“?CharSet?=?CharSet.Unicode?SetLastError?=?true)]
????????[return:?MarshalAs(UnmanagedType.Bool)]
????????public?static?extern?bool?LookupPrivilegeValue(string?lpSystemName?string?lpName
????????????[MarshalAs(UnmanagedType.Struct)]?ref?LUID?lpLuid);
????????[DllImport(“advapi32.dll“?CharSet?=?CharSet.Unicode?SetLastError?=?true)]
????????[return:?MarshalAs(UnmanagedType.Bool)]
????????public?static?extern?bool?AdjustTokenPrivileges(IntPtr?TokenHandle
????????????[MarshalAs(UnmanagedType.Bool)]?bool?DisableAllPrivileges
????????????[MarshalAs(UnmanagedType.Struct)]ref?TOKEN_PRIVILEGES?NewState
????????????uint?BufferLength?IntPtr?PreviousState?uint?ReturnLength);
????????//?如果進程的訪問令牌中沒有關聯某權限,則AdjustTokenPrivileges函數調用將會返回錯誤碼ERROR_NOT_ALL_ASSIGNED(值為1300)
????????public?const?int?ERROR_NOT_ALL_ASSIGNED?=?1300;
????}
????[StructLayout(LayoutKind.Sequential?CharSet?=?CharSet.Unicode)]
????struct?LUID
????{
????????internal?int?LowPart;
????????internal?uint?HighPart;
????}
????[StructLayout(LayoutKind.Sequential?CharSet?=?CharSet.Unicode)]
????struct?LUID_AND_ATTRIBUTES
????{
????????internal?LUID?Luid;
????????internal?uint?Attributes;
????}
????[StructLayout(LayoutKind.Sequential?CharSet?=?CharSet.Unicode)]
????struct?TOKEN_PRIVILEGES
????{
????????internal?int?PrivilegeCount;
????????internal?LUID_AND_ATTRIBUTES?Privilege;
????}
????class?TokenAccess
????{
????????internal?const?uint?STANDARD_RIGHTS_REQUIRED?=?0x000F0000;
????????internal?const?uint?STANDARD_RIGHTS_READ?=?0x00020000;
????????internal?const?uint?TOKEN_ASSIGN_PRIMARY?=?0x0001;
????????internal?const?uint?TOKEN_DUPLICATE?=?0x0002;
????????internal?const?uint?TOKEN_IMPERSONATE?=?0x0004;
????????internal?const?uint?TOKEN_QUERY?=?0x0008;
????????internal?const?uint?TOKEN_QUERY_SOURCE?=?0x0010;
????????internal?const?uint?TOKEN_ADJUST_PRIVILEGES?=?0x0020;
????????internal?const?uint?TOKEN_ADJUST_GROUPS?=?0x0040;
????????internal?const?uint?TOKEN_ADJUST_DEFAULT?=?0x0080;
????????internal?const?uint?TOKEN_ADJUST_SESSIONID?=?0x0100;
????????internal?const?uint?TOKEN_READ?=?(STANDARD_RIGHTS_READ?|?TOKEN_QUERY);
????????internal?const?uint?TOKEN_ALL_ACCESS?=?(STANDARD_RIGHTS_REQUIRED?|?TOKEN_AS
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????6521??2017-10-04?17:28??進程權限設置(C#)\PrivilegeAPI.cs
?????文件????????5159??2017-10-04?16:42??進程權限設置(C#)\PrivilegeUtil.cs
?????目錄???????????0??2017-10-04?16:40??進程權限設置(C#)\
評論
共有 條評論