mirror of
https://git.ugnet.gay/CrossTalk/azul.git
synced 2026-11-25 08:19:55 +00:00
123 lines
5.5 KiB
Python
123 lines
5.5 KiB
Python
import random
|
|
import struct
|
|
|
|
# the V5 scrambling table: V5 packets are encrypted with it, and V3/V4/V5
|
|
# checkcodes point into it
|
|
V5_TABLE = bytes((
|
|
0x59, 0x60, 0x37, 0x6B, 0x65, 0x62, 0x46, 0x48, 0x53, 0x61, 0x4C, 0x59, 0x60, 0x57, 0x5B, 0x3D,
|
|
0x5E, 0x34, 0x6D, 0x36, 0x50, 0x3F, 0x6F, 0x67, 0x53, 0x61, 0x4C, 0x59, 0x40, 0x47, 0x63, 0x39,
|
|
0x50, 0x5F, 0x5F, 0x3F, 0x6F, 0x47, 0x43, 0x69, 0x48, 0x33, 0x31, 0x64, 0x35, 0x5A, 0x4A, 0x42,
|
|
0x56, 0x40, 0x67, 0x53, 0x41, 0x07, 0x6C, 0x49, 0x58, 0x3B, 0x4D, 0x46, 0x68, 0x43, 0x69, 0x48,
|
|
0x33, 0x31, 0x44, 0x65, 0x62, 0x46, 0x48, 0x53, 0x41, 0x07, 0x6C, 0x69, 0x48, 0x33, 0x51, 0x54,
|
|
0x5D, 0x4E, 0x6C, 0x49, 0x38, 0x4B, 0x55, 0x4A, 0x62, 0x46, 0x48, 0x33, 0x51, 0x34, 0x6D, 0x36,
|
|
0x50, 0x5F, 0x5F, 0x5F, 0x3F, 0x6F, 0x47, 0x63, 0x59, 0x40, 0x67, 0x33, 0x31, 0x64, 0x35, 0x5A,
|
|
0x6A, 0x52, 0x6E, 0x3C, 0x51, 0x34, 0x6D, 0x36, 0x50, 0x5F, 0x5F, 0x3F, 0x4F, 0x37, 0x4B, 0x35,
|
|
0x5A, 0x4A, 0x62, 0x66, 0x58, 0x3B, 0x4D, 0x66, 0x58, 0x5B, 0x5D, 0x4E, 0x6C, 0x49, 0x58, 0x3B,
|
|
0x4D, 0x66, 0x58, 0x3B, 0x4D, 0x46, 0x48, 0x53, 0x61, 0x4C, 0x59, 0x40, 0x67, 0x33, 0x31, 0x64,
|
|
0x55, 0x6A, 0x32, 0x3E, 0x44, 0x45, 0x52, 0x6E, 0x3C, 0x31, 0x64, 0x55, 0x6A, 0x52, 0x4E, 0x6C,
|
|
0x69, 0x48, 0x53, 0x61, 0x4C, 0x39, 0x30, 0x6F, 0x47, 0x63, 0x59, 0x60, 0x57, 0x5B, 0x3D, 0x3E,
|
|
0x64, 0x35, 0x3A, 0x3A, 0x5A, 0x6A, 0x52, 0x4E, 0x6C, 0x69, 0x48, 0x53, 0x61, 0x6C, 0x49, 0x58,
|
|
0x3B, 0x4D, 0x46, 0x68, 0x63, 0x39, 0x50, 0x5F, 0x5F, 0x3F, 0x6F, 0x67, 0x53, 0x41, 0x25, 0x41,
|
|
0x3C, 0x51, 0x54, 0x3D, 0x5E, 0x54, 0x5D, 0x4E, 0x4C, 0x39, 0x50, 0x5F, 0x5F, 0x5F, 0x3F, 0x6F,
|
|
0x47, 0x43, 0x69, 0x48, 0x33, 0x51, 0x54, 0x5D, 0x6E, 0x3C, 0x31, 0x64, 0x35, 0x5A, 0x00, 0x00,
|
|
))
|
|
|
|
# the V4 table is the first 256 bytes of the tips text ICQ 98a ships with
|
|
V4_TABLE = (
|
|
b'\n[1] You can modify the sounds ICQ makes. Just select "Sounds" from the "preferences/misc" in ICQ or from the '
|
|
b'"Sounds" in the control panel. Credit: Eran\n[2] Can\'t remember what was said? Double-click on a user to get a '
|
|
b'dialog of all messages sent incomin'
|
|
)
|
|
|
|
assert len(V5_TABLE) == 256 and len(V4_TABLE) == 256
|
|
|
|
# a V5 client packet's checkcode sits at 0x14, a server packet's at 0x11
|
|
V5_CLIENT_CHECKCODE_OFFSET = 0x14
|
|
V5_SERVER_CHECKCODE_OFFSET = 0x11
|
|
# a V4 client packet's checkcode sits at 0x10, after the header it stands in for
|
|
V4_CLIENT_CHECKCODE_OFFSET = 0x10
|
|
|
|
|
|
def _u32(value: int) -> int:
|
|
return value & 0xFFFFFFFF
|
|
|
|
def _descramble_v5_checkcode(check: int) -> int:
|
|
# the checkcode's bits are shuffled before it goes into the key
|
|
a1 = (check & 0x0001F000) >> 0x0C
|
|
a2 = (check & 0x07C007C0) >> 0x01
|
|
a3 = _u32((check & 0x003E0001) << 0x0A)
|
|
a4 = (check & 0xF8000000) >> 0x10
|
|
a5 = _u32((check & 0x0000083E) << 0x0F)
|
|
return _u32(a5 + a1 + a2 + a3 + a4)
|
|
|
|
def decrypt_v5(packet: bytes) -> bytes:
|
|
# a V5 client packet is encrypted from 0x0A on with a key made from its length and checkcode; the checkcode itself
|
|
# (0x14..0x17) is left as it is
|
|
if len(packet) < 0x18:
|
|
return packet
|
|
data = bytearray(packet)
|
|
length = len(data)
|
|
check = struct.unpack_from('<L', data, V5_CLIENT_CHECKCODE_OFFSET)[0]
|
|
key = _u32(length * 0x68656C6C + _descramble_v5_checkcode(check))
|
|
for i in range(0x0A, length + 3, 4):
|
|
k = _u32(key + V5_TABLE[i & 0xFF])
|
|
if i != 0x16:
|
|
if i < length:
|
|
data[i] ^= k & 0xFF
|
|
if i + 1 < length:
|
|
data[i + 1] ^= (k >> 8) & 0xFF
|
|
if i != 0x12:
|
|
if i + 2 < length:
|
|
data[i + 2] ^= (k >> 16) & 0xFF
|
|
if i + 3 < length:
|
|
data[i + 3] ^= (k >> 24) & 0xFF
|
|
return bytes(data)
|
|
|
|
def v5_checkcode(packet: bytes) -> int:
|
|
# bytes 8, 4, 2 and 6 of the packet, against a random byte of the packet's first 16 and a random table entry
|
|
b2, b4, b6, b8 = packet[2], packet[4], packet[6], packet[8]
|
|
number1 = (b8 << 24) | (b4 << 16) | (b2 << 8) | b6
|
|
x4 = random.randrange(0x10)
|
|
x3 = packet[x4]
|
|
x2 = random.randrange(0xFF)
|
|
x1 = V5_TABLE[x2]
|
|
number2 = ((x4 << 24) | (x3 << 16) | (x2 << 8) | x1) ^ 0x00FF00FF
|
|
return _u32(number1 ^ number2)
|
|
|
|
def add_v5_server_checkcode(packet: bytearray) -> None:
|
|
# server packets aren't encrypted, only stamped with a checkcode at 0x11 the client verifies
|
|
struct.pack_into('<L', packet, V5_SERVER_CHECKCODE_OFFSET, v5_checkcode(bytes(packet)))
|
|
|
|
def decrypt_v4(packet: bytes) -> bytes:
|
|
# V4 only encrypts the first quarter of a packet, a dword at a time with a key made from its length and checkcode; the
|
|
# checkcode dword (the fifth) is skipped, and the version (never really encrypted) is put back
|
|
if len(packet) < 0x14:
|
|
return packet
|
|
data = bytearray(packet)
|
|
length = len(data)
|
|
check = struct.unpack_from('<L', data, V4_CLIENT_CHECKCODE_OFFSET)[0]
|
|
key = _u32(length * 0x66756B65 + check)
|
|
count = ((length + 3) // 4 + 3) // 4
|
|
for i in range(count):
|
|
if i == 4:
|
|
continue
|
|
pos = i * 4
|
|
if pos >= length:
|
|
break
|
|
k = _u32(key + V4_TABLE[pos & 0xFF])
|
|
for j in range(4):
|
|
if pos + j < length:
|
|
data[pos + j] ^= (k >> (j * 8)) & 0xFF
|
|
data[0:2] = b'\x04\x00'
|
|
return bytes(data)
|
|
|
|
def v3_header_checkcode(header: bytes) -> int:
|
|
# what a V4 client checks a server packet's V3 header against: bytes 8, 4, 2 and 6 of it, xored with an offset into the
|
|
# header and the inverse of the byte there, and an offset into the V4 table and the inverse of that entry (the offsets
|
|
# are the server's to pick; the first byte of seq1 and the first entry)
|
|
check_a = (header[8] << 24) | (header[4] << 16) | (header[2] << 8) | header[6]
|
|
packet_offset = 4
|
|
table_offset = 0
|
|
check_b = (packet_offset << 24) | ((~header[packet_offset] & 0xFF) << 16) | (table_offset << 8) | (~V4_TABLE[table_offset] & 0xFF)
|
|
return _u32(check_a ^ check_b)
|