Script for automating a large assortment of AME related actions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
6.9 KiB

9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
  1. using System;
  2. using System.DirectoryServices.AccountManagement;
  3. using System.Linq;
  4. using System.Security.Principal;
  5. using Ameliorated.ConsoleUtils;
  6. using Microsoft.Win32;
  7. using amecs.Actions;
  8. using Menu = Ameliorated.ConsoleUtils.Menu;
  9. namespace amecs
  10. {
  11. internal class Program
  12. {
  13. private const string Ver = "2.1";
  14. public static ConsoleTUI.Frame Frame;
  15. [STAThread]
  16. public static void Main(string[] args)
  17. {
  18. ConsoleTUI.Initialize("Central AME Script");
  19. try
  20. {
  21. NSudo.GetSystemPrivilege();
  22. if (!WindowsIdentity.GetCurrent().IsSystem)
  23. throw new Exception("Identity did not change.");
  24. NSudo.RunAsUser(() =>
  25. {
  26. Globals.Username = WindowsIdentity.GetCurrent().Name.Split('\\').Last();
  27. Globals.UserDomain = WindowsIdentity.GetCurrent().Name.Split('\\').FirstOrDefault();
  28. Globals.UserSID = WindowsIdentity.GetCurrent().User.ToString();
  29. });
  30. try
  31. {
  32. Globals.UserFolder = Registry.Users.OpenSubKey(Globals.UserSID + "\\Volatile Environment").GetValue("USERPROFILE").ToString();
  33. } catch (Exception e)
  34. {
  35. Console.WriteLine(Globals.Username);
  36. ConsoleTUI.ShowErrorBox($"Could not fetch user folder information from user with SID '{Globals.UserSID}': " + e, "Central AME Script");
  37. Environment.Exit(1);
  38. }
  39. PrincipalContext context = new PrincipalContext(ContextType.Machine);
  40. PrincipalSearcher userPrincipalSearcher = new PrincipalSearcher(new UserPrincipal(context));
  41. Globals.User = userPrincipalSearcher.FindAll().FirstOrDefault(x => (x is UserPrincipal) && x.Sid.Value == Globals.UserSID) as UserPrincipal;
  42. PrincipalSearcher groupPrincipalSearcher = new PrincipalSearcher(new GroupPrincipal(context));
  43. Globals.Administrators = groupPrincipalSearcher.FindAll().FirstOrDefault(x => (x is GroupPrincipal) && x.Sid.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid)) as GroupPrincipal;
  44. } catch (Exception e)
  45. {
  46. ConsoleTUI.ShowErrorBox("Could not acquire System privileges: " + e, "Central AME Script");
  47. Environment.Exit(1);
  48. }
  49. Frame = new ConsoleTUI.Frame($"| Central AME Script v{Ver} |", false);
  50. Frame.Open();
  51. while (true)
  52. {
  53. Globals.UserElevated = Globals.User.IsMemberOf(Globals.Administrators);
  54. Frame.Clear();
  55. bool usernameRequirement = new Reg.Value()
  56. {
  57. KeyName = @"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System",
  58. ValueName = "dontdisplaylastusername",
  59. Data = 1,
  60. }.IsEqual();
  61. bool autoLogonEnabled = new Reg.Value()
  62. {
  63. KeyName = @"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon",
  64. ValueName = "DefaultUsername",
  65. Data = Globals.Username,
  66. }.IsEqual() &&
  67. new Reg.Value()
  68. {
  69. KeyName = @"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon",
  70. ValueName = "AutoAdminLogon",
  71. Data = "1",
  72. }.IsEqual();
  73. bool netInstalled = new Reg.Key()
  74. {
  75. KeyName = @"HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5",
  76. Operation = RegistryOperation.Add
  77. }.IsEqual();
  78. var mainMenu = new Ameliorated.ConsoleUtils.Menu()
  79. {
  80. Choices =
  81. {
  82. new Menu.MenuItem("Change Username or Password", new Func<bool>(UserPass.ShowMenu)),
  83. new Menu.MenuItem("Change Lockscreen Image", new Func<bool>(Lockscreen.ChangeImage)),
  84. new Menu.MenuItem("Change Profile Image", new Func<bool>(Profile.ChangeImage)),
  85. Globals.UserElevated ?
  86. new Menu.MenuItem( "De-elevate User", new Func<bool>(Elevation.DeElevate)) :
  87. new Menu.MenuItem("Elevate User to Administrator", new Func<bool>(Elevation.Elevate)),
  88. usernameRequirement ?
  89. new Menu.MenuItem( "Disable Username Login Requirement", new Func<bool>(UsernameRequirement.Disable)) :
  90. new Menu.MenuItem("Enable Username Login Requirement", new Func<bool>(UsernameRequirement.Enable)),
  91. autoLogonEnabled ?
  92. new Menu.MenuItem( "Disable AutoLogon", new Func<bool>(AutoLogon.Disable)) :
  93. new Menu.MenuItem("Enable AutoLogon", new Func<bool>(AutoLogon.Enable)),
  94. new Menu.MenuItem("Manage Language Settings", new Func<bool>(Languages.ShowMenu)),
  95. new Menu.MenuItem("Manage Users", new Func<bool>(Users.ShowMenu)),
  96. !netInstalled ?
  97. new Menu.MenuItem("Install .NET 3.5", new Func<bool>(_NET.Install)) :
  98. new Menu.MenuItem("Install .NET 3.5", new Func<bool>(_NET.Install)) {SecondaryText = "[Installed]", SecondaryTextForeground = ConsoleColor.Yellow, PrimaryTextForeground = ConsoleColor.DarkGray},
  99. Menu.MenuItem.Blank,
  100. new Menu.MenuItem("Uninstall AME", new Func<bool>(Deameliorate.DeAme)),
  101. new Menu.MenuItem("Extra", new Func<bool>(Extra.Extra.ShowMenu)),
  102. new Menu.MenuItem("Exit", new Func<bool>(Globals.Exit))
  103. },
  104. SelectionForeground = ConsoleColor.Green
  105. };
  106. Func<bool> result;
  107. try
  108. {
  109. mainMenu.Write();
  110. result = (Func<bool>)mainMenu.Load();
  111. } catch (Exception e)
  112. {
  113. Console.WriteLine(e);
  114. Console.ReadLine();
  115. return;
  116. }
  117. try
  118. {
  119. result.Invoke();
  120. } catch (Exception e)
  121. {
  122. ConsoleTUI.ShowErrorBox("Error while running an action: " + e.ToString(), null);
  123. }
  124. }
  125. }
  126. }
  127. }