2023-07-27 15:04:57 +00:00
|
|
|
|
|
|
|
# LOW LEVEL imessage function, decryption etc
|
|
|
|
# Don't handle APNS etc, accept it already setup
|
|
|
|
|
|
|
|
## HAVE ANOTHER FILE TO SETUP EVERYTHING AUTOMATICALLY, etc
|
|
|
|
# JSON parsing of keys, don't pass around strs??
|
|
|
|
|
|
|
|
import apns
|
|
|
|
import ids
|
|
|
|
|
2023-07-27 15:52:20 +00:00
|
|
|
import plistlib
|
|
|
|
from io import BytesIO
|
|
|
|
|
|
|
|
from cryptography.hazmat.primitives.asymmetric import ec, padding
|
|
|
|
from cryptography.hazmat.primitives import hashes
|
|
|
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
|
|
|
|
|
|
|
import gzip
|
2023-07-28 23:20:32 +00:00
|
|
|
import uuid
|
|
|
|
import random
|
2023-07-29 17:57:20 +00:00
|
|
|
import time
|
2023-07-27 15:52:20 +00:00
|
|
|
|
2023-07-28 00:03:39 +00:00
|
|
|
from hashlib import sha1
|
2023-07-27 21:34:38 +00:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger("imessage")
|
|
|
|
|
2023-07-27 15:52:20 +00:00
|
|
|
NORMAL_NONCE = b"\x00" * 15 + b"\x01"
|
|
|
|
|
2023-07-28 00:03:39 +00:00
|
|
|
class BalloonBody:
|
|
|
|
def __init__(self, type: str, data: bytes):
|
|
|
|
self.type = type
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
# TODO : Register handlers based on type id
|
|
|
|
|
|
|
|
class iMessage:
|
2023-07-28 23:20:32 +00:00
|
|
|
text: str = ""
|
2023-07-28 00:03:39 +00:00
|
|
|
xml: str | None = None
|
2023-07-28 23:20:32 +00:00
|
|
|
participants: list[str] = []
|
|
|
|
sender: str | None = None
|
|
|
|
id: str | None = None
|
|
|
|
group_id: str | None = None
|
2023-07-28 00:03:39 +00:00
|
|
|
body: BalloonBody | None = None
|
|
|
|
|
2023-07-28 23:20:32 +00:00
|
|
|
_compressed: bool = True
|
|
|
|
|
2023-07-28 00:03:39 +00:00
|
|
|
_raw: dict | None = None
|
|
|
|
|
|
|
|
def from_raw(message: dict) -> 'iMessage':
|
|
|
|
self = iMessage()
|
|
|
|
|
|
|
|
self._raw = message
|
|
|
|
|
|
|
|
self.text = message.get('t')
|
|
|
|
self.xml = message.get('x')
|
|
|
|
self.participants = message.get('p', [])
|
|
|
|
if self.participants != []:
|
|
|
|
self.sender = self.participants[-1]
|
|
|
|
else:
|
|
|
|
self.sender = None
|
|
|
|
|
|
|
|
self.id = message.get('r')
|
|
|
|
self.group_id = message.get('gid')
|
|
|
|
|
|
|
|
if 'bid' in message:
|
|
|
|
# This is a message extension body
|
|
|
|
self.body = BalloonBody(message['bid'], message['b'])
|
|
|
|
|
2023-07-28 23:20:32 +00:00
|
|
|
if 'compressed' in message: # This is a hack, not a real field
|
|
|
|
self._compressed = message['compressed']
|
|
|
|
|
2023-07-28 00:03:39 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
def to_raw(self) -> dict:
|
|
|
|
return {
|
|
|
|
"t": self.text,
|
|
|
|
"x": self.xml,
|
|
|
|
"p": self.participants,
|
|
|
|
"r": self.id,
|
|
|
|
"gid": self.group_id,
|
2023-07-28 23:20:32 +00:00
|
|
|
"compressed": self._compressed,
|
2023-07-28 00:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if self._raw is not None:
|
|
|
|
return str(self._raw)
|
|
|
|
else:
|
|
|
|
return f"iMessage({self.text} from {self.sender})"
|
|
|
|
|
2023-07-27 15:04:57 +00:00
|
|
|
class iMessageUser:
|
|
|
|
|
2023-07-27 15:52:20 +00:00
|
|
|
def __init__(self, connection: apns.APNSConnection, user: ids.IDSUser):
|
|
|
|
self.connection = connection
|
|
|
|
self.user = user
|
2023-07-27 15:04:57 +00:00
|
|
|
|
2023-07-27 15:52:20 +00:00
|
|
|
def _get_raw_message(self):
|
|
|
|
"""
|
|
|
|
Returns a raw APNs message corresponding to the next conforming notification in the queue
|
2023-07-28 00:03:39 +00:00
|
|
|
Returns None if no conforming notification is found
|
2023-07-27 15:52:20 +00:00
|
|
|
"""
|
|
|
|
def check_response(x):
|
|
|
|
if x[0] != 0x0A:
|
|
|
|
return False
|
2023-07-28 00:03:39 +00:00
|
|
|
if apns._get_field(x[1], 2) != sha1("com.apple.madrid".encode()).digest():
|
|
|
|
return False
|
2023-07-27 15:52:20 +00:00
|
|
|
resp_body = apns._get_field(x[1], 3)
|
|
|
|
if resp_body is None:
|
2023-07-28 00:03:39 +00:00
|
|
|
#logger.debug("Rejecting madrid message with no body")
|
2023-07-27 15:52:20 +00:00
|
|
|
return False
|
|
|
|
resp_body = plistlib.loads(resp_body)
|
|
|
|
if "P" not in resp_body:
|
2023-07-28 00:03:39 +00:00
|
|
|
#logger.debug(f"Rejecting madrid message with no payload : {resp_body}")
|
2023-07-27 15:52:20 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2023-07-28 00:03:39 +00:00
|
|
|
payload = self.connection.incoming_queue.pop_find(check_response)
|
|
|
|
if payload is None:
|
|
|
|
return None
|
2023-07-27 15:52:20 +00:00
|
|
|
id = apns._get_field(payload[1], 4)
|
|
|
|
|
|
|
|
return payload
|
2023-07-27 15:04:57 +00:00
|
|
|
|
2023-07-27 15:52:20 +00:00
|
|
|
def _parse_payload(payload: bytes) -> tuple[bytes, bytes]:
|
|
|
|
payload = BytesIO(payload)
|
2023-07-27 15:04:57 +00:00
|
|
|
|
2023-07-27 15:52:20 +00:00
|
|
|
tag = payload.read(1)
|
2023-07-28 23:20:32 +00:00
|
|
|
print("TAG", tag)
|
2023-07-27 15:52:20 +00:00
|
|
|
body_length = int.from_bytes(payload.read(2), "big")
|
|
|
|
body = payload.read(body_length)
|
|
|
|
|
|
|
|
signature_len = payload.read(1)[0]
|
|
|
|
signature = payload.read(signature_len)
|
|
|
|
|
|
|
|
return (body, signature)
|
|
|
|
|
2023-07-28 23:20:32 +00:00
|
|
|
def _construct_payload(body: bytes, signature: bytes) -> bytes:
|
|
|
|
payload = b"\x02" + len(body).to_bytes(2, "big") + body + len(signature).to_bytes(1, "big") + signature
|
|
|
|
return payload
|
|
|
|
|
|
|
|
def _encrypt_sign_payload(self, key: ids.identity.IDSIdentity, message: dict) -> dict[str, bytes]:
|
|
|
|
# Dump the message plist
|
|
|
|
compressed = message.get('compressed', False)
|
|
|
|
message = plistlib.dumps(message, fmt=plistlib.FMT_BINARY)
|
|
|
|
|
|
|
|
# Compress the message
|
|
|
|
if compressed:
|
|
|
|
message = gzip.compress(message, mtime=0)
|
|
|
|
|
|
|
|
# Generate a random AES key
|
|
|
|
aes_key = random.randbytes(16)
|
|
|
|
|
|
|
|
# Encrypt the message with the AES key
|
|
|
|
cipher = Cipher(algorithms.AES(aes_key), modes.CTR(NORMAL_NONCE))
|
|
|
|
encrypted = cipher.encryptor().update(message)
|
|
|
|
|
|
|
|
# Encrypt the AES key with the public key of the recipient
|
|
|
|
recipient_key = ids._helpers.parse_key(key.encryption_public_key)
|
|
|
|
rsa_body = recipient_key.encrypt(
|
|
|
|
aes_key + encrypted[:100],
|
|
|
|
padding.OAEP(
|
|
|
|
mgf=padding.MGF1(algorithm=hashes.SHA1()),
|
|
|
|
algorithm=hashes.SHA1(),
|
|
|
|
label=None,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Construct the payload
|
|
|
|
body = rsa_body + encrypted[100:]
|
|
|
|
sig = ids._helpers.parse_key(self.user.encryption_identity.signing_key).sign(body, ec.ECDSA(hashes.SHA1()))
|
|
|
|
payload = iMessageUser._construct_payload(body, sig)
|
|
|
|
|
|
|
|
return payload
|
|
|
|
|
2023-07-27 15:52:20 +00:00
|
|
|
def _decrypt_payload(self, payload: bytes) -> dict:
|
|
|
|
payload = iMessageUser._parse_payload(payload)
|
|
|
|
|
|
|
|
body = BytesIO(payload[0])
|
|
|
|
rsa_body = ids._helpers.parse_key(self.user.encryption_identity.encryption_key).decrypt(
|
|
|
|
body.read(160),
|
|
|
|
padding.OAEP(
|
|
|
|
mgf=padding.MGF1(algorithm=hashes.SHA1()),
|
|
|
|
algorithm=hashes.SHA1(),
|
|
|
|
label=None,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2023-07-28 23:20:32 +00:00
|
|
|
logger.debug(f"RSA BODY LEN: {len(rsa_body)}")
|
|
|
|
|
2023-07-27 15:52:20 +00:00
|
|
|
cipher = Cipher(algorithms.AES(rsa_body[:16]), modes.CTR(NORMAL_NONCE))
|
|
|
|
decrypted = cipher.decryptor().update(rsa_body[16:] + body.read())
|
|
|
|
|
|
|
|
# Try to gzip decompress the payload
|
2023-07-28 23:20:32 +00:00
|
|
|
compressed = False
|
2023-07-27 15:52:20 +00:00
|
|
|
try:
|
|
|
|
decrypted = gzip.decompress(decrypted)
|
2023-07-28 23:20:32 +00:00
|
|
|
compressed = True
|
2023-07-27 15:52:20 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2023-07-28 23:20:32 +00:00
|
|
|
pl = plistlib.loads(decrypted)
|
|
|
|
pl['compressed'] = compressed # This is a hack so that messages can be re-encrypted with the same compression
|
|
|
|
|
|
|
|
return pl
|
2023-07-27 15:52:20 +00:00
|
|
|
|
|
|
|
def _verify_payload(self, payload: bytes, sender: str, sender_token: str) -> bool:
|
|
|
|
# Get the public key for the sender
|
|
|
|
lookup = self.user.lookup([sender])[sender]
|
|
|
|
|
|
|
|
sender_iden = None
|
|
|
|
for identity in lookup['identities']:
|
|
|
|
if identity['push-token'] == sender_token:
|
|
|
|
sender_iden = identity
|
|
|
|
break
|
|
|
|
|
|
|
|
identity_keys = sender_iden['client-data']['public-message-identity-key']
|
|
|
|
identity_keys = ids.identity.IDSIdentity.decode(identity_keys)
|
|
|
|
|
|
|
|
sender_ec_key = ids._helpers.parse_key(identity_keys.signing_public_key)
|
|
|
|
|
|
|
|
|
|
|
|
payload = iMessageUser._parse_payload(payload)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Verify the signature (will throw an exception if it fails)
|
|
|
|
sender_ec_key.verify(
|
|
|
|
payload[1],
|
|
|
|
payload[0],
|
|
|
|
ec.ECDSA(hashes.SHA1()),
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2023-07-28 00:03:39 +00:00
|
|
|
def receive(self) -> iMessage | None:
|
|
|
|
"""
|
|
|
|
Will return the next iMessage in the queue, or None if there are no messages
|
|
|
|
"""
|
2023-07-27 15:52:20 +00:00
|
|
|
raw = self._get_raw_message()
|
2023-07-28 00:03:39 +00:00
|
|
|
if raw is None:
|
|
|
|
return None
|
2023-07-27 15:52:20 +00:00
|
|
|
body = apns._get_field(raw[1], 3)
|
|
|
|
body = plistlib.loads(body)
|
2023-07-29 17:57:20 +00:00
|
|
|
print(f"Got body message {body}")
|
2023-07-27 15:52:20 +00:00
|
|
|
payload = body["P"]
|
|
|
|
decrypted = self._decrypt_payload(payload)
|
2023-07-27 21:34:38 +00:00
|
|
|
if "p" in decrypted:
|
2023-07-29 17:57:20 +00:00
|
|
|
#if not self._verify_payload(payload, decrypted["p"][-1], body["t"]):
|
|
|
|
# raise Exception("Failed to verify payload")
|
|
|
|
pass
|
2023-07-27 21:34:38 +00:00
|
|
|
else:
|
|
|
|
logger.warning("Unable to verify, couldn't determine sender! Dropping message! (TODO work out a way to verify these anyway)")
|
|
|
|
return self.receive() # Call again to get the next message
|
|
|
|
return iMessage.from_raw(decrypted)
|
|
|
|
|
2023-07-29 21:14:25 +00:00
|
|
|
KEY_CACHE: dict[bytes, tuple[bytes, bytes]] = {} # Mapping of push token : (public key, session token)
|
|
|
|
USER_CACHE: dict[str, list[bytes]] = {} # Mapping of handle : [push tokens]
|
2023-07-29 17:57:20 +00:00
|
|
|
def _cache_keys(self, participants: list[str]):
|
|
|
|
# Look up the public keys for the participants, and cache a token : public key mapping
|
|
|
|
lookup = self.user.lookup(participants)
|
|
|
|
|
|
|
|
for key, participant in lookup.items():
|
|
|
|
if not key in self.USER_CACHE:
|
|
|
|
self.USER_CACHE[key] = []
|
|
|
|
|
|
|
|
for identity in participant['identities']:
|
|
|
|
if not 'client-data' in identity:
|
|
|
|
continue
|
|
|
|
if not 'public-message-identity-key' in identity['client-data']:
|
|
|
|
continue
|
|
|
|
if not 'push-token' in identity:
|
|
|
|
continue
|
2023-07-29 21:14:25 +00:00
|
|
|
if not 'session-token' in identity:
|
|
|
|
continue
|
2023-07-29 17:57:20 +00:00
|
|
|
|
|
|
|
self.USER_CACHE[key].append(identity['push-token'])
|
|
|
|
|
2023-07-29 21:14:25 +00:00
|
|
|
print(identity)
|
|
|
|
|
|
|
|
self.KEY_CACHE[identity['push-token']] = (identity['client-data']['public-message-identity-key'], identity['session-token'])
|
2023-07-29 17:57:20 +00:00
|
|
|
|
2023-07-28 00:03:39 +00:00
|
|
|
def send(self, message: iMessage):
|
2023-07-28 23:20:32 +00:00
|
|
|
# Set the sender, if it isn't already
|
|
|
|
if message.sender is None:
|
|
|
|
message.sender = self.user.handles[0] # TODO : Which handle to use?
|
|
|
|
if message.sender not in message.participants:
|
|
|
|
message.participants.append(message.sender)
|
|
|
|
|
2023-07-29 17:57:20 +00:00
|
|
|
self._cache_keys(message.participants)
|
|
|
|
|
2023-07-28 23:20:32 +00:00
|
|
|
# Set the group id, if it isn't already
|
|
|
|
if message.group_id is None:
|
|
|
|
message.group_id = str(uuid.uuid4()).upper() # TODO: Keep track of group ids?
|
2023-07-29 17:57:20 +00:00
|
|
|
|
|
|
|
message_id = uuid.uuid4()
|
2023-07-28 23:20:32 +00:00
|
|
|
if message.id is None:
|
2023-07-29 17:57:20 +00:00
|
|
|
message.id = str(message_id).upper()
|
|
|
|
|
|
|
|
# Turn the message into a raw message
|
|
|
|
raw = message.to_raw()
|
|
|
|
import base64
|
|
|
|
bundled_payloads = []
|
|
|
|
for participant in message.participants:
|
|
|
|
for push_token in self.USER_CACHE[participant]:
|
2023-07-29 21:14:25 +00:00
|
|
|
identity_keys = ids.identity.IDSIdentity.decode(self.KEY_CACHE[push_token][0])
|
2023-07-29 17:57:20 +00:00
|
|
|
payload = self._encrypt_sign_payload(identity_keys, raw)
|
|
|
|
|
|
|
|
bundled_payloads.append({
|
|
|
|
'tP': participant,
|
|
|
|
'D': not participant == message.sender, # TODO: Should this be false sometimes? For self messages?
|
2023-07-29 21:14:25 +00:00
|
|
|
'sT': self.KEY_CACHE[push_token][1],
|
2023-07-29 17:57:20 +00:00
|
|
|
#'sT': self.connection.token,
|
2023-07-29 21:14:25 +00:00
|
|
|
#'sT': base64.b64decode("jJ86jTYbv1mGVwO44PyfuZ9lh3o56QjOE39Jk8Z99N8="),
|
|
|
|
#'sT': b'\x06\x01(\x1b\xc8\x9d\x9b\x956\xf8\xb2m\xc14F\xffKLze\x04\xd4\x16\x9f\xd01\xd48d\xbf\xf1\x1f1\x1a',
|
2023-07-29 17:57:20 +00:00
|
|
|
'P': payload,
|
|
|
|
't': push_token
|
|
|
|
})
|
|
|
|
|
|
|
|
body = {
|
|
|
|
'fcn': 1,
|
|
|
|
'c': 100,
|
|
|
|
'E': 'pair',
|
|
|
|
'ua': '[macOS,13.4.1,22F82,MacBookPro18,3]',
|
|
|
|
'v': 8,
|
|
|
|
'i': 0, # TODO:??
|
|
|
|
'U': message_id.bytes,
|
|
|
|
'dtl': bundled_payloads,
|
|
|
|
'sP': message.sender,
|
|
|
|
#'oe': time.time_ns(),
|
|
|
|
'e': time.time_ns()
|
|
|
|
#'rc': 2
|
|
|
|
}
|
|
|
|
|
|
|
|
# body = {
|
|
|
|
# 'sP': message.sender,
|
|
|
|
# 'fcn': 1,
|
|
|
|
# 'c': 100,
|
|
|
|
# 'E': 'pair',
|
|
|
|
# 'ua': '[macOS,13.4.1,22F82,MacBookPro18,3]',
|
|
|
|
# #'rc': 1,
|
|
|
|
# 'v': 8,
|
|
|
|
# 'i': 1903314481,
|
|
|
|
# #'oe': 1690650841881000000,
|
|
|
|
# 'e': time.time_ns(),
|
|
|
|
# 'dtl': [{'xx': 'xx', 'tP': 'mailto:testu3@icloud.com', 'D': False, 'sT': b'\x06\x01\x86\xde\xa3/\x9f\x87\x8e\x97\x1b\x02~\x19-\xdd\x0b\xe5_\x86\xa8\x94\x80\xf1]O\xe7\x88\xe1/\xc95\xb3\xd7\x1a\xd0', 'P': b'\x02\x01\x1d\x85Y1C\xc9\x12)t\xb8\x99 \xe7\x83\xbc\x9d\x18\'\xf0^\xb7\x9bG\x1e\x14\xd5\xc9.\x98\x1a\xd3\xc9<\xccvk\x95\xe2\xb3\xfd\x93P(\x87\xb2\xcf=Q\xbe@\x16\x9f\x1e\xdb.\xad\xe0\xcb\x83\xad\x8ex\x86`B\x1b\x1d\xfb\x04\x89"V\x94\xd9\xf5\x94c\x88O\t\x18uNK&\x89\x10\xe6\xa6\xa1\xf9\xaa\xd5\xba\x7f\x92\xe1\xaaK\x10\x895\xc1\x07\xd2\t\xf3\r\xae\x01\xd1\xac\xeep\x0fUhb\x98\xa2\xd1H?Dn\xeb\x85\xce\xbd\x97O\xc8\xb9\xf2\xe9\xecQ\x8c\x96V\xbdN\x1c\x83\xf5\xb9\xe4]\xb8X\xd7\x1d\xe8\xc4A\xae\x17>]*\xb7&Wn\xe9\xa29\xa2\xfdU\xcf\x8e^\x02R8\xbbuT\nP\x9e{:\xe8\\\x9f!\xba\x1a9dY\xe5F\x93\x8a\x9ds\xdbh\x87\xe5\xf9d\xac\x0ekA\x91?{\xda-\x99[\xc9\x9c\x03\xe8\xa1\x1fZut}\x18\x85\x97M\xf8\xc9\x00!\xde\xa0\xbe\x0f\xcb&M\xd8)\x9b\xfa1\x97\x99Iq\xf9\xa3\xdf\x1eQ.\xd3p\xdd\x84\x9a\xf3\x9cO\'\xac\xfa\x8b\x7f\xefA8\x7f\xc8r\xd1\xe3M\xf8\xda[\xa6\xbbPL\xd9H0F\x02!\x00\x95!C\x06\x81\xb6\xf6}:\xee\xedx\xd1mpJ(\x9c\xa3k(\xb5\x93\x01\xd3\r`\xc9dY\xf8\xa3\x02!\x00\x96\x9b\xa9\x9d\xd8\n\xeeRN,c74k\x0e\xd4\xc7\x03p\xb4\x00\xc4b\xaa\xc8\xdc\xe7X\xdf\xe4\x85\xa0', 't': b'\xc6\xe1}R\xc3\xde\xd29G\xf0DN\xf4+\xde\x91y{|\xb7\xb1\xd8\xbd\xb2\xc4\x86\xec@9\xa1\xc0\xac'}, {'tP': 'mailto:testu3@icloud.com', 'D': False, 'sT': b'\x06\x01n\xb0\xbe8\xd0\xc4\xddp\xdd\xc8c\xaaE\xc68z\x02-\xf1\x0b8\xb45)\xfd\x8b\x92\xcc:\xc6g\xefS\xcc', 'P': b'\x02\x01\x1dB)>\x11\x9b\x04d\x1b~\x81\xe2\xb2\xc8\xe9.\xe0\xceV\xd7g\x8bq\xc7\xe8\x9ee\x8e\x85\xf5\xd4\xf3\x95\xd7\xb4|K=8\xb7\x8f\x1b\x0b\x11\xabK\x16\x1aP\x88\xd5B\x9d;=\xd5\xd6\x13O\xc74{\xd9\xfd\xb8\x1c\xfa\xf1\\i\x1c\n3\x07\x7f\xb2\'\x80\xe8f\xa4kp\xfaX\x8a\x9609\xa6\x1dJ\xca\xee\xe0?\x078_9lL\x15A\x81\x14\xe5;"\x1b\xe9O\xabc\xd2\xf5*\x96\xbc\'\xdf10\xa5S\xb2\xae\x7f!K\x0fU\r\xf8o\x1a\xb1\xe7t\xea:\x0f\x9c\x0f\xe5X\xc2\x9e\xf4\xba&/-+,U*\x83/\x07\x01l\x00\xb0\x16m\x0b\x17\xbb\x07p\xfe\x04\xee48\xa1\xf7\xb83\x874\x9b\xb1\xc0y\xa5\x9c\x8c\xe7\xe4 /8`Uc\x85E\xda\xb9Cn]wFb\xfdz\xf1&Y\xbf\xfb8\x9d\xb4\xed\xd5\xeb\xf2\x1b\xec\x9b\xddk *[\xbf\n\xbb1\xf4%\xae\x80yz\xcf\xc6Q\xe3G\xae\xf9\x85\xda|\xe4u\xe6\x9a#_f\xeb(K\x8d\x12\x8a\x96AC=\x1d#Jm\xfe\x8bV\xae_=\xfc\x8a\xc42@\xa5\xb3"\x95\xa0G0E\x02!\x00\xb6\xde\xd9\x05\x1cf\x083\xd6w\xcd"F\x96\xfa\xf4\xedS\xffi\xc0\xc1\x9e\xf3T\xa1:\xb4\xc0?=\x0f\x02 \x11q\xd2\xb03\xd3\xfe\xa2u\xc6\xf8\n0\xbe\x0f\x0c\xa8\x9b\x9eB\xd90\x12\xd9a2\x944\xef\xc1\x96\x18', 't': b'\x96\xb0\xcc\xb6\xe5\xb3=,\x8b\x19\x18.\x1e@\n\xefL\x02\x0bv\x00>4!\x04\x1d\xceV\x0fI\x85y'}, {'tP': 'tel:+16106632676', 'D': True, 'sT': b'\x06\x01\xad\tn\x91\xbd\x96\x0emez\xc4\x08\xa0\xd8\x8e\xfe\x82\xb4\x08\xeew\xa5\xeb\x9d\xe3\xf07\xad\xae\xfaB\x8d3\xa9', 'P': b'\x02\x01\x1d\x8c\x1db\x81\x981%5\x0e\xae\x06\x9eg\x0fs\xf1odu\x90\xd5\xec\xe4\x0fY\t-m\xa2k\x15b\x0c\xfe\xd2\x148\x91T\xefjKOs\xa7k\xc8[Y\xf3\xfa\x12\x82\xc0#NG+^>\x88\xb9\x12\x0fV\xeb\xfa\x8a>\x05x\x8a\xbd\x1d\xdb\xe0\xd2GIY\xd6\xd91U\xf8c\xd9b\xeas\x94EB\x8a\xd7L\xea\x8e\x9e\x07\x8d(\xbd\xb8\xca3\x8fn\x89\xee\x1f\x9c\xb7b\xc6q\xca\xb9\xc6\xfd\xc7\x9d\x15\t\xe4|\x93\xfc\\\x16%\x04\xb7\xd1Y\x9b\x86Tr\x94W\xe8\x95\x82Rh?\xe2w\xd8\xeb\x8a\xf2\xc1eA\\\xdf:j\xc9(7\x02%\x8b\xc9\x96-y\xd1\x8c\xea\xb0p\xb5\xd1\x9cSOo\xa4y\x8c4)\xffh\x19o\x90\x07\xc3@\xf2\xb3\xca\xb3/:\xd22\x17K\x9d\x0b\x9b\xdb\x1aek/]\x10\xf2\xb5\xedX\xff\xdf\xfe\xde\xf9B\xf9\x18\x01\x07\xe4\x9bU\x02\x8aB?\x10\xc9\xab\x84H\xffW\\\xad\x1a[Xu\xfc\xbc8\x9dt\x0fw\xacF\xd8\x12A0 \xab\x992D\xcb\x9d+9U;\xb4\xbb\x90\\\x93\xc8\xefy\x08W[G|G0E\x02!\x00\xf3QW=\xeb\x07\xd3\xc6DK_\xbb\xe6\xc3\xfe"\x9d\xd1\xdf\xcd\\\x95Y"\xe5?\'\x0cC\xb7)R\x02 S~\x8f\x99\xc2U\xf0\x1f\xa1\xf9\x04\xba\xaa\xf7\x7f\xe6\x9a\xecv[\xc3\x9f\xa9*\x9b\xba\xa0\xef\x81F\xa8\xd5', 't': b'\x13j\xb1\xdaH\x94\xd0\xacg\xca\x17\x0b\x12\xb5\x07\x12uP\x00\x11\x1a\x11\x07\xcf\x10B\x12\x99L~\xe2>'}, {'tP': 'mailto:testu3@icloud.com', 'D': False, 'sT': b'\x06\x01i\x11\xf3\x91\xb0N\xb9\x9d\xdf.z\xd7\xa4\x8b\xd1E\x1a)\xb4\x8d\x90.Li\xd4\xf8\xfcg\xbc\x0
|
|
|
|
# #'dtl': [{}],
|
|
|
|
# #'U': b'Qk\xfb7\x91\xe5N\xf0\xbb\xd6\x8eu\th\xe0U'
|
|
|
|
# 'U': message_id.bytes,
|
|
|
|
# }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(body)
|
|
|
|
|
|
|
|
body = plistlib.dumps(body, fmt=plistlib.FMT_BINARY)
|
|
|
|
|
|
|
|
self.connection.send_message("com.apple.madrid", body)
|
|
|
|
|
|
|
|
def check_response(x):
|
|
|
|
if x[0] != 0x0A:
|
|
|
|
return False
|
|
|
|
if apns._get_field(x[1], 2) != sha1("com.apple.madrid".encode()).digest():
|
|
|
|
return False
|
|
|
|
resp_body = apns._get_field(x[1], 3)
|
|
|
|
if resp_body is None:
|
|
|
|
return False
|
|
|
|
resp_body = plistlib.loads(resp_body)
|
|
|
|
if 'c' not in resp_body or resp_body['c'] != 255:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
num_recv = 0
|
|
|
|
while True:
|
|
|
|
if num_recv == len(bundled_payloads) -1:
|
|
|
|
break
|
|
|
|
payload = self.connection.incoming_queue.wait_pop_find(check_response)
|
|
|
|
if payload is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
resp_body = apns._get_field(payload[1], 3)
|
|
|
|
resp_body = plistlib.loads(resp_body)
|
|
|
|
logger.error(resp_body)
|
|
|
|
num_recv += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # Encrypt the message for each participant
|
|
|
|
# lookup = self.user.lookup(message.participants[:-1])
|
|
|
|
# for participant in message.participants[:-1]:
|
|
|
|
# for identity in lookup[participant]['identities']:
|
|
|
|
# if 'client-data' in identity and 'public-message-identity-key' in identity['client-data'] and 'push-token' in identity:
|
|
|
|
# push_token = identity['push-token']
|
|
|
|
# identity_keys = ids.identity.IDSIdentity.decode(identity['client-data']['public-message-identity-key'])
|
|
|
|
# payload = self._encrypt_sign_payload(identity_keys, raw)
|
|
|
|
# #import time
|
|
|
|
# body = {
|
|
|
|
# "t": self.connection.token,
|
|
|
|
# "P": payload,
|
|
|
|
# "c": 100,
|
|
|
|
# "E": "pair",
|
|
|
|
# "sP": self.user.handles[0],
|
|
|
|
# "tP": participant,
|
|
|
|
# "U": mid.bytes,
|
|
|
|
# 'v': 8,
|
|
|
|
# #'D': True,
|
|
|
|
# 'e': time.time_ns(),
|
|
|
|
# #'htu': True
|
|
|
|
# #'e': 1,
|
|
|
|
# # missing 'e'????
|
|
|
|
# }
|
|
|
|
# logger.debug(f"body {body}")
|
|
|
|
# body = plistlib.dumps(body, fmt=plistlib.FMT_BINARY)
|
|
|
|
# from base64 import b64encode
|
|
|
|
# logger.debug(f"Sending message to {participant} with payload {body} and token {b64encode(push_token)}")
|
|
|
|
# self.connection.send_message("com.apple.madrid", body)
|
|
|
|
|
|
|
|
# # Wait for a response
|
|
|
|
# def check_response(x):
|
|
|
|
# if x[0] != 0x0A:
|
|
|
|
# return False
|
|
|
|
# if apns._get_field(x[1], 2) != sha1("com.apple.madrid".encode()).digest():
|
|
|
|
# return False
|
|
|
|
# resp_body = apns._get_field(x[1], 3)
|
|
|
|
# if resp_body is None:
|
|
|
|
# return False
|
|
|
|
# resp_body = plistlib.loads(resp_body)
|
|
|
|
# return True
|
|
|
|
|
|
|
|
# # Wait for a few sec to wait for it
|
|
|
|
# for i in range(10):
|
|
|
|
# payload = self.connection.incoming_queue.wait_pop_find(check_response)
|
|
|
|
# if payload is not None:
|
|
|
|
# break
|
|
|
|
# time.sleep(0.1)
|
|
|
|
|
|
|
|
# if payload is None:
|
|
|
|
# raise Exception("Failed to send message")
|
|
|
|
|
|
|
|
# # Check the response
|
|
|
|
# resp_body = apns._get_field(payload[1], 3)
|
|
|
|
# resp_body = plistlib.loads(resp_body)
|
|
|
|
# logger.error(resp_body)
|
|
|
|
|
|
|
|
|
|
|
|
logger.error(f"Sent {message}")
|
|
|
|
|
|
|
|
def testing(self, message: iMessage):
|
|
|
|
# Set the group id, if it isn't already
|
|
|
|
if message.group_id is None:
|
|
|
|
message.group_id = str(uuid.uuid4()).upper() # TODO: Keep track of group ids?
|
|
|
|
mid = uuid.uuid4()
|
|
|
|
message.id = str(mid).upper()
|
2023-07-28 23:20:32 +00:00
|
|
|
|
|
|
|
# Turn the message into a raw message
|
|
|
|
raw = message.to_raw()
|
|
|
|
|
2023-07-29 17:57:20 +00:00
|
|
|
payload = self._encrypt_sign_payload(self.user.encryption_identity, raw)
|
|
|
|
|
|
|
|
body = {
|
|
|
|
"t": self.connection.token,
|
|
|
|
"P": payload,
|
|
|
|
"c": 101,
|
|
|
|
"E": "pair",
|
|
|
|
"sP": self.user.handles[0],
|
|
|
|
"tP": self.user.handles[0],
|
|
|
|
"U": mid.bytes,
|
|
|
|
'v': 1,
|
|
|
|
'D': True,
|
|
|
|
'e': time.time_ns(),
|
|
|
|
'htu': True
|
|
|
|
}
|
|
|
|
|
|
|
|
#body = {'t': b"\xe5^\xc0c\xe8\xa4\x1e\xbe\x03\x89'\xea\xd5m\x94\x05\xae\xf5\x1bqK\x1aJTH\xa4\xeb8\xb8<\xd7)", 'e': 1690644797594380146, 'tP': 'mailto:testu3@icloud.com', 'U': b'\xbcL\x1fL\x84\x85E\xb8\xb2\x1c\x8d+\xd7\x02-\x0b', 'v': 8, 'P': b"\x02\x01\x1cQT\x03Y\xe4\xa2l;\x8b\x89'#\xb2\xde}\xa5\xc8#\x0b\xeer\xa7\xfc\xf7W\xc5\x9f\xf0\x98\x8dve\xd8?\x04,v\xb1B?@\xce\x15\x1c,\x90\xb6\x91\x96\xe2/\xae?\x86+%\xa30T\x0b[\x90\xde$ED/\xf2\x88#{\xb3\x1d|@\x0fG\xfaV\xc8\x85#\xc45\xcf\x8d\xfd\x96B\x9c\x04\x19\xac\xa0vu\xa1h|A!\x9d\x1a\xd8\xf9\xe9\xe9\xe3\xdf\xe0\xbd\x19}\xcb\xdd\x0b,\xcc\x06S\x9d\x8cag\x82\xb2sa\x9c\xb2%\x16\xfc?\x86\xf6\xcc\x8c\xce\x06\xdf\xe1G\xc5\xf5@\xcc'\x8c\xdcj\xcfpbC\xf6\xcbl\xa4\xde\x8a\xb14\xf1s\x0f\x84\x98\\\xb9~):\x8d\xa6g\xed\tEv\xda\x0e\xc6\x84~d\xf8\x83\xb8\xc9\xec>.(\xa6\x10U\xb6\x80Zr\xbc\xf4\x1c@nc,\x9a\xc5'\x99z\x9c\xc9\xc5-\xba\xe1\xb7\xf1p\xf7\xe4\xa4/am\xde\xecB\xd9(\xec\x1e\xe5\x8f\xd8\xfa7\xca\xa6\xec\xf9\x8b\xeb\xb0\xad\x14*\x05\x17\xb5a0<\x193\xbf\xfc\x12\xcfxf{\xd7\xef\x93\xa3nS\x07\xc9;\xac'!\xb7\x14\x03\xeflZ[G0E\x02!\x00\xb5\x08\xfb\x11\xd5o\x05\xa9\xb2\xa64H\xab/\xcf7;\x97c54a\xea\xc8\x16\x91R\xc2D\x82W\xbf\x02 \nZ\xe6\x9a\xcf\x9do\x17\x9b\xa4\x1d\x11\xf8\x1a\x8c/\t\xf7\xedb\x98,\x8c?\xd2\xb8q\xee\xe5V\xf4", 'sP': 'mailto:testu3@icloud.com', 'E': 'pair', 'htu': True, 'c': 100}
|
|
|
|
|
|
|
|
|
|
|
|
print(body)
|
|
|
|
|
|
|
|
|
|
|
|
body = plistlib.dumps(body, fmt=plistlib.FMT_BINARY)
|
|
|
|
|
|
|
|
self.connection.send_message("com.apple.madrid", body)
|
|
|
|
|
|
|
|
#body2 = plistlib.loads(body)
|
|
|
|
#dec = self._decrypt_payload(body2['P'])
|
|
|
|
|
|
|
|
#print(dec)
|
|
|
|
|
|
|
|
def check_response(x):
|
|
|
|
if x[0] != 0x0A:
|
|
|
|
return False
|
|
|
|
if apns._get_field(x[1], 2) != sha1("com.apple.madrid".encode()).digest():
|
|
|
|
return False
|
|
|
|
resp_body = apns._get_field(x[1], 3)
|
|
|
|
if resp_body is None:
|
|
|
|
return False
|
|
|
|
resp_body = plistlib.loads(resp_body)
|
|
|
|
return True
|
|
|
|
|
|
|
|
for i in range(10):
|
|
|
|
payload = self.connection.incoming_queue.wait_pop_find(check_response)
|
|
|
|
if payload is not None:
|
|
|
|
break
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
|
|
|
|
resp_body = apns._get_field(payload[1], 3)
|
|
|
|
resp_body = plistlib.loads(resp_body)
|
|
|
|
logger.error(resp_body)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # Encrypt the message for each participant
|
|
|
|
# lookup = self.user.lookup(message.participants[:-1])
|
|
|
|
# for participant in message.participants[:-1]:
|
|
|
|
# for identity in lookup[participant]['identities']:
|
|
|
|
# if 'client-data' in identity and 'public-message-identity-key' in identity['client-data'] and 'push-token' in identity:
|
|
|
|
# push_token = identity['push-token']
|
|
|
|
# identity_keys = ids.identity.IDSIdentity.decode(identity['client-data']['public-message-identity-key'])
|
|
|
|
# payload = self._encrypt_sign_payload(identity_keys, raw)
|
|
|
|
# body = {
|
|
|
|
# "t": self.connection.token,
|
|
|
|
# "P": payload,
|
|
|
|
# "c": 100,
|
|
|
|
# "E": "pair",
|
|
|
|
# "sP": self.user.handles[0],
|
|
|
|
# "tP": participant,
|
|
|
|
# "U": mid.bytes,
|
|
|
|
# 'v': 8,
|
|
|
|
# 'D': True,
|
|
|
|
# 'e': time.time_ns(),
|
|
|
|
# 'htu': True
|
|
|
|
# #'e': 1,
|
|
|
|
# # missing 'e'????
|
|
|
|
# }
|
|
|
|
# logger.debug(f"body {body}")
|
|
|
|
# body = plistlib.dumps(body, fmt=plistlib.FMT_BINARY)
|
|
|
|
# from base64 import b64encode
|
|
|
|
# logger.debug(f"Sending message to {participant} with payload {body} and token {b64encode(push_token)}")
|
|
|
|
# self.connection.send_message("com.apple.madrid", body)
|
|
|
|
|
|
|
|
# # Wait for a response
|
|
|
|
# def check_response(x):
|
|
|
|
# if x[0] != 0x0A:
|
|
|
|
# return False
|
|
|
|
# if apns._get_field(x[1], 2) != sha1("com.apple.madrid".encode()).digest():
|
|
|
|
# return False
|
|
|
|
# resp_body = apns._get_field(x[1], 3)
|
|
|
|
# if resp_body is None:
|
|
|
|
# return False
|
|
|
|
# resp_body = plistlib.loads(resp_body)
|
|
|
|
# return True
|
2023-07-29 02:33:29 +00:00
|
|
|
|
2023-07-29 17:57:20 +00:00
|
|
|
# # Wait for a few sec to wait for it
|
|
|
|
# for i in range(10):
|
|
|
|
# payload = self.connection.incoming_queue.wait_pop_find(check_response)
|
|
|
|
# if payload is not None:
|
|
|
|
# break
|
|
|
|
# time.sleep(0.1)
|
|
|
|
|
|
|
|
# if payload is None:
|
|
|
|
# raise Exception("Failed to send message")
|
2023-07-29 02:33:29 +00:00
|
|
|
|
2023-07-29 17:57:20 +00:00
|
|
|
# # Check the response
|
|
|
|
# resp_body = apns._get_field(payload[1], 3)
|
|
|
|
# resp_body = plistlib.loads(resp_body)
|
|
|
|
# logger.error(resp_body)
|
2023-07-29 02:33:29 +00:00
|
|
|
|
2023-07-28 23:20:32 +00:00
|
|
|
|
2023-07-29 17:57:20 +00:00
|
|
|
# logger.error(f"Sent {message}")
|