Skip to content

Commit 78aa7ae

Browse files
committed
Add TCP Abridged obfuscated protocol
1 parent e1e6e4b commit 78aa7ae

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
import logging
20+
import os
21+
22+
from .tcp import TCP
23+
from ....crypto.aes import AES
24+
25+
log = logging.getLogger(__name__)
26+
27+
28+
class TCPAbridgedO(TCP):
29+
RESERVED = (b"HEAD", b"POST", b"GET ", b"OPTI", b"\xee" * 4)
30+
31+
def __init__(self, proxy: dict):
32+
super().__init__(proxy)
33+
self.encrypt = None
34+
self.decrypt = None
35+
36+
def connect(self, address: tuple):
37+
super().connect(address)
38+
39+
while True:
40+
nonce = bytearray(os.urandom(64))
41+
42+
if (nonce[0] != b"\xef"
43+
and nonce[:4] not in self.RESERVED
44+
and nonce[4:4] != b"\x00" * 4):
45+
nonce[56] = nonce[57] = nonce[58] = nonce[59] = 0xef
46+
break
47+
48+
temp = bytearray(nonce[55:7:-1])
49+
50+
self.encrypt = (nonce[8:40], nonce[40:56], bytearray(1))
51+
self.decrypt = (temp[0:32], temp[32:48], bytearray(1))
52+
53+
nonce[56:64] = AES.ctr256_encrypt(nonce, *self.encrypt)[56:64]
54+
55+
super().sendall(nonce)
56+
57+
log.info("Connected{}!".format(" with proxy" if self.proxy_enabled else ""))
58+
59+
def sendall(self, data: bytes, *args):
60+
length = len(data) // 4
61+
62+
super().sendall(
63+
AES.ctr256_encrypt(
64+
(bytes([length])
65+
if length <= 126
66+
else b"\x7f" + length.to_bytes(3, "little"))
67+
+ data,
68+
*self.encrypt
69+
)
70+
)
71+
72+
def recvall(self, length: int = 0) -> bytes or None:
73+
length = super().recvall(1)
74+
75+
if length is None:
76+
return None
77+
78+
length = AES.ctr256_decrypt(length, *self.decrypt)
79+
80+
if length == b"\x7f":
81+
length = super().recvall(3)
82+
83+
if length is None:
84+
return None
85+
86+
length = AES.ctr256_decrypt(length, *self.decrypt)
87+
88+
data = super().recvall(int.from_bytes(length, "little") * 4)
89+
90+
if data is None:
91+
return None
92+
93+
return AES.ctr256_decrypt(data, *self.decrypt)

0 commit comments

Comments
 (0)