/// <summary>Constructor.</summary> /// <param name="type">Indicates the type of information to configure or obtain; must not be <see cref="DeviceInfoType.None"/>.</param> /// <param name="size">The size, in bytes, of the description (including the header, which is 20 bytes).</param> /// <param name="adapterId">The id of the adapter the device information refers to.</param> /// <param name="id">The identifier of the source or target to get or set information for.</param> /// <exception cref="ArgumentException"/> internal DeviceInformation( DeviceInfoType type, int size, Luid adapterId, int id ) { if( type == DeviceInfoType.None ) throw new ArgumentException( "Invalid DisplayConfig device info type.", "type" ); infoType = type; structSize = size; this.adapterId = adapterId; this.id = id; }
public static bool EnablePrivilege(string lpszPrivilege, bool bEnablePrivilege) { var retval = false; var ltkpOld = 0; var hToken = IntPtr.Zero; var tkp = new TokenPrivileges {Privileges = new int[3]}; new TokenPrivileges {Privileges = new int[3]}; var tLuid = new Luid(); tkp.PrivilegeCount = 1; if (bEnablePrivilege) tkp.Privileges[2] = SePrivilegeEnabled; else tkp.Privileges[2] = 0; if (LookupPrivilegeValue(null, lpszPrivilege, ref tLuid)) { var proc = Process.GetCurrentProcess(); if (proc.Handle != IntPtr.Zero) { if (OpenProcessToken(proc.Handle, TokenAdjustPrivileges | TokenQuery, ref hToken) != 0) { tkp.PrivilegeCount = 1; tkp.Privileges[2] = SePrivilegeEnabled; 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 } } Marshal.FreeHGlobal(tu); } } } if (hToken != IntPtr.Zero) { CloseHandle(hToken); } return retval; }
private static string MonitorFriendlyName(Luid adapterId, uint targetId) { var deviceName = new DisplayconfigTargetDeviceName { header = { size = (uint) Marshal.SizeOf(typeof(DisplayconfigTargetDeviceName)), adapterId = adapterId, id = targetId, type = DisplayconfigDeviceInfoType.DisplayconfigDeviceInfoGetTargetName } }; var error = DisplayConfigGetDeviceInfo(ref deviceName); if (error != ErrorSuccess) throw new Win32Exception(error); return deviceName.monitorFriendlyDeviceName; }
public void AllocateLocallyUniqueIdTest() { Luid expected = new Luid(100, 100); CopyStructToAddrMock<Luid> target = new CopyStructToAddrMock<Luid>(); target.Result = expected; Mock<ISafeNativeMethods> mock = new Mock<ISafeNativeMethods>(); SafeNativeMethods.Instance = mock.Object; mock.Setup(o => o.AllocateLocallyUniqueIdImpl(It.IsAny<IntPtr>())).Callback((IntPtr pLuid) => { target.Execute(pLuid); }).Returns(NativeMethods.SUCCESS); Luid actual = RasHelper.Instance.AllocateLocallyUniqueId(); Assert.AreEqual(expected, actual); }
private Privilege(TokenHandle tokenHandle, string name, bool hasLuid, Luid luid, SePrivilegeAttributes attributes) : base(tokenHandle != null) { _tokenHandle = tokenHandle; if (_tokenHandle != null) _tokenHandle.Reference(); _name = name; _attributes = attributes; if (!hasLuid) { if (_name == null) throw new ArgumentException("You must specify either a LUID or a name."); _luid = LsaPolicyHandle.LookupPolicyHandle.LookupPrivilegeValue(_name); } else { _luid = luid; } }
public static SystemLogonSession GetLogonSession(Luid logonId) { IntPtr logonSessionData; Win32.LsaGetLogonSessionData( ref logonId, out logonSessionData ).ThrowIf(); using (var logonSessionDataAlloc = new LsaMemoryAlloc(logonSessionData, true)) { var info = logonSessionDataAlloc.ReadStruct<SecurityLogonSessionData>(); return new SystemLogonSession( info.AuthenticationPackage.Text, info.DnsDomainName.Text, info.LogonDomain.Text, info.LogonId, info.LogonServer.Text, DateTime.FromFileTime(info.LogonTime), info.LogonType, info.Session, new Sid(info.Sid), info.Upn.Text, info.UserName.Text ); } }
public string LookupPrivilegeDisplayName(Luid value) { return(this.LookupPrivilegeDisplayName(this.LookupPrivilegeName(value))); }
[System.Security.SecurityCritical] // auto-generated public Privilege( string privilegeName ) { if ( privilegeName == null ) { throw new ArgumentNullException( "privilegeName" ); } Contract.EndContractBlock(); this.luid = LuidFromPrivilege( privilegeName ); }
public Privilege(TokenHandle tokenHandle, Luid luid) : this(tokenHandle, luid, 0) { }
public Privilege(TokenHandle tokenHandle, Luid luid, SePrivilegeAttributes attributes) : this(tokenHandle, null, true, luid, attributes) { }
internal static extern NtStatus LsaGetLogonSessionData( ref Luid LogonId, out SafeLsaReturnBufferHandle ppLogonSessionData );
/// <summary>Initializes a new <see cref="TargetBaseInformation"/>.</summary> /// <param name="adapterId">The adapter the device information refers to.</param> /// <param name="id">The identifier of the source or target to get or set information for.</param> /// <exception cref="ArgumentException"/> public TargetBaseInformation( Luid adapterId, int id ) : base( DeviceInfoType.GetTargetBaseType, 24, adapterId, id ) { }
private static Luid LuidFromPrivilege(string privilege) { Luid luid; luid.LowPart = 0; luid.HighPart = 0; // // Look up the privilege LUID inside the cache // RuntimeHelpers.PrepareConstrainedRegions(); try { privilegeLock.AcquireReaderLock(Timeout.Infinite); if (luids.Contains(privilege)) { luid = (Luid)luids[privilege]; privilegeLock.ReleaseReaderLock(); } else { privilegeLock.ReleaseReaderLock(); if (false == NativeMethods.LookupPrivilegeValue(null, privilege, ref luid)) { int error = Marshal.GetLastWin32Error(); if (error == NativeMethods.ERROR_NOT_ENOUGH_MEMORY) { throw new OutOfMemoryException(); } else if (error == NativeMethods.ERROR_ACCESS_DENIED) { throw new UnauthorizedAccessException("Caller does not have the rights to look up privilege local unique identifier"); } else if (error == NativeMethods.ERROR_NO_SUCH_PRIVILEGE) { throw new ArgumentException( string.Format("{0} is not a valid privilege name", privilege), "privilege"); } else { throw new Win32Exception(error); } } privilegeLock.AcquireWriterLock(Timeout.Infinite); } } finally { if (privilegeLock.IsReaderLockHeld) { privilegeLock.ReleaseReaderLock(); } if (privilegeLock.IsWriterLockHeld) { if (!luids.Contains(privilege)) { luids[privilege] = luid; privileges[luid] = privilege; } privilegeLock.ReleaseWriterLock(); } } return(luid); }
internal VirtualResolutionSupportInformation(Luid adapterId, int id) : base(DeviceInfoType.GetSupportVirtualResolution, 24, adapterId, id) { }
public DisplayConfigAdapterName(Luid adapter) : this() => this.Header = new DisplayConfigDeviceInfoHeader(adapter, this.GetType());
public static extern bool AllocateLocallyUniqueId( [Out] out Luid pLuid);
private static extern bool LookupPrivilegeValue(string systemName, string name, out Luid luid);
/// <summary> /// Get a logon session. /// </summary> /// <param name="luid">The logon session ID.</param> /// <returns>The logon session.</returns> public static LogonSession GetLogonSession(Luid luid) { return(GetLogonSession(luid, true).Result); }
/// <summary> /// Get a logon session. /// </summary> /// <param name="luid">The logon session ID.</param> /// <param name="throw_on_error">True to thrown on error.</param> /// <returns>The logon session.</returns> public static NtResult <LogonSession> GetLogonSession(Luid luid, bool throw_on_error) { return(LogonSession.GetLogonSession(luid, throw_on_error)); }
public SystemLogonSession( string authenticationPackage, string dnsDomainName, string logonDomain, Luid logonId, string logonServer, DateTime logonTime, LogonType logonType, int session, Sid sid, string upn, string userName ) { this.AuthenticationPackage = authenticationPackage; this.DnsDomainName = dnsDomainName; this.LogonDomain = logonDomain; this.LogonId = logonId; this.LogonServer = logonServer; this.LogonTime = logonTime; this.LogonType = logonType; this.Session = session; this.Sid = sid; this.Upn = upn; this.UserName = userName; }
internal static extern bool LookupPrivilegeName( string lpSystemName, ref Luid lpLuid, [Out] StringBuilder lpName, ref int cchName);
internal static extern bool PInvoke_LookupPrivilegeValue(string lpSystemName, string lpName, out Luid Luid);
public static SystemLogonSession GetLogonSession(Luid logonId) { NtStatus status; IntPtr logonSessionData; if ((status = Win32.LsaGetLogonSessionData( ref logonId, out logonSessionData )) >= NtStatus.Error) Win32.ThrowLastError(status); using (var logonSessionDataAlloc = new LsaMemoryAlloc(logonSessionData, true)) { var info = logonSessionDataAlloc.ReadStruct<SecurityLogonSessionData>(); return new SystemLogonSession( info.AuthenticationPackage.Read(), info.DnsDomainName.Read(), info.LogonDomain.Read(), info.LogonId, info.LogonServer.Read(), DateTime.FromFileTime(info.LogonTime), info.LogonType, info.Session, new Sid(info.Sid), info.Upn.Read(), info.UserName.Read() ); } }
internal DisplayConfigInfo( Luid adapterId, int id, TopologyIndicators topology ) { this.adapterId = adapterId; this.id = id; this.topology = topology; }
/// <summary>Initializes a new <see cref="TargetPreferredModeInformation"/>.</summary> /// <param name="adapterId">The adapter LUID of the target.</param> /// <param name="id">The target id.</param> public TargetPreferredModeInformation( Luid adapterId, int id ) : base( DeviceInfoType.GetTargetPreferredMode, 76, adapterId, id ) { }
public Privilege(Luid luid) : this(null, luid) { }
/// <summary>Initializes a new <see cref="SourceDeviceInformation"/>.</summary> /// <param name="adapterId">The identifier of the source adapter device the information packet refers to.</param> /// <param name="id">The identifier of the source adapter to get or set the device information for.</param> public SourceDeviceInformation( Luid adapterId, int id ) : base( DeviceInfoType.GetSourceName, 84, adapterId, id ) { viewGdiDeviceName = string.Empty; }
public Privilege(Luid luid, SePrivilegeAttributes attributes) : this(null, luid, attributes) { }
private static Luid LuidFromPrivilege( string privilege ) { Luid luid; luid.LowPart = 0; luid.HighPart = 0; // // Look up the privilege LUID inside the cache // RuntimeHelpers.PrepareConstrainedRegions(); try { privilegeLock.AcquireReaderLock(Timeout.Infinite); if ( luids.Contains( privilege )) { luid = ( Luid )luids[ privilege ]; privilegeLock.ReleaseReaderLock(); } else { privilegeLock.ReleaseReaderLock(); if ( false == NativeMethods.LookupPrivilegeValue( null, privilege, ref luid )) { int error = Marshal.GetLastWin32Error(); if ( error == NativeMethods.ERROR_NOT_ENOUGH_MEMORY ) { throw new OutOfMemoryException(); } else if ( error == NativeMethods.ERROR_ACCESS_DENIED ) { throw new UnauthorizedAccessException( "Caller does not have the rights to look up privilege local unique identifier" ); } else if ( error == NativeMethods.ERROR_NO_SUCH_PRIVILEGE ) { throw new ArgumentException( string.Format( "{0} is not a valid privilege name", privilege ), "privilege" ); } else { throw new Win32Exception( error ); } } privilegeLock.AcquireWriterLock(Timeout.Infinite); } } finally { if ( privilegeLock.IsReaderLockHeld ) { privilegeLock.ReleaseReaderLock(); } if ( privilegeLock.IsWriterLockHeld ) { if ( !luids.Contains( privilege )) { luids[ privilege ] = luid; privileges[ luid ] = privilege; } privilegeLock.ReleaseWriterLock(); } } return luid; }
static string FormatLuid(Luid luid) { return(String.Format("{0:X08}-{1:X08}", luid.HighPart, luid.LowPart)); }
internal static extern bool PInvoke_LookupPrivilegeName(string lpSystemName, out Luid lpLuid, string lpName, out int cchName);
public static Luid[] GetLogonSessions() { NtStatus status; int logonSessionCount; IntPtr logonSessionList; if ((status = Win32.LsaEnumerateLogonSessions( out logonSessionCount, out logonSessionList )) >= NtStatus.Error) Win32.ThrowLastError(status); Luid[] logonSessions = new Luid[logonSessionCount]; using (var logonSessionListAlloc = new LsaMemoryAlloc(logonSessionList, true)) { for (int i = 0; i < logonSessionCount; i++) logonSessions[i] = logonSessionListAlloc.ReadStruct<Luid>(i); return logonSessions; } }
internal static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, out Luid lpLuid);
/// <summary>Initializes a new <see cref="AdapterInformation"/>.</summary> /// <param name="adapterId">The adapter the device information refers to.</param> /// <param name="id">The identifier of the source or target to get or set information for.</param> public AdapterInformation( Luid adapterId, int id ) : base( DeviceInfoType.GetAdapterName, 276, adapterId, id ) { devicePath = string.Empty; }
static extern bool AllocateLocallyUniqueId(out Luid Luid);
public Privilege( string privilegeName ) { if ( privilegeName == null ) { throw new ArgumentNullException( "privilegeName" ); } this.luid = LuidFromPrivilege( privilegeName ); }
public TOKEN_SOURCE(string source_name) { SourceName = Encoding.ASCII.GetBytes(source_name); Array.Resize(ref SourceName, 8); SourceIdentifier = new Luid(); }
public TokenHandle LogonUser( string originName, SecurityLogonType logonType, IAuthenticationPackage package, TokenSource source, out object profileData, out Luid logonId, out NtStatus subStatus ) { IntPtr profileBuffer; int profileBufferLength; IntPtr token; QuotaLimits quotas; AnsiString originNameStr = new AnsiString(originName); try { using (MemoryRegion logonData = package.GetAuthData()) { Win32.LsaLogonUser( this, ref originNameStr, logonType, this.LookupAuthenticationPackage(package.PackageName), logonData, logonData.Size, IntPtr.Zero, ref source, out profileBuffer, out profileBufferLength, out logonId, out token, out quotas, out subStatus ).ThrowIf(); using (new LsaMemoryAlloc(profileBuffer, true)) { profileData = package.GetProfileData(new MemoryRegion(profileBuffer, 0, profileBufferLength)); } return new TokenHandle(token, true); } } finally { originNameStr.Dispose(); } }
static extern NtStatus LsaLogonUser(SafeLsaHandle LsaHandle, LsaString OriginName, SecurityLogonType LogonType, uint AuthenticationPackage, SafeBuffer AuthenticationInformation, int AuthenticationInformationLength, IntPtr LocalGroups, TOKEN_SOURCE SourceContext, out IntPtr ProfileBuffer, out int ProfileBufferLength, out Luid LogonId, out SafeKernelObjectHandle Token, out QUOTA_LIMITS Quotas, out NtStatus SubStatus );
private static Luid LuidFromPrivilege( string privilege ) { Luid luid; luid.LowPart = 0; luid.HighPart = 0; // // Look up the privilege LUID inside the cache // RuntimeHelpers.PrepareConstrainedRegions(); try { privilegeLock.AcquireReaderLock( -1 ); if ( luids.Contains( privilege )) { luid = ( Luid )luids[ privilege ]; privilegeLock.ReleaseReaderLock(); } else { privilegeLock.ReleaseReaderLock(); if ( false == Win32Native.LookupPrivilegeValue( null, privilege, ref luid )) { int error = Marshal.GetLastWin32Error(); if ( error == Win32Native.ERROR_NOT_ENOUGH_MEMORY ) { throw new OutOfMemoryException(); } else if ( error == Win32Native.ERROR_ACCESS_DENIED ) { throw new UnauthorizedAccessException(); } else if ( error == Win32Native.ERROR_NO_SUCH_PRIVILEGE ) { throw new ArgumentException( Environment.GetResourceString( "Argument_InvalidPrivilegeName", privilege )); } else { Contract.Assert( false, string.Format( CultureInfo.InvariantCulture, "LookupPrivilegeValue() failed with unrecognized error code {0}", error )); throw new InvalidOperationException(); } } privilegeLock.AcquireWriterLock( -1 ); } } finally { if ( privilegeLock.IsReaderLockHeld ) { privilegeLock.ReleaseReaderLock(); } if ( privilegeLock.IsWriterLockHeld ) { if ( !luids.Contains( privilege )) { luids[ privilege ] = luid; privileges[ luid ] = privilege; } privilegeLock.ReleaseWriterLock(); } } return luid; }
public static Luid[] GetLogonSessions() { int logonSessionCount; IntPtr logonSessionList; Win32.LsaEnumerateLogonSessions( out logonSessionCount, out logonSessionList ).ThrowIf(); Luid[] logonSessions = new Luid[logonSessionCount]; using (LsaMemoryAlloc logonSessionListAlloc = new LsaMemoryAlloc(logonSessionList, true)) { for (int i = 0; i < logonSessionCount; i++) logonSessions[i] = logonSessionListAlloc.ReadStruct<Luid>(0, Luid.SizeOf, i); return logonSessions; } }
static string FormatLuid(Luid luid) { return String.Format("{0:X08}-{1:X08}", luid.HighPart, luid.LowPart); }
/// <summary>Initializes a new <see cref="TargetDeviceInformation"/>.</summary> /// <param name="adapterId">The adapter identifier.</param> /// <param name="id">The target device identifier.</param> public TargetDeviceInformation( Luid adapterId, int id ) : base( DeviceInfoType.GetTargetName, 420, adapterId, id ) { }
static extern bool LookupPrivilegeValue( string lpSystemName, string lpName, ref Luid lpLuid);
public void NewLuid() { Luid actual = Luid.NewLuid(); Assert.AreNotEqual(Luid.Empty, actual); }