public void DuplicateTokenExTest() { using (var tok = SafeTokenHandle.FromThread(GetCurrentThread())) { Assert.That(tok.IsInvalid, Is.False); } }
public void AdjustTokenPrivilegesTest() { using (var t = SafeTokenHandle.FromThread(GetCurrentThread(), TokenAccess.TOKEN_ADJUST_PRIVILEGES | TokenAccess.TOKEN_QUERY)) { Assert.That(LookupPrivilegeValue(null, "SeShutdownPrivilege", out LUID luid)); var ptp = new PTOKEN_PRIVILEGES(luid, PrivilegeAttributes.SE_PRIVILEGE_ENABLED); var old = PTOKEN_PRIVILEGES.GetAllocatedAndEmptyInstance(); var rLen = (uint)old.Size; Assert.That(AdjustTokenPrivileges(t, false, ptp, ptp.SizeInBytes, old, ref rLen)); rLen = 0U; Assert.That(AdjustTokenPrivileges(t, false, ptp, ptp.SizeInBytes, SafeCoTaskMemHandle.Null, ref rLen)); } }
public void GetTokenInformationTest() { //var p = SafeTokenHandle.CurrentProcessToken.GetInfo<PTOKEN_PRIVILEGES>(TOKEN_INFORMATION_CLASS.TokenPrivileges).Privileges; using (var t = SafeTokenHandle.FromProcess(GetCurrentProcess(), TokenAccess.TOKEN_QUERY)) { Assert.That(t, Is.Not.Null); var p = t.GetInfo <PTOKEN_PRIVILEGES>(TOKEN_INFORMATION_CLASS.TokenPrivileges); Assert.That(p, Is.Not.Null); Assert.That(p.PrivilegeCount, Is.GreaterThan(0)); TestContext.WriteLine("Privs: " + string.Join("; ", p.Privileges.Select(i => i.ToString()))); using (var hMem = PTOKEN_PRIVILEGES.GetAllocatedAndEmptyInstance(50)) { var b = GetTokenInformation(t, TOKEN_INFORMATION_CLASS.TokenPrivileges, hMem, hMem.Size, out int sz); Assert.That(b); var p1 = PTOKEN_PRIVILEGES.FromPtr((IntPtr)hMem); Assert.That(p1.PrivilegeCount, Is.EqualTo(p.PrivilegeCount)); Assert.That(p.Privileges, Is.EquivalentTo(p1.Privileges)); } } using (var t = SafeTokenHandle.FromThread(GetCurrentThread(), TokenAccess.TOKEN_QUERY)) { var id = t.GetInfo <uint>(TOKEN_INFORMATION_CLASS.TokenSessionId); Assert.That(id, Is.Not.Zero); TestContext.WriteLine($"SessId: {id}"); var ve = t.GetInfo <bool>(TOKEN_INFORMATION_CLASS.TokenVirtualizationEnabled); TestContext.WriteLine($"VirtEnable: {ve}"); var et = t.GetInfo <TOKEN_ELEVATION_TYPE>(TOKEN_INFORMATION_CLASS.TokenElevationType); Assert.That(et, Is.Not.Zero); TestContext.WriteLine($"ElevType: {et}"); var e = t.GetInfo <TOKEN_ELEVATION>(TOKEN_INFORMATION_CLASS.TokenElevation); Assert.That(e, Is.Not.Zero); TestContext.WriteLine($"Elev: {e.TokenIsElevated}"); } }