From 46a86f53ae383e82bc2b21d69333499594ab1ee8 Mon Sep 17 00:00:00 2001 From: Bluzume <39113159+KuromeSan@users.noreply.github.com> Date: Tue, 19 Jan 2021 01:33:58 +1300 Subject: [PATCH] Fix permissions --- DontTouchMyFlash/FlashPwner.cs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/DontTouchMyFlash/FlashPwner.cs b/DontTouchMyFlash/FlashPwner.cs index 8834b4f..9ba312d 100644 --- a/DontTouchMyFlash/FlashPwner.cs +++ b/DontTouchMyFlash/FlashPwner.cs @@ -42,17 +42,22 @@ namespace DontTouchMyFlash { FileSecurity fileS = File.GetAccessControl(filepath); - IdentityReference sid = fileS.GetOwner(typeof(SecurityIdentifier)); - string ntAccount = sid.Translate(typeof(NTAccount)).ToString(); - if(ntAccount == @"NT SERVICE\TrustedInstaller") - { - SecurityIdentifier cu = WindowsIdentity.GetCurrent().User; - fileS.SetOwner(cu); - fileS.SetAccessRule(new FileSystemAccessRule(cu, FileSystemRights.FullControl, AccessControlType.Allow)); + SecurityIdentifier cu = WindowsIdentity.GetCurrent().User; + SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null); - File.SetAccessControl(filepath, fileS); - } + fileS.SetOwner(cu); + fileS.SetAccessRuleProtection(false, false); + + fileS.RemoveAccessRuleAll(new FileSystemAccessRule(everyone, FileSystemRights.FullControl, AccessControlType.Deny)); + fileS.RemoveAccessRuleAll(new FileSystemAccessRule(cu, FileSystemRights.FullControl, AccessControlType.Deny)); + + fileS.SetAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.FullControl, AccessControlType.Allow)); + fileS.SetAccessRule(new FileSystemAccessRule(cu, FileSystemRights.FullControl , AccessControlType.Allow)); + + File.SetAccessControl(filepath, fileS); + File.SetAttributes(filepath, FileAttributes.Normal); } + public bool CheckFileAndAdd(string filepath) { try