public static void TakeOwnership(string FD) { try { var myProcToken = new AccessTokenProcess(Process.GetCurrentProcess().Id, TokenAccessType.TOKEN_ALL_ACCESS | TokenAccessType.TOKEN_ADJUST_PRIVILEGES); myProcToken.EnablePrivilege(new Microsoft.Win32.Security.TokenPrivilege(Microsoft.Win32.Security.TokenPrivilege.SE_TAKE_OWNERSHIP_NAME, true)); SecurityIdentifier identifier = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null); NTAccount identity = (NTAccount)identifier.Translate(typeof(NTAccount)); if (File.Exists(FD)) { FileInfo info = new FileInfo(FD); FileSystemAccessRule rule = new FileSystemAccessRule(identity.Value, FileSystemRights.FullControl, AccessControlType.Allow); FileSecurity accessControl = info.GetAccessControl(AccessControlSections.Owner); accessControl.SetOwner(new NTAccount(identity.Value)); info.SetAccessControl(accessControl); accessControl.AddAccessRule(rule); info.SetAccessControl(accessControl); } if (Directory.Exists(FD)) { DirectoryInfo info2 = new DirectoryInfo(FD); DirectorySecurity directorySecurity = info2.GetAccessControl(AccessControlSections.All); directorySecurity.SetOwner(identity); info2.SetAccessControl(directorySecurity); directorySecurity.AddAccessRule(new FileSystemAccessRule(identity, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow)); info2.SetAccessControl(directorySecurity); } Clear(FD); } catch (Exception) { } }
public void AdjustToken(bool enable) { var p = Process.GetCurrentProcess(); var at = new AccessTokenProcess(p.Id, TokenAccessType.TOKEN_ADJUST_PRIVILEGES); var tp = new TokenPrivilege(TokenPrivilege.SE_SHUTDOWN_NAME, enable); at.EnablePrivilege(tp); }
//static Sid SysUser = new Sid(); private static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.White; Console.Write(ProgramHeader); Console.ResetColor(); try { _cmdLineArgs = ProcessCmdArgs(args, new char[] { '?', 'f', 'l', 'p' }); if (_cmdLineArgs.ContainsKey('?')) { Console.Write(PROGRAM_HELP_INFO); Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("\nPlease make sure you use lowercase for the /f, /l, and /p"); Console.ResetColor(); Environment.Exit(1); } if (!_cmdLineArgs.ContainsKey('f') || string.IsNullOrEmpty(_cmdLineArgs['f'])) { if (!_cmdLineArgs.ContainsKey('l')) { Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("\n/f option not specified. Please use /? for help"); Console.ResetColor(); Environment.Exit(1); } } else { _packageFile = Path.Combine(_cmdLineArgs['f'], ""); } if (!_cmdLineArgs.ContainsKey('p') || string.IsNullOrEmpty(_cmdLineArgs['p'])) { Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("\n/p option not specified. Please use /? for help"); Console.ResetColor(); Environment.Exit(1); } _hiveFileInfo = Path.Combine(_cmdLineArgs['p'], REGISTRY_PATH); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("MountPath : {0}", "\"" + _cmdLineArgs['p'] + "\""); Console.ResetColor(); if (string.IsNullOrEmpty(_hiveFileInfo)) { Environment.Exit(-2); } if (!File.Exists(_hiveFileInfo)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Registry file not found, please make sure your mount path is correct!"); Console.ResetColor(); Environment.Exit(-532459699); } if (_cmdLineArgs.ContainsKey('l')) { Console.Write("Writing to Log (Packages.txt) "); if (File.Exists(PackLog)) { File.Delete(PackLog); } ListComponentSubkeys(_pkgDirectory + "Packages\\"); Console.ForegroundColor = ConsoleColor.Green; Console.Write("OK"); Console.ResetColor(); Ending(); } if (!File.Exists(_packageFile)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Package file not found, please make sure your package file path is correct!"); Console.ResetColor(); Environment.Exit(-532459698); } Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("\n------------------Starting-----------------"); Console.ResetColor(); Console.Write("Reading package list... "); string[] _comp = File.ReadAllLines(_packageFile); for (int i = 0; i < _comp.Length; i++) { while (_comp[i].Contains("~")) { _comp[i] = _comp[i].Substring(0, _comp[i].Length - 1); } } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.ResetColor(); Console.Write("Mounting registry file... "); if (!Contains <string[], string>(Registry.LocalMachine.GetSubKeyNames(), HIVE_MOUNT_DIR)) { if (!LoadHive(_hiveFileInfo, HIVE_MOUNT_POINT)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("FAIL"); Console.ResetColor(); Ending(); } } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.ResetColor(); Console.Write("Taking Ownership... "); AccessTokenProcess myProcToken = new AccessTokenProcess(Process.GetCurrentProcess().Id, TokenAccessType.TOKEN_ALL_ACCESS | TokenAccessType.TOKEN_ADJUST_PRIVILEGES); myProcToken.EnablePrivilege(new TokenPrivilege(TokenPrivilege.SE_TAKE_OWNERSHIP_NAME, true)); if (Win32.GetLastError() != 0) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("FAIL"); Console.WriteLine("You must be logged as Administrator."); Console.ResetColor(); Ending(); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.ResetColor(); Console.Write("Editing \'Packages\' subkeys "); try { if (CleanComponentSubkeys(_pkgDirectory + "Packages\\", _comp, false)) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.ResetColor(); } } catch { } Console.Write("Editing \'PackagesPending\' subkeys "); try { if (CleanComponentSubkeys(_pkgDirectory + "PackagesPending\\", _comp, true)) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.ResetColor(); } } catch { } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Modifying registry completed sucessfully."); Console.ResetColor(); if (Contains <string[], string>(Microsoft.Win32.Registry.LocalMachine.GetSubKeyNames(), HIVE_MOUNT_DIR)) { Console.Write("Unmounting key... "); if (!UnloadHive(HIVE_MOUNT_POINT)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("FAIL"); Console.WriteLine("You must unmount registry hive manually."); Console.WriteLine("Hit any key to close."); Console.ResetColor(); Console.ReadKey(); Environment.Exit(-3); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.ResetColor(); } Console.Write("Removing \'Packages\'... "); if (RemoveComponentSubkeys(_pkgDirectory + "Packages\\", _comp)) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.WriteLine("Removed packages successfully."); Console.ResetColor(); } Console.Write("Removing \'PackagesPending\'... "); if (RemoveComponentSubkeys(_pkgDirectory + "Packages\\", _comp)) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.WriteLine("Removed packages successfully."); Console.ResetColor(); } Ending(); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("FAIL"); Console.WriteLine("Unhandled error occured."); Console.ResetColor(); Console.WriteLine(ex.Message); Ending(); } }
//static Sid SysUser = new Sid(); static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.White; Console.Write(ProgramHeader); Console.ResetColor(); try { _cmdLineArgs = ProcessCmdArgs(args, new char[] { 'p', '?', 'c', 'o', 'l', 'r', 'n', 'h', 'd' }); if (_cmdLineArgs.ContainsKey('?')) { Console.Write(PROGRAM_HELP_INFO); Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("\nPlease make sure you use lowercase for the /p, /c, /o and /l"); Console.ResetColor(); Environment.Exit(1); } if (_cmdLineArgs.ContainsKey('c')) { if (!string.IsNullOrEmpty(_cmdLineArgs['c'])) { _comp = Path.Combine(_cmdLineArgs['c'], ""); } else { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Type the name of the package, if nothing is entered all packages will be made visible :"); Console.ForegroundColor = ConsoleColor.Cyan; _comp = Path.Combine(Console.ReadLine(), ""); } Console.ResetColor(); } if (_cmdLineArgs.ContainsKey('o')) { _hiveFileInfo = Path.Combine(System.IO.Path.GetPathRoot(Environment.SystemDirectory), REGISTRY_PATH); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("MountPath : Online"); Console.ResetColor(); _pkgDirectory = _pkgDirectory.Replace("windows6_x_software", "Software"); _online = true; } if (_cmdLineArgs.ContainsKey('h')) { _vis = true; } if (!_cmdLineArgs.ContainsKey('o')) { if (!_cmdLineArgs.ContainsKey('p')) { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Type path to mounted install.wim :"); Console.ForegroundColor = ConsoleColor.Cyan; _hiveFileInfo = Path.Combine(Console.ReadLine(), REGISTRY_PATH); if (_hiveFileInfo.Substring(0, _hiveFileInfo.Length - REGISTRY_PATH.Length).Length == 3) { Console.WriteLine("MountPath : Online"); _pkgDirectory = _pkgDirectory.Replace("windows6_x_software", "Software"); _online = true; } else { Console.WriteLine("MountPath : {0}", "\"" + _hiveFileInfo.Substring(0, _hiveFileInfo.Length - REGISTRY_PATH.Length) + "\""); _online = false; } Console.ResetColor(); } else { _hiveFileInfo = Path.Combine(_cmdLineArgs['p'], REGISTRY_PATH); Console.ForegroundColor = ConsoleColor.Cyan; if (_cmdLineArgs['p'].Length == 3) { Console.WriteLine("MountPath : Online"); _pkgDirectory = _pkgDirectory.Replace("windows6_x_software", "Software"); _online = true; } else { Console.WriteLine("MountPath : {0}", "\"" + _cmdLineArgs['p'] + "\""); _online = false; } Console.ResetColor(); } } if (string.IsNullOrEmpty(_hiveFileInfo)) { Environment.Exit(-2); } if (!File.Exists(_hiveFileInfo)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Registry file not found, please make sure your mount path is correct!"); Console.ResetColor(); _failed = true; Environment.Exit(-532459699); } if (!string.IsNullOrEmpty(_comp)) { string T = _comp; while (T.Contains("~")) { T = T.Substring(0, T.Length - 1); } Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("Component : " + "\"" + T + "\""); Console.ResetColor(); } Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("\n------------------Starting-----------------"); Console.ResetColor(); if (_online == false) { if (!_cmdLineArgs.ContainsKey('l') && !_cmdLineArgs.ContainsKey('n')) { Console.Write("Creating BKP of registry file... "); _bkpFile = Path.Combine(Environment.CurrentDirectory, "SOFTWAREBKP"); if (!File.Exists(_bkpFile)) { File.Copy(_hiveFileInfo, _bkpFile, true); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.ResetColor(); } Console.Write("Mounting registry file... "); if (!Contains <string[], string>(Registry.LocalMachine.GetSubKeyNames(), HIVE_MOUNT_DIR)) { if (!LoadHive(_hiveFileInfo, HIVE_MOUNT_POINT)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("FAIL"); Console.ResetColor(); _failed = true; Ending(); } } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.ResetColor(); } if (_cmdLineArgs.ContainsKey('l')) { Console.Write("Writing to Log (Packages.txt) "); if (File.Exists(PackLog)) { File.Delete(PackLog); } ListComponentSubkeys(_pkgDirectory + "Packages\\"); Console.ForegroundColor = ConsoleColor.Green; Console.Write("OK"); Console.ResetColor(); Ending(); } Console.Write("Taking Ownership... "); var myProcToken = new AccessTokenProcess(Process.GetCurrentProcess().Id, TokenAccessType.TOKEN_ALL_ACCESS | TokenAccessType.TOKEN_ADJUST_PRIVILEGES); myProcToken.EnablePrivilege(new TokenPrivilege(TokenPrivilege.SE_TAKE_OWNERSHIP_NAME, true)); if (Win32.GetLastError() != 0) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("FAIL"); Console.WriteLine("You must be logged as Administrator."); Console.ResetColor(); _failed = true; Ending(); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.ResetColor(); Console.Write("Editing \'Packages\' subkeys "); try { if (CleanComponentSubkeys(_pkgDirectory + "Packages\\", _comp)) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.ResetColor(); } } catch { } if (_online == false) { Console.Write("Editing \'PackagesPending\' subkeys "); try { if (CleanComponentSubkeys(_pkgDirectory + "PackagesPending\\", _comp)) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.ResetColor(); } } catch { } } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Modifying registry completed sucessfully."); Console.ResetColor(); if (_cmdLineArgs.ContainsKey('r')) { if (Contains <string[], string>(Microsoft.Win32.Registry.LocalMachine.GetSubKeyNames(), HIVE_MOUNT_DIR)) { Console.Write("Unmounting key... "); if (!UnloadHive(HIVE_MOUNT_POINT)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("FAIL"); Console.WriteLine("You must unmount registry hive manually."); Console.WriteLine("Hit any key to close."); Console.ResetColor(); Console.ReadKey(); Environment.Exit(-3); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.ResetColor(); } Console.Write("Removing \'Packages\'... "); if (RemoveComponentSubkeys(_pkgDirectory + "Packages\\", _comp)) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.WriteLine("Removed packages successfully."); Console.ResetColor(); } Console.Write("Removing \'PackagesPending\'... "); if (RemoveComponentSubkeys(_pkgDirectory + "Packages\\", _comp)) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("OK"); Console.WriteLine("Removed packages successfully."); Console.ResetColor(); } } Ending(); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("FAIL"); Console.WriteLine("Unhandled error occured."); Console.ResetColor(); Console.WriteLine(ex.Message); _failed = true; Ending(); } }