implemented --encrypt

This commit is contained in:
Carson McManus 2016-08-22 21:12:48 -04:00
parent a4f164a128
commit 34a78da416
2 changed files with 50 additions and 30 deletions

View file

@ -41,11 +41,6 @@ public class Manifest
private static Manifest _manifest { get; set; } private static Manifest _manifest { get; set; }
public static string GetExecutableDir()
{
return Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
}
public static Manifest GetManifest(bool forceLoad = false) public static Manifest GetManifest(bool forceLoad = false)
{ {
// Check if already staticly loaded // Check if already staticly loaded
@ -111,7 +106,6 @@ public class Manifest
// Take a pre-manifest version and generate a manifest for it. // Take a pre-manifest version and generate a manifest for it.
if (scanDir) if (scanDir)
{ {
if (Directory.Exists(Program.SteamGuardPath)) if (Directory.Exists(Program.SteamGuardPath))
{ {
DirectoryInfo dir = new DirectoryInfo(Program.SteamGuardPath); DirectoryInfo dir = new DirectoryInfo(Program.SteamGuardPath);
@ -132,15 +126,16 @@ public class Manifest
}; };
newManifest.Entries.Add(newEntry); newManifest.Entries.Add(newEntry);
} }
catch (Exception) catch (Exception ex)
{ {
if (Program.Verbose) Console.WriteLine("warn: {0}", ex.Message);
} }
} }
if (newManifest.Entries.Count > 0) if (newManifest.Entries.Count > 0)
{ {
newManifest.Save(); newManifest.Save();
newManifest.PromptSetupPassKey("This version of SDA has encryption. Please enter a passkey below, or hit cancel to remain unencrypted"); newManifest.PromptSetupPassKey(true);
} }
} }
} }
@ -183,33 +178,33 @@ public class Manifest
} }
// TODO: move PromptSetupPassKey to Program.cs // TODO: move PromptSetupPassKey to Program.cs
public string PromptSetupPassKey(string initialPrompt = "Enter passkey, or hit cancel to remain unencrypted.") public string PromptSetupPassKey(bool inAccountSetupProcess = false)
{ {
Console.Write("Would you like to use encryption? [Y/n] "); if (inAccountSetupProcess)
string doEncryptAnswer = Console.ReadLine();
if (doEncryptAnswer == "n" || doEncryptAnswer == "N")
{ {
Console.WriteLine("WARNING: You chose to not encrypt your files. Doing so imposes a security risk for yourself. If an attacker were to gain access to your computer, they could completely lock you out of your account and steal all your items."); Console.Write("Would you like to use encryption? [Y/n] ");
return null; string doEncryptAnswer = Console.ReadLine();
if (doEncryptAnswer == "n" || doEncryptAnswer == "N")
{
Console.WriteLine("WARNING: You chose to not encrypt your files. Doing so imposes a security risk for yourself. If an attacker were to gain access to your computer, they could completely lock you out of your account and steal all your items.");
return null;
}
} }
string newPassKey = ""; string newPassKey = "";
string confirmPassKey = ""; string confirmPassKey = "";
do do
{ {
Console.Write("Enter passkey: "); Console.Write("Enter" + (inAccountSetupProcess ? " " : " new ") + "passkey: ");
newPassKey = Console.ReadLine(); newPassKey = Console.ReadLine();
Console.Write("Confirm passkey: "); Console.Write("Confirm" + (inAccountSetupProcess ? " " : " new ") + "passkey: ");
confirmPassKey = Console.ReadLine(); confirmPassKey = Console.ReadLine();
if (newPassKey != confirmPassKey) if (newPassKey != confirmPassKey)
{ {
Console.WriteLine("Passkeys do not match."); Console.WriteLine("Passkeys do not match.");
} }
} while (newPassKey != confirmPassKey); } while (newPassKey != confirmPassKey || newPassKey == "");
Console.WriteLine("Unable to set passkey.");
return null;
return newPassKey; return newPassKey;
} }
@ -307,13 +302,11 @@ public class Manifest
return false; return false;
} }
public bool SaveAccount(SteamGuardAccount account, bool encrypt, string passKey = null) public bool SaveAccount(SteamGuardAccount account, bool encrypt, string passKey = null, string salt = null, string iV = null)
{ {
if (encrypt && String.IsNullOrEmpty(passKey)) return false; if (encrypt && String.IsNullOrEmpty(passKey)) return false;
if (!encrypt && this.Encrypted) return false; if (!encrypt && this.Encrypted) return false;
string salt = null;
string iV = null;
string jsonAccount = JsonConvert.SerializeObject(account); string jsonAccount = JsonConvert.SerializeObject(account);
string filename = account.Session.SteamID.ToString() + ".maFile"; string filename = account.Session.SteamID.ToString() + ".maFile";
@ -354,7 +347,6 @@ public class Manifest
try try
{ {
Stream stream = null; Stream stream = null;
FileStream fileStream = File.OpenWrite(Path.Combine(Program.SteamGuardPath, newEntry.Filename));
MemoryStream ms = null; MemoryStream ms = null;
RijndaelManaged aes256; RijndaelManaged aes256;
@ -371,12 +363,12 @@ public class Manifest
Mode = CipherMode.CBC Mode = CipherMode.CBC
}; };
ICryptoTransform decryptor = aes256.CreateDecryptor(aes256.Key, aes256.IV); ICryptoTransform encryptor = aes256.CreateEncryptor(aes256.Key, aes256.IV);
stream = new CryptoStream(ms, decryptor, CryptoStreamMode.Write); stream = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
} }
else else
{ {
stream = fileStream; stream = File.OpenWrite(Path.Combine(Program.SteamGuardPath, newEntry.Filename));
} }
using (StreamWriter writer = new StreamWriter(stream)) using (StreamWriter writer = new StreamWriter(stream))
@ -386,14 +378,15 @@ public class Manifest
if (Encrypted) if (Encrypted)
{ {
File.WriteAllText(Convert.ToBase64String(ms.ToArray()), Path.Combine(Program.SteamGuardPath, newEntry.Filename)); File.WriteAllText(Path.Combine(Program.SteamGuardPath, newEntry.Filename), Convert.ToBase64String(ms.ToArray()));
} }
stream.Close(); stream.Close();
return true; return true;
} }
catch (Exception) catch (Exception ex)
{ {
if (Program.Verbose) Console.WriteLine("error: {0}", ex.ToString());
return false; return false;
} }
} }

View file

@ -92,13 +92,15 @@ public static class Program
} }
if (Verbose) Console.WriteLine("maFiles path: {0}", SteamGuardPath); if (Verbose) Console.WriteLine("maFiles path: {0}", SteamGuardPath);
if (Verbose) Console.WriteLine("Action: {0}", action);
// Perform desired action // Perform desired action
switch (action) switch (action)
{ {
case "generate-code": case "generate-code":
GenerateCode(user); GenerateCode(user);
break; break;
case "encrypt": case "encrypt": // Can also be used to change passkey
Encrypt();
break; break;
case "decrypt": case "decrypt":
break; break;
@ -157,4 +159,29 @@ public static class Program
else else
Console.WriteLine("error: No Steam accounts found in {0}", SteamGuardAccounts); Console.WriteLine("error: No Steam accounts found in {0}", SteamGuardAccounts);
} }
static void Encrypt()
{
if (Verbose) Console.WriteLine("Opening manifest...");
Manifest = Manifest.GetManifest(true);
if (Verbose) Console.WriteLine("Reading accounts from manifest...");
if (Manifest.Encrypted)
{
string passkey = Manifest.PromptForPassKey();
SteamGuardAccounts = Manifest.GetAllAccounts(passkey);
}
else
{
SteamGuardAccounts = Manifest.GetAllAccounts();
}
string newPassKey = Manifest.PromptSetupPassKey();
for (int i = 0; i < SteamGuardAccounts.Length; i++)
{
var account = SteamGuardAccounts[i];
bool success = Manifest.SaveAccount(account, true, newPassKey, Manifest.GetRandomSalt(), Manifest.GetInitializationVector());
if (Verbose) Console.WriteLine("Encrypted {0}: {1}", account.AccountName, success);
}
}
} }