csharp // Vulnerable code using System.IO; class IncorrectDefaults { static void Main() { FileStream fs = new FileStream("importantData.txt", FileMode.Create); using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine("Sensitive Data"); } } } - Use `File.GetAccessControl()` method to get the access control object for the file. - Call `SetAccessRuleProtection(true, false)` on the file security object to disable inheritance and remove inherited access rules. - Create a new access rule for the current user allowing Read and Write permissions. - Set the new access rule using the `SetAccessRule()` method. - Apply the modified access control settings to the file using `File.SetAccessControl()`.
csharp// Fixed codeusingSystem.IO;usingSystem.Security.AccessControl;classCorrectDefaults{staticvoidMain(){stringfilePath="importantData.txt";FileStreamfs=newFileStream(filePath,FileMode.Create);// Set file permission to read and write only for the current userFileSecurityfileSec=File.GetAccessControl(filePath);fileSec.SetAccessRuleProtection(true,false);// Disable inheritance, remove inherited access rulesFileSystemAccessRuleaccessRule=newFileSystemAccessRule(Environment.UserName,FileSystemRights.Read|FileSystemRights.Write,AccessControlType.Allow);fileSec.SetAccessRule(accessRule);File.SetAccessControl(filePath,fileSec);using(StreamWritersw=newStreamWriter(fs)){sw.WriteLine("Sensitive Data");}}}