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.

mtproto_cipher.go 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package wrappers
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/md5" // nolint: gas
  7. "crypto/sha1" // nolint: gosec
  8. "encoding/binary"
  9. "net"
  10. "github.com/9seconds/mtg/mtproto/rpc"
  11. "github.com/9seconds/mtg/utils"
  12. )
  13. type cipherPurpose uint8
  14. const (
  15. cipherPurposeClient cipherPurpose = iota
  16. cipherPurposeServer
  17. )
  18. var emptyIP = [4]byte{0x00, 0x00, 0x00, 0x00}
  19. // NewMiddleProxyCipher creates new block cipher to proxy<->telegram
  20. // connection.
  21. func NewMiddleProxyCipher(conn StreamReadWriteCloser,
  22. req *rpc.NonceRequest, resp *rpc.NonceResponse, secret []byte) StreamReadWriteCloser {
  23. localAddr := conn.LocalAddr()
  24. remoteAddr := conn.RemoteAddr()
  25. encKey, encIV := deriveKeys(cipherPurposeClient, req, resp, localAddr, remoteAddr, secret)
  26. decKey, decIV := deriveKeys(cipherPurposeServer, req, resp, localAddr, remoteAddr, secret)
  27. enc, _ := makeEncrypterDecrypter(encKey, encIV)
  28. _, dec := makeEncrypterDecrypter(decKey, decIV)
  29. return NewBlockCipher(conn, enc, dec)
  30. }
  31. func deriveKeys(purpose cipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceResponse,
  32. client, remote *net.TCPAddr, secret []byte) ([]byte, []byte) {
  33. message := bytes.Buffer{}
  34. message.Write(resp.Nonce) // nolint: gosec
  35. message.Write(req.Nonce) // nolint: gosec
  36. message.Write(req.CryptoTS) // nolint: gosec
  37. clientIPv4 := emptyIP[:]
  38. serverIPv4 := emptyIP[:]
  39. if client.IP.To4() != nil {
  40. clientIPv4 = utils.ReverseBytes(client.IP.To4())
  41. serverIPv4 = utils.ReverseBytes(remote.IP.To4())
  42. }
  43. message.Write(serverIPv4) // nolint: gosec
  44. var port [2]byte
  45. binary.LittleEndian.PutUint16(port[:], uint16(client.Port))
  46. message.Write(port[:]) // nolint: gosec
  47. switch purpose {
  48. case cipherPurposeClient:
  49. message.WriteString("CLIENT") // nolint: gosec
  50. case cipherPurposeServer:
  51. message.WriteString("SERVER") // nolint: gosec
  52. default:
  53. panic("Unexpected cipher purpose")
  54. }
  55. message.Write(clientIPv4) // nolint: gosec
  56. binary.LittleEndian.PutUint16(port[:], uint16(remote.Port))
  57. message.Write(port[:]) // nolint: gosec
  58. message.Write(secret) // nolint: gosec
  59. message.Write(resp.Nonce) // nolint: gosec
  60. if client.IP.To4() == nil {
  61. message.Write(client.IP.To16()) // nolint: gosec
  62. message.Write(remote.IP.To16()) // nolint: gosec
  63. }
  64. message.Write(req.Nonce) // nolint: gosec
  65. data := message.Bytes()
  66. md5sum := md5.Sum(data[1:]) // nolint: gas
  67. sha1sum := sha1.Sum(data) // nolint: gosec
  68. key := append(md5sum[:12], sha1sum[:]...)
  69. iv := md5.Sum(data[2:]) // nolint: gas
  70. return key, iv[:]
  71. }
  72. func makeEncrypterDecrypter(key, iv []byte) (cipher.BlockMode, cipher.BlockMode) {
  73. block, err := aes.NewCipher(key)
  74. if err != nil {
  75. panic(err)
  76. }
  77. return cipher.NewCBCEncrypter(block, iv), cipher.NewCBCDecrypter(block, iv)
  78. }