public void GetOwnershipTest2() { // Arrange var tmpDir = Path.Combine(Path.GetTempPath(), "dirtools-test-" + Guid.NewGuid().ToString()); Directory.CreateDirectory(tmpDir); var tmpFile = Path.Combine(tmpDir, "asdf"); var localSystem = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null); var fileSec = new FileSecurity(); fileSec.SetOwner(localSystem); File.Create(tmpFile, 1, FileOptions.None, fileSec).Dispose(); // Act var curIdentity = new NTAccount(Environment.UserDomainName, Environment.UserName); DirectoryTools.GetOwnershipForDirectory(tmpFile, curIdentity); // Assert var curFilesec = new FileSecurity(tmpFile, AccessControlSections.Owner); IdentityReference owner = curFilesec.GetOwner(typeof(NTAccount)); Assert.IsTrue(curIdentity == owner); }
private static void RemoverPermissaoProcessos() { var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null); Console.Write("Removendo permissões Gbp... "); foreach (var dir in CaminhosGBP) { try { var dirInfo = new DirectoryInfo(Environment.ExpandEnvironmentVariables(dir)); if (dirInfo.Exists) { foreach (var f in dirInfo.GetFiles()) { if (Path.GetExtension(f.Name).ToLower() == ".exe") { var fs = new FileSecurity(); fs.SetOwner(new NTAccount(WindowsIdentity.GetCurrent().Name)); fs.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.ExecuteFile, AccessControlType.Deny)); f.SetAccessControl(fs); } } } } catch (Exception ex) { Console.Error.WriteLine(ex.Message); } } Console.WriteLine("OK."); }
public static FileStream GetSecureFileStream(string path, int bufferSize, FileOptions options) { if (path == null) throw new ArgumentNullException("path"); if (bufferSize <= 0) throw new ArgumentOutOfRangeException("bufferSize"); if ((options & ~(FileOptions.Asynchronous | FileOptions.DeleteOnClose | FileOptions.Encrypted | FileOptions.RandomAccess | FileOptions.SequentialScan | FileOptions.WriteThrough)) != FileOptions.None) throw new ArgumentOutOfRangeException("options"); new FileIOPermission(FileIOPermissionAccess.Write, path).Demand(); SecurityIdentifier user = WindowsIdentity.GetCurrent().User; FileSecurity fileSecurity = new FileSecurity(); fileSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.FullControl, AccessControlType.Allow)); fileSecurity.SetAccessRuleProtection(true, false); fileSecurity.SetOwner(user); // Attempt to create a unique file three times before giving up. // It is highly improbable that there will ever be a name clash, // therefore we do not check to see if the file first exists. for (int attempt = 0; attempt < 3; attempt++) { try { return new FileStream(Path.Combine(path, Path.GetRandomFileName()), FileMode.CreateNew, FileSystemRights.FullControl, FileShare.None, bufferSize, options, fileSecurity); } catch (IOException) { if (attempt == 2) throw; } } // This code can never be reached. // The compiler thinks otherwise. throw new IOException(); }
private static void GrantAccess(string filepath) { FileSecurity fs; try { fs = File.GetAccessControl(filepath); } catch { fs = new FileSecurity(); } //var sid = fs.GetOwner(typeof(SecurityIdentifier)); var ntAccount = new NTAccount(Environment.UserDomainName, Environment.UserName); try { fs.SetOwner(ntAccount); File.SetAccessControl(filepath, fs); var currentRules = fs.GetAccessRules(true, false, typeof(NTAccount)); var newRule = new FileSystemAccessRule( ntAccount, FileSystemRights.FullControl, AccessControlType.Allow); fs.AddAccessRule(newRule); File.SetAccessControl(filepath, fs); File.SetAttributes(filepath, FileAttributes.Normal); } catch { } finally { fs = null; ntAccount = null; } }
public static void TakeOwnership(string path) { try { using (var user = WindowsIdentity.GetCurrent()) { var ownerSecurity = new FileSecurity(); ownerSecurity.SetOwner(user.User); File.SetAccessControl(path, ownerSecurity); var accessSecurity = new FileSecurity(); accessSecurity.AddAccessRule(new FileSystemAccessRule(user.User, FileSystemRights.FullControl, AccessControlType.Allow)); File.SetAccessControl(path, accessSecurity); } } catch(UnauthorizedAccessException) { EndProcessAdmin(); } }
public void setFileSecurity(String path) { FileSecurity fs; FileSystemAccessRule fsRule = new FileSystemAccessRule(self.Name, FileSystemRights.FullControl, AccessControlType.Allow); foreach (String file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)) { try { fs = new FileSecurity(file, AccessControlSections.Access); fs.AddAccessRule(fsRule); File.SetAccessControl(file, fs); File.SetAttributes(file, FileAttributes.Normal); fs.SetOwner(self.Owner); } catch (Exception err) { } FileInfo finfo = new FileInfo(file); finfo.Attributes = FileAttributes.Normal; FileSecurity sec = finfo.GetAccessControl(); sec.SetAccessRule(fsRule); sec.SetOwner(self.Owner); Application.DoEvents(); try { File.Delete(file); Application.DoEvents(); } catch (Exception err) { MessageBox.Show(err.Message); } Application.DoEvents(); } }