|
| 1 | +using Microsoft.Win32; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Security.Cryptography; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +namespace SharpXDecrypt |
| 10 | +{ |
| 11 | + class XClass |
| 12 | + { |
| 13 | + public struct Xsh |
| 14 | + { |
| 15 | + public string host; |
| 16 | + public string userName; |
| 17 | + public string password; |
| 18 | + public string encryptPw; |
| 19 | + public string version; |
| 20 | + } |
| 21 | + static private Boolean enableMaterPasswd = false; |
| 22 | + static private string hashMasterPasswd = null; |
| 23 | + public static Boolean Decrypt() |
| 24 | + { |
| 25 | + List<string> userDataPaths = GetUserDataPath(); |
| 26 | + Utils.UserSID userSID = Utils.GetUserSID(); |
| 27 | + foreach (string userDataPath in userDataPaths) |
| 28 | + { |
| 29 | + CheckMasterPw(userDataPath); |
| 30 | + List<string> xshPathList = EnumXshPath(userDataPath); |
| 31 | + foreach (string xshPath in xshPathList) |
| 32 | + { |
| 33 | + Xsh xsh = XSHParser(xshPath); |
| 34 | + if (xsh.encryptPw != null) |
| 35 | + { |
| 36 | + Console.WriteLine(" XSHPath: " + xshPath); |
| 37 | + xsh.password = Xdecrypt(xsh, userSID); |
| 38 | + Console.WriteLine(" Host: " + xsh.host); |
| 39 | + Console.WriteLine(" UserName: " + xsh.userName); |
| 40 | + Console.WriteLine(" Password: " + xsh.password); |
| 41 | + Console.WriteLine(" Version: " + xsh.version); |
| 42 | + Console.WriteLine(); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + return true; |
| 47 | + } |
| 48 | + public static string Xdecrypt(Xsh xsh, Utils.UserSID userSID) |
| 49 | + { |
| 50 | + string password = null; |
| 51 | + if (!enableMaterPasswd) |
| 52 | + { |
| 53 | + if (xsh.version.StartsWith("5.0") || xsh.version.StartsWith("4") || xsh.version.StartsWith("3") || xsh.version.StartsWith("2")) |
| 54 | + { |
| 55 | + byte[] data = Convert.FromBase64String(xsh.encryptPw); |
| 56 | + byte[] Key = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes("!X@s#h$e%l^l&")); |
| 57 | + byte[] passData = new byte[data.Length - 0x20]; |
| 58 | + Array.Copy(data, 0, passData, 0, data.Length - 0x20); |
| 59 | + byte[] decrypted = RC4.Decrypt(Key, passData); |
| 60 | + password = Encoding.ASCII.GetString(decrypted); |
| 61 | + } |
| 62 | + else if (xsh.version.StartsWith("5.1") || xsh.version.StartsWith("5.2")) |
| 63 | + { |
| 64 | + byte[] data = Convert.FromBase64String(xsh.encryptPw); |
| 65 | + byte[] Key = new SHA256Managed().ComputeHash(Encoding.ASCII.GetBytes(userSID.SID)); |
| 66 | + byte[] passData = new byte[data.Length - 0x20]; |
| 67 | + Array.Copy(data, 0, passData, 0, data.Length - 0x20); |
| 68 | + byte[] decrypted = RC4.Decrypt(Key, passData); |
| 69 | + password = Encoding.ASCII.GetString(decrypted); |
| 70 | + } |
| 71 | + else if (xsh.version.StartsWith("5") || xsh.version.StartsWith("6")) |
| 72 | + { |
| 73 | + byte[] data = Convert.FromBase64String(xsh.encryptPw); |
| 74 | + byte[] Key = new SHA256Managed().ComputeHash(Encoding.ASCII.GetBytes(userSID.Name + userSID.SID)); |
| 75 | + byte[] passData = new byte[data.Length - 0x20]; |
| 76 | + Array.Copy(data, 0, passData, 0, data.Length - 0x20); |
| 77 | + byte[] decrypted = RC4.Decrypt(Key, passData); |
| 78 | + password = Encoding.ASCII.GetString(decrypted); |
| 79 | + }else if (xsh.version.StartsWith("7")) |
| 80 | + { |
| 81 | + string strkey1 = new string(userSID.Name.ToCharArray().Reverse().ToArray()) + userSID.SID; |
| 82 | + string strkey2 = new string(strkey1.ToCharArray().Reverse().ToArray()); |
| 83 | + byte[] data = Convert.FromBase64String(xsh.encryptPw); |
| 84 | + byte[] Key = new SHA256Managed().ComputeHash(Encoding.ASCII.GetBytes(strkey2)); |
| 85 | + byte[] passData = new byte[data.Length - 0x20]; |
| 86 | + Array.Copy(data, 0, passData, 0, data.Length - 0x20); |
| 87 | + byte[] decrypted = RC4.Decrypt(Key, passData); |
| 88 | + password = Encoding.ASCII.GetString(decrypted); |
| 89 | + } |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + Console.WriteLine(" MasterPassword Enable !"); |
| 94 | + } |
| 95 | + return password; |
| 96 | + } |
| 97 | + |
| 98 | + public static void DecryptMasterPw() |
| 99 | + { |
| 100 | + |
| 101 | + } |
| 102 | + public static Xsh XSHParser(string xshPath) |
| 103 | + { |
| 104 | + Xsh xsh; |
| 105 | + xsh.host = null; |
| 106 | + xsh.userName = null; |
| 107 | + xsh.password = null; |
| 108 | + xsh.version = null; |
| 109 | + xsh.encryptPw = null; |
| 110 | + using (StreamReader sr = new StreamReader(xshPath)) |
| 111 | + { |
| 112 | + string rawPass; |
| 113 | + while ((rawPass = sr.ReadLine()) != null) |
| 114 | + { |
| 115 | + if (System.Text.RegularExpressions.Regex.IsMatch(rawPass, @"Host=(.*?)")) |
| 116 | + { |
| 117 | + xsh.host = rawPass.Replace("Host=", ""); |
| 118 | + } |
| 119 | + if (System.Text.RegularExpressions.Regex.IsMatch(rawPass, @"Password=(.*?)")) |
| 120 | + { |
| 121 | + rawPass = rawPass.Replace("Password=", ""); |
| 122 | + rawPass = rawPass.Replace("\r\n", ""); |
| 123 | + if (rawPass.Equals("")) |
| 124 | + { |
| 125 | + continue; |
| 126 | + } |
| 127 | + xsh.encryptPw = rawPass; |
| 128 | + } |
| 129 | + if (System.Text.RegularExpressions.Regex.IsMatch(rawPass, @"UserName=(.*?)")) |
| 130 | + { |
| 131 | + xsh.userName = rawPass.Replace("UserName=", ""); |
| 132 | + } |
| 133 | + if (System.Text.RegularExpressions.Regex.IsMatch(rawPass, @"Version=(.*?)")) |
| 134 | + { |
| 135 | + xsh.version = rawPass.Replace("Version=", ""); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + return xsh; |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + public static List<string> EnumXshPath(string userDataPath) |
| 144 | + { |
| 145 | + List<string> xshPathList = new List<string>(); |
| 146 | + string sessionsPath = userDataPath + "\\Xshell\\Sessions"; |
| 147 | + if (Directory.Exists(sessionsPath))//判断是否存在 |
| 148 | + { |
| 149 | + DirectoryInfo directoryInfo = new DirectoryInfo(sessionsPath); |
| 150 | + FileInfo[] files = directoryInfo.GetFiles(); |
| 151 | + foreach (FileInfo fileInfo in files) |
| 152 | + { |
| 153 | + string name = fileInfo.Name; |
| 154 | + if (fileInfo.Extension.Equals(".xsh")) |
| 155 | + { |
| 156 | + string sessionPath = sessionsPath + "\\" + name; |
| 157 | + xshPathList.Add(sessionPath); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + return xshPathList; |
| 162 | + } |
| 163 | + public static List<string> GetUserDataPath() |
| 164 | + { |
| 165 | + Console.WriteLine("[*] Start GetUserPath...."); |
| 166 | + List<string> userDataPath = new List<string>(); |
| 167 | + string strRegPath = @"Software\\NetSarang\\Common"; |
| 168 | + RegistryKey regRootKey; |
| 169 | + RegistryKey regSubKey; |
| 170 | + regRootKey = Registry.CurrentUser; |
| 171 | + regSubKey = regRootKey.OpenSubKey(strRegPath); |
| 172 | + foreach (string version in regSubKey.GetSubKeyNames()) |
| 173 | + { |
| 174 | + if (!version.Equals("LiveUpdate")) |
| 175 | + { |
| 176 | + string strUserDataRegPath = strRegPath + "\\" + version + "\\UserData"; |
| 177 | + regSubKey = regRootKey.OpenSubKey(strUserDataRegPath); |
| 178 | + Console.WriteLine(" UserPath: " + regSubKey.GetValue("UserDataPath")); |
| 179 | + userDataPath.Add(regSubKey.GetValue("UserDataPath").ToString()); |
| 180 | + } |
| 181 | + } |
| 182 | + regSubKey.Close(); |
| 183 | + regRootKey.Close(); |
| 184 | + Console.WriteLine("[*] Get UserPath Success !"); |
| 185 | + Console.WriteLine(); |
| 186 | + return userDataPath; |
| 187 | + } |
| 188 | + public static void CheckMasterPw(string userDataPath) |
| 189 | + { |
| 190 | + string masterPwPath = userDataPath + "\\common\\MasterPassword.mpw"; |
| 191 | + using (StreamReader sr = new StreamReader(masterPwPath)) |
| 192 | + { |
| 193 | + string rawPass; |
| 194 | + while ((rawPass = sr.ReadLine()) != null) |
| 195 | + { |
| 196 | + if (System.Text.RegularExpressions.Regex.IsMatch(rawPass, @"EnblMasterPasswd=(.*?)")) |
| 197 | + { |
| 198 | + rawPass = rawPass.Replace("EnblMasterPasswd=", ""); |
| 199 | + if (rawPass.Equals("1")) |
| 200 | + { |
| 201 | + enableMaterPasswd = true; |
| 202 | + } |
| 203 | + else |
| 204 | + { |
| 205 | + enableMaterPasswd = false; |
| 206 | + } |
| 207 | + } |
| 208 | + if (System.Text.RegularExpressions.Regex.IsMatch(rawPass, @"HashMasterPasswd=(.*?)")) |
| 209 | + { |
| 210 | + rawPass = rawPass.Replace("HashMasterPasswd=", ""); |
| 211 | + if (rawPass.Length > 1) |
| 212 | + { |
| 213 | + hashMasterPasswd = rawPass; |
| 214 | + } |
| 215 | + else |
| 216 | + { |
| 217 | + hashMasterPasswd = null; |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments