Guest User

Untitled

a guest
Nov 9th, 2017
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SteamKit2;
  7. using System.IO;
  8. using System.Threading;
  9.  
  10. namespace SteamBot
  11. {
  12.     class Program
  13.     {
  14.        
  15.  
  16.        
  17.  
  18.         static string user, pass;
  19.  
  20.         static SteamClient steamClient;
  21.         static CallbackManager manager;
  22.         static SteamUser steamUser;
  23.         static SteamFriends steamFriends;
  24.  
  25.         static bool isRunning = false;
  26.  
  27.         static string authCode;
  28.  
  29.         static void Main(string[] args) // main string creating the console and any required files.
  30.         {
  31.             if (!File.Exists("chat.txt"))
  32.             {
  33.                 File.Create("chat.txt").Close();
  34.                 File.WriteAllText("chat.txt", "abc | 123");
  35.             }
  36.  
  37.             if (!File.Exists("admin.txt"))
  38.             {
  39.                 File.Create("admin.txt").Close();
  40.                 File.WriteAllText("admin.txt", "76561198070576110");
  41.             }
  42.  
  43.  
  44.             Console.Title = "SteamBot Console";
  45.             Console.ForegroundColor = ConsoleColor.White;
  46.             Console.BackgroundColor = ConsoleColor.Blue;
  47.             Console.WriteLine("Broke the ESC / CTRL+C quitting method... Use the 'X' or ALT + F4 -Dev");
  48.             Console.ResetColor();
  49.  
  50.             Console.ForegroundColor = ConsoleColor.Blue;
  51.             Console.Write("Username: ");
  52.             Console.ResetColor();
  53.  
  54.             Console.ForegroundColor = ConsoleColor.White;
  55.             user = Console.ReadLine();
  56.             Console.ResetColor();
  57.  
  58.             Console.ForegroundColor = ConsoleColor.Blue;
  59.             Console.Write("Password: ");
  60.             Console.ResetColor();
  61.  
  62.             Console.ForegroundColor = ConsoleColor.White;
  63.             pass = Console.ReadLine();
  64.             Console.ResetColor();
  65.             SteamLogIn();
  66.         }
  67.  
  68.         static void SteamLogIn() //originates the managers + is the logging in func.
  69.         {
  70.             steamClient = new SteamClient();
  71.  
  72.             manager = new CallbackManager(steamClient);
  73.  
  74.             steamUser = steamClient.GetHandler<SteamUser>();
  75.  
  76.             steamFriends = steamClient.GetHandler<SteamFriends>();
  77.  
  78.             new Callback<SteamClient.ConnectedCallback>(OnConnected);
  79.             new Callback<SteamClient.DisconnectedCallback>(OnDisconnected, manager);
  80.  
  81.             new Callback<SteamUser.LoggedOnCallback>(OnLoggedOn, manager);
  82.             new Callback<SteamUser.LoggedOffCallback>(OnLoggedOff, manager);
  83.  
  84.             new Callback<SteamUser.AccountInfoCallback>(OnAccountInfo, manager);
  85.             new Callback<SteamFriends.FriendMsgCallback>(OnChatMessage, manager);
  86.  
  87.             new Callback<SteamFriends.FriendsListCallback>(OnFriendsList, manager);
  88.  
  89.             new Callback<SteamUser.UpdateMachineAuthCallback>(OnMachineAuth, manager);
  90.  
  91.             isRunning = true;
  92.  
  93.             Console.ForegroundColor = ConsoleColor.Blue;
  94.             Console.WriteLine("Connecting to Steam...");
  95.             Console.ResetColor();
  96.  
  97.             steamClient.Connect();
  98.  
  99.  
  100.             while (isRunning)
  101.             {
  102.                 manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
  103.             }
  104.             Console.ReadKey();
  105.         }
  106.  
  107.         static void OnConnected(SteamClient.ConnectedCallback callback) //Handles the connection, if it was successful or it was refused etc.
  108.         {
  109.             if (callback.Result != EResult.OK)
  110.             {
  111.                 Console.ForegroundColor = ConsoleColor.Red;
  112.                 Console.WriteLine("Unable to connect to steam: {0}", callback.Result);
  113.                 Console.ResetColor();
  114.  
  115.                 isRunning = false;
  116.                 return;
  117.             }
  118.             Console.ForegroundColor = ConsoleColor.Blue;
  119.             Console.WriteLine("Connected to Steam. \nLogging in {0}... \n", user);
  120.             Console.ResetColor();
  121.  
  122.             byte[] sentryHash = null;
  123.  
  124.             if (File.Exists("sentry.bin"))
  125.             {
  126.                 byte[] sentryFile = File.ReadAllBytes("sentry.bin");
  127.  
  128.                 sentryHash = CryptoHelper.SHAHash(sentryFile);
  129.             }
  130.  
  131.             steamUser.LogOn(new SteamUser.LogOnDetails
  132.             {
  133.                 Username = user,
  134.                 Password = pass,
  135.  
  136.                 AuthCode = authCode,
  137.  
  138.                 SentryFileHash = sentryHash,
  139.             });
  140.         }
  141.  
  142.         static void OnLoggedOn(SteamUser.LoggedOnCallback callback) //handles the steamguard email part- and it checks callbacks to give the user the go ahead signal of being logged in.
  143.         {
  144.             if (callback.Result != EResult.AccountLogonDenied)
  145.             {
  146.                 Console.ForegroundColor = ConsoleColor.Yellow;
  147.                 Console.WriteLine("Account is SteamGuard protected.");
  148.                 Console.ResetColor();
  149.  
  150.                 Console.ForegroundColor = ConsoleColor.Blue;
  151.                 Console.Write("Please enter the authentication code sent to the connected email {0}: ", callback.EmailDomain);
  152.                 Console.ResetColor();
  153.  
  154.                 Console.ForegroundColor = ConsoleColor.White;
  155.                 authCode = Console.ReadLine();
  156.                 Console.ResetColor();
  157.                 return;
  158.             }
  159.  
  160.             if (callback.Result != EResult.OK)
  161.             {
  162.                 Console.ForegroundColor = ConsoleColor.Red;
  163.                 Console.WriteLine("Unable to log in to Steam: {0}\n", callback.Result);
  164.                 Console.ResetColor();
  165.  
  166.                 isRunning = false;
  167.                 return;
  168.             }
  169.             Console.ForegroundColor = ConsoleColor.Blue;
  170.             Console.WriteLine("{0} succesfully logged in.", user);
  171.             Console.ResetColor();
  172.         }
  173.  
  174.         static void OnMachineAuth(SteamUser.UpdateMachineAuthCallback callback) //handles the sentryHash file and subsequently updates sentry.bin
  175.         {
  176.             Console.ForegroundColor = ConsoleColor.Yellow;
  177.             Console.WriteLine("Updating sentry file...");
  178.             Console.ResetColor();
  179.  
  180.             byte[] sentryHash = CryptoHelper.SHAHash(callback.Data);
  181.  
  182.             File.WriteAllBytes("sentry.bin", callback.Data);
  183.  
  184.             steamUser.SendMachineAuthResponse(new SteamUser.MachineAuthDetails
  185.             {
  186.                 JobID = callback.JobID,
  187.                 FileName = callback.FileName,
  188.                 BytesWritten = callback.BytesToWrite,
  189.                 FileSize = callback.Data.Length,
  190.                 Offset = callback.Offset,
  191.                 Result = EResult.OK,
  192.                 LastError = 0,
  193.                 OneTimePassword = callback.OneTimePassword,
  194.                 SentryFileHash = sentryHash,
  195.             });
  196.  
  197.             Console.ForegroundColor = ConsoleColor.Blue;
  198.             Console.WriteLine("Done.");
  199.             Console.ResetColor();
  200.         }
  201.  
  202.         static void OnDisconnected(SteamClient.DisconnectedCallback callback) //handles reconnecting to steam once steam disconnects and reconnnects to check the bin or loses connection.
  203.         {
  204.             Console.ForegroundColor = ConsoleColor.Yellow;
  205.             Console.WriteLine("\n {0} disconnected from Steam, reconnecting in 3 sec...\n", user);
  206.             Console.ResetColor();
  207.  
  208.             Thread.Sleep(TimeSpan.FromSeconds(3));
  209.  
  210.             steamClient.Connect();
  211.         }
  212.  
  213.         static void OnLoggedOff(SteamUser.LoggedOffCallback callback) //informs the user they logged the bot off using simple callbacks
  214.         {
  215.             Console.ForegroundColor = ConsoleColor.Red;
  216.             Console.WriteLine("Logged off from Steam: {0}", callback.Result);
  217.             Console.ResetColor();
  218.         }
  219.  
  220.         static void OnAccountInfo(SteamUser.AccountInfoCallback callback) //handles setting the steam status to 'online' in the friends submenu
  221.         {
  222.             steamFriends.SetPersonaState(EPersonaState.Online);
  223.  
  224.         }
  225.  
  226.         static void OnFriendsList(SteamFriends.FriendsListCallback callback) //Automatically adds someon if they send a request; also sends a standard greeting, I.E "Hello, I'm a bot."
  227.         {
  228.             Thread.Sleep(TimeSpan.FromSeconds(3));
  229.  
  230.             foreach (var friend in callback.FriendList)
  231.             {
  232.                 if (friend.Relationship == EFriendRelationship.RequestRecipient)
  233.                 {
  234.                     steamFriends.AddFriend(friend.SteamID);
  235.                     Thread.Sleep(500);
  236.                     steamFriends.SendChatMessage(friend.SteamID, EChatEntryType.ChatMsg, "Hello, I'm a bot.");
  237.                 }
  238.             }
  239.         }
  240.  
  241.         static void OnChatMessage(SteamFriends.FriendMsgCallback callback)
  242.         {
  243.             string[] args;
  244.  
  245.             if (callback.EntryType == EChatEntryType.ChatMsg)
  246.             {
  247.                 if (callback.Message.Length > 1)
  248.                 {
  249.                     if (callback.Message.Remove(1) == "!")
  250.                     {
  251.                         string command = callback.Message;
  252.                         if (callback.Message.Contains(" "))
  253.                         {
  254.                             command = callback.Message.Remove(callback.Message.IndexOf(' '));
  255.                         }
  256.  
  257.                         switch (command)
  258.                         {
  259.                             //ALL COMMAND REGIONS; ADD NEW COMMANDS IN SEPERATE REGIONS HERE. for organization mostly, make it look pretty.
  260.                             #region send
  261.                             case "!send":
  262.                                 if (!IsBotAdmin(callback.Sender))
  263.                                     break;
  264.  
  265.  
  266.                                 args = seperate(2, ' ', callback.Message);
  267.                                 Console.ForegroundColor = ConsoleColor.Yellow;
  268.                                 Console.WriteLine("!send " + args[1] + args[2] + " command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender)); // writes to the console that the bot has recived a command
  269.                                 Console.ResetColor();
  270.                                 if (args[0] == "-1")
  271.                                 {
  272.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !send [friend] [message]");
  273.                                     return;
  274.                                 }
  275.                                 for (int i = 0; i < steamFriends.GetFriendCount(); i++)
  276.                                 {
  277.                                     SteamID friend = steamFriends.GetFriendByIndex(i);
  278.                                     if (steamFriends.GetFriendPersonaName(friend).ToLower().Contains(args[1].ToLower()))
  279.                                     {
  280.                                         steamFriends.SendChatMessage(friend, EChatEntryType.ChatMsg, args[2]);
  281.                                     }
  282.                                 }
  283.                                 break;
  284.                             #endregion
  285.                             //for the !send command.
  286.                             #region friends
  287.                             case "!friends":
  288.                                 Console.ForegroundColor = ConsoleColor.Yellow;
  289.                                 Console.WriteLine("!friends command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  290.                                 Console.ResetColor();
  291.                                 for (int i = 0; i < steamFriends.GetFriendCount(); i++)
  292.                                 {
  293.                                     SteamID friend = steamFriends.GetFriendByIndex(i);
  294.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Friend: " + steamFriends.GetFriendPersonaName(friend) + "  State:  " + steamFriends.GetFriendPersonaState(friend));
  295.                                 }
  296.                                 break;
  297.                             #endregion
  298.                             //lists all the bots friends, aswell as status.
  299.                             #region friend
  300.  
  301.                             case "!friend":
  302.  
  303.                                 args = seperate(1, ' ', callback.Message);
  304.                                 Console.ForegroundColor = ConsoleColor.Yellow;
  305.                                 Console.WriteLine("!friend " + args[1] + " | " + steamFriends.GetFriendPersonaName(Convert.ToUInt64(args[1])) + " command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  306.                                 Console.ResetColor();
  307.  
  308.                                 if (!IsBotAdmin(callback.Sender))
  309.                                     return;
  310.  
  311.                                 if (args[0] == "-1")
  312.                                 {
  313.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !friend [Steam ID]");
  314.                                     return;
  315.                                 }
  316.  
  317.                                 try
  318.                                 {
  319.                                     SteamID validSID = Convert.ToUInt64(args[1]);
  320.                                     if (!validSID.IsValid)
  321.                                     {
  322.                                         steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Invalid SteamID");
  323.                                         break;
  324.                                     }
  325.                                     steamFriends.AddFriend(validSID);
  326.                                 }
  327.                                 catch (FormatException)
  328.                                 {
  329.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Invalid SteamID64.");
  330.                                 }
  331.                                 break;
  332.                             #endregion
  333.                             //the !friend command which is used to get the bot to add a specific SID / steam64id
  334.                             #region changename
  335.                             case "!changename":
  336.  
  337.                                 args = seperate(1, ' ', callback.Message);
  338.                                 Console.ForegroundColor = ConsoleColor.Yellow;
  339.                                 Console.WriteLine("!changename " + args[1] + " | " + steamFriends.GetFriendPersonaName(Convert.ToUInt64(args[1])) + " command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  340.                                 Console.ResetColor();
  341.  
  342.                                 if (!IsBotAdmin(callback.Sender)) // simple check to see if the user who initiated the command is listed as an admin in "admin.txt"
  343.                                     return;
  344.  
  345.                                 if (args[0] == "-1")
  346.                                 {
  347.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !change [name]"); // appears if the !change command fails, it'll loop back into a false state and spew out the syntax for the command.
  348.                                     return;
  349.                                 }
  350.  
  351.                                 steamFriends.SetPersonaName(args[1]);
  352.                                 break;
  353.                                 #endregion
  354.                             // simple command to change the bots name instead of having to log in everytime, because that shit's annoying.
  355.                         }
  356.                     }
  357.                 }
  358.                 string rLine;
  359.                 string trimmed = callback.Message;
  360.  
  361.                 char[] trim = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\\', '|', ';', ':', '"', '\'', ',', '<', '.', '>', '/', '?' };
  362.  
  363.                 for (int i = 0; i < 30; i++)
  364.                 {
  365.                     trimmed = trimmed.Replace(trim[i].ToString(), "");
  366.                 }
  367.  
  368.                 StreamReader sReader = new StreamReader("chat.txt"); // reads the chat.txt, scanning one line every runthrough of a cycle as it looks for a command and or an exception to catch.
  369.  
  370.                 while ((rLine = sReader.ReadLine()) != null)
  371.                 {
  372.                     string text = rLine.Remove(rLine.IndexOf('|') - 1);
  373.                     string response = rLine.Remove(0, rLine.IndexOf('|') + 2);
  374.  
  375.                     if (callback.Message.Contains(text))
  376.                     {
  377.                         steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, response);
  378.                         sReader.Close(); // remember to always close the StreamReader like a good person you twat.
  379.                         return;
  380.                     }
  381.                 }
  382.  
  383.             }
  384.         }
  385.  
  386.         public static bool IsBotAdmin(SteamID sid) // checks to see if the users SID / steam64id matchs those found in admin.txt, if not it returns false, updating the console and catching all errors by returning them false if someone tries to exploit the admin func.
  387.         {
  388.             try
  389.             {
  390.                 if (sid.ConvertToUInt64() == Convert.ToUInt64(File.ReadAllText("admin.txt")))
  391.                 {
  392.                     return true;
  393.                 }
  394.  
  395.                 steamFriends.SendChatMessage(sid, EChatEntryType.ChatMsg, "You are not a bot admin."); // returns to the user stating no admin privledges were found for said user, also posts to the bot console window that a user attempted an admin command.
  396.                 Console.ForegroundColor = ConsoleColor.Red;
  397.                 Console.WriteLine(steamFriends.GetFriendPersonaName(sid) + " attempted to use an administrator command without privledges.");
  398.                 Console.ResetColor();
  399.                 return false;
  400.             }
  401.             catch (Exception e)
  402.             {
  403.                 Console.ForegroundColor = ConsoleColor.Red;
  404.                 Console.WriteLine("ERROR! - Please fix this ;(");
  405.                 Console.WriteLine(e.Message);
  406.                 Console.ResetColor();
  407.                 return false;
  408.             }
  409.         }
  410.  
  411.         public static string[] seperate(int number, char seperator, string thestring) // variables determining the number of seperators and strings / commands and arguments sent to the bot, I think. Don't qoute me -Dev
  412.         {
  413.             string[] returned = new string[4];
  414.  
  415.             int i = 0;
  416.  
  417.             int error = 0;
  418.  
  419.             int lenght = thestring.Length;
  420.  
  421.             foreach (char c in thestring)
  422.             {
  423.                 if (i != number)
  424.                 {
  425.                     if (error > lenght || number > 5)
  426.                     {
  427.                         returned[0] = "-1";
  428.                         return returned;
  429.                     }
  430.                     else if (c == seperator)
  431.                     {
  432.                         returned[i] = thestring.Remove(thestring.IndexOf(c));
  433.                         thestring = thestring.Remove(0, thestring.IndexOf(c) + 1);
  434.                         i++;
  435.                     }
  436.                     error++;
  437.  
  438.                     if (error > lenght && i != number)
  439.                     {
  440.                         returned[0] = "-1";
  441.                         return returned;
  442.                     }
  443.                 }
  444.                 else
  445.                 {
  446.                     returned[i] = thestring;
  447.                 }
  448.             }
  449.             return returned;
  450.         }
  451.     }
  452. }
Add Comment
Please, Sign In to add comment