From a25c919eaa3f3646cdad7097c2faa918a7b3c7ee Mon Sep 17 00:00:00 2001 From: he3als Date: Sat, 24 Feb 2024 15:37:06 +0000 Subject: [PATCH 1/9] feat: menu using WASD --- src/Ameliorated.ConsoleUtils/ConsoleTUI/Menu.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Ameliorated.ConsoleUtils/ConsoleTUI/Menu.cs b/src/Ameliorated.ConsoleUtils/ConsoleTUI/Menu.cs index cc62416..143ff42 100644 --- a/src/Ameliorated.ConsoleUtils/ConsoleTUI/Menu.cs +++ b/src/Ameliorated.ConsoleUtils/ConsoleTUI/Menu.cs @@ -164,7 +164,7 @@ namespace Ameliorated.ConsoleUtils while (true) { keyPressed = Console.ReadKey(true).Key; - if (keyPressed == ConsoleKey.DownArrow) + if (keyPressed == ConsoleKey.DownArrow || keyPressed == ConsoleKey.S) { if (validIndex >= currentValidChoices.Count - 1) continue; Console.SetCursorPosition(Math.Max(TotalOffset - 2, 0), Console.CursorTop); @@ -182,7 +182,7 @@ namespace Ameliorated.ConsoleUtils validIndex += 1; } - if (keyPressed == ConsoleKey.UpArrow) + if (keyPressed == ConsoleKey.UpArrow || keyPressed == ConsoleKey.W) { if (!(validIndex > 0)) continue; @@ -201,7 +201,7 @@ namespace Ameliorated.ConsoleUtils validIndex -= 1; } - if (keyPressed == ConsoleKey.RightArrow || keyPressed == ConsoleKey.PageUp || + if (keyPressed == ConsoleKey.RightArrow || keyPressed == ConsoleKey.A || keyPressed == ConsoleKey.PageUp || (keyPressed == ConsoleKey.Enter && currentValidChoices[validIndex].IsNextButton)) { if (pageIndex == pages) @@ -245,7 +245,7 @@ namespace Ameliorated.ConsoleUtils continue; } - if (keyPressed == ConsoleKey.LeftArrow || keyPressed == ConsoleKey.PageDown || + if (keyPressed == ConsoleKey.LeftArrow || keyPressed == ConsoleKey.D || keyPressed == ConsoleKey.PageDown || (keyPressed == ConsoleKey.Enter && currentValidChoices[validIndex].IsPreviousButton)) { if (pageIndex == 1) From 5f44c7cb79334a76481babe520d2e15d8695bfb0 Mon Sep 17 00:00:00 2001 From: he3als Date: Sat, 24 Feb 2024 15:43:49 +0000 Subject: [PATCH 2/9] fix: update all packages --- src/App.config | 10 +++++++ src/amecs.csproj | 64 +++++++++++++++++++++++++++------------------ src/packages.config | 44 +++++++++++++++++-------------- 3 files changed, 72 insertions(+), 46 deletions(-) create mode 100644 src/App.config diff --git a/src/App.config b/src/App.config new file mode 100644 index 0000000..9816074 --- /dev/null +++ b/src/App.config @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/amecs.csproj b/src/amecs.csproj index cfac80e..8f9cdd8 100644 --- a/src/amecs.csproj +++ b/src/amecs.csproj @@ -46,12 +46,11 @@ packages\Costura.Fody.5.7.0\lib\netstandard1.0\Costura.dll - packages\JetBrains.Annotations.2022.3.1\lib\net20\JetBrains.Annotations.dll - False + packages\JetBrains.Annotations.2023.3.0\lib\net20\JetBrains.Annotations.dll - packages\Microsoft.Dism.2.5.2\lib\net40\Microsoft.Dism.dll + packages\Microsoft.Dism.3.1.0\lib\net40\Microsoft.Dism.dll packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll @@ -61,20 +60,22 @@ packages\System.AppContext.4.3.0\lib\net463\System.AppContext.dll - - packages\System.CodeDom.7.0.0\lib\net462\System.CodeDom.dll + + packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll + + + packages\System.CodeDom.8.0.0\lib\net462\System.CodeDom.dll - - packages\System.Console.4.3.0\lib\net46\System.Console.dll + + packages\System.Console.4.3.1\lib\net46\System.Console.dll - - packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - False + + packages\System.Diagnostics.DiagnosticSource.8.0.0\lib\net462\System.Diagnostics.DiagnosticSource.dll packages\System.Diagnostics.Tracing.4.3.0\lib\net462\System.Diagnostics.Tracing.dll @@ -115,22 +116,31 @@ packages\System.Linq.Expressions.4.3.0\lib\net463\System.Linq.Expressions.dll + + packages\System.Memory.4.5.5\lib\net461\System.Memory.dll + - - packages\System.Net.Http.4.3.0\lib\net46\System.Net.Http.dll + + packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll + + packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll + packages\System.Reflection.4.3.0\lib\net462\System.Reflection.dll - - packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll + + packages\System.Runtime.4.3.1\lib\net462\System.Runtime.dll + + + packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll - - packages\System.Runtime.Extensions.4.3.0\lib\net462\System.Runtime.Extensions.dll + + packages\System.Runtime.Extensions.4.3.1\lib\net462\System.Runtime.Extensions.dll packages\System.Runtime.InteropServices.4.3.0\lib\net463\System.Runtime.InteropServices.dll @@ -144,7 +154,7 @@ False - packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net463\System.Security.Cryptography.Algorithms.dll + packages\System.Security.Cryptography.Algorithms.4.3.1\lib\net463\System.Security.Cryptography.Algorithms.dll packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll @@ -152,12 +162,11 @@ packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll - - packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll + + packages\System.Security.Cryptography.X509Certificates.4.3.2\lib\net461\System.Security.Cryptography.X509Certificates.dll - - packages\System.Security.Permissions.7.0.0\lib\net462\System.Security.Permissions.dll - False + + packages\System.Security.Permissions.8.0.0\lib\net462\System.Security.Permissions.dll packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll @@ -165,14 +174,14 @@ - packages\System.Text.RegularExpressions.4.3.0\lib\net463\System.Text.RegularExpressions.dll + packages\System.Text.RegularExpressions.4.3.1\lib\net463\System.Text.RegularExpressions.dll - packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll + packages\System.Xml.ReaderWriter.4.3.1\lib\net46\System.Xml.ReaderWriter.dll @@ -210,6 +219,7 @@ + @@ -231,16 +241,18 @@ - This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}. - + + + + + + + \ No newline at end of file From 8e3b009195559733aee8a2217319b7ca4e8590d1 Mon Sep 17 00:00:00 2001 From: he3als Date: Tue, 12 Mar 2024 23:22:14 +0000 Subject: [PATCH 5/9] feat: deameliorate function --- src/Actions/Deameliorate.cs | 192 +++++++++++++++++++++++++++++++++ src/Misc/SelectWindowsImage.cs | 33 +++--- src/Program.cs | 25 +---- src/amecs.csproj | 1 + 4 files changed, 213 insertions(+), 38 deletions(-) create mode 100644 src/Actions/Deameliorate.cs diff --git a/src/Actions/Deameliorate.cs b/src/Actions/Deameliorate.cs new file mode 100644 index 0000000..9050412 --- /dev/null +++ b/src/Actions/Deameliorate.cs @@ -0,0 +1,192 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Security.Principal; +using System.Threading; +using amecs.Misc; +using Ameliorated.ConsoleUtils; +using Microsoft.Win32; + +namespace amecs.Actions +{ + public class Deameliorate + { + private static string _mountedPath; + private static string _winVer; + + public static bool DeAme() + { + if (new ChoicePrompt() + { + Text = @" +This will de-ameliorate by reinstalling Windows. + +Although user data should be kept, we strongly recommend +making backups of any important user data. + +Continue? (Y/N): " + }.Start().Value == 2) return true; + + Program.Frame.Clear(); + (_mountedPath, _, _winVer, _, _) = SelectWindowsImage.GetMediaPath(); + if (_mountedPath == null) return false; + + if (new ChoicePrompt {Text = $"\r\nYour Windows image is {_winVer}. Continue? (Y/N): "}.Start().Value == 2) + return true; + + var fc = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Yellow; + + string userSid = null; + try + { + NSudo.GetSystemPrivilege(); + NSudo.RunAsUser(() => + { + userSid = WindowsIdentity.GetCurrent().User.ToString(); + }); + } + catch + { + // do nothing + } + + try + { + ConsoleTUI.OpenFrame.WriteCentered("\r\nUninstalling AME-installed UI software..."); + + string openShellId = null; + + var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"); + foreach (var item in key.GetSubKeyNames()) + { + try + { + if (((string)key.OpenSubKey(item).GetValue("DisplayName")).Equals("Open-Shell")) + { + openShellId = item; + } + } + catch + { + // do nothing + } + } + + if (openShellId != null) + { + ConsoleTUI.OpenFrame.WriteCentered("\r\nUninstalling Open-Shell..."); + + var proc = Process.Start("MsiExec.exe", $"/X{openShellId} /quiet"); + proc.WaitForExit(); + + if (userSid != null) + { + var appData = (string)Registry.Users.OpenSubKey(userSid + @"\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders").GetValue("AppData"); + + if (Directory.Exists(Path.Combine(appData, "OpenShell"))) + Directory.Delete(Path.Combine(appData, "OpenShell"), true); + } + } + + var epSetupPath = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)}\ExplorerPatcher\ep_setup.exe"; + if (File.Exists(epSetupPath)) + { + ConsoleTUI.OpenFrame.WriteCentered("\r\nUninstalling ExplorerPatcher, you'll need to click the prompts..."); + + var proc = Process.Start(epSetupPath, $"/uninstall"); + proc.WaitForExit(); + } + + Program.Frame.Clear(); + } catch (Exception e) + { + ConsoleTUI.OpenFrame.Close( + "Error when uninstalling software: " + e.Message, + ConsoleColor.Red, Console.BackgroundColor, + new ChoicePrompt { AnyKey = true, Text = "Press any key to continue anyways: " }); + + Program.Frame.Clear(); + ConsoleTUI.OpenFrame.WriteCentered("\r\nContinuing without uninstalling software...\r\n"); + } + + // ExplorerPatcher re-opens Explorer as SYSTEM after uninstalling, so it has to be restarted + // no silent uninstall is available, so user has to manually click 'Yes' to uninstall + // https://github.com/valinet/ExplorerPatcher/discussions/2007 + Thread.Sleep(500); + var winlogon = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true); + winlogon.SetValue("AutoRestartShell", 0); + foreach (var exp in Process.GetProcessesByName("explorer")) + { + exp.Kill(); + } + winlogon.SetValue("AutoRestartShell", 1); + NSudo.RunProcessAsUser(NSudo.GetUserToken(), "explorer.exe", "", 0); + + // all policies are cleared as a user that's de-ameliorating is unlikely + // to have their own policies in the first place + ConsoleTUI.OpenFrame.WriteCentered("\r\nClearing policies..."); + foreach (var keyPath in new[] { + $@"HKU\{userSid}\Software\Microsoft\Windows\CurrentVersion\Policies", + $@"HKU\{userSid}\Software\Policies", + @"HKLM\Software\Microsoft\Windows\CurrentVersion\Policies", + @"HKLM\Software\Policies", + @"HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Policies" + }) + { + var hive = RegistryHive.LocalMachine; + if (keyPath.StartsWith("HKU")) + hive = RegistryHive.Users; + + var baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default); + var subKeyPath = keyPath.Substring(keyPath.IndexOf('\\') + 1); + var key = baseKey.OpenSubKey(subKeyPath, true); + if (key == null) continue; + + try + { + baseKey.DeleteSubKeyTree(subKeyPath); + } + catch + { + // do nothing - some values might fail, but almost all are deleted + } + + key.Close(); + } + + Console.ReadKey(); + Program.Frame.Clear(); + ConsoleTUI.OpenFrame.WriteCentered("\r\nCompleted initial setup!", ConsoleColor.Green); + ConsoleTUI.OpenFrame.WriteCentered("\r\nWindows Setup will display as 'Windows Server,' but it's not actually installing Windows Server and is only set as such to bypass hardware requirements."); + + Console.WriteLine(); + ConsoleTUI.OpenFrame.WriteCentered("\r\nWaiting 10 seconds and starting..."); + + Console.ForegroundColor = fc; + Thread.Sleep(10000); + + Console.WriteLine(); + try + { + Process.Start(Path.Combine(_mountedPath, "setup.exe"), "/Auto Upgrade /DynamicUpdate Disable /Product Server"); + } catch (Exception e) + { + ConsoleTUI.OpenFrame.Close( + $"There was an error when trying to run the Windows Setup: {e}\r\nTry running the Windows Setup manually from File Explorer.", + ConsoleColor.Red, Console.BackgroundColor, + new ChoicePrompt { AnyKey = true, Text = $"Press any key to exit: " }); + + return false; + } + + ConsoleTUI.OpenFrame.Close( + "Completed, Windows Setup should have started.", + ConsoleColor.Cyan, Console.BackgroundColor, + new ChoicePrompt { AnyKey = true, Text = $"Press any key to go back: " }); + + return true; + } + } +} \ No newline at end of file diff --git a/src/Misc/SelectWindowsImage.cs b/src/Misc/SelectWindowsImage.cs index 4e27fef..a12c339 100644 --- a/src/Misc/SelectWindowsImage.cs +++ b/src/Misc/SelectWindowsImage.cs @@ -24,7 +24,7 @@ namespace amecs.Misc catch (SecurityException e) { Console.WriteLine(); - ConsoleTUI.OpenFrame.Close("Security exception: " + e.Message, ConsoleColor.Red, Console.BackgroundColor, new ChoicePrompt() { AnyKey = true, Text = "Press any key to return to the Menu: " }); + ConsoleTUI.OpenFrame.Close("Security exception: " + e.Message, ConsoleColor.Red, Console.BackgroundColor, new ChoicePrompt { AnyKey = true, Text = "Press any key to return to the Menu: " }); return true; } @@ -80,7 +80,7 @@ namespace amecs.Misc { var error = ((string)null, "none", (string)null, (int?)null, (bool?)null); var choice = - new ChoicePrompt() { Text = "To continue, Windows installation media is needed.\r\nDo you have a Windows USB instead of an ISO file? (Y/N): " }.Start(); + new ChoicePrompt { Text = "To continue, Windows installation media is needed.\r\nDo you have a Windows USB instead of an ISO file? (Y/N): " }.Start(); if (!choice.HasValue) return error; // Folder/drive chosen @@ -103,7 +103,7 @@ namespace amecs.Misc { Console.WriteLine(); ConsoleTUI.OpenFrame.Close("\r\nYou must select a folder or drive containing Windows installation media.", - new ChoicePrompt() { AnyKey = true, Text = "Press any key to return to the Menu: " }); + new ChoicePrompt { AnyKey = true, Text = "Press any key to return to the Menu: " }); return error; } } @@ -128,7 +128,7 @@ namespace amecs.Misc { Console.WriteLine(); ConsoleTUI.OpenFrame.Close("\r\nYou must select an ISO.", - new ChoicePrompt() { AnyKey = true, Text = "Press any key to return to the Menu: " }); + new ChoicePrompt { AnyKey = true, Text = "Press any key to return to the Menu: " }); return error; } @@ -182,18 +182,18 @@ namespace amecs.Misc { case true when isoBuildMustBeReturned: ConsoleTUI.OpenFrame.Close( - $"Multiple Windows versions were found in the Windows image, can't determine which Windows build it is. Please use an unmodified Windows ISO.", + "Multiple Windows versions were found in the Windows image, can't determine which Windows build it is. Please use an unmodified Windows ISO.", ConsoleColor.Red, Console.BackgroundColor, - new ChoicePrompt() { AnyKey = true, Text = "Press any key to return to the Menu: " }); + new ChoicePrompt { AnyKey = true, Text = "Press any key to return to the Menu: " }); return error; case true when winVersionsMustMatch: ConsoleTUI.OpenFrame.Close( - $"Multiple Windows versions were found in the Windows image, can't determine which Windows build it is. If your Windows version doesn't match the ISO, there will be problems.", + "Multiple Windows versions were found in the Windows image, can't determine which Windows build it is. If your Windows version doesn't match the ISO, there will be problems.", ConsoleColor.Red, Console.BackgroundColor, - new ChoicePrompt() { AnyKey = true, Text = "Press any key to continue anyways: " }); + new ChoicePrompt { AnyKey = true, Text = "Press any key to continue anyways: " }); Program.Frame.Clear(); - ConsoleTUI.OpenFrame.WriteCentered("\r\nContinuing without version check\r\n"); + ConsoleTUI.OpenFrame.WriteCentered("\r\nContinuing without version check...\r\n"); break; } @@ -207,7 +207,7 @@ namespace amecs.Misc ConsoleTUI.OpenFrame.Close( "Error checking ISO version: " + e.Message.TrimEnd('\n').TrimEnd('\r'), ConsoleColor.Red, Console.BackgroundColor, - new ChoicePrompt() { AnyKey = true, Text = "Press any key to return to the Menu: " }); + new ChoicePrompt { AnyKey = true, Text = "Press any key to return to the Menu: " }); return error; } finally @@ -226,18 +226,19 @@ namespace amecs.Misc var hostVersion = Environment.OSVersion.Version; var hostWinver = GetWindowsVersion(float.Parse($"{hostVersion.Major}.{hostVersion.Minor}"), hostVersion.Build); - // If it all matches - if (hostWinver == _isoWinVer && winVersionsMustMatch) return (_mountedPath, _isoPath, _isoWinVer, _isoBuild, true); - // If ISO version doesn't match host version, and winVersionsMustMatch is true + // If it all matches & winVersionsMustMatch + if (hostWinver == _isoWinVer) return (_mountedPath, _isoPath, _isoWinVer, _isoBuild, true); + // If ISO version doesn't match host version & winVersionsMustMatch if (hostWinver != _isoWinVer && winVersionsMustMatch) { if (!string.IsNullOrEmpty(_isoPath)) DismountIso(_isoPath); ConsoleTUI.OpenFrame.Close( $"You're on {hostWinver}, but the selected image is {_isoWinVer}. You can only use an ISO that matches your Windows version.", ConsoleColor.Red, Console.BackgroundColor, - new ChoicePrompt() { AnyKey = true, Text = "Press any key to return to the Menu: " }); + new ChoicePrompt { AnyKey = true, Text = "Press any key to return to the Menu: " }); return error; } + // If ISO version doesn't match host version, and winVersionsMustMatch is true if (hostWinver != _isoWinVer) return (_mountedPath, _isoPath, _isoWinVer, _isoBuild, false); } @@ -247,9 +248,9 @@ namespace amecs.Misc : "Press any key to continue anyways"; ConsoleTUI.OpenFrame.Close( - $"No Windows installation image was found inside the selected Windows media. No version check can be done, things might break.", + "No Windows installation image was found inside the selected Windows media. No version check can be done, things might break.", ConsoleColor.Red, Console.BackgroundColor, - new ChoicePrompt() { AnyKey = true, Text = $"{noWimText}: " }); + new ChoicePrompt { AnyKey = true, Text = $"{noWimText}: " }); Program.Frame.Clear(); ConsoleTUI.OpenFrame.WriteCentered("\r\nContinuing without version check\r\n"); diff --git a/src/Program.cs b/src/Program.cs index db070db..2bede50 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,9 +1,7 @@ using System; using System.DirectoryServices.AccountManagement; using System.Linq; -using System.Runtime.InteropServices; using System.Security.Principal; -using System.Threading.Tasks; using Ameliorated.ConsoleUtils; using Microsoft.Win32; using amecs.Actions; @@ -13,23 +11,6 @@ namespace amecs { internal class Program { - private static void RunAsUser(Action action) - { - var token = NSudo.GetUserToken(); - Task.Run((Action)Delegate.Combine((Action)(() => { NSudo.GetUserPrivilege(token); }), - action)).Wait(); - Marshal.FreeHGlobal(token); - } - - private static async Task RunAsUserAsync(Action action) - { - var token = NSudo.GetUserToken(); - await Task.Run((Action)Delegate.Combine((Action)(() => { NSudo.GetUserPrivilege(token); }), - action)); - Marshal.FreeHGlobal(token); - } - - private const string Ver = "2.1"; public static ConsoleTUI.Frame Frame; [STAThread] @@ -43,7 +24,7 @@ namespace amecs NSudo.GetSystemPrivilege(); if (!WindowsIdentity.GetCurrent().IsSystem) throw new Exception("Identity did not change."); - RunAsUser(() => + NSudo.RunAsUser(() => { Globals.Username = WindowsIdentity.GetCurrent().Name.Split('\\').Last(); Globals.UserDomain = WindowsIdentity.GetCurrent().Name.Split('\\').FirstOrDefault(); @@ -74,9 +55,8 @@ namespace amecs } Frame = new ConsoleTUI.Frame($"| Central AME Script v{Ver} |", false); - Frame.Open(); - + while (true) { Globals.UserElevated = Globals.User.IsMemberOf(Globals.Administrators); @@ -129,6 +109,7 @@ namespace amecs new Menu.MenuItem("Install .NET 3.5", new Func(_NET.Install)) : new Menu.MenuItem("Install .NET 3.5", new Func(_NET.Install)) {SecondaryText = "[Installed]", SecondaryTextForeground = ConsoleColor.Yellow, PrimaryTextForeground = ConsoleColor.DarkGray}, Menu.MenuItem.Blank, + new Menu.MenuItem("Uninstall AME", new Func(Deameliorate.DeAme)), new Menu.MenuItem("Extra", new Func(Extra.Extra.ShowMenu)), new Menu.MenuItem("Exit", new Func(Globals.Exit)) }, diff --git a/src/amecs.csproj b/src/amecs.csproj index 1cfcbd5..28a4d5f 100644 --- a/src/amecs.csproj +++ b/src/amecs.csproj @@ -188,6 +188,7 @@ + From a19de34df2d5192132d0d75477a723aa7c0fa910 Mon Sep 17 00:00:00 2001 From: he3als Date: Tue, 12 Mar 2024 23:27:20 +0000 Subject: [PATCH 6/9] fix(deameliorate): remove debugging ReadKey --- src/Actions/Deameliorate.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Actions/Deameliorate.cs b/src/Actions/Deameliorate.cs index 9050412..3c00a10 100644 --- a/src/Actions/Deameliorate.cs +++ b/src/Actions/Deameliorate.cs @@ -155,8 +155,7 @@ Continue? (Y/N): " key.Close(); } - - Console.ReadKey(); + Program.Frame.Clear(); ConsoleTUI.OpenFrame.WriteCentered("\r\nCompleted initial setup!", ConsoleColor.Green); ConsoleTUI.OpenFrame.WriteCentered("\r\nWindows Setup will display as 'Windows Server,' but it's not actually installing Windows Server and is only set as such to bypass hardware requirements."); From 2311044abc411877749c338ec0c3efacdb9c095c Mon Sep 17 00:00:00 2001 From: he3als Date: Fri, 15 Mar 2024 22:59:19 +0000 Subject: [PATCH 7/9] fix(de-ame): no /Server for Win 10 --- src/Actions/Deameliorate.cs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Actions/Deameliorate.cs b/src/Actions/Deameliorate.cs index 3c00a10..39334f7 100644 --- a/src/Actions/Deameliorate.cs +++ b/src/Actions/Deameliorate.cs @@ -1,7 +1,6 @@ using System; using System.Diagnostics; using System.IO; -using System.Linq; using System.Security.Principal; using System.Threading; using amecs.Misc; @@ -14,6 +13,9 @@ namespace amecs.Actions { private static string _mountedPath; private static string _winVer; + private static string _win11Setup = ""; + private static bool _win11 = Environment.OSVersion.Version.Build >= 22000; + private const string ExplorerPatcherId = "D17F1E1A-5919-4427-8F89-A1A8503CA3EB"; public static bool DeAme() { @@ -64,9 +66,7 @@ Continue? (Y/N): " try { if (((string)key.OpenSubKey(item).GetValue("DisplayName")).Equals("Open-Shell")) - { openShellId = item; - } } catch { @@ -78,8 +78,7 @@ Continue? (Y/N): " { ConsoleTUI.OpenFrame.WriteCentered("\r\nUninstalling Open-Shell..."); - var proc = Process.Start("MsiExec.exe", $"/X{openShellId} /quiet"); - proc.WaitForExit(); + Process.Start("MsiExec.exe", $"/X{openShellId} /quiet")?.WaitForExit(); if (userSid != null) { @@ -156,20 +155,23 @@ Continue? (Y/N): " key.Close(); } + Thread.Sleep(3000); Program.Frame.Clear(); ConsoleTUI.OpenFrame.WriteCentered("\r\nCompleted initial setup!", ConsoleColor.Green); - ConsoleTUI.OpenFrame.WriteCentered("\r\nWindows Setup will display as 'Windows Server,' but it's not actually installing Windows Server and is only set as such to bypass hardware requirements."); - - Console.WriteLine(); - ConsoleTUI.OpenFrame.WriteCentered("\r\nWaiting 10 seconds and starting..."); + if (_win11) + { + ConsoleTUI.OpenFrame.WriteCentered("\r\nWindows Setup will display as 'Windows Server,' but it's not actually installing Windows Server and is only set as such to bypass hardware requirements."); + Console.WriteLine(); + } + ConsoleTUI.OpenFrame.WriteCentered("\r\nWaiting 10 seconds and starting Windows Setup..."); Console.ForegroundColor = fc; Thread.Sleep(10000); - Console.WriteLine(); try { - Process.Start(Path.Combine(_mountedPath, "setup.exe"), "/Auto Upgrade /DynamicUpdate Disable /Product Server"); + if (_win11) _win11Setup = "/Product Server"; + Process.Start(Path.Combine(_mountedPath, "setup.exe"), $"/Auto Upgrade /DynamicUpdate Disable {_win11Setup}"); } catch (Exception e) { ConsoleTUI.OpenFrame.Close( From 69f5cb0be78868a8f9fb166b90deacb9d14a8526 Mon Sep 17 00:00:00 2001 From: he3als Date: Fri, 15 Mar 2024 22:59:57 +0000 Subject: [PATCH 8/9] feat(de-ame): auto-uninstall of ExplorerPatcher --- src/Actions/Deameliorate.cs | 84 ++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 19 deletions(-) diff --git a/src/Actions/Deameliorate.cs b/src/Actions/Deameliorate.cs index 39334f7..4633aa8 100644 --- a/src/Actions/Deameliorate.cs +++ b/src/Actions/Deameliorate.cs @@ -92,10 +92,63 @@ Continue? (Y/N): " var epSetupPath = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)}\ExplorerPatcher\ep_setup.exe"; if (File.Exists(epSetupPath)) { - ConsoleTUI.OpenFrame.WriteCentered("\r\nUninstalling ExplorerPatcher, you'll need to click the prompts..."); - - var proc = Process.Start(epSetupPath, $"/uninstall"); - proc.WaitForExit(); + ConsoleTUI.OpenFrame.WriteCentered("\r\nUninstalling ExplorerPatcher..."); + + var winlogon = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true); + winlogon?.SetValue("AutoRestartShell", 0); + + // kill processes that the files use + foreach (var processName in new[] {"explorer.exe", "rundll32.exe", "dllhost.exe", "ShellExperienceHost.exe", "StartMenuExperienceHost.exe"}) + { + foreach (var process in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(processName))) + { + process.Kill(); + process.WaitForExit(); + } + } + + // delete DWM service that removes rounded corners + Process.Start("sc", $"stop \"ep_dwm_{ExplorerPatcherId}\"")?.WaitForExit(); + Process.Start("sc", $"delete \"ep_dwm_{ExplorerPatcherId}\"")?.WaitForExit(); + + // remove registered DLL + var explorerPatcherDllPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "ExplorerPatcher", "ExplorerPatcher.amd64.dll"); + Process.Start("regsvr32.exe", $"/s /u \"{explorerPatcherDllPath}\"")?.WaitForExit(); + + // delete files + foreach (var file in new[] + { + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), + @"SystemApps\ShellExperienceHost_cw5n1h2txyewy\dxgi.dll"), + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), + @"SystemApps\ShellExperienceHost_cw5n1h2txyewy\wincorlib.dll"), + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), + @"SystemApps\ShellExperienceHost_cw5n1h2txyewy\wincorlib_orig.dll"), + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), + @"SystemApps\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\dxgi.dll"), + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), + @"SystemApps\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\wincorlib.dll"), + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), + @"SystemApps\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\wincorlib_orig.dll"), + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), + "dxgi.dll") + }) + { + if (File.Exists(file)) File.Delete(file); + } + foreach (var folder in new[] + { + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), + "ExplorerPatcher"), + Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), + @"Microsoft\Windows\Start Menu\Programs\ExplorerPatcher") + }) + { + if (Directory.Exists(folder)) Directory.Delete(folder, true); + } + + winlogon?.SetValue("AutoRestartShell", 1); } Program.Frame.Clear(); @@ -110,25 +163,18 @@ Continue? (Y/N): " ConsoleTUI.OpenFrame.WriteCentered("\r\nContinuing without uninstalling software...\r\n"); } - // ExplorerPatcher re-opens Explorer as SYSTEM after uninstalling, so it has to be restarted - // no silent uninstall is available, so user has to manually click 'Yes' to uninstall - // https://github.com/valinet/ExplorerPatcher/discussions/2007 - Thread.Sleep(500); - var winlogon = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true); - winlogon.SetValue("AutoRestartShell", 0); - foreach (var exp in Process.GetProcessesByName("explorer")) - { - exp.Kill(); - } - winlogon.SetValue("AutoRestartShell", 1); - NSudo.RunProcessAsUser(NSudo.GetUserToken(), "explorer.exe", "", 0); - - // all policies are cleared as a user that's de-ameliorating is unlikely - // to have their own policies in the first place + // restart Explorer + if (Process.GetProcessesByName("explorer").Length == 0) + NSudo.RunProcessAsUser(NSudo.GetUserToken(), "explorer.exe", "", 0); + + // all policies are cleared as a user that's de-ameliorating is unlikely to have their own policies in the first place + // also clear ExplorerPatcher Registry entries ConsoleTUI.OpenFrame.WriteCentered("\r\nClearing policies..."); foreach (var keyPath in new[] { $@"HKU\{userSid}\Software\Microsoft\Windows\CurrentVersion\Policies", $@"HKU\{userSid}\Software\Policies", + $@"HKU\{userSid}\Software\ExplorerPatcher", + $@"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{{{ExplorerPatcherId}}}_ExplorerPatcher", @"HKLM\Software\Microsoft\Windows\CurrentVersion\Policies", @"HKLM\Software\Policies", @"HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Policies" From ad8f622ffe562facbef6f6ddb8ea37e11f7b8845 Mon Sep 17 00:00:00 2001 From: he3als Date: Sat, 16 Mar 2024 11:58:46 +0000 Subject: [PATCH 9/9] fix(de-ame): ChoicePrompt starts from 0, not 1 --- src/Actions/Deameliorate.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Actions/Deameliorate.cs b/src/Actions/Deameliorate.cs index 4633aa8..eb24c3c 100644 --- a/src/Actions/Deameliorate.cs +++ b/src/Actions/Deameliorate.cs @@ -28,13 +28,13 @@ Although user data should be kept, we strongly recommend making backups of any important user data. Continue? (Y/N): " - }.Start().Value == 2) return true; + }.Start().Value == 1) return true; Program.Frame.Clear(); (_mountedPath, _, _winVer, _, _) = SelectWindowsImage.GetMediaPath(); if (_mountedPath == null) return false; - if (new ChoicePrompt {Text = $"\r\nYour Windows image is {_winVer}. Continue? (Y/N): "}.Start().Value == 2) + if (new ChoicePrompt {Text = $"\r\nYour Windows image is {_winVer}. Continue? (Y/N): "}.Start().Value == 1) return true; var fc = Console.ForegroundColor;