Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

frame.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package obfuscated2
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "encoding/binary"
  6. "io"
  7. "github.com/juju/errors"
  8. "github.com/9seconds/mtg/mtproto"
  9. )
  10. // [frameOffsetFirst:frameOffsetKey:frameOffsetIV:frameOffsetMagic:frameOffsetDC:frameOffsetEnd]
  11. const (
  12. frameLenKey = 32
  13. frameLenIV = 16
  14. frameLenMagic = 4
  15. frameLenDC = 2
  16. frameOffsetFirst = 8
  17. frameOffsetKey = frameOffsetFirst + frameLenKey
  18. frameOffsetIV = frameOffsetKey + frameLenIV
  19. frameOffsetMagic = frameOffsetIV + frameLenMagic
  20. frameOffsetDC = frameOffsetMagic + frameLenDC
  21. FrameLen = 64
  22. )
  23. // Frame represents handshake frame. Telegram sends 64 bytes of obfuscated2
  24. // initialization data first.
  25. // https://blog.susanka.eu/how-telegram-obfuscates-its-mtproto-traffic/
  26. type Frame []byte
  27. // Key returns AES encryption key.
  28. func (f Frame) Key() []byte {
  29. return f[frameOffsetFirst:frameOffsetKey]
  30. }
  31. // IV returns AES encryption initialization vector
  32. func (f Frame) IV() []byte {
  33. return f[frameOffsetKey:frameOffsetIV]
  34. }
  35. // Magic returns magic bytes from last 8 bytes of frame. Telegram checks
  36. // for values there. If after decryption magic is not as expected,
  37. // connection considered as failed.
  38. func (f Frame) Magic() []byte {
  39. return f[frameOffsetIV:frameOffsetMagic]
  40. }
  41. // DC returns number of datacenter IP client wants to use.
  42. func (f Frame) DC() (n int16) {
  43. buf := bytes.NewReader(f[frameOffsetMagic:frameOffsetDC])
  44. if err := binary.Read(buf, binary.LittleEndian, &n); err != nil {
  45. n = 1
  46. }
  47. return
  48. }
  49. // ConnectionType identifies connection type of the handshake frame.
  50. func (f Frame) ConnectionType() (mtproto.ConnectionType, error) {
  51. return mtproto.ConnectionTagFromHandshake(f.Magic())
  52. }
  53. // Invert inverts frame for extracting encryption keys. Pkease check that link:
  54. // https://blog.susanka.eu/how-telegram-obfuscates-its-mtproto-traffic/
  55. func (f Frame) Invert() Frame {
  56. reversed := make(Frame, FrameLen)
  57. copy(reversed, f)
  58. for i := 0; i < frameLenKey+frameLenIV; i++ {
  59. reversed[frameOffsetFirst+i] = f[frameOffsetIV-1-i]
  60. }
  61. return reversed
  62. }
  63. // ExtractFrame extracts exact obfuscated2 handshake frame from given reader.
  64. func ExtractFrame(conn io.Reader) (Frame, error) {
  65. frame := make(Frame, FrameLen)
  66. buf := bytes.NewBuffer(frame)
  67. buf.Reset()
  68. if _, err := io.CopyN(buf, conn, FrameLen); err != nil {
  69. return nil, errors.Annotate(err, "Cannot extract obfuscated header")
  70. }
  71. copy(frame, buf.Bytes())
  72. return frame, nil
  73. }
  74. func generateFrame(connectionType mtproto.ConnectionType) Frame {
  75. frame := make(Frame, FrameLen)
  76. for {
  77. if _, err := rand.Read(frame); err != nil {
  78. continue
  79. }
  80. if frame[0] == 0xef {
  81. continue
  82. }
  83. val := (uint32(frame[3]) << 24) | (uint32(frame[2]) << 16) | (uint32(frame[1]) << 8) | uint32(frame[0])
  84. if val == 0x44414548 || val == 0x54534f50 || val == 0x20544547 || val == 0x4954504f || val == 0xeeeeeeee {
  85. continue
  86. }
  87. val = (uint32(frame[7]) << 24) | (uint32(frame[6]) << 16) | (uint32(frame[5]) << 8) | uint32(frame[4])
  88. if val == 0x00000000 {
  89. continue
  90. }
  91. // error has to be checked before calling this function
  92. tag, _ := connectionType.Tag() // nolint: errcheck
  93. copy(frame.Magic(), tag)
  94. return frame
  95. }
  96. }