/// <summary> /// Get the security descriptor for a service. /// </summary> /// <param name="name">The name of the service.</param> /// <param name="security_information">Parts of the security descriptor to return.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The security descriptor.</returns> public static NtResult <SecurityDescriptor> GetServiceSecurityDescriptor(string name, SecurityInformation security_information, bool throw_on_error) { var desired_access = NtSecurity.QuerySecurityAccessMask(security_information).ToSpecificAccess <ServiceAccessRights>(); using (var service = OpenService(name, desired_access, throw_on_error)) { if (!service.IsSuccess) { return(service.Cast <SecurityDescriptor>()); } return(GetServiceSecurityDescriptor(service.Result, "service", security_information, throw_on_error)); } }
/// <summary> /// Get the security descriptor of the SCM. /// </summary> /// <param name="security_information">Parts of the security descriptor to return.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The SCM security descriptor.</returns> public static NtResult <SecurityDescriptor> GetScmSecurityDescriptor(SecurityInformation security_information, bool throw_on_error) { var desired_access = NtSecurity.QuerySecurityAccessMask(security_information).ToSpecificAccess <ServiceControlManagerAccessRights>(); using (SafeServiceHandle scm = Win32NativeMethods.OpenSCManager(null, null, ServiceControlManagerAccessRights.Connect | desired_access)) { if (scm.IsInvalid) { return(Win32Utils.GetLastWin32Error().CreateResultFromDosError <SecurityDescriptor>(throw_on_error)); } return(GetServiceSecurityDescriptor(scm, "scm", security_information, throw_on_error)); } }
/// <summary> /// Overridden ProcessRecord /// </summary> protected override void ProcessRecord() { AccessMask mask; switch (ParameterSetName) { case "FromAce": mask = AccessControlEntry.Mask; break; case "FromSecurityInformation": if (SetSecurity) { mask = NtSecurity.SetSecurityAccessMask(SecurityInformation); } else { mask = NtSecurity.QuerySecurityAccessMask(SecurityInformation); } break; default: mask = AccessMask; mask |= MapGeneric(SpecificAccessType.File, FileAccess); mask |= MapGeneric(SpecificAccessType.File, FileDirectoryAccess); mask |= MapGeneric(SpecificAccessType.IoCompletion, IoCompletionAccess); mask |= MapGeneric(SpecificAccessType.Mutant, MutantAccess); mask |= MapGeneric(SpecificAccessType.Semaphore, SemaphoreAccess); mask |= MapGeneric(SpecificAccessType.RegistryTransaction, RegistryTransactionAccess); mask |= MapGeneric(SpecificAccessType.ALPCPort, AlpcPortAccess); mask |= MapGeneric(SpecificAccessType.Section, SectionAccess); mask |= MapGeneric(SpecificAccessType.Key, KeyAccess); mask |= MapGeneric(SpecificAccessType.Event, EventAccess); mask |= MapGeneric(SpecificAccessType.SymbolicLink, SymbolicLinkAccess); mask |= MapGeneric(SpecificAccessType.Token, TokenAccess); mask |= GenericAccess; mask |= MapGeneric(SpecificAccessType.Directory, DirectoryAccess); mask |= MapGeneric(SpecificAccessType.Thread, ThreadAccess); mask |= MapGeneric(SpecificAccessType.DebugObject, DebugObjectAccess); mask |= MapGeneric(SpecificAccessType.Job, JobAccess); mask |= MapGeneric(SpecificAccessType.Process, ProcessAccess); mask |= MapGeneric(SpecificAccessType.Transaction, TransactionAccess); mask |= MapGeneric(SpecificAccessType.TransactionManager, TransactionManagerAccess); mask |= MapGeneric(SpecificAccessType.ResourceManager, ResourceManagerAccess); mask |= MapGeneric(SpecificAccessType.Enlistment, EnlistmentAccess); mask |= (uint)ManadatoryLabelPolicy; break; } if (ToGenericAccess) { WriteObject(mask.ToGenericAccess()); } else if (ToMandatoryLabelPolicy) { WriteObject(mask.ToMandatoryLabelPolicy()); } else if (ToSpecificAccess == SpecificAccessType.None && ToTypeAccess == null) { WriteObject(mask); } else { NtType type = ToTypeAccess ?? GetTypeObject(ToSpecificAccess); WriteObject(mask.ToSpecificAccess(type.AccessRightsType)); } }