本文整理汇总了C#中TOKEN_PRIVILEGES类的典型用法代码示例。如果您正苦于以下问题:C# TOKEN_PRIVILEGES类的具体用法?C# TOKEN_PRIVILEGES怎么用?C# TOKEN_PRIVILEGES使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TOKEN_PRIVILEGES类属于命名空间,在下文中一共展示了TOKEN_PRIVILEGES类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetPrivileges
public static bool SetPrivileges()
{
IntPtr hProc;
IntPtr hToken;
long luid_Security;
TOKEN_PRIVILEGES tp = new TOKEN_PRIVILEGES();
// get the process token
hProc = Process.GetCurrentProcess().Handle;
hToken = IntPtr.Zero;
if (!(OpenProcessToken(hProc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref hToken)))
{
return false;
}
// lookup the ID for the privilege we want to enable
luid_Security = 0;
//if (!(LookupPrivilegeValue(null, SE_SECURITY_NAME, ref luid_Security)))
if (!(LookupPrivilegeValue(null, SE_DEBUG_NAME, ref luid_Security)))
{
return false;
}
tp.PrivilegeCount = 1;
tp.Privilege1.Luid = luid_Security;
tp.Privilege1.Attributes = SE_PRIVILEGE_ENABLED;
// enable the privilege
if (!(AdjustTokenPrivileges(hToken, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero)))
{
return false;
}
return true;
}
示例2: LoadHive
/// <summary>
/// Load RegistryHive from file.
/// </summary>
/// <param name="hivename">RegistryHive name</param>
/// <param name="filepath">RegistyHive filepath</param>
/// <param name="rkey">Registrykey</param>
/// <returns>When loading is failed, return false. When loading is succeeded, return true.</returns>
public static bool LoadHive(string hivename, string filepath, ExRegistryKey rkey)
{
int tokenHandle = 0;
LUID serLuid = new LUID();
LUID sebLuid = new LUID();
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref tokenHandle);
TOKEN_PRIVILEGES tokenp = new TOKEN_PRIVILEGES();
tokenp.PrivilegeCount = 1;
LookupPrivilegeValue(null, "SeBackupPrivilege", ref sebLuid);
tokenp.Luid = sebLuid;
tokenp.Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(tokenHandle, false, ref tokenp, 0, 0, 0);
tokenp.PrivilegeCount = 1;
LookupPrivilegeValue(null, "SeRestorePrivilege", ref serLuid);
tokenp.Luid = serLuid;
tokenp.Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(tokenHandle, false, ref tokenp, 0, 0, 0);
CloseHandle(tokenHandle);
int rtn = RegLoadKey((uint)rkey, hivename + "\\", filepath);
if (rtn == 0)
{
return true;
}
else
{
return false;
}
}
示例3: AdjustTokenPrivileges
internal static extern bool AdjustTokenPrivileges(
IntPtr TokenHandle,
[MarshalAs(UnmanagedType.Bool)] bool DisableAllPrivileges,
ref TOKEN_PRIVILEGES NewState,
uint BufferLength,
out TOKEN_PRIVILEGES PreviousState,
out uint ReturnLength);
示例4: RegistryInterop
public RegistryInterop(RegistryHives hive, string subKey, string filePath)
{
int token = 0;
int retval = 0;
TOKEN_PRIVILEGES TP = new TOKEN_PRIVILEGES();
TOKEN_PRIVILEGES TP2 = new TOKEN_PRIVILEGES();
LUID RestoreLuid = new LUID();
LUID BackupLuid = new LUID();
retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);
retval = LookupPrivilegeValue(null, SE_RESTORE_NAME, ref RestoreLuid);
retval = LookupPrivilegeValue(null, SE_BACKUP_NAME, ref BackupLuid);
TP.PrivilegeCount = 1;
TP.Attributes = SE_PRIVILEGE_ENABLED;
TP.Luid = RestoreLuid;
TP2.PrivilegeCount = 1;
TP2.Attributes = SE_PRIVILEGE_ENABLED;
TP2.Luid = BackupLuid;
retval = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
retval = AdjustTokenPrivileges(token, 0, ref TP2, 1024, 0, 0);
uint regHive = (uint)hive;
this.Hive = hive;
this.SubKey = subKey;
RegLoadKey(regHive, subKey, filePath);
this.IsUnloaded = false;
}
示例5: SetPrivilege
public static bool SetPrivilege(string lpszPrivilege, bool
bEnablePrivilege )
{
bool retval = false;
int ltkpOld = 0;
IntPtr hToken = IntPtr.Zero;
TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES();
tkp.Privileges = new int[3];
TOKEN_PRIVILEGES tkpOld = new TOKEN_PRIVILEGES();
tkpOld.Privileges = new int[3];
LUID tLUID = new LUID();
tkp.PrivilegeCount = 1;
if (bEnablePrivilege)
tkp.Privileges[2] = SE_PRIVILEGE_ENABLED;
else
tkp.Privileges[2] = 0;
if(LookupPrivilegeValue(null , lpszPrivilege , ref tLUID))
{
Process proc = Process.GetCurrentProcess();
if(proc.Handle != IntPtr.Zero)
{
if (OpenProcessToken(proc.Handle, TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
ref hToken) != 0)
{
tkp.PrivilegeCount = 1;
tkp.Privileges[2] = SE_PRIVILEGE_ENABLED;
tkp.Privileges[1] = tLUID.HighPart;
tkp.Privileges[0] = tLUID.LowPart;
const int bufLength = 256;
IntPtr tu = Marshal.AllocHGlobal( bufLength );
Marshal.StructureToPtr(tkp, tu, true);
if(AdjustTokenPrivileges(hToken, 0, tu, bufLength, IntPtr.Zero, ref
ltkpOld) != 0)
{
// successful AdjustTokenPrivileges doesn't mean privilege could be changed
if (Marshal.GetLastWin32Error() == 0)
{
retval = true; // Token changed
}
}
TOKEN_PRIVILEGES tokp = (TOKEN_PRIVILEGES) Marshal.PtrToStructure(tu,
typeof(TOKEN_PRIVILEGES) );
Marshal.FreeHGlobal( tu );
}
}
}
if (hToken != IntPtr.Zero)
{
CloseHandle(hToken);
}
return retval;
}
示例6: SetDebugPrivilege
public static void SetDebugPrivilege()
{
IntPtr handle = Kernel32.OpenProcess(PROCESS_QUERY_INFORMATION, false, (uint)Process.GetCurrentProcess().Id);
IntPtr tokenHandle;
OpenProcessToken(handle, TOKEN_ADJUST_PRIVILEGES, out tokenHandle);
LUID luid;
LookupPrivilegeValueW(null, SE_DEBUG_NAME, out luid);
TOKEN_PRIVILEGES privileges = new TOKEN_PRIVILEGES();
privileges.PrivilegeCount = 1;
privileges.Privileges = new LUID_AND_ATTRIBUTES[] { new LUID_AND_ATTRIBUTES() };
privileges.Privileges[0].Luid = luid;
privileges.Privileges[0].Attributes = (int)SE_PRIVILEGE_ENABLED;
IntPtr pPrivileges = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TOKEN_PRIVILEGES)));
Marshal.StructureToPtr(privileges, pPrivileges, false);
uint returnLength = 0;
AdjustTokenPrivileges(tokenHandle, false, pPrivileges, 0, IntPtr.Zero, ref returnLength);
Marshal.FreeHGlobal(pPrivileges);
Kernel32.CloseHandle(tokenHandle);
Kernel32.CloseHandle(handle);
}
示例7: AdjustToken
private static void AdjustToken()
{
const uint TOKEN_ADJUST_PRIVILEGES = 0x20;
const uint TOKEN_QUERY = 0x8;
const int SE_PRIVILEGE_ENABLED = 0x2;
const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
if (Environment.OSVersion.Platform != PlatformID.Win32NT) return;
IntPtr procHandle = GetCurrentProcess();
IntPtr tokenHandle;
OpenProcessToken(procHandle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out tokenHandle);
TOKEN_PRIVILEGES tp = new TOKEN_PRIVILEGES();
tp.Attributes = SE_PRIVILEGE_ENABLED;
tp.PrivilegeCount = 1;
LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, out tp.Luid);
AdjustTokenPrivileges(tokenHandle, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
CloseHandle(tokenHandle);
}
示例8: LoadHive
/// <summary>
///
/// </summary>
/// <param name="Base">The existing hive root to load as a subkey of.</param>
/// <param name="NewHive">The registry hive file to mount/load.</param>
/// <returns>String containing the subkey from the Base where NewHive was mounted. Null if unsuccessful.</returns>
public static string LoadHive(RegistryHive Base, string NewHive)
{
try
{
int token = 0;
LUID RestLUID = new LUID();
LUID BackLUID = new LUID();
TOKEN_PRIVILEGES RestPriv = new TOKEN_PRIVILEGES { Attributes = SE_PRIVILEGE_ENABLED, PrivilegeCount = 1 };
TOKEN_PRIVILEGES BackPriv = new TOKEN_PRIVILEGES { Attributes = SE_PRIVILEGE_ENABLED, PrivilegeCount = 1 };
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);
LookupPrivilegeValue(null, SE_RESTORE_NAME, ref RestLUID);
LookupPrivilegeValue(null, SE_BACKUP_NAME, ref BackLUID);
RestPriv.Luid = RestLUID;
BackPriv.Luid = BackLUID;
AdjustTokenPrivileges(token, false, ref RestPriv, 0, 0, 0);
AdjustTokenPrivileges(token, false, ref BackPriv, 0, 0, 0);
string reference = (Path.GetFileNameWithoutExtension(NewHive) + DateTime.Now.Ticks + '-' + new Random().Next());
return RegLoadKey((uint)Base, reference, NewHive) == 0 ? reference : null;
}
catch (Exception)
{ return null; }
}
示例9: GrantShutdownPrivileges
static void GrantShutdownPrivileges()
{
//log.Debug(m => m("Granting shutdown privileges to process user..."));
IntPtr tokenHandle = IntPtr.Zero;
var tkp = new TOKEN_PRIVILEGES();
long luid = 0;
int retLen = 0;
try
{
IntPtr processHandle = Process.GetCurrentProcess().Handle;
bool success = OpenProcessToken(processHandle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref tokenHandle);
if (!success)
throw new Win32Exception(Marshal.GetLastWin32Error(), "Unable to open process token.");
LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref luid);
tkp.PrivilegeCount = 1;
tkp.Privileges.Luid = luid;
tkp.Privileges.Attributes = SE_PRIVILEGE_ENABLED;
success = AdjustTokenPrivileges(tokenHandle, false, ref tkp, 0, IntPtr.Zero, ref retLen);
if (!success)
throw new Win32Exception(Marshal.GetLastWin32Error(), "Unable to shutdown priveleges.");
}
finally
{
if (tokenHandle != IntPtr.Zero)
Marshal.FreeHGlobal(tokenHandle);
//log.Debug(m => m("Done granting shutdown privileges to process user."));
}
}
示例10: SetPrivilege
private static void SetPrivilege(uint privilege)
{
Process process = Process.GetCurrentProcess();
IntPtr tokenHandle = IntPtr.Zero;
bool success = OpenProcessToken(process.Handle, TOKEN_ADJUST_PRIVILEGES, out tokenHandle);
if (!success)
throw new Win32Exception();
GC.KeepAlive(process); // TODO get on SafeHandles.
TOKEN_PRIVILEGES privileges = new TOKEN_PRIVILEGES();
privileges.PrivilegeCount = 1;
privileges.Luid.LowPart = privilege;
privileges.Attributes = SE_PRIVILEGE_ENABLED;
success = AdjustTokenPrivileges(tokenHandle, false, ref privileges, 0, IntPtr.Zero, IntPtr.Zero);
CloseHandle(tokenHandle);
if (!success)
throw new Win32Exception();
}
示例11: UnloadHive
public static bool UnloadHive(RegistryHive Base, string Reference)
{
try
{
int token = 0;
LUID RestLUID = new LUID();
LUID BackLUID = new LUID();
TOKEN_PRIVILEGES RestPriv = new TOKEN_PRIVILEGES { Attributes = SE_PRIVILEGE_ENABLED, PrivilegeCount = 1 };
TOKEN_PRIVILEGES BackPriv = new TOKEN_PRIVILEGES { Attributes = SE_PRIVILEGE_ENABLED, PrivilegeCount = 1 };
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);
LookupPrivilegeValue(null, SE_RESTORE_NAME, ref RestLUID);
LookupPrivilegeValue(null, SE_BACKUP_NAME, ref BackLUID);
RestPriv.Luid = RestLUID;
BackPriv.Luid = BackLUID;
AdjustTokenPrivileges(token, false, ref RestPriv, 0, 0, 0);
AdjustTokenPrivileges(token, false, ref BackPriv, 0, 0, 0);
return RegUnLoadKey((uint)Base, Reference) == 0;
}
catch (Exception)
{ return false; }
}
示例12: AdjustTokenPrivileges
internal static extern bool AdjustTokenPrivileges(IntPtr tokenhandle, [MarshalAs(UnmanagedType.Bool)] bool disableAllPrivileges, ref TOKEN_PRIVILEGES newstate, uint bufferlength, IntPtr previousState, IntPtr returnlength);
示例13: EnableDisablePrivilege
public static void EnableDisablePrivilege(string PrivilegeName, bool EnableDisable)
{
var htok = IntPtr.Zero;
if (!OpenProcessToken(Process.GetCurrentProcess().Handle, TokenAccessLevels.AdjustPrivileges | TokenAccessLevels.Query, out htok))
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
return;
}
var tkp = new TOKEN_PRIVILEGES { PrivilegeCount = 1, Privileges = new LUID_AND_ATTRIBUTES[1] };
LUID luid;
if (!LookupPrivilegeValue(null, PrivilegeName, out luid))
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
return;
}
tkp.Privileges[0].LUID = luid;
tkp.Privileges[0].Attributes = (uint)(EnableDisable ? 2 : 0);
if (!AdjustTokenPrivileges(htok, false, ref tkp, 0, IntPtr.Zero, IntPtr.Zero))
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
return;
}
}
示例14: adjustToken
/// <summary>
/// Change process security token access rights
/// </summary>
/// <param name="Enable">Boolean - Ebnable or disable a privilege</param>
/// <returns>Boolean</returns>
Boolean adjustToken(Boolean Enable, string[] rights)
{
IntPtr hToken = IntPtr.Zero;
IntPtr hProcess = IntPtr.Zero;
LUID tLuid = new LUID();
TOKEN_PRIVILEGES NewState = new TOKEN_PRIVILEGES();
UInt32 uPriv = (UInt32)(ETOKEN_PRIVILEGES.TOKEN_ADJUST_PRIVILEGES | ETOKEN_PRIVILEGES.TOKEN_QUERY | ETOKEN_PRIVILEGES.TOKEN_QUERY_SOURCE);
try
{
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, GetCurrentProcessId());
if (hProcess == IntPtr.Zero)
return false;
if (OpenProcessToken(hProcess, uPriv, ref hToken) == 0)
return false;
for (Int32 i = 0; i < rights.Length; i++)
{
// Get the local unique id for the privilege.
if (LookupPrivilegeValueW(0, rights[i], ref tLuid) == 0)
return false;
}
// Assign values to the TOKEN_PRIVILEGE structure.
NewState.PrivilegesCount = 1;
NewState.Privileges.pLuid = tLuid;
NewState.Privileges.Attributes = (Enable ? SE_PRIVILEGE_ENABLED : 0);
// Adjust the token privilege
return (AdjustTokenPrivileges(hToken, false, ref NewState, (uint)Marshal.SizeOf(NewState), IntPtr.Zero, IntPtr.Zero));
}
finally
{
if (hToken != IntPtr.Zero)
CloseHandle(hToken);
if (hProcess != IntPtr.Zero)
CloseHandle(hProcess);
}
}
示例15: AdjustTokenPrivileges
private static extern int AdjustTokenPrivileges(int tokenhandle, int disableprivs, ref TOKEN_PRIVILEGES Newstate, int bufferlength, int PreivousState, int Returnlength);