ImportAccountForm.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using SteamAuth;
  11. using Newtonsoft.Json;
  12. using Newtonsoft.Json.Linq;
  13. using System.IO;
  14. namespace Steam_Desktop_Authenticator
  15. {
  16. public partial class ImportAccountForm : Form
  17. {
  18. private Manifest mManifest;
  19. public ImportAccountForm()
  20. {
  21. InitializeComponent();
  22. this.mManifest = Manifest.GetManifest();
  23. }
  24. private void btnImport_Click(object sender, EventArgs e)
  25. {
  26. // check if data already added is encripted
  27. #region check if data already added is encripted
  28. string ContiuneImport = "0";
  29. string ManifestFile = "maFiles/manifest.json";
  30. if (File.Exists(ManifestFile))
  31. {
  32. string AppManifestContents = File.ReadAllText(ManifestFile);
  33. AppManifest AppManifestData = JsonConvert.DeserializeObject<AppManifest>(AppManifestContents);
  34. bool AppManifestData_encrypted = AppManifestData.Encrypted;
  35. if (AppManifestData_encrypted == true)
  36. {
  37. MessageBox.Show("You can't import an .maFile because the existing account in the app is encrypted.\nDecrypt it and try again.");
  38. this.Close();
  39. }
  40. else if (AppManifestData_encrypted == false)
  41. {
  42. ContiuneImport = "1";
  43. }
  44. else
  45. {
  46. MessageBox.Show("invalid value for variable 'encrypted' inside manifest.json");
  47. this.Close();
  48. }
  49. }
  50. else
  51. {
  52. MessageBox.Show("An Error occurred, Restart the program!");
  53. }
  54. #endregion
  55. // Continue
  56. #region Continue
  57. if (ContiuneImport == "1")
  58. {
  59. this.Close();
  60. // read EncriptionKey from imput box
  61. string ImportUsingEncriptionKey = txtBox.Text;
  62. // Open file browser > to select the file
  63. OpenFileDialog openFileDialog1 = new OpenFileDialog();
  64. // Set filter options and filter index.
  65. openFileDialog1.Filter = "maFiles (.maFile)|*.maFile|All Files (*.*)|*.*";
  66. openFileDialog1.FilterIndex = 1;
  67. openFileDialog1.Multiselect = false;
  68. // Call the ShowDialog method to show the dialog box.
  69. DialogResult userClickedOK = openFileDialog1.ShowDialog();
  70. // Process input if the user clicked OK.
  71. if (userClickedOK == DialogResult.OK)
  72. {
  73. // Open the selected file to read.
  74. System.IO.Stream fileStream = openFileDialog1.OpenFile();
  75. string fileContents = null;
  76. using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
  77. {
  78. fileContents = reader.ReadToEnd();
  79. }
  80. fileStream.Close();
  81. try
  82. {
  83. if (ImportUsingEncriptionKey == "")
  84. {
  85. // Import maFile
  86. //-------------------------------------------
  87. #region Import maFile
  88. SteamGuardAccount maFile = JsonConvert.DeserializeObject<SteamGuardAccount>(fileContents);
  89. if (maFile.Session == null || maFile.Session.SteamID == 0 || maFile.Session.IsAccessTokenExpired())
  90. {
  91. // Have the user to relogin to steam to get a new session
  92. LoginForm loginForm = new LoginForm(LoginForm.LoginType.Import, maFile);
  93. loginForm.ShowDialog();
  94. if (loginForm.Session == null || loginForm.Session.SteamID == 0)
  95. {
  96. MessageBox.Show("Login failed. Try to import this account again.", "Account Import", MessageBoxButtons.OK, MessageBoxIcon.Error);
  97. return;
  98. }
  99. // Save new session to the maFile
  100. maFile.Session = loginForm.Session;
  101. }
  102. // Save account
  103. mManifest.SaveAccount(maFile, false);
  104. MessageBox.Show("Account Imported!", "Account Import", MessageBoxButtons.OK);
  105. #endregion
  106. }
  107. else
  108. {
  109. // Import Encripted maFile
  110. //-------------------------------------------
  111. #region Import Encripted maFile
  112. //Read manifest.json encryption_iv encryption_salt
  113. string ImportFileName_Found = "0";
  114. string Salt_Found = null;
  115. string IV_Found = null;
  116. string ReadManifestEx = "0";
  117. //No directory means no manifest file anyways.
  118. ImportManifest newImportManifest = new ImportManifest();
  119. newImportManifest.Encrypted = false;
  120. newImportManifest.Entries = new List<ImportManifestEntry>();
  121. // extract folder path
  122. string fullPath = openFileDialog1.FileName;
  123. string fileName = openFileDialog1.SafeFileName;
  124. string path = fullPath.Replace(fileName, "");
  125. // extract fileName
  126. string ImportFileName = fullPath.Replace(path, "");
  127. string ImportManifestFile = path + "manifest.json";
  128. if (File.Exists(ImportManifestFile))
  129. {
  130. string ImportManifestContents = File.ReadAllText(ImportManifestFile);
  131. try
  132. {
  133. ImportManifest account = JsonConvert.DeserializeObject<ImportManifest>(ImportManifestContents);
  134. //bool Import_encrypted = account.Encrypted;
  135. List<ImportManifest> newEntries = new List<ImportManifest>();
  136. foreach (var entry in account.Entries)
  137. {
  138. string FileName = entry.Filename;
  139. string encryption_iv = entry.IV;
  140. string encryption_salt = entry.Salt;
  141. if (ImportFileName == FileName)
  142. {
  143. ImportFileName_Found = "1";
  144. IV_Found = entry.IV;
  145. Salt_Found = entry.Salt;
  146. }
  147. }
  148. }
  149. catch (Exception)
  150. {
  151. ReadManifestEx = "1";
  152. MessageBox.Show("Invalid content inside manifest.json!\nImport Failed.");
  153. }
  154. // DECRIPT & Import
  155. //--------------------
  156. #region DECRIPT & Import
  157. if (ReadManifestEx == "0")
  158. {
  159. if (ImportFileName_Found == "1" && Salt_Found != null && IV_Found != null)
  160. {
  161. string decryptedText = FileEncryptor.DecryptData(ImportUsingEncriptionKey, Salt_Found, IV_Found, fileContents);
  162. if (decryptedText == null)
  163. {
  164. MessageBox.Show("Decryption Failed.\nImport Failed.");
  165. }
  166. else
  167. {
  168. string fileText = decryptedText;
  169. SteamGuardAccount maFile = JsonConvert.DeserializeObject<SteamGuardAccount>(fileText);
  170. if (maFile.Session == null || maFile.Session.SteamID == 0 || maFile.Session.IsAccessTokenExpired())
  171. {
  172. // Have the user to relogin to steam to get a new session
  173. LoginForm loginForm = new LoginForm(LoginForm.LoginType.Import, maFile);
  174. loginForm.ShowDialog();
  175. if (loginForm.Session == null || loginForm.Session.SteamID == 0)
  176. {
  177. MessageBox.Show("Login failed. Try to import this account again.", "Account Import", MessageBoxButtons.OK, MessageBoxIcon.Error);
  178. return;
  179. }
  180. // Save new session to the maFile
  181. maFile.Session = loginForm.Session;
  182. }
  183. // Save account
  184. mManifest.SaveAccount(maFile, false);
  185. MessageBox.Show("Account Imported!\nYour Account in now Decrypted!", "Account Import", MessageBoxButtons.OK);
  186. }
  187. }
  188. else
  189. {
  190. if (ImportFileName_Found == "0")
  191. {
  192. MessageBox.Show("Account not found inside manifest.json.\nImport Failed.");
  193. }
  194. else if (Salt_Found == null && IV_Found == null)
  195. {
  196. MessageBox.Show("manifest.json does not contain encrypted data.\nYour account may be unencrypted!\nImport Failed.");
  197. }
  198. else
  199. {
  200. if (IV_Found == null)
  201. {
  202. MessageBox.Show("manifest.json does not contain: encryption_iv\nImport Failed.");
  203. }
  204. else if (IV_Found == null)
  205. {
  206. MessageBox.Show("manifest.json does not contain: encryption_salt\nImport Failed.");
  207. }
  208. }
  209. }
  210. }
  211. #endregion //DECRIPT & Import END
  212. }
  213. else
  214. {
  215. MessageBox.Show("manifest.json is missing!\nImport Failed.");
  216. }
  217. #endregion //Import Encripted maFile END
  218. }
  219. }
  220. catch (Exception)
  221. {
  222. MessageBox.Show("This file is not a valid SteamAuth maFile.\nImport Failed.");
  223. }
  224. }
  225. }
  226. #endregion // Continue End
  227. }
  228. private void btnCancel_Click(object sender, EventArgs e)
  229. {
  230. this.Close();
  231. }
  232. private void Import_maFile_Form_FormClosing(object sender, FormClosingEventArgs e)
  233. {
  234. }
  235. }
  236. public class AppManifest
  237. {
  238. [JsonProperty("encrypted")]
  239. public bool Encrypted { get; set; }
  240. }
  241. public class ImportManifest
  242. {
  243. [JsonProperty("encrypted")]
  244. public bool Encrypted { get; set; }
  245. [JsonProperty("entries")]
  246. public List<ImportManifestEntry> Entries { get; set; }
  247. }
  248. public class ImportManifestEntry
  249. {
  250. [JsonProperty("encryption_iv")]
  251. public string IV { get; set; }
  252. [JsonProperty("encryption_salt")]
  253. public string Salt { get; set; }
  254. [JsonProperty("filename")]
  255. public string Filename { get; set; }
  256. [JsonProperty("steamid")]
  257. public ulong SteamID { get; set; }
  258. }
  259. }