Advertisement
Looong

SteamKit make offer

Sep 6th, 2014
1,350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Linq;
  5. using System.Text;
  6. using System.IO;
  7. using System.Threading;
  8. using System.Web;
  9. using System.Net;
  10.  
  11. using SteamKit2;
  12.  
  13. namespace TestCS
  14. {
  15.     class Program
  16.     {
  17.         static SteamClient steamClient;
  18.         static CallbackManager manager;
  19.  
  20.         static SteamUser steamUser;
  21.  
  22.         static bool isRunning;
  23.  
  24.         static string user, pass;
  25.         static string authCode;
  26.  
  27.         static string sessionId;
  28.         static string MyLoginKey;
  29.         static string token;
  30.  
  31.         static void Main(string[] args)
  32.         {
  33.             if (args.Length < 2)
  34.             {
  35.                 Console.WriteLine("Sample5: No username and password specified!");
  36.                 return;
  37.             }
  38.  
  39.             // save our logon details
  40.             user = args[0];
  41.             pass = args[1];
  42.  
  43.             // create our steamclient instance
  44.             steamClient = new SteamClient();
  45.             // create the callback manager which will route callbacks to function calls
  46.             manager = new CallbackManager(steamClient);
  47.  
  48.             // get the steamuser handler, which is used for logging on after successfully connecting
  49.             steamUser = steamClient.GetHandler<SteamUser>();
  50.  
  51.             // register a few callbacks we're interested in
  52.             // these are registered upon creation to a callback manager, which will then route the callbacks
  53.             // to the functions specified
  54.             new Callback<SteamClient.ConnectedCallback>(OnConnected, manager);
  55.             new Callback<SteamClient.DisconnectedCallback>(OnDisconnected, manager);
  56.  
  57.             new Callback<SteamUser.LoggedOnCallback>(OnLoggedOn, manager);
  58.             new Callback<SteamUser.LoggedOffCallback>(OnLoggedOff, manager);
  59.  
  60.             // this callback is triggered when the steam servers wish for the client to store the sentry file
  61.             new JobCallback<SteamUser.UpdateMachineAuthCallback>(OnMachineAuth, manager);
  62.  
  63.             new Callback<SteamUser.LoginKeyCallback>(OnLoginKey, manager);
  64.  
  65.             isRunning = true;
  66.  
  67.             Console.WriteLine("Connecting to Steam...");
  68.  
  69.             // initiate the connection
  70.             steamClient.Connect();
  71.  
  72.             // create our callback handling loop
  73.             while (isRunning)
  74.             {
  75.                 // in order for the callbacks to get routed, they need to be handled by the manager
  76.                 manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
  77.             }
  78.  
  79.             TradeOffer();
  80.         }
  81.  
  82.         static void OnConnected(SteamClient.ConnectedCallback callback)
  83.         {
  84.             if (callback.Result != EResult.OK)
  85.             {
  86.                 Console.WriteLine("Unable to connect to Steam: {0}", callback.Result);
  87.  
  88.                 isRunning = false;
  89.                 return;
  90.             }
  91.  
  92.             Console.WriteLine("Connected to Steam! Logging in '{0}'...", user);
  93.  
  94.             byte[] sentryHash = null;
  95.             if (File.Exists("sentry.bin"))
  96.             {
  97.                 // if we have a saved sentry file, read and sha-1 hash it
  98.                 byte[] sentryFile = File.ReadAllBytes("sentry.bin");
  99.                 sentryHash = CryptoHelper.SHAHash(sentryFile);
  100.             }
  101.  
  102.             steamUser.LogOn(new SteamUser.LogOnDetails
  103.             {
  104.                 Username = user,
  105.                 Password = pass,
  106.  
  107.                 // in this sample, we pass in an additional authcode
  108.                 // this value will be null (which is the default) for our first logon attempt
  109.                 AuthCode = authCode,
  110.  
  111.                 // our subsequent logons use the hash of the sentry file as proof of ownership of the file
  112.                 // this will also be null for our first (no authcode) and second (authcode only) logon attempts
  113.                 SentryFileHash = sentryHash,
  114.             });
  115.         }
  116.  
  117.         static void OnDisconnected(SteamClient.DisconnectedCallback callback)
  118.         {
  119.             // after recieving an AccountLogonDenied, we'll be disconnected from steam
  120.             // so after we read an authcode from the user, we need to reconnect to begin the logon flow again
  121.  
  122.             Console.WriteLine("Disconnected from Steam, reconnecting in 5...");
  123.  
  124.             Thread.Sleep(TimeSpan.FromSeconds(5));
  125.  
  126.             steamClient.Connect();
  127.         }
  128.  
  129.         static void OnLoggedOn(SteamUser.LoggedOnCallback callback)
  130.         {
  131.             if (callback.Result == EResult.AccountLogonDenied)
  132.             {
  133.                 Console.WriteLine("This account is SteamGuard protected!");
  134.                 Console.Write("Please enter the auth code sent to the email at {0}: ", callback.EmailDomain);
  135.  
  136.                 authCode = Console.ReadLine();
  137.                 return;
  138.             }
  139.  
  140.             if (callback.Result != EResult.OK)
  141.             {
  142.                 Console.WriteLine("Unable to logon to Steam: {0} / {1}", callback.Result, callback.ExtendedResult);
  143.  
  144.                 isRunning = false;
  145.                 return;
  146.             }
  147.  
  148.             Console.WriteLine("Successfully logged on!");
  149.  
  150.             MyLoginKey = callback.WebAPIUserNonce;
  151.             // at this point, we'd be able to perform actions on Steam
  152.         }
  153.  
  154.         static void OnLoggedOff(SteamUser.LoggedOffCallback callback)
  155.         {
  156.             Console.WriteLine("Logged off of Steam: {0}", callback.Result);
  157.         }
  158.  
  159.         static void OnMachineAuth(SteamUser.UpdateMachineAuthCallback callback, JobID jobID)
  160.         {
  161.             Console.WriteLine("Updating sentryfile...");
  162.  
  163.             byte[] sentryHash = CryptoHelper.SHAHash(callback.Data);
  164.  
  165.             // write out our sentry file
  166.             // ideally we'd want to write to the filename specified in the callback
  167.             // but then this sample would require more code to find the correct sentry file to read during logon
  168.             // for the sake of simplicity, we'll just use "sentry.bin"
  169.             File.WriteAllBytes("sentry.bin", callback.Data);
  170.  
  171.             // inform the steam servers that we're accepting this sentry file
  172.             steamUser.SendMachineAuthResponse(new SteamUser.MachineAuthDetails
  173.             {
  174.                 JobID = jobID,
  175.  
  176.                 FileName = callback.FileName,
  177.  
  178.                 BytesWritten = callback.BytesToWrite,
  179.                 FileSize = callback.Data.Length,
  180.                 Offset = callback.Offset,
  181.  
  182.                 Result = EResult.OK,
  183.                 LastError = 0,
  184.  
  185.                 OneTimePassword = callback.OneTimePassword,
  186.  
  187.                 SentryFileHash = sentryHash,
  188.             });
  189.  
  190.             Console.WriteLine("Done!");
  191.         }
  192.  
  193.         static void OnLoginKey(SteamUser.LoginKeyCallback callback)
  194.         {
  195.             sessionId = Convert.ToBase64String(Encoding.UTF8.GetBytes(callback.UniqueID.ToString()));
  196.  
  197.             while (true)
  198.             {
  199.                 using (dynamic userAuth = WebAPI.GetInterface("ISteamUserAuth"))
  200.                 {
  201.                     // generate an AES session key
  202.                     var sessionKey = CryptoHelper.GenerateRandomBlock(32);
  203.  
  204.                     // rsa encrypt it with the public key for the universe we're on
  205.                     byte[] cryptedSessionKey = null;
  206.                     using (RSACrypto rsa = new RSACrypto(KeyDictionary.GetPublicKey(steamClient.ConnectedUniverse)))
  207.                     {
  208.                         cryptedSessionKey = rsa.Encrypt(sessionKey);
  209.                     }
  210.  
  211.  
  212.                     byte[] loginKey = new byte[20];
  213.                     Array.Copy(Encoding.ASCII.GetBytes(MyLoginKey), loginKey, MyLoginKey.Length);
  214.  
  215.                     // aes encrypt the loginkey with our session key
  216.                     byte[] cryptedLoginKey = CryptoHelper.SymmetricEncrypt(loginKey, sessionKey);
  217.  
  218.                     KeyValue authResult;
  219.  
  220.                     try
  221.                     {
  222.                         authResult = userAuth.AuthenticateUser(
  223.                             steamid: steamClient.SteamID.ConvertToUInt64(),
  224.                             sessionkey: HttpUtility.UrlEncode(cryptedSessionKey),
  225.                             encrypted_loginkey: HttpUtility.UrlEncode(cryptedLoginKey),
  226.                             method: "POST"
  227.                             );
  228.  
  229.                         token = authResult["token"].AsString();
  230.                         isRunning = false;
  231.                         break;
  232.                     }
  233.                     catch (Exception)
  234.                     { }
  235.                 }
  236.             }
  237.         }
  238.  
  239.         static string SteamCommunityDomain = "steamcommunity.com";
  240.  
  241.         static HttpWebResponse Request(string url, string method, NameValueCollection data = null, CookieContainer cookies = null, bool ajax = true)
  242.         {
  243.             HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
  244.  
  245.             request.Method = method;
  246.             request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
  247.             request.Referer = "http://steamcommunity.com/tradeoffer/new/?partner=173790148";
  248.             request.Timeout = 50000; //Timeout after 50 seconds
  249.  
  250.             // Cookies
  251.             request.CookieContainer = cookies ?? new CookieContainer();
  252.  
  253.             // Request data
  254.             if (data != null)
  255.             {
  256.                 string dataString = String.Join("&", Array.ConvertAll(data.AllKeys, key =>
  257.                     String.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(data[key]))
  258.                 ));
  259.  
  260.                 byte[] dataBytes = Encoding.UTF8.GetBytes(dataString);
  261.                 request.ContentLength = dataBytes.Length;
  262.  
  263.                 using (Stream requestStream = request.GetRequestStream())
  264.                 {
  265.                     requestStream.Write(dataBytes, 0, dataBytes.Length);
  266.                 }
  267.             }
  268.  
  269.             // Get the response
  270.             return request.GetResponse() as HttpWebResponse;
  271.         }
  272.  
  273.         static void TradeOffer()
  274.         {
  275.             CookieContainer cookies = new CookieContainer();
  276.  
  277.             cookies.Add(new Cookie("sessionid", sessionId, string.Empty, SteamCommunityDomain));
  278.             cookies.Add(new Cookie("steamLogin", token, string.Empty, SteamCommunityDomain));
  279.  
  280.             NameValueCollection data = new NameValueCollection();
  281.             data.Add("sessionid", sessionId);
  282.             data.Add("partner", "76561198134055876");
  283.             data.Add("tradeoffermessage", "Test nè mày :v");
  284.             data.Add("json_tradeoffer", "{\"newversion\":true,\"version\":2,\"me\":{\"assets\":[],\"currency\":[],\"ready\":false},\"them\":{\"assets\":[{\"appid\":570,\"contextid\":2,\"amount\":1,\"assetid\":2727203350}],\"currency\":[],\"ready\":false}}");
  285.  
  286.             HttpWebResponse response = Request("https://steamcommunity.com/tradeoffer/new/send", "POST", data, cookies);
  287.         }
  288.     }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement