Guest User

Untitled

a guest
Jan 4th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. using PasswordResetter.DTOs;
  2. using PasswordResetter.Types;
  3. using System.Management.Automation;
  4. using System.Collections.ObjectModel;
  5. using System.Management.Automation.Runspaces;
  6. using System.Management;
  7. using System.Configuration;
  8. using System.Linq;
  9.  
  10. namespace PasswordResetter.Services
  11. {
  12. public static class ResetCredentials
  13. {
  14. public static bool SetPassword(AccountReference accountReference, string password)
  15. {
  16. switch (accountReference.ReferenceType)
  17. {
  18. case ReferenceType.WindowsService:
  19. {
  20. return SetWindowsServicePassword(accountReference, password);
  21. }
  22. case ReferenceType.ScheduledTask:
  23. {
  24. return SetScheduledTaskPassword(accountReference, password);
  25. }
  26. case ReferenceType.IIS:
  27. {
  28. return SetIISPassword(accountReference, password);
  29. }
  30. default:
  31. {
  32. Logging.Log(string.Format("SetPassword(..) Reference Type Unsupported: {0}", accountReference.ReferenceType.ToString()), LogType.WARNING);
  33. return false;
  34. }
  35. }
  36. }
  37.  
  38. private static bool SetWindowsServicePassword(AccountReference accountReference, string password)
  39. {
  40. Logging.Log(string.Format("Setting Password for {0}", accountReference.ReferenceName), LogType.INFO);
  41.  
  42. ConnectionOptions connection = new ConnectionOptions();
  43.  
  44. var adminDomain = ConfigurationManager.AppSettings["AdminDomain"];
  45. var adminUser = ConfigurationManager.AppSettings["AdminAccount"];
  46. var adminPassword = ConfigurationManager.AppSettings["AdminPassword"];
  47.  
  48. connection.Username = string.Format("{0}\\{1}", adminDomain, adminUser);
  49. connection.Password = adminPassword;
  50. connection.EnablePrivileges = true;
  51. connection.Authentication = AuthenticationLevel.Default;
  52. connection.Impersonation = ImpersonationLevel.Impersonate;
  53.  
  54. ManagementScope scope = new ManagementScope(string.Format("\\\\{0}\\root\\CIMV2", accountReference.Server), connection);
  55. scope.Connect();
  56.  
  57. ObjectQuery query = new ObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", accountReference.ReferenceName));
  58.  
  59. ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
  60.  
  61. var res = searcher.Get();
  62.  
  63. foreach (ManagementObject queryObj in res)
  64. {
  65. object[] wmiParams = new object[10];
  66.  
  67. wmiParams[6] = accountReference.AccountName;
  68. wmiParams[7] = password;
  69.  
  70. queryObj.InvokeMethod("Change", wmiParams);
  71. }
  72.  
  73. return true;
  74. }
  75.  
  76. private static bool SetScheduledTaskPassword(AccountReference accountReference, string password)
  77. {
  78. Logging.Log(string.Format("Setting Password for {0}", accountReference.ReferenceName), LogType.INFO);
  79.  
  80. var errors = false;
  81.  
  82. var connectioninfo = new WSManConnectionInfo
  83. {
  84. ComputerName = accountReference.Server
  85. };
  86. Runspace runspace = RunspaceFactory.CreateRunspace(connectioninfo);
  87. runspace.Open();
  88.  
  89. using (PowerShell powerShellInstance = PowerShell.Create())
  90. {
  91. powerShellInstance.Runspace = runspace;
  92.  
  93. powerShellInstance.AddScript(string.Format("Set-ScheduledTask '{0}' -User '{1}' -Password '{2}'", accountReference.ReferenceName, accountReference.AccountName, password));
  94.  
  95. var result = powerShellInstance.Invoke();
  96.  
  97. if (powerShellInstance.Streams.Error.Count > 0)
  98. {
  99. errors = true;
  100. Logging.Log(string.Format("Error(s) Occurred While Setting Password for {0} : {1}", accountReference.ReferenceName, string.Join(",", powerShellInstance.Streams.Error.Select(e => e.Exception.Message))), LogType.ERROR);
  101. }
  102. }
  103.  
  104. runspace.Close();
  105.  
  106. return !errors;
  107. }
  108.  
  109. private static bool SetIISPassword(AccountReference accountReference, string password)
  110. {
  111. Logging.Log(string.Format("Setting Password for {0}", accountReference.ReferenceName), LogType.INFO);
  112.  
  113. var errors = false;
  114.  
  115. var connectioninfo = new WSManConnectionInfo
  116. {
  117. ComputerName = accountReference.Server
  118. };
  119. Runspace runspace = RunspaceFactory.CreateRunspace(connectioninfo);
  120. runspace.Open();
  121.  
  122. using (PowerShell powerShellInstance = PowerShell.Create())
  123. {
  124. powerShellInstance.Runspace = runspace;
  125.  
  126. powerShellInstance.AddScript("Import-Module WebAdministration");
  127. powerShellInstance.AddScript(string.Format("Set-ItemProperty IIS:\\AppPools\\{0} -name processModel.identityType -Value 3", accountReference.ReferenceName));
  128. powerShellInstance.AddScript(string.Format("Set-ItemProperty IIS:\\AppPools\\{0} -name processModel.userName -Value {1}", accountReference.ReferenceName, accountReference.AccountName));
  129. powerShellInstance.AddScript(string.Format("Set-ItemProperty IIS:\\AppPools\\{0} -name processModel.password -Value {1}", accountReference.ReferenceName, password));
  130.  
  131. var result = powerShellInstance.Invoke();
  132.  
  133. if (powerShellInstance.Streams.Error.Count > 0)
  134. {
  135. errors = true;
  136. Logging.Log(string.Format("Error(s) Occurred While Setting Password for {0} : {1}", accountReference.ReferenceName, string.Join(",", powerShellInstance.Streams.Error.Select(e => e.Exception.Message))), LogType.ERROR);
  137. }
  138. }
  139.  
  140. runspace.Close();
  141.  
  142. return !errors;
  143. }
  144. }
  145. }
Add Comment
Please, Sign In to add comment