/* * Process Hacker - * LSA account handle * * Copyright (C) 2009 wj32 * * This file is part of Process Hacker. * * Process Hacker is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Process Hacker is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Process Hacker. If not, see . */ using System; using System.Collections.Generic; using ProcessHacker.Native.Api; using ProcessHacker.Native.Security; namespace ProcessHacker.Native.Objects { /// /// Represents a handle to a LSA account. /// public sealed class LsaAccountHandle : LsaHandle { public static LsaAccountHandle Create(LsaAccountAccess access, LsaPolicyHandle policyHandle, Sid sid) { NtStatus status; IntPtr handle; if ((status = Win32.LsaCreateAccount( policyHandle, sid, access, out handle )) >= NtStatus.Error) Win32.ThrowLastError(status); return new LsaAccountHandle(handle, true); } private LsaAccountHandle(IntPtr handle, bool owned) : base(handle, owned) { } /// /// Opens a LSA account. /// /// A handle to a LSA policy. /// The SID of the account to open. /// The desired access to the account. public LsaAccountHandle(LsaPolicyHandle policyHandle, Sid sid, LsaAccountAccess access) { NtStatus status; IntPtr handle; if ((status = Win32.LsaOpenAccount( policyHandle, sid, access, out handle )) >= NtStatus.Error) Win32.ThrowLastError(status); this.Handle = handle; } public void AddPrivileges(PrivilegeSet privileges) { NtStatus status; using (var privilegeSetMemory = privileges.ToMemory()) { if ((status = Win32.LsaAddPrivilegesToAccount( this, privilegeSetMemory )) >= NtStatus.Error) Win32.ThrowLastError(status); } } public PrivilegeSet GetPrivileges() { NtStatus status; IntPtr privileges; if ((status = Win32.LsaEnumeratePrivilegesOfAccount( this, out privileges )) >= NtStatus.Error) Win32.ThrowLastError(status); using (var privilegesAlloc = new LsaMemoryAlloc(privileges)) { return new PrivilegeSet(privilegesAlloc); } } public QuotaLimits GetQuotas() { NtStatus status; QuotaLimits quotas; if ((status = Win32.LsaGetQuotasForAccount( this, out quotas )) >= NtStatus.Error) Win32.ThrowLastError(status); return quotas; } public SecuritySystemAccess GetSystemAccess() { NtStatus status; SecuritySystemAccess access; if ((status = Win32.LsaGetSystemAccessAccount( this, out access )) >= NtStatus.Error) Win32.ThrowLastError(status); return access; } public void RemovePrivileges() { NtStatus status; if ((status = Win32.LsaRemovePrivilegesFromAccount( this, true, IntPtr.Zero )) >= NtStatus.Error) Win32.ThrowLastError(status); } private void RemovePrivileges(PrivilegeSet privileges) { NtStatus status; using (var privilegeSetMemory = privileges.ToMemory()) { if ((status = Win32.LsaRemovePrivilegesFromAccount( this, false, privilegeSetMemory )) >= NtStatus.Error) Win32.ThrowLastError(status); } } public void SetQuotas(QuotaLimits quotas) { NtStatus status; if ((status = Win32.LsaSetQuotasForAccount( this, ref quotas )) >= NtStatus.Error) Win32.ThrowLastError(status); } public void SetSystemAccess(SecuritySystemAccess access) { NtStatus status; if ((status = Win32.LsaSetSystemAccessAccount( this, access )) >= NtStatus.Error) Win32.ThrowLastError(status); } } }