Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function global:Set-FilePermissionsWinAPI {
- param (
- [string]$filePath,
- [bool]$allow # $true = full access, $false = full access - FILE_ADD_FILE
- )
- # Definicje dla WinAPI
- Add-Type -TypeDefinition @"
- using System;
- using System.Runtime.InteropServices;
- public class SafeMemoryHandle : SafeHandle
- {
- public SafeMemoryHandle(IntPtr handle) : base(handle, true) {}
- public override bool IsInvalid => this.handle == IntPtr.Zero;
- protected override bool ReleaseHandle()
- {
- return NativeMethods.LocalFree(this.handle) == IntPtr.Zero;
- }
- }
- public class NativeMethods {
- [DllImport("advapi32.dll", SetLastError = true)]
- public static extern bool InitializeSecurityDescriptor(IntPtr pSecurityDescriptor, uint dwRevision);
- [DllImport("advapi32.dll", SetLastError = true)]
- public static extern bool SetSecurityDescriptorDacl(IntPtr pSecurityDescriptor, bool bDaclPresent, IntPtr pDacl, bool bDaclDefaulted);
- [DllImport("advapi32.dll", SetLastError = true)]
- public static extern bool SetFileSecurity(string lpFileName, int SecurityInformation, IntPtr pSecurityDescriptor);
- [DllImport("advapi32.dll", SetLastError = true)]
- public static extern bool InitializeAcl(IntPtr pAcl, uint nAclLength, uint dwAclRevision);
- [DllImport("advapi32.dll", SetLastError = true)]
- public static extern bool AddAccessAllowedAce(IntPtr pAcl, uint dwAceRevision, uint AccessMask, IntPtr pSid);
- [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
- public static extern bool ConvertStringSidToSid(string StringSid, out IntPtr Sid);
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern IntPtr LocalAlloc(uint uFlags, uint uBytes);
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern IntPtr LocalFree(IntPtr hMem);
- public const int DACL_SECURITY_INFORMATION = 0x00000004;
- public const uint SECURITY_DESCRIPTOR_REVISION = 1;
- public const uint ACL_REVISION = 2;
- public const uint FULL_CONTROL = 0x1F01FF; // Full access
- public const uint LIMITED_CONTROL = 0x1F01FF - 0x0002; // Full access without FILE_ADD_FILE
- }
- "@
- try {
- # Read current permissions
- $currentAcl = Get-Acl -Path $filePath
- $currentPermissions = $currentAcl.Access
- # Memory allocation for ACL
- $aclLength = 2048
- $aclPtr = [NativeMethods]::LocalAlloc(0, $aclLength)
- if ($aclPtr -eq [IntPtr]::Zero) {
- Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $methodName --> Memory for ACL cannot be allocated" -ForegroundColor Red
- }
- $aclHandle = New-Object SafeMemoryHandle($aclPtr)
- [NativeMethods]::InitializeAcl($aclHandle.DangerousGetHandle(), $aclLength, [NativeMethods]::ACL_REVISION) | Out-Null
- # Specify the appropriate permissions
- $targetPermissions = if ($allow) { [NativeMethods]::FULL_CONTROL } else { [NativeMethods]::LIMITED_CONTROL }
- foreach ($perm in (Get-Acl -Path $filePath).Access) {
- $sidPtr = [IntPtr]::Zero
- try {
- # SID Processing
- $sid = $perm.IdentityReference.Translate([System.Security.Principal.SecurityIdentifier])
- [NativeMethods]::ConvertStringSidToSid($sid.Value, [ref]$sidPtr) | Out-Null
- if ($sidPtr -eq [IntPtr]::Zero) {
- throw "Conversion failed for SID: $($perm.IdentityReference)"
- }
- # Add Permission to ACL
- if ($sid.Value -eq "S-1-5-11") {
- [NativeMethods]::AddAccessAllowedAce($aclHandle.DangerousGetHandle(), [NativeMethods]::ACL_REVISION, $targetPermissions, $sidPtr) | Out-Null
- } else {
- [NativeMethods]::AddAccessAllowedAce($aclHandle.DangerousGetHandle(), [NativeMethods]::ACL_REVISION, $perm.FileSystemRights, $sidPtr) | Out-Null
- }
- }
- catch {
- Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $methodName --> Error: $_" -ForegroundColor Red
- }
- finally {
- # Freeing up the memory
- if ($sidPtr -ne [IntPtr]::Zero) {
- [NativeMethods]::LocalFree($sidPtr) | Out-Null
- }
- }
- }
- # Initialising the pSecurityDescriptor structure
- $pSecurityDescriptor = [NativeMethods]::LocalAlloc(0, 0x14)
- if ($pSecurityDescriptor -eq [IntPtr]::Zero) {
- Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $methodName --> Memory for SecurityDescriptor cannot be allocated" -ForegroundColor Red
- }
- $securityDescriptorHandle = New-Object SafeMemoryHandle($pSecurityDescriptor)
- [NativeMethods]::InitializeSecurityDescriptor($securityDescriptorHandle.DangerousGetHandle(), [NativeMethods]::SECURITY_DESCRIPTOR_REVISION) | Out-Null
- [NativeMethods]::SetSecurityDescriptorDacl($securityDescriptorHandle.DangerousGetHandle(), $true, $aclHandle.DangerousGetHandle(), $false) | Out-Null
- # Setting file permissions
- if (-not [NativeMethods]::SetFileSecurity($filePath, [NativeMethods]::DACL_SECURITY_INFORMATION, $securityDescriptorHandle.DangerousGetHandle())) {
- Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $methodName --> File permission setting error." -ForegroundColor Red
- } else {
- Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $methodName --> Permissions have been successfully updated." -ForegroundColor Green
- }
- }
- catch {
- Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $methodName --> Error: $_" -ForegroundColor Red
- }
- finally {
- if ($aclHandle -ne $null) {
- $aclHandle.Dispose()
- }
- if ($securityDescriptorHandle -ne $null) {
- $securityDescriptorHandle.Dispose()
- }
- if ($aclPtr-ne [IntPtr]::Zero) {
- [NativeMethods]::LocalFree($aclPtr) | Out-Null
- }
- if ($pSecurityDescriptor-ne [IntPtr]::Zero) {
- [NativeMethods]::LocalFree($pSecurityDescriptor) | Out-Null
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement