资源简介
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#)\
评论
共有 条评论