Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Threading;
- using System.Web;
- using System.Net;
- using SteamKit2;
- namespace TestCS
- {
- class Program
- {
- static SteamClient steamClient;
- static CallbackManager manager;
- static SteamUser steamUser;
- static bool isRunning;
- static string user, pass;
- static string authCode;
- static string sessionId;
- static string MyLoginKey;
- static string token;
- static void Main(string[] args)
- {
- if (args.Length < 2)
- {
- Console.WriteLine("Sample5: No username and password specified!");
- return;
- }
- // save our logon details
- user = args[0];
- pass = args[1];
- // create our steamclient instance
- steamClient = new SteamClient();
- // create the callback manager which will route callbacks to function calls
- manager = new CallbackManager(steamClient);
- // get the steamuser handler, which is used for logging on after successfully connecting
- steamUser = steamClient.GetHandler<SteamUser>();
- // register a few callbacks we're interested in
- // these are registered upon creation to a callback manager, which will then route the callbacks
- // to the functions specified
- new Callback<SteamClient.ConnectedCallback>(OnConnected, manager);
- new Callback<SteamClient.DisconnectedCallback>(OnDisconnected, manager);
- new Callback<SteamUser.LoggedOnCallback>(OnLoggedOn, manager);
- new Callback<SteamUser.LoggedOffCallback>(OnLoggedOff, manager);
- // this callback is triggered when the steam servers wish for the client to store the sentry file
- new JobCallback<SteamUser.UpdateMachineAuthCallback>(OnMachineAuth, manager);
- new Callback<SteamUser.LoginKeyCallback>(OnLoginKey, manager);
- isRunning = true;
- Console.WriteLine("Connecting to Steam...");
- // initiate the connection
- steamClient.Connect();
- // create our callback handling loop
- while (isRunning)
- {
- // in order for the callbacks to get routed, they need to be handled by the manager
- manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
- }
- TradeOffer();
- }
- static void OnConnected(SteamClient.ConnectedCallback callback)
- {
- if (callback.Result != EResult.OK)
- {
- Console.WriteLine("Unable to connect to Steam: {0}", callback.Result);
- isRunning = false;
- return;
- }
- Console.WriteLine("Connected to Steam! Logging in '{0}'...", user);
- byte[] sentryHash = null;
- if (File.Exists("sentry.bin"))
- {
- // if we have a saved sentry file, read and sha-1 hash it
- byte[] sentryFile = File.ReadAllBytes("sentry.bin");
- sentryHash = CryptoHelper.SHAHash(sentryFile);
- }
- steamUser.LogOn(new SteamUser.LogOnDetails
- {
- Username = user,
- Password = pass,
- // in this sample, we pass in an additional authcode
- // this value will be null (which is the default) for our first logon attempt
- AuthCode = authCode,
- // our subsequent logons use the hash of the sentry file as proof of ownership of the file
- // this will also be null for our first (no authcode) and second (authcode only) logon attempts
- SentryFileHash = sentryHash,
- });
- }
- static void OnDisconnected(SteamClient.DisconnectedCallback callback)
- {
- // after recieving an AccountLogonDenied, we'll be disconnected from steam
- // so after we read an authcode from the user, we need to reconnect to begin the logon flow again
- Console.WriteLine("Disconnected from Steam, reconnecting in 5...");
- Thread.Sleep(TimeSpan.FromSeconds(5));
- steamClient.Connect();
- }
- static void OnLoggedOn(SteamUser.LoggedOnCallback callback)
- {
- if (callback.Result == EResult.AccountLogonDenied)
- {
- Console.WriteLine("This account is SteamGuard protected!");
- Console.Write("Please enter the auth code sent to the email at {0}: ", callback.EmailDomain);
- authCode = Console.ReadLine();
- return;
- }
- if (callback.Result != EResult.OK)
- {
- Console.WriteLine("Unable to logon to Steam: {0} / {1}", callback.Result, callback.ExtendedResult);
- isRunning = false;
- return;
- }
- Console.WriteLine("Successfully logged on!");
- MyLoginKey = callback.WebAPIUserNonce;
- // at this point, we'd be able to perform actions on Steam
- }
- static void OnLoggedOff(SteamUser.LoggedOffCallback callback)
- {
- Console.WriteLine("Logged off of Steam: {0}", callback.Result);
- }
- static void OnMachineAuth(SteamUser.UpdateMachineAuthCallback callback, JobID jobID)
- {
- Console.WriteLine("Updating sentryfile...");
- byte[] sentryHash = CryptoHelper.SHAHash(callback.Data);
- // write out our sentry file
- // ideally we'd want to write to the filename specified in the callback
- // but then this sample would require more code to find the correct sentry file to read during logon
- // for the sake of simplicity, we'll just use "sentry.bin"
- File.WriteAllBytes("sentry.bin", callback.Data);
- // inform the steam servers that we're accepting this sentry file
- steamUser.SendMachineAuthResponse(new SteamUser.MachineAuthDetails
- {
- JobID = jobID,
- FileName = callback.FileName,
- BytesWritten = callback.BytesToWrite,
- FileSize = callback.Data.Length,
- Offset = callback.Offset,
- Result = EResult.OK,
- LastError = 0,
- OneTimePassword = callback.OneTimePassword,
- SentryFileHash = sentryHash,
- });
- Console.WriteLine("Done!");
- }
- static void OnLoginKey(SteamUser.LoginKeyCallback callback)
- {
- sessionId = Convert.ToBase64String(Encoding.UTF8.GetBytes(callback.UniqueID.ToString()));
- while (true)
- {
- using (dynamic userAuth = WebAPI.GetInterface("ISteamUserAuth"))
- {
- // generate an AES session key
- var sessionKey = CryptoHelper.GenerateRandomBlock(32);
- // rsa encrypt it with the public key for the universe we're on
- byte[] cryptedSessionKey = null;
- using (RSACrypto rsa = new RSACrypto(KeyDictionary.GetPublicKey(steamClient.ConnectedUniverse)))
- {
- cryptedSessionKey = rsa.Encrypt(sessionKey);
- }
- byte[] loginKey = new byte[20];
- Array.Copy(Encoding.ASCII.GetBytes(MyLoginKey), loginKey, MyLoginKey.Length);
- // aes encrypt the loginkey with our session key
- byte[] cryptedLoginKey = CryptoHelper.SymmetricEncrypt(loginKey, sessionKey);
- KeyValue authResult;
- try
- {
- authResult = userAuth.AuthenticateUser(
- steamid: steamClient.SteamID.ConvertToUInt64(),
- sessionkey: HttpUtility.UrlEncode(cryptedSessionKey),
- encrypted_loginkey: HttpUtility.UrlEncode(cryptedLoginKey),
- method: "POST"
- );
- token = authResult["token"].AsString();
- isRunning = false;
- break;
- }
- catch (Exception)
- { }
- }
- }
- }
- static string SteamCommunityDomain = "steamcommunity.com";
- static HttpWebResponse Request(string url, string method, NameValueCollection data = null, CookieContainer cookies = null, bool ajax = true)
- {
- HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
- request.Method = method;
- request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
- request.Referer = "http://steamcommunity.com/tradeoffer/new/?partner=173790148";
- request.Timeout = 50000; //Timeout after 50 seconds
- // Cookies
- request.CookieContainer = cookies ?? new CookieContainer();
- // Request data
- if (data != null)
- {
- string dataString = String.Join("&", Array.ConvertAll(data.AllKeys, key =>
- String.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(data[key]))
- ));
- byte[] dataBytes = Encoding.UTF8.GetBytes(dataString);
- request.ContentLength = dataBytes.Length;
- using (Stream requestStream = request.GetRequestStream())
- {
- requestStream.Write(dataBytes, 0, dataBytes.Length);
- }
- }
- // Get the response
- return request.GetResponse() as HttpWebResponse;
- }
- static void TradeOffer()
- {
- CookieContainer cookies = new CookieContainer();
- cookies.Add(new Cookie("sessionid", sessionId, string.Empty, SteamCommunityDomain));
- cookies.Add(new Cookie("steamLogin", token, string.Empty, SteamCommunityDomain));
- NameValueCollection data = new NameValueCollection();
- data.Add("sessionid", sessionId);
- data.Add("partner", "76561198134055876");
- data.Add("tradeoffermessage", "Test nè mày :v");
- 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}}");
- HttpWebResponse response = Request("https://steamcommunity.com/tradeoffer/new/send", "POST", data, cookies);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement