/*****
*
* VERY ROUGH IMPLEMENTATION OF MSIM IN C#.NET
*
* BY: JOWY :P
*
*/
MSIM oMSIM = new MSIM(username, password, interface_address);
response = oMSIM.Connect();
oMSIM.ReadPacket();
kv_resp = response.Split(new Char[] { '\\' });
oMSIM.AddPacketVar("login2", "196610");
oMSIM.AddPacketVar("username", username);
oMSIM.AddPacketVar("response", oMSIM.ProcessChallenge(kv_resp[4]));
oMSIM.AddPacketVar("clientver", "823");
oMSIM.AddPacketVar("reconn", "0");
oMSIM.AddPacketVar("status", "100");
oMSIM.AddPacketVar("id", "1");
response = oMSIM.SendPacket();
oMSIM.FlushPacket();
kv_resp = response.Split(new Char[] { '\\' });
if (kv_resp[3] == "sesskey") {
//LOGGED IN!
}
public class MSIM
{
TcpClient oTcp;
Encoding encUTF16LE;
SHA1 oSha;
StringBuilder sbPacket;
string username;
string password;
string interface_address;
public MSIM(string z_username, string z_password, string z_interface_address)
{
username = z_username;
password = z_password;
interface_address = z_interface_address;
if (interface_address == "" || interface_address == null)
{
oTcp = new TcpClient();
}
else
{
oTcp = new TcpClient(new IPEndPoint(IPAddress.Parse(interface_address), 0));
}
sbPacket = new StringBuilder();
encUTF16LE = Encoding.GetEncoding("UTF-16LE");
oSha = new SHA1CryptoServiceProvider();
}
public string Connect()
{
try
{
oTcp.Connect("im.myspace.akadns.net", 1863);
}
catch
{
return null;
}
return ReadPacket();
}
public void Close()
{
oTcp.Close();
}
public string ReadPacket()
{
byte[] byte_initial = new byte[4096];
try
{
Stream oStream = oTcp.GetStream();
oStream.ReadTimeout = 2000;
oStream.Read(byte_initial, 0, 4096);
oStream.Flush();
}
catch
{
return null;
}
return Encoding.ASCII.GetString(byte_initial).Replace("\0", "");
}
public string SendPacket()
{
try
{
Stream oStream = oTcp.GetStream();
byte[] byte_data = Encoding.ASCII.GetBytes((sbPacket.ToString() + "\\final\\").ToCharArray());
oStream.Write(byte_data, 0, byte_data.Length);
}
catch
{
return null;
}
return ReadPacket();
}
public void AddPacketVar(string key, bool value)
{
if (value)
{
sbPacket.Append("\\" + key + "\\1");
}
else
{
sbPacket.Append("\\" + key + "\\\\");
}
}
public void AddPacketVar(string key, int value)
{
sbPacket.Append("\\" + key + "\\" + value.ToString());
}
public void AddPacketVar(string key, string value)
{
sbPacket.Append("\\" + key + "\\" + value);
}
public void AddPacketVar(string key, byte[] value)
{
sbPacket.Append("\\" + key + "\\" + Convert.ToBase64String(value));
}
public void FlushPacket()
{
sbPacket.Clear();
}
public byte[] ProcessChallenge(string challenge)
{
byte[] byte_nc1 = new byte[32];
byte[] byte_nc2 = new byte[32];
byte[] byte_rc4_key = new byte[16];
try
{
byte[] byte_challenge = Convert.FromBase64String(challenge);
for (int i = 0; i < 32; i++)
byte_nc1[i] = byte_challenge[i];
for (int i = 0; i < 32; i++)
byte_nc2[i] = byte_challenge[i + 32];
}
catch
{
return null;
}
byte[] byte_password = encUTF16LE.GetBytes(password.ToCharArray());
byte[] byte_hash_phase1 = oSha.ComputeHash(byte_password);
byte[] byte_hash_phase2 = new byte[byte_hash_phase1.Length + byte_nc2.Length];
byte_hash_phase1.CopyTo(byte_hash_phase2, 0);
byte_nc2.CopyTo(byte_hash_phase2, byte_hash_phase1.Length);
byte[] byte_hash_total = oSha.ComputeHash(byte_hash_phase2);
for (int i = 0; i < 16; i++)
byte_rc4_key[i] = byte_hash_total[i];
byte[] byte_username = Encoding.ASCII.GetBytes(username.ToCharArray());
byte[] byte_four_null = new byte[4];
byte[] byte_network_count = new byte[1];
byte_network_count[0] = 1;
byte[] byte_interface_ip = new byte[4];
byte_interface_ip[0] = 127;
byte_interface_ip[1] = 0;
byte_interface_ip[2] = 0;
byte_interface_ip[3] = 1;
int l1 = byte_nc1.Length;
int l2 = byte_username.Length;
int l3 = byte_four_null.Length;
int l4 = byte_network_count.Length;
int l5 = byte_interface_ip.Length;
byte[] byte_rc4_data = new byte[l1 + l2 + l3 + l4 + l5 + l5];
byte_nc1.CopyTo(byte_rc4_data, 0);
byte_username.CopyTo(byte_rc4_data, l1);
byte_four_null.CopyTo(byte_rc4_data, l1 + l2);
byte_network_count.CopyTo(byte_rc4_data, l1 + l2 + l3);
byte_interface_ip.CopyTo(byte_rc4_data, l1 + l2 + l3 + l4);
byte_interface_ip.CopyTo(byte_rc4_data, l1 + l2 + l3 + l4 + l5);
return RC4.Encrypt(byte_rc4_key, byte_rc4_data);
}
}
public static class RC4
{
public static byte[] Encrypt(byte[] key, byte[] data)
{
return EncryptOutput(key, data).ToArray();
}
private static byte[] EncryptInitalize(byte[] key)
{
byte[] s = Enumerable.Range(0, 256)
.Select(i => (byte)i)
.ToArray();
for (int i = 0, j = 0; i < 256; i++)
{
j = (j + key[i % key.Length] + s[i]) & 255;
Swap(s, i, j);
}
return s;
}
private static IEnumerable<byte> EncryptOutput(byte[] key, IEnumerable<byte> data)
{
byte[] s = EncryptInitalize(key);
int i = 0;
int j = 0;
return data.Select((b) =>
{
i = (i + 1) & 255;
j = (j + s[i]) & 255;
Swap(s, i, j);
return (byte)(b ^ s[(s[i] + s[j]) & 255]);
});
}
private static void Swap(byte[] s, int i, int j)
{
byte c = s[i];
s[i] = s[j];
s[j] = c;
}
}