I’ve recently started to work with the telegram api. In the first stage, I made a request to receive auth_key
.
This is my c# code :
// auth_key_id in unencrypted message is ZERO
Int64 auth_key_id = 0;
// this is current time stamp that used as message id
Int64 message_id = DateTime.Now.Ticks;
// message type for req_pq is 0x60469778 in big-ending format
byte[] message_type = {120, 151, 70, 96};
// this is data lenght, it determind in run time
Int32 data_lenght;
// data is combined message_type and an int128 bit value called nonce
// nonce create by random
byte[] nonce = new byte[16];
Random rand = new Random(1);
rand.NextBytes(nonce);
// make data
List<byte> dataList = new List<byte>();
dataList.AddRange(message_type);
dataList.AddRange(nonce);
byte[] data = dataList.ToArray();
// make packet
List<byte> packetList = new List<byte>();
packetList.AddRange(BitConverter.GetBytes(auth_key_id));
packetList.AddRange(BitConverter.GetBytes(message_id));
data_lenght = data.Length;
packetList.AddRange(BitConverter.GetBytes(data_lenght));
packetList.AddRange(data);
byte[] packet = packetList.ToArray();
try
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect("149.154.167.40", 443);
if (s.Connected)
{
IPEndPoint remote = s.RemoteEndPoint as IPEndPoint;
Console.WriteLine("Connected To : "+remote.Address+":"+remote.Port);
}
int sendLength = s.Send(packet);
Console.WriteLine("Send " +sendLength+" Byte(s)");
byte[] received = new byte[128];
int recLen = s.Receive(received);
Console.WriteLine("Received " + recLen + " Byte(s)");
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
I capture send data by wireshark and get this payload :
0000 10 fe ed f4 8e 97 20 6a 8a 54 28 95 08 00 45 00
0010 00 50 02 a3 40 00 80 06 00 00 c0 a8 01 64 95 9a
0020 a7 28 23 e3 01 bb 0e 4d aa 3b 61 c3 01 b6 50 18
0030 01 01 ff 11 00 00 00 00 00 00 00 00 00 00 af 20
0040 82 e0 b4 08 d3 08 14 00 00 00 78 97 46 60 46 d0
0050 86 82 40 97 e4 a3 95 cf ff 46 69 9c 73 c4
The bold part is my payload and according to telegram documentations it seems correct but i don’t get any response from server.
2
Answers
Slight remark: I know nothing about the Telegram API.
First off, you’re connecting to
149.154.167.40:443
. This is the default port of an HTTPS endpoint, which means your client needs to ‘speak’ SSL in order to communicate.Basically what happens is that you connect to the port, and the server waits for a valid SSL handshake. But what you’re actually sending is the actual content itself. The server notices you are sending something, but since the amount of bytes received is less than a valid SSL handshake, it keeps on waiting until you send more.
What you want to do instead, is ‘speak’ this SSL protocol. SSL enabled your sent and/or received content to be encrypted. This can be done by using the SslStream, for example. This is slightly higher level, but it prevents you from implementing an SSL implementation instead.
On the other hand, you can try to connect to port
80
, if available, but this disables the encryption. I wouldn’t recommend doing so.Your data.Length is 40 Bytes
You forgot other 12 Bytes 🙂
You must Capsulate your data before send
— 04 Bytes — Length of your data (data.Length + 12) == 52
— 04 Bytes — Sequence Number (started from 0) == 0
— 40 Bytes — your data
— 04 Bytes — CRC32 (Calculate Hash for 48 Bytes)
Now your packet length is 52 Bytes
Now you can send it
After send you will receive Response (96 Bytes)
Now you can go to Step 2 for Authorization
Be sure, I did it a few days ago
Good luck !!!