Advertisement
Guest User

MySpace IM Protocol C# .NET

a guest
Oct 13th, 2010
1,127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. /*****
  2.  *
  3.  * VERY ROUGH IMPLEMENTATION OF MSIM IN C#.NET
  4.  *  
  5.  * BY: JOWY :P
  6.  *
  7.  */
  8.  
  9. MSIM oMSIM = new MSIM(username, password, interface_address);
  10. response = oMSIM.Connect();
  11. oMSIM.ReadPacket();
  12.  
  13. kv_resp = response.Split(new Char[] { '\\' });
  14.  
  15. oMSIM.AddPacketVar("login2", "196610");
  16. oMSIM.AddPacketVar("username", username);
  17. oMSIM.AddPacketVar("response", oMSIM.ProcessChallenge(kv_resp[4]));
  18. oMSIM.AddPacketVar("clientver", "823");
  19. oMSIM.AddPacketVar("reconn", "0");
  20. oMSIM.AddPacketVar("status", "100");
  21. oMSIM.AddPacketVar("id", "1");
  22.  
  23. response = oMSIM.SendPacket();
  24.  
  25. oMSIM.FlushPacket();
  26.  
  27. kv_resp = response.Split(new Char[] { '\\' });
  28.  
  29. if (kv_resp[3] == "sesskey") {
  30.     //LOGGED IN!
  31. }
  32.  
  33. public class MSIM
  34. {
  35.     TcpClient oTcp;
  36.     Encoding encUTF16LE;
  37.     SHA1 oSha;
  38.     StringBuilder sbPacket;
  39.  
  40.     string username;
  41.     string password;
  42.     string interface_address;
  43.  
  44.     public MSIM(string z_username, string z_password, string z_interface_address)
  45.     {
  46.         username = z_username;
  47.         password = z_password;
  48.         interface_address = z_interface_address;
  49.  
  50.         if (interface_address == "" || interface_address == null)
  51.         {
  52.             oTcp = new TcpClient();
  53.         }
  54.         else
  55.         {
  56.             oTcp = new TcpClient(new IPEndPoint(IPAddress.Parse(interface_address), 0));
  57.         }
  58.  
  59.         sbPacket = new StringBuilder();
  60.         encUTF16LE = Encoding.GetEncoding("UTF-16LE");
  61.         oSha = new SHA1CryptoServiceProvider();
  62.     }
  63.  
  64.     public string Connect()
  65.     {
  66.         try
  67.         {
  68.             oTcp.Connect("im.myspace.akadns.net", 1863);
  69.         }
  70.         catch
  71.         {
  72.             return null;
  73.         }
  74.  
  75.         return ReadPacket();
  76.     }
  77.  
  78.     public void Close()
  79.     {
  80.         oTcp.Close();
  81.     }
  82.  
  83.     public string ReadPacket()
  84.     {
  85.         byte[] byte_initial = new byte[4096];
  86.  
  87.         try
  88.         {
  89.             Stream oStream = oTcp.GetStream();
  90.  
  91.             oStream.ReadTimeout = 2000;
  92.  
  93.             oStream.Read(byte_initial, 0, 4096);
  94.  
  95.             oStream.Flush();
  96.         }
  97.         catch
  98.         {
  99.             return null;
  100.         }
  101.  
  102.         return Encoding.ASCII.GetString(byte_initial).Replace("\0", "");
  103.     }
  104.  
  105.     public string SendPacket()
  106.     {
  107.         try
  108.         {
  109.             Stream oStream = oTcp.GetStream();
  110.            
  111.             byte[] byte_data = Encoding.ASCII.GetBytes((sbPacket.ToString() + "\\final\\").ToCharArray());
  112.  
  113.             oStream.Write(byte_data, 0, byte_data.Length);
  114.         }
  115.         catch
  116.         {
  117.             return null;
  118.         }
  119.  
  120.         return ReadPacket();
  121.     }
  122.  
  123.     public void AddPacketVar(string key, bool value)
  124.     {
  125.         if (value)
  126.         {
  127.             sbPacket.Append("\\" + key + "\\1");
  128.         }
  129.         else
  130.         {
  131.             sbPacket.Append("\\" + key + "\\\\");
  132.         }
  133.     }
  134.  
  135.     public void AddPacketVar(string key, int value)
  136.     {
  137.         sbPacket.Append("\\" + key + "\\" + value.ToString());
  138.     }
  139.  
  140.     public void AddPacketVar(string key, string value)
  141.     {
  142.         sbPacket.Append("\\" + key + "\\" + value);
  143.     }
  144.  
  145.     public void AddPacketVar(string key, byte[] value)
  146.     {
  147.         sbPacket.Append("\\" + key + "\\" + Convert.ToBase64String(value));
  148.     }
  149.  
  150.     public void FlushPacket()
  151.     {
  152.         sbPacket.Clear();
  153.     }
  154.  
  155.     public byte[] ProcessChallenge(string challenge)
  156.     {
  157.         byte[] byte_nc1 = new byte[32];
  158.         byte[] byte_nc2 = new byte[32];
  159.         byte[] byte_rc4_key = new byte[16];
  160.  
  161.         try
  162.         {
  163.             byte[] byte_challenge = Convert.FromBase64String(challenge);
  164.  
  165.             for (int i = 0; i < 32; i++)
  166.                 byte_nc1[i] = byte_challenge[i];
  167.  
  168.             for (int i = 0; i < 32; i++)
  169.                 byte_nc2[i] = byte_challenge[i + 32];
  170.         }
  171.         catch
  172.         {
  173.             return null;
  174.         }
  175.  
  176.         byte[] byte_password = encUTF16LE.GetBytes(password.ToCharArray());
  177.  
  178.         byte[] byte_hash_phase1 = oSha.ComputeHash(byte_password);
  179.  
  180.         byte[] byte_hash_phase2 = new byte[byte_hash_phase1.Length + byte_nc2.Length];
  181.  
  182.         byte_hash_phase1.CopyTo(byte_hash_phase2, 0);
  183.  
  184.         byte_nc2.CopyTo(byte_hash_phase2, byte_hash_phase1.Length);
  185.  
  186.         byte[] byte_hash_total = oSha.ComputeHash(byte_hash_phase2);
  187.  
  188.         for (int i = 0; i < 16; i++)
  189.             byte_rc4_key[i] = byte_hash_total[i];
  190.  
  191.         byte[] byte_username = Encoding.ASCII.GetBytes(username.ToCharArray());
  192.  
  193.         byte[] byte_four_null = new byte[4];
  194.  
  195.         byte[] byte_network_count = new byte[1];
  196.  
  197.         byte_network_count[0] = 1;
  198.  
  199.         byte[] byte_interface_ip = new byte[4];
  200.  
  201.         byte_interface_ip[0] = 127;
  202.         byte_interface_ip[1] = 0;
  203.         byte_interface_ip[2] = 0;
  204.         byte_interface_ip[3] = 1;
  205.  
  206.         int l1 = byte_nc1.Length;
  207.         int l2 = byte_username.Length;
  208.         int l3 = byte_four_null.Length;
  209.         int l4 = byte_network_count.Length;
  210.         int l5 = byte_interface_ip.Length;
  211.  
  212.         byte[] byte_rc4_data = new byte[l1 + l2 + l3 + l4 + l5 + l5];
  213.  
  214.         byte_nc1.CopyTo(byte_rc4_data, 0);
  215.         byte_username.CopyTo(byte_rc4_data, l1);
  216.         byte_four_null.CopyTo(byte_rc4_data, l1 + l2);
  217.         byte_network_count.CopyTo(byte_rc4_data, l1 + l2 + l3);
  218.         byte_interface_ip.CopyTo(byte_rc4_data, l1 + l2 + l3 + l4);
  219.         byte_interface_ip.CopyTo(byte_rc4_data, l1 + l2 + l3 + l4 + l5);
  220.  
  221.         return RC4.Encrypt(byte_rc4_key, byte_rc4_data);
  222.     }
  223. }
  224.  
  225. public static class RC4
  226. {
  227.     public static byte[] Encrypt(byte[] key, byte[] data)
  228.     {
  229.         return EncryptOutput(key, data).ToArray();
  230.     }
  231.  
  232.     private static byte[] EncryptInitalize(byte[] key)
  233.     {
  234.         byte[] s = Enumerable.Range(0, 256)
  235.            .Select(i => (byte)i)
  236.            .ToArray();
  237.  
  238.         for (int i = 0, j = 0; i < 256; i++)
  239.         {
  240.             j = (j + key[i % key.Length] + s[i]) & 255;
  241.  
  242.             Swap(s, i, j);
  243.         }
  244.  
  245.         return s;
  246.     }
  247.  
  248.     private static IEnumerable<byte> EncryptOutput(byte[] key, IEnumerable<byte> data)
  249.     {
  250.         byte[] s = EncryptInitalize(key);
  251.  
  252.         int i = 0;
  253.         int j = 0;
  254.  
  255.         return data.Select((b) =>
  256.         {
  257.             i = (i + 1) & 255;
  258.             j = (j + s[i]) & 255;
  259.  
  260.             Swap(s, i, j);
  261.  
  262.             return (byte)(b ^ s[(s[i] + s[j]) & 255]);
  263.         });
  264.     }
  265.  
  266.     private static void Swap(byte[] s, int i, int j)
  267.     {
  268.         byte c = s[i];
  269.  
  270.         s[i] = s[j];
  271.         s[j] = c;
  272.     }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement