| Server IP : 185.208.173.17 / Your IP : 87.236.161.98 Web Server : Microsoft-IIS/10.0 System : Windows NT SRV8576125506 10.0 build 26100 (Windows Server 2016) AMD64 User : IUSR ( 0) PHP Version : 7.4.13 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /Windows/System32/WindowsPowerShell/v1.0/Modules/Carbon/Security/ |
Upload File : |
# Copyright 2012 Aaron Jensen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
function Grant-Permission
{
<#
.SYNOPSIS
Grants permission on a file, directory or registry key.
.DESCRIPTION
Granting access to a file system entry or registry key requires a lot of steps. This method reduces it to one call. Very helpful.
It has the advantage that it will set permissions on a file system object or a registry. If `Path` is absolute, the correct provider (file system or registry) is used. If `Path` is relative, the provider of the current location will be used.
The `Permissions` attribute can be a list of [FileSystemRights](http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemrights.aspx) or [RegistryRights](http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.registryrights.aspx).
This command will show you the values for the `FileSystemRights`:
[Enum]::GetValues([Security.AccessControl.FileSystemRights])
This command will show you the values for the `RegistryRights`:
[Enum]::GetValues([Security.AccessControl.RegistryRights])
When setting permissions on a container (directory/registry key) You can control inheritance and propagation flags using the `ApplyTo` parameter. There are 13 possible combinations. Examples work best. Here is a simple hierarchy:
C
/ \
CC CL
/ \
GC GL
C is the **C**ontainer permissions are getting set on
CC is a **C**hild **C**ontainer
CL is a **C**hild **L**eaf
GC is a **G**randchild **C**ontainer and includes all sub-containers below it
GL is a **G**randchild **L**eaf
The `ApplyTo` parameter takes one of the following 13 values and applies permissions to:
* **Container** - The container itself and nothing below it.
* **SubContainers** - All sub-containers under the container, e.g. CC and GC.
* **Leaves** - All leaves under the container, e.g. CL and GL.
* **ChildContainers** - Just the container's child containers, e.g. CC.
* **ChildLeaves** - Just the container's child leaves, e.g. CL.
* **ContainerAndSubContainers** - The container and all its sub-containers, e.g. C, CC, and GC.
* **ContainerAndLeaves** - The container and all leaves under it, e.g. C and CL.
* **SubContainerAndLeaves** - All sub-containers and leaves, but not the container itself, e.g. CC, CL, GC, and GL.
* **ContainerAndChildContainers** - The container and all just its child containers, e.g. C and CC.
* **ContainerAndChildLeaves** - The container and just its child leaves, e.g. C and CL.
* **ContainerAndChildContainersAndChildLeaves** - The container and just its child containers/leaves, e.g. C, CC, and CL.
* **ContainerAndSubContainersAndLeaves** - Everything, full inheritance/propogation, e.g. C, CC, GC, GL. **This is the default.**
* **ChildContainersAndChildLeaves** - Just the container's child containers/leaves, e.g. CC and CL.
The following table maps `ContainerInheritanceFlags` values to the actual `InheritanceFlags` and `PropagationFlags` values used:
ContainerInheritanceFlags InheritanceFlags PropagationFlags
------------------------- ---------------- ----------------
Container None None
SubContainers ContainerInherit InheritOnly
Leaves ObjectInherit InheritOnly
ChildContainers ContainerInherit InheritOnly,
NoPropagateInherit
ChildLeaves ObjectInherit InheritOnly
ContainerAndSubContainers ContainerInherit None
ContainerAndLeaves ObjectInherit None
SubContainerAndLeaves ContainerInherit,ObjectInherit InheritOnly
ContainerAndChildContainers ContainerInherit None
ContainerAndChildLeaves ObjectInherit None
ContainerAndChildContainersAndChildLeaves ContainerInherit,ObjectInherit NoPropagateInherit
ContainerAndSubContainersAndLeaves ContainerInherit,ObjectInherit None
ChildContainersAndChildLeaves ContainerInherit,ObjectInherit InheritOnly
The above information adpated from [Manage Access to Windows Objects with ACLs and the .NET Framework](http://msdn.microsoft.com/en-us/magazine/cc163885.aspx#S3), published in the November 2004 copy of *MSDN Magazine*.
.LINK
http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemrights.aspx
.LINK
http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.registryrights.aspx
.LINK
http://msdn.microsoft.com/en-us/magazine/cc163885.aspx#S3
.LINK
Protect-Acl
.EXAMPLE
Grant-Permission -Identity ENTERPRISE\Engineers -Permission FullControl -Path C:\EngineRoom
Grants the Enterprise's engineering group full control on the engine room. Very important if you want to get anywhere.
.EXAMPLE
Grant-Permission -Identity ENTERPRISE\Interns -Permission ReadKey,QueryValues,EnumerateSubKeys -Path rklm:\system\WarpDrive
Grants the Enterprise's interns access to read about the warp drive. They need to learn someday, but at least they can't change anything.
.EXAMPLE
Grant-Permission -Identity ENTERPRISE\Engineers -Permission FullControl -Path C:\EngineRoom -Clear
Grants the Enterprise's engineering group full control on the engine room. Any non-inherited, existing access rules are removed from `C:\EngineRoom`.
#>
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[Parameter(Mandatory=$true)]
[string]
# The path on which the permissions should be granted. Can be a file system or registry path.
$Path,
[Parameter(Mandatory=$true)]
[string]
# The user or group getting the permissions
$Identity,
[Parameter(Mandatory=$true)]
[string[]]
# The permission: e.g. FullControl, Read, etc. For file system items, use values from [System.Security.AccessControl.FileSystemRights](http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemrights.aspx). For registry items, use values from [System.Security.AccessControl.RegistryRights](http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.registryrights.aspx).
[Alias('Permissions')]
$Permission,
[Carbon.Security.ContainerInheritanceFlags]
# How to apply container permissions. This controls the inheritance and propagation flags. Default is full inheritance, e.g. `ContainersAndSubContainersAndLeaves`. This parameter is ignored if `Path` is to a leaf item.
$ApplyTo = ([Carbon.Security.ContainerInheritanceFlags]::ContainerAndSubContainersAndLeaves),
[Switch]
# Removes all non-inherited permissions on the item.
$Clear
)
$Path = Resolve-Path $Path
$providerName = Get-PathProvider -Path $Path | Select-Object -ExpandProperty 'Name'
if( $providerName -ne 'Registry' -and $providerName -ne 'FileSystem' )
{
Write-Error "Unsupported path: '$Path' belongs to the '$providerName' provider. Only registry and file system paths are supported."
return
}
$rights = $Permission | ConvertTo-ProviderAccessControlRights -ProviderName $providerName
if( -not $rights )
{
Write-Error ('Unable to grant {0} {1} permissions on {2}: received an unknown permission.' -f $Identity,($Permission -join ','),$Path)
return
}
Write-Host "Granting $Identity $Permission on $Path."
# We don't use Get-Acl because it returns the whole security descriptor, which includes owner information.
# When passed to Set-Acl, this causes intermittent errors. So, we just grab the ACL portion of the security descriptor.
# See http://www.bilalaslam.com/2010/12/14/powershell-workaround-for-the-security-identifier-is-not-allowed-to-be-the-owner-of-this-object-with-set-acl/
$currentAcl = (Get-Item $Path).GetAccessControl("Access")
$inheritanceFlags = [Security.AccessControl.InheritanceFlags]::None
$propagationFlags = [Security.AccessControl.PropagationFlags]::None
if( Test-Path $Path -PathType Container )
{
$inheritanceFlags = ConvertTo-InheritanceFlag -ContainerInheritanceFlag $ApplyTo
$propagationFlags = ConvertTo-PropagationFlag -ContainerInheritanceFlag $ApplyTo
}
else
{
if( $PSBoundParameters.ContainsKey( 'ApplyTo' ) )
{
Write-Warning "Can't apply inheritance rules to a leaf. Please omit `ApplyTo` parameter when `Path` is a leaf."
}
}
$accessRule = New-Object "Security.AccessControl.$($providerName)AccessRule" $identity,$rights,$inheritanceFlags,$propagationFlags,"Allow"
if( $Clear )
{
$rules = $currentAcl.Access |
Where-Object { -not $_.IsInherited }
if( $rules )
{
$rules |
ForEach-Object { [void] $currentAcl.RemoveAccessRule( $_ ) }
}
}
$currentAcl.SetAccessRule( $accessRule )
Set-Acl $Path $currentAcl
}
Set-Alias -Name 'Grant-Permissions' -Value 'Grant-Permission'