Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Numerics;
  8. using System.Threading.Tasks;
  9. using Newtonsoft.Json;
  10.  
  11. class Web
  12. {
  13. public const string STEAM_COMMUNITY = "http://steamcommunity.com";
  14. public const string STEAM_COMMUNITY_GETRSA = "https://steamcommunity.com/login/getrsakey";
  15. public const string STEAM_COMMUNITY_LOGIN = "https://steamcommunity.com/login/dologin/";
  16.  
  17.  
  18. HttpClient m_HttpClient;
  19. CookieContainer m_CookieContainer;
  20.  
  21. public Web()
  22. {
  23. m_CookieContainer = new CookieContainer();
  24. HttpClientHandler msgHandler = new HttpClientHandler { CookieContainer = m_CookieContainer };
  25. m_HttpClient = new HttpClient(msgHandler);
  26. }
  27.  
  28. public async Task Login(string pUsername, string pPassword)
  29. {
  30. Console.WriteLine("Steamcommunity Login");
  31.  
  32. //Get RSA
  33. Dictionary<string, string> data = new Dictionary<string, string>();
  34.  
  35. data.Add("username", pUsername);
  36. var request = await m_HttpClient.GetAsync(STEAM_COMMUNITY_GETRSA + "?username=" + pUsername);
  37. var result = await request.Content.ReadAsStringAsync();
  38. RsaKey rsaKey = JsonConvert.DeserializeObject<RsaKey>(result);
  39.  
  40. if(!rsaKey.success)
  41. {
  42. Console.WriteLine("Unsuccessfull RSA Key request.");
  43. return;
  44. }
  45.  
  46. RsaParameters rsaParam = new RsaParameters
  47. {
  48. Exponent = rsaKey.publickey_exp,
  49. Modulus = rsaKey.publickey_mod,
  50. Password = pPassword
  51. };
  52.  
  53. var encrypted = string.Empty;
  54. while (encrypted.Length < 2 || encrypted.Substring(encrypted.Length - 2) != "==")
  55. {
  56. encrypted = EncryptPassword(rsaParam);
  57. }
  58. data.Clear();
  59.  
  60. data.Add("username", pUsername);
  61. data.Add("password", encrypted);
  62. data.Add("twofactorcode", "");
  63. data.Add("emailauth", "");
  64. data.Add("loginfriendlyname", "");
  65. data.Add("captchagid", "-1");
  66. data.Add("captcha_text", "");
  67. data.Add("emailsteamid", "");
  68. data.Add("rsatimestamp", rsaKey.timestamp);
  69. data.Add("remember_login", "false");
  70.  
  71. request = await m_HttpClient.PostAsync(STEAM_COMMUNITY_LOGIN, new FormUrlEncodedContent(data));
  72. result = await request.Content.ReadAsStringAsync();
  73.  
  74. LoginResult loginResult = JsonConvert.DeserializeObject<LoginResult>(result);
  75.  
  76. if(loginResult.success)
  77. {
  78. IEnumerable<Cookie> responseCookies = m_CookieContainer.GetCookies(new Uri(STEAM_COMMUNITY)).Cast<Cookie>();
  79.  
  80. foreach(var cookie in responseCookies)
  81. {
  82. Console.WriteLine("Name {0}, {1}", cookie.Name, cookie.Value);
  83. }
  84.  
  85. Console.WriteLine("Successfully logged in.");
  86.  
  87. //SendCookies
  88. }
  89. else
  90. {
  91. Console.WriteLine("Couldn't login...");
  92. Console.WriteLine(result);
  93. }
  94.  
  95. }
  96.  
  97. private string EncryptPassword(RsaParameters rsaParam)
  98. {
  99. // Convert the public keys to BigIntegers
  100. var modulus = CreateBigInteger(rsaParam.Modulus);
  101. var exponent = CreateBigInteger(rsaParam.Exponent);
  102.  
  103. // (modulus.ToByteArray().Length - 1) * 8
  104. //modulus has 256 bytes multiplied by 8 bits equals 2048
  105. var encryptedNumber = Pkcs1Pad2(rsaParam.Password, (2048 + 7) >> 3);
  106.  
  107. // And now, the RSA encryption
  108. encryptedNumber = BigInteger.ModPow(encryptedNumber, exponent, modulus);
  109.  
  110. //Reverse number and convert to base64
  111. var encryptedString = Convert.ToBase64String(encryptedNumber.ToByteArray().Reverse().ToArray());
  112.  
  113. return encryptedString;
  114. }
  115.  
  116. public static BigInteger Pkcs1Pad2(string data, int keySize)
  117. {
  118. if (keySize < data.Length + 11)
  119. return new BigInteger();
  120.  
  121. var buffer = new byte[256];
  122. var i = data.Length - 1;
  123.  
  124. while (i >= 0 && keySize > 0)
  125. {
  126. buffer[--keySize] = (byte)data[i--];
  127. }
  128.  
  129. // Padding, I think
  130. var random = new Random();
  131. buffer[--keySize] = 0;
  132. while (keySize > 2)
  133. {
  134. buffer[--keySize] = (byte)random.Next(1, 256);
  135. //buffer[--keySize] = 5;
  136. }
  137.  
  138. buffer[--keySize] = 2;
  139. buffer[--keySize] = 0;
  140.  
  141. Array.Reverse(buffer);
  142.  
  143. return new BigInteger(buffer);
  144. }
  145.  
  146. public static BigInteger CreateBigInteger(string hex)
  147. {
  148. return BigInteger.Parse("00" + hex, NumberStyles.AllowHexSpecifier);
  149. }
  150. }
  151.  
  152. public class LoginResult
  153. {
  154. public bool success;
  155. public bool emailauth_needed;
  156. public bool captcha_needed;
  157.  
  158. public string message;
  159. public string captcha_gid;
  160. public string emailsteamid;
  161. }
  162.  
  163. public class RsaParameters
  164. {
  165. public string Exponent;
  166. public string Modulus;
  167. public string Password;
  168. }
  169.  
  170. public class RsaKey
  171. {
  172. public bool success;
  173.  
  174. public string publickey_mod;
  175. public string publickey_exp;
  176. public string timestamp;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement