Advertisement
Guest User

Untitled

a guest
May 28th, 2017
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. public bool DoLogin(string username, string password)
  2. {
  3. var data = new NameValueCollection { { "username", username } };
  4. // First get the RSA key with which we will encrypt our password.
  5. string response = Fetch("https://steamcommunity.com/login/getrsakey", "POST", data, false);
  6. GetRsaKey rsaJson = JsonConvert.DeserializeObject<GetRsaKey>(response);
  7.  
  8. // Validate, if we could get the rsa key.
  9. if (!rsaJson.success)
  10. {
  11. return false;
  12. }
  13.  
  14. // RSA Encryption.
  15. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  16. RSAParameters rsaParameters = new RSAParameters
  17. {
  18. Exponent = HexToByte(rsaJson.publickey_exp),
  19. Modulus = HexToByte(rsaJson.publickey_mod)
  20. };
  21.  
  22. rsa.ImportParameters(rsaParameters);
  23.  
  24. // Encrypt the password and convert it.
  25. byte[] bytePassword = Encoding.ASCII.GetBytes(password);
  26. byte[] encodedPassword = rsa.Encrypt(bytePassword, false);
  27. string encryptedBase64Password = Convert.ToBase64String(encodedPassword);
  28.  
  29. SteamResult loginJson = null;
  30. CookieCollection cookieCollection;
  31. string steamGuardText = "";
  32. string steamGuardId = "";
  33. string twofactorcode = "";
  34.  
  35. // Do this while we need a captcha or need email authentification. Probably you have misstyped the captcha or the SteamGaurd code if this comes multiple times.
  36. do
  37. {
  38. Console.WriteLine("SteamWeb: Logging In...");
  39.  
  40. bool captcha = loginJson != null && loginJson.captcha_needed;
  41. bool steamGuard = loginJson != null && loginJson.emailauth_needed;
  42. bool twofactor = loginJson != null && loginJson.requires_twofactor;
  43.  
  44. string time = Uri.EscapeDataString(rsaJson.timestamp);
  45.  
  46. string capGid = string.Empty;
  47. // Response does not need to send if captcha is needed or not.
  48. // ReSharper disable once MergeSequentialChecks
  49. if (loginJson != null && loginJson.captcha_gid != null)
  50. {
  51. capGid = Uri.EscapeDataString(loginJson.captcha_gid);
  52. }
  53.  
  54. data = new NameValueCollection { { "password", encryptedBase64Password }, { "username", username } };
  55.  
  56. // Captcha Check.
  57. string capText = "";
  58. if (captcha)
  59. {
  60. Console.WriteLine("SteamWeb: Captcha is needed.");
  61. System.Diagnostics.Process.Start("https://steamcommunity.com/public/captcha.php?gid=" + loginJson.captcha_gid);
  62. Console.WriteLine("SteamWeb: Type the captcha:");
  63. string consoleText = Console.ReadLine();
  64. if (!string.IsNullOrEmpty(consoleText))
  65. {
  66. capText = Uri.EscapeDataString(consoleText);
  67. }
  68. }
  69.  
  70. data.Add("captchagid", captcha ? capGid : "-1");
  71. data.Add("captcha_text", captcha ? capText : "");
  72. // Captcha end.
  73.  
  74. if (twofactor)
  75. {
  76. Console.WriteLine("SteamWeb: TwoFactor is needed.");
  77. Console.WriteLine("SteamWeb: Type the code displayed on your mobile authenticator");
  78. string consoleText = Console.ReadLine();
  79. if (!string.IsNullOrEmpty(consoleText))
  80. {
  81. twofactorcode = Uri.EscapeDataString(consoleText);
  82. }
  83. }
  84.  
  85. // Added Header for two factor code.
  86. data.Add("twofactorcode", twofactor ? twofactorcode : "");
  87.  
  88. // Added Header for remember login. It can also set to true.
  89. data.Add("remember_login", "false");
  90.  
  91. // SteamGuard check. If SteamGuard is enabled you need to enter it. Care probably you need to wait 7 days to trade.
  92. // For further information about SteamGuard see: https://support.steampowered.com/kb_article.php?ref=4020-ALZM-5519&l=english.
  93. if (steamGuard)
  94. {
  95. Console.WriteLine("SteamWeb: SteamGuard is needed.");
  96. Console.WriteLine("SteamWeb: Type the code:");
  97. string consoleText = Console.ReadLine();
  98. if (!string.IsNullOrEmpty(consoleText))
  99. {
  100. steamGuardText = Uri.EscapeDataString(consoleText);
  101. }
  102. steamGuardId = loginJson.emailsteamid;
  103.  
  104. // Adding the machine name to the NameValueCollection, because it is requested by steam.
  105. Console.WriteLine("SteamWeb: Type your machine name:");
  106. consoleText = Console.ReadLine();
  107. var machineName = string.IsNullOrEmpty(consoleText) ? "" : Uri.EscapeDataString(consoleText);
  108. data.Add("loginfriendlyname", machineName != "" ? machineName : "defaultSteamBotMachine");
  109. }
  110.  
  111. data.Add("emailauth", steamGuardText);
  112. data.Add("emailsteamid", steamGuardId);
  113. // SteamGuard end.
  114.  
  115. // Added unixTimestamp. It is included in the request normally.
  116. var unixTimestamp = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
  117. // Added three "0"'s because Steam has a weird unix timestamp interpretation.
  118. data.Add("donotcache", unixTimestamp + "000");
  119.  
  120. data.Add("rsatimestamp", time);
  121.  
  122. // Sending the actual login.
  123. using (HttpWebResponse webResponse = Request("https://steamcommunity.com/login/dologin/", "POST", data, false))
  124. {
  125. var stream = webResponse.GetResponseStream();
  126. if (stream == null)
  127. {
  128. return false;
  129. }
  130. using (StreamReader reader = new StreamReader(stream))
  131. {
  132. string json = reader.ReadToEnd();
  133. loginJson = JsonConvert.DeserializeObject<SteamResult>(json);
  134. cookieCollection = webResponse.Cookies;
  135. }
  136. }
  137. } while (loginJson.captcha_needed || loginJson.emailauth_needed || loginJson.requires_twofactor);
  138.  
  139. // If the login was successful, we need to enter the cookies to steam.
  140. if (loginJson.success)
  141. {
  142. _cookies = new CookieContainer();
  143. foreach (Cookie cookie in cookieCollection)
  144. {
  145. _cookies.Add(cookie);
  146. }
  147. SubmitCookies(_cookies);
  148. return true;
  149. }
  150. else
  151. {
  152. Console.WriteLine("SteamWeb Error: " + loginJson.message);
  153. return false;
  154. }
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement