|
| 1 | +import datetime, zlib, os, binascii, hmac, hashlib, time, random, collections |
| 2 | + |
| 3 | +packstr = lambda s, n=2: len(s).to_bytes(n, 'big') + s |
| 4 | +toint = lambda s, o='big': int.from_bytes(s, o) |
| 5 | + |
| 6 | +class BasePlugin(object): |
| 7 | + async def init_client_data(self, reader, writer, cipher): |
| 8 | + pass |
| 9 | + async def init_server_data(self, reader, writer, cipher, raddr): |
| 10 | + pass |
| 11 | + def apply_cipher(self, reader, writer): |
| 12 | + pass |
| 13 | + @classmethod |
| 14 | + def name(cls): |
| 15 | + return cls.__name__.replace('_Plugin', '').replace('__', '.').lower() |
| 16 | + |
| 17 | +class Plain_Plugin(BasePlugin): |
| 18 | + pass |
| 19 | + |
| 20 | +class Origin_Plugin(BasePlugin): |
| 21 | + pass |
| 22 | + |
| 23 | +class Http_Simple_Plugin(BasePlugin): |
| 24 | + async def init_client_data(self, reader, writer, cipher): |
| 25 | + buf = await reader.read_until(b'\r\n\r\n') |
| 26 | + data = buf.split(b' ')[:2] |
| 27 | + data = bytes.fromhex(data[1][1:].replace(b'%',b'').decode()) |
| 28 | + reader._buffer[0:0] = data |
| 29 | + writer.write(b'HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Encoding: gzip\r\nContent-Type: text/html\r\nDate: ' + datetime.datetime.now().strftime('%a, %d %b %Y %H:%M:%S GMT').encode() + b'\r\nServer: nginx\r\nVary: Accept-Encoding\r\n\r\n') |
| 30 | + async def init_server_data(self, reader, writer, cipher, raddr): |
| 31 | + writer.write(f'GET / HTTP/1.1\r\nHost: {raddr}\r\nUser-Agent: curl\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\n\r\n'.encode()) |
| 32 | + await reader.read_until(b'\r\n\r\n') |
| 33 | + |
| 34 | +TIMESTAMP_TOLERANCE = 5 * 60 |
| 35 | + |
| 36 | +class Tls1__2_Ticket_Auth_Plugin(BasePlugin): |
| 37 | + CACHE = collections.deque(maxlen = 100) |
| 38 | + async def init_client_data(self, reader, writer, cipher): |
| 39 | + key = cipher.cipher(cipher.key).key |
| 40 | + assert await reader.read_n(3) == b'\x16\x03\x01' |
| 41 | + header = await reader.read_n(toint(await reader.read_n(2))) |
| 42 | + assert header[:2] == b'\x01\x00' |
| 43 | + assert header[4:6] == b'\x03\x03' |
| 44 | + cacheid = header[6:28] |
| 45 | + sessionid = header[39:39+header[38]] |
| 46 | + assert cacheid not in self.CACHE |
| 47 | + self.CACHE.append(cacheid) |
| 48 | + utc_time = int(time.time()) |
| 49 | + assert hmac.new(key+sessionid, cacheid, hashlib.sha1).digest()[:10] == header[28:38] |
| 50 | + assert abs(toint(header[6:10]) - utc_time) < TIMESTAMP_TOLERANCE |
| 51 | + addhmac = lambda s: s + hmac.new(key+sessionid, s, hashlib.sha1).digest()[:10] |
| 52 | + writer.write(addhmac((b"\x16\x03\x03" + packstr(b"\x02\x00" + packstr(b'\x03\x03' + addhmac(utc_time.to_bytes(4, 'big') + os.urandom(18)) + b'\x20' + sessionid + b'\xc0\x2f\x00\x00\x05\xff\x01\x00\x01\x00')) + (b"\x16\x03\x03" + packstr(b"\x04\x00" + packstr(os.urandom(random.randrange(164)*2+64))) if random.randint(0, 8) < 1 else b'') + b"\x14\x03\x03\x00\x01\x01\x16\x03\x03" + packstr(os.urandom(random.choice((32, 40)))))[:-10])) |
| 53 | + |
| 54 | + async def init_server_data(self, reader, writer, cipher, raddr): |
| 55 | + key = cipher.cipher(cipher.key).key |
| 56 | + sessionid = os.urandom(32) |
| 57 | + addhmac = lambda s: s + hmac.new(key+sessionid, s, hashlib.sha1).digest()[:10] |
| 58 | + writer.write(b"\x16\x03\x01" + packstr(b"\x01\x00" + packstr(b'\x03\x03' + addhmac(int(time.time()).to_bytes(4, 'big') + os.urandom(18)) + b"\x20" + sessionid + b"\x00\x1c\xc0\x2b\xc0\x2f\xcc\xa9\xcc\xa8\xcc\x14\xcc\x13\xc0\x0a\xc0\x14\xc0\x09\xc0\x13\x00\x9c\x00\x35\x00\x2f\x00\x0a\x01\x00" + packstr(b"\xff\x01\x00\x01\x00\x00\x00" + packstr(packstr(b"\x00" + packstr(raddr.encode()))) + b"\x00\x17\x00\x00\x00\x23" + packstr(os.urandom((random.randrange(17)+8)*16)) + b"\x00\x0d\x00\x16\x00\x14\x06\x01\x06\x03\x05\x01\x05\x03\x04\x01\x04\x03\x03\x01\x03\x03\x02\x01\x02\x03\x00\x05\x00\x05\x01\x00\x00\x00\x00\x00\x12\x00\x00\x75\x50\x00\x00\x00\x0b\x00\x02\x01\x00\x00\x0a\x00\x06\x00\x04\x00\x17\x00\x18")))) |
| 59 | + writer.write(addhmac(b'\x14\x03\x03\x00\x01\x01\x16\x03\x03\x00\x20' + os.urandom(22))) |
| 60 | + |
| 61 | + def apply_cipher(self, reader, writer): |
| 62 | + self.buf = bytearray() |
| 63 | + def decrypt(s): |
| 64 | + self.buf.extend(s) |
| 65 | + ret = b'' |
| 66 | + while len(self.buf) >= 5: |
| 67 | + l = int.from_bytes(self.buf[3:5], 'big') |
| 68 | + if len(self.buf) < l: |
| 69 | + break |
| 70 | + if self.buf[:3] in (b'\x16\x03\x03', b'\x14\x03\x03'): |
| 71 | + del self.buf[:5+l] |
| 72 | + continue |
| 73 | + assert self.buf[:3] == b'\x17\x03\x03' |
| 74 | + data = self.buf[5:5+l] |
| 75 | + ret += data |
| 76 | + del self.buf[:5+l] |
| 77 | + return ret |
| 78 | + def pack(s): |
| 79 | + return b'\x17\x03\x03' + packstr(s) |
| 80 | + def encrypt(s): |
| 81 | + ret = b'' |
| 82 | + while len(s) > 2048: |
| 83 | + size = min(random.randrange(4096)+100, len(s)) |
| 84 | + ret += pack(s[:size]) |
| 85 | + s = s[size:] |
| 86 | + if s: |
| 87 | + ret += pack(s) |
| 88 | + return ret |
| 89 | + reader.plugin_decrypt2 = decrypt |
| 90 | + writer.plugin_encrypt2 = encrypt |
| 91 | + |
| 92 | +class Verify_Simple_Plugin(BasePlugin): |
| 93 | + def apply_cipher(self, reader, writer): |
| 94 | + self.buf = bytearray() |
| 95 | + def decrypt(s): |
| 96 | + self.buf.extend(s) |
| 97 | + ret = b'' |
| 98 | + while len(self.buf) >= 2: |
| 99 | + l = int.from_bytes(self.buf[:2], 'big') |
| 100 | + if len(self.buf) < l: |
| 101 | + break |
| 102 | + data = self.buf[2+self.buf[2]:l-4] |
| 103 | + crc = (-1 - binascii.crc32(self.buf[:l-4])) & 0xffffffff |
| 104 | + assert int.from_bytes(self.buf[l-4:l], 'little') == crc |
| 105 | + ret += data |
| 106 | + del self.buf[:l] |
| 107 | + return ret |
| 108 | + def pack(s): |
| 109 | + rnd_data = os.urandom(os.urandom(1)[0] % 16) |
| 110 | + data = bytes([len(rnd_data)+1]) + rnd_data + s |
| 111 | + data = (len(data)+6).to_bytes(2, 'big') + data |
| 112 | + crc = (-1 - binascii.crc32(data)) & 0xffffffff |
| 113 | + return data + crc.to_bytes(4, 'little') |
| 114 | + def encrypt(s): |
| 115 | + ret = b'' |
| 116 | + while len(s) > 8100: |
| 117 | + ret += pack(s[:8100]) |
| 118 | + s = s[8100:] |
| 119 | + if s: |
| 120 | + ret += pack(s) |
| 121 | + return ret |
| 122 | + reader.plugin_decrypt = decrypt |
| 123 | + writer.plugin_encrypt = encrypt |
| 124 | + |
| 125 | +class Verify_Deflate_Plugin(BasePlugin): |
| 126 | + def apply_cipher(self, reader, writer): |
| 127 | + self.buf = bytearray() |
| 128 | + def decrypt(s): |
| 129 | + self.buf.extend(s) |
| 130 | + ret = b'' |
| 131 | + while len(self.buf) >= 2: |
| 132 | + l = int.from_bytes(self.buf[:2], 'big') |
| 133 | + if len(self.buf) < l: |
| 134 | + break |
| 135 | + ret += zlib.decompress(b'x\x9c' + self.buf[2:l]) |
| 136 | + del self.buf[:l] |
| 137 | + return ret |
| 138 | + def pack(s): |
| 139 | + packed = zlib.compress(s) |
| 140 | + return len(packed).to_bytes(2, 'big') + packed[2:] |
| 141 | + def encrypt(s): |
| 142 | + ret = b'' |
| 143 | + while len(s) > 32700: |
| 144 | + ret += pack(s[:32700]) |
| 145 | + s = s[32700:] |
| 146 | + if s: |
| 147 | + ret += pack(s) |
| 148 | + return ret |
| 149 | + reader.plugin_decrypt = decrypt |
| 150 | + writer.plugin_encrypt = encrypt |
| 151 | + |
| 152 | +PLUGIN = {cls.name(): cls for name, cls in globals().items() if name.endswith('_Plugin')} |
| 153 | + |
| 154 | +def get_plugin(plugin_name): |
| 155 | + if plugin_name not in PLUGIN: |
| 156 | + return f'existing plugins: {sorted(PLUGIN.keys())}', None |
| 157 | + return None, PLUGIN[plugin_name]() |
| 158 | + |
0 commit comments