Someonewhoissia
Someonewhoissia

Reputation: 63

Telegram Api - Creating an Authorization Key 404 error

I am trying to write a simple program in python to use telegram api, (not bot api, main messaging api) Now i have written this code

#!/usr/bin/env python

import socket
import random
import time
import struct
import requests

def swap64(i):
        return struct.unpack("<L", struct.pack(">L", i))[0]


MESSAGE = '0000000000000000'+format(swap32(int(time.time()*1000%1000)<<21|random.randint(0,1048575)<<3|4),'x')+format(swap32(int(time.time())),'x')+'140000007897466068edeaecd1372139bbb0394b6fd775d3'

res = requests.post(url='http://149.154.167.40',
                    data=bytes.fromhex(MESSAGE),
                    headers={'connection': 'keep-alive'})


print("received data:", res)

For payload of post data i used the source code of telegram web, The 0 auth id, message id is generated using the algo in telegram web, next is length (14000000) just like in the source and main doc and then the method and so on, When i run this code i get received data: <Response [404]> i have used both tcp and http transport with this and tcp one gives me nothing as answer from server, i don't know where i'm wrong in my code i would be glad if someone can show the error in my code btw here is hex dump of my generated req:

0000   34 08 04 17 7a ec 48 5d 60 84 ba ed 08 00 45 00
0010   00 50 c6 07 40 00 40 06 76 28 c0 a8 01 0d 95 9a
0020   a7 28 c9 62 00 50 0d 1a 3b df 41 5a 40 7f 50 18
0030   72 10 ca 39 00 00 00 00 00 00 00 00 00 00 6c 28
0040   22 4a 94 a9 c9 56 14 00 00 00 78 97 46 60 68 ed
0050   ea ec d1 37 21 39 bb b0 39 4b 6f d7 75 d3

i have already read this and this and many other docs but cant find out my problem thanks in advance

update

i used this code as suggested

TCP_IP = '149.154.167.40'
TCP_PORT = 80

MESSAGE = 'ef0000000000000000'+"{0:0{1}x}".format(int(time.time()*4294.967296*1000),16)+'140000007897466068edeaecd1372139bbb0394b6fd775d3'

BUFFER_SIZE = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(bytes.fromhex(MESSAGE))
data = s.recv(BUFFER_SIZE)
s.close()

and i still get no response hex dump of my request:

0000   34 08 04 17 7a ec 48 5d 60 84 ba ed 08 00 45 00
0010   00 51 e1 44 40 00 40 06 5a ea c0 a8 01 0d 95 9a
0020   a7 28 df 8c 00 50 e4 0d 12 46 e2 98 bf a3 50 18
0030   72 10 af 66 00 00 ef 00 00 00 00 00 00 00 00 00
0040   16 37 dc e1 28 39 23 14 00 00 00 78 97 46 60 68
0050   ed ea ec d1 37 21 39 bb b0 39 4b 6f d7 75 d3

Fixed code

Finally got it working with this code

import socket
import random
import time
import struct
import requests

def swap32(i):
        return struct.unpack("<L", struct.pack(">L", i))[0]

TCP_IP = '149.154.167.40'
TCP_PORT = 80
z = int(time.time()*4294.967296*1000000)
z = format(z,'x')
q = bytearray.fromhex(z)
e = q[::-1].hex()
MESSAGE = 'ef0a0000000000000000'+e+'140000007897466068edeaecd1372139bbb0394b6fd775d3'

BUFFER_SIZE = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(bytes.fromhex(MESSAGE))
data = s.recv(BUFFER_SIZE)
s.close()

print(data)

Upvotes: 2

Views: 1878

Answers (1)

Charles Okwuagwu
Charles Okwuagwu

Reputation: 10876

here is sample data from a simple TCP handshake with Telegram Servers:

Connect:Success:0
Connected to 149.154.167.40:443

    raw_data: 000000000000000000F011DB3B2AA9561400000078974660A9729A4F5B51F18F7943F9C0D61B1315
 auth_key_id: 0000000000000000  0
  message_id: 56A92A3BDB11F000  6244568794892726272
 data_length: 00000014  20
message_data: 78974660A9729A4F5B51F18F7943F9C0D61B1315
message_type: 60469778

>> EF0A000000000000000000F011DB3B2AA9561400000078974660A9729A4F5B51F18F7943F9C0D61B1315
Send:Success:42
Receive:Success:85
<< 15000000000000000001CC0CC93D2AA9564000000063241605A9729A4F5B51F18F7943F9C0D61B1315B4445B94718B3C6DD4136466FAC62DCD082311272BE9FF8F9700000015C4B51C01000000216BE86C022BB4C3

    raw_data: 000000000000000001CC0CC93D2AA9564000000063241605A9729A4F5B51F18F7943F9C0D61B1315B4445B94718B3C6DD4136466FAC62DCD082311272BE9FF8F9700000015C4B51C01000000216BE86C022BB4C3
 auth_key_id: 0000000000000000  0
  message_id: 56A92A3DC90CCC01  6244568803180334081
 data_length: 00000040  64
message_data: 63241605A9729A4F5B51F18F7943F9C0D61B1315B4445B94718B3C6DD4136466FAC62DCD082311272BE9FF8F9700000015C4B51C01000000216BE86C022BB4C3
message_type: 05162463


     classid: resPQ#05162463
       nonce: A9729A4F5B51F18F7943F9C0D61B1315
server_nonce: B4445B94718B3C6DD4136466FAC62DCD
          pq: 2311272BE9FF8F97  2526843935494475671
       count: 00000001  1
fingerprints: C3B42B026CE86B21  14101943622620965665

Lets break it down:

  1. We are using the TCP abridged version, so we start off with 0xEF

  2. The format for plain-text Telegram messages is auth_ke_id + msg_id + msg_len + msg

  3. auth_key_id is always 0 for plain-text messages hence we always start with 0000000000000000

  4. msg_id must approximately equal unixtime*2^32(see here) I have also seen that some variant of this works quite well for msg_id in any language on any platform: whole_part_of(current_micro_second_time_stamp * 4294.967296)

  5. The first message you start with for Auth_key generation is reqPQ which is defined as: reqPQ#0x60469778 {:nonce, :int128} so it is simply a TL-header + a 128-bit random integer the total length will always be 4 + 16 = 20 encoded as little-endian that would be msg_len = 14000000

  6. say we have a 128-bit random integer= 55555555555555555555555555555555, then our reqPQ message would be 7897466055555555555555555555555555555555, which is simply TL-type 60469778 or 78974660 in little-endian followed by your randomly chooses 128-bit nonce.

  7. Before you send out the packet, again recall that TCP-abridged mode required you to include the total packet length in front of the other bytes just after the initial 0xEA . This packet length is computed as follows

    let len = total_length / 4

    a) If len < 127 then len_header = len as byte

    b) If len >=127 then len_header = 0x7f + to_3_byte_little_endian(len)

finally we have:

EF0A000000000000000000F011DB3B2AA956140000007897466055555555555555555555555555555555

or

EF0A
0000000000000000
00F011DB3B2AA956
14000000
78974660
55555555555555555555555555555555

compared to yours:

0000000000000000
6C28224A94A9C956
14000000
78974660
68EDEAECD1372139BBB0394B6FD775D3

I would say, try using TCP-abriged mode by include the 0xEF starting bit and re-check your msg_id computation

cheers.

Upvotes: 2

Related Questions