C#Steam WebRequests

时间:2017-04-21 23:37:21

标签: c# web rsa steam

我是初学者,在尝试登录Steam时遇到了问题:

public Steam(string password, string login)
{
    Password = password;
    Login = login;
    _client.DefaultRequestHeaders.Add("User-Agent",
        "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
    _rsa = new RSACryptoServiceProvider();
    _rsaParameters = new RSAParameters();
    _params = new Dictionary<string, string>();
}
public async Task GetRsa()
{
    var request = await _client.GetAsync($"https://steamcommunity.com/login/getrsakey?username=" + Login);
    var content = await request.Content.ReadAsStringAsync();                
    Timestamp = content.Between("timestamp\":\"", "\"", StringComparison.InvariantCulture);
    TokenGid = content.Between("token_gid\":\"", "\"", StringComparison.InvariantCulture);
    Mod = content.Between("publickey_mod\":\"", "\"", StringComparison.InvariantCulture);
    Exp = content.Between("publickey_exp\":\"", "\"", StringComparison.InvariantCulture);
    _rsaParameters.Modulus = HexToByte(Mod);
    _rsaParameters.Exponent = HexToByte(Exp);
    _rsa.ImportParameters(_rsaParameters);
    var bytePassword = Encoding.ASCII.GetBytes(Password);
    var encodedPassword = _rsa.Encrypt(bytePassword, false);
    EncryptedBase64Password = Convert.ToBase64String(encodedPassword);
}
public async Task TryLogin()
{
    var unixTimestamp = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
    //_params["donotcache"] = unixTimestamp + "000";
    _params["username"] = Login;
    _params["password"] = EncryptedBase64Password;
    _params["twofactorcode"] = "";
    _params["emailauth"] = "";
    _params["loginfriendlyname"] = "";
    _params["captchagid"] = "-1";
    _params["captcha_text"] = "";
    _params["emailsteamid"] = "";
    _params["rsatimestamp"] = Timestamp;
    _params["remember_login"] = "false";

    var responce2 = await _client.PostAsync($"https://steamcommunity.com/login/dologin/", new FormUrlEncodedContent(_params));
    var content2 = await responce2.Content.ReadAsStringAsync();
}

我得到了这个答案:

"success":false,"requires_twofactor":false,"message":"The account name or password that you have entered is incorrect.","clear_password_field":true,"captcha_needed":false,"captcha_gid":-1

而且我不明白我的错误在哪里。也许我的RSA密码错误或者cookie存在问题。

2 个答案:

答案 0 :(得分:1)

查看我的课程

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Numerics;
using System.Threading.Tasks;
using Newtonsoft.Json;

class Web
{
    public const string STEAM_COMMUNITY = "http://steamcommunity.com";
    public const string STEAM_COMMUNITY_GETRSA = "https://steamcommunity.com/login/getrsakey";
    public const string STEAM_COMMUNITY_LOGIN = "https://steamcommunity.com/login/dologin/";


    HttpClient m_HttpClient;
    CookieContainer m_CookieContainer;

    public Web()
    {
        m_CookieContainer = new CookieContainer();
        HttpClientHandler msgHandler = new HttpClientHandler { CookieContainer = m_CookieContainer };
        m_HttpClient = new HttpClient(msgHandler);
    }

    public async Task Login(string pUsername, string pPassword)
    {
        Console.WriteLine("Steamcommunity Login");

        //Get RSA
        Dictionary<string, string> data = new Dictionary<string, string>();

        var request = await m_HttpClient.GetAsync(STEAM_COMMUNITY_GETRSA + "?username=" + pUsername);
        var result = await request.Content.ReadAsStringAsync();
        RsaKey rsaKey = JsonConvert.DeserializeObject<RsaKey>(result);

        if(!rsaKey.success)
        {
            Console.WriteLine("Unsuccessfull RSA Key request.");
            return;
        }

        RsaParameters rsaParam = new RsaParameters
        {
            Exponent = rsaKey.publickey_exp,
            Modulus = rsaKey.publickey_mod,
            Password = pPassword
        };

        var encrypted = string.Empty;
        while (encrypted.Length < 2 || encrypted.Substring(encrypted.Length - 2) != "==")
        {
            encrypted = EncryptPassword(rsaParam);
        }

        data.Add("username", pUsername);
        data.Add("password", encrypted);
        data.Add("twofactorcode", "");
        data.Add("emailauth", "");
        data.Add("loginfriendlyname", "");
        data.Add("captchagid", "-1");
        data.Add("captcha_text", "");
        data.Add("emailsteamid", "");
        data.Add("rsatimestamp", rsaKey.timestamp);
        data.Add("remember_login", "false");

        request = await m_HttpClient.PostAsync(STEAM_COMMUNITY_LOGIN, new FormUrlEncodedContent(data));
        result = await request.Content.ReadAsStringAsync();

        LoginResult loginResult = JsonConvert.DeserializeObject<LoginResult>(result);

        if(loginResult.success)
        {
            IEnumerable<Cookie> responseCookies = m_CookieContainer.GetCookies(new Uri(STEAM_COMMUNITY)).Cast<Cookie>();

            foreach(var cookie in responseCookies)
            {
                Console.WriteLine("Name {0}, {1}", cookie.Name, cookie.Value);
            }

            Console.WriteLine("Successfully logged in.");

            //SendCookies
        }
        else
        {
            Console.WriteLine("Couldn't login...");
            Console.WriteLine(result);
        }

    }

    private string EncryptPassword(RsaParameters rsaParam)
    {
        // Convert the public keys to BigIntegers
        var modulus = CreateBigInteger(rsaParam.Modulus);
        var exponent = CreateBigInteger(rsaParam.Exponent);

        // (modulus.ToByteArray().Length - 1) * 8
        //modulus has 256 bytes multiplied by 8 bits equals 2048
        var encryptedNumber = Pkcs1Pad2(rsaParam.Password, (2048 + 7) >> 3);

        // And now, the RSA encryption
        encryptedNumber = BigInteger.ModPow(encryptedNumber, exponent, modulus);

        //Reverse number and convert to base64
        var encryptedString = Convert.ToBase64String(encryptedNumber.ToByteArray().Reverse().ToArray());

        return encryptedString;
    }

    public static BigInteger Pkcs1Pad2(string data, int keySize)
    {
        if (keySize < data.Length + 11)
            return new BigInteger();

        var buffer = new byte[256];
        var i = data.Length - 1;

        while (i >= 0 && keySize > 0)
        {
            buffer[--keySize] = (byte)data[i--];
        }

        // Padding, I think
        var random = new Random();
        buffer[--keySize] = 0;
        while (keySize > 2)
        {
            buffer[--keySize] = (byte)random.Next(1, 256);
            //buffer[--keySize] = 5;
        }

        buffer[--keySize] = 2;
        buffer[--keySize] = 0;

        Array.Reverse(buffer);

        return new BigInteger(buffer);
    }

    public static BigInteger CreateBigInteger(string hex)
    {
        return BigInteger.Parse("00" + hex, NumberStyles.AllowHexSpecifier);
    }
}

public class LoginResult
{
    public bool success;
    public bool emailauth_needed;
    public bool captcha_needed;

    public string message;
    public string captcha_gid;
    public string emailsteamid;
}

public class RsaParameters
{
    public string Exponent;
    public string Modulus;
    public string Password;
}

public class RsaKey
{
    public bool success;

    public string publickey_mod;
    public string publickey_exp;
    public string timestamp;
}

希望有所帮助

答案 1 :(得分:0)

您的代码看起来不错。要检查的事情: 1.确保您的用户名和用户名。密码是正确的; 2.当你调用getRsaKey时,检查你是否解析了publickey_mod&amp; publickey_exp正确;