commit b7df7a3b1e83d596308c6b3fca944e38bcd57a6a Author: JJTech0130 Date: Wed Apr 5 19:52:14 2023 -0400 initial version :tada: diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0ef0f7a --- /dev/null +++ b/.gitignore @@ -0,0 +1,164 @@ +# APNS keys +push.crt +push.key + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ \ No newline at end of file diff --git a/albert.py b/albert.py new file mode 100644 index 0000000..501a471 --- /dev/null +++ b/albert.py @@ -0,0 +1,102 @@ +import plistlib +import re +import uuid +from base64 import b64decode, b64encode + +import requests +from cryptography import x509 +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import padding, rsa +from cryptography.x509.oid import NameOID + +# These can probably be any valid chain and key in a similar format? Seems like a CoreTrust-like bug in validation? +# I got them from https://github.com/MiUnlockCode/albertsimlockapple/blob/main/ALBERTBUGBYMIUNLOCK.php +FAIRPLAY_PRIVATE_KEY = b64decode( + "LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlDV3dJQkFBS0JnUUMzQktyTFBJQmFiaHByKzRTdnVRSG5iRjBzc3FSSVE2Ny8xYlRmQXJWdVVGNnA5c2RjdjcwTityOHlGeGVzRG1wVG1LaXRMUDA2c3pLTkFPMWs1SlZ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A1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBQr0GlHlHYJ/vRrjS5ApvdHTX8IXjAfBgNVHSMEGDAWgBQr0GlHlHYJ/vRrjS5ApvdHTX8IXjCCAREGA1UdIASCAQgwggEEMIIBAAYJKoZIhvdjZAUBMIHyMCoGCCsGAQUFBwIBFh5odHRwczovL3d3dy5hcHBsZS5jb20vYXBwbGVjYS8wgcMGCCsGAQUFBwICMIG2GoGzUmVsaWFuY2Ugb24gdGhpcyBjZXJ0aWZpY2F0ZSBieSBhbnkgcGFydHkgYXNzdW1lcyBhY2NlcHRhbmNlIG9mIHRoZSB0aGVuIGFwcGxpY2FibGUgc3RhbmRhcmQgdGVybXMgYW5kIGNvbmRpdGlvbnMgb2YgdXNlLCBjZXJ0aWZpY2F0ZSBwb2xpY3kgYW5kIGNlcnRpZmljYXRpb24gcHJhY3RpY2Ugc3RhdGVtZW50cy4wDQYJKoZIhvcNAQEFBQADggEBAFw2mUwteLftjJvc83eb8nbSdzBPwR+Fg4UbmT1HN/Kpm0COLNSxkBLYvvRzm+7SZA/LeU802KI++Xj/a8gH7H05g4tTINM4xLG/mk8Ka/8r/FmnBQl8F0BWER5007eLIztHo9VvJOLr0bdw3w9F4SfK8W147ee1Fxeo3H4iNcol1dkP1mvUoiQjEfehrI9zgWDGG1sJL5Ky+ERI8GA4nhX1PSZnIIozavcNgs/e66Mv+VNqW2TAYzN39zoHLFbr2g8hDtq6cxlPtdk2f8GHVdmnmbkyQvvY1XGefqFStxu9k0IkEirHDx22TZxeY8hLgBdQqorV2uT80AkHN7B1dSE=" +) + + +def _generate_csr(private_key: rsa.RSAPrivateKey) -> str: + csr = ( + x509.CertificateSigningRequestBuilder() + .subject_name( + x509.Name( + [ + x509.NameAttribute(NameOID.COUNTRY_NAME, "US"), + x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "CA"), + x509.NameAttribute(NameOID.LOCALITY_NAME, "Cupertino"), + x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Apple Inc."), + x509.NameAttribute(NameOID.ORGANIZATIONAL_UNIT_NAME, "iPhone"), + x509.NameAttribute(NameOID.COMMON_NAME, str(uuid.uuid4())), + ] + ) + ) + .sign(private_key, hashes.SHA256()) + ) + return csr.public_bytes(serialization.Encoding.PEM).decode("utf-8") + + +# Generates an APNs push certificate by talking to Albert +# Returns [private key PEM, certificate PEM] +def generate_push_cert() -> tuple[str, str]: + private_key = rsa.generate_private_key( + public_exponent=65537, key_size=2048, backend=default_backend() + ) + csr = _generate_csr(private_key) + + activation_info = { + "ActivationRandomness": str(uuid.uuid4()), + "ActivationState": "Unactivated", + "BuildVersion": "10.6.4", + "DeviceCertRequest": csr.encode("utf-8"), + "DeviceClass": "Windows", + "ProductType": "windows1,1", + "ProductVersion": "10.6.4", + "SerialNumber": "WindowSerial", + "UniqueDeviceID": str(uuid.uuid4()), + } + + activation_info = plistlib.dumps(activation_info) + + # Load the private key + fairplay_key = serialization.load_pem_private_key( + FAIRPLAY_PRIVATE_KEY, password=None, backend=default_backend() + ) + + # Sign the activation info + signature = fairplay_key.sign(activation_info, padding.PKCS1v15(), hashes.SHA1()) + + body = { + "ActivationInfoComplete": True, + "ActivationInfoXML": activation_info, + "FairPlayCertChain": FAIRPLAY_CERT_CHAIN, + "FairPlaySignature": signature, + } + + resp = requests.post( + "https://albert.apple.com/WebObjects/ALUnbrick.woa/wa/deviceActivation?device=Windows", + data={"activation-info": plistlib.dumps(body)}, + ) + + protocol = re.search("(.*)", resp.text).group(1) + protocol = plistlib.loads(protocol.encode("utf-8")) + + return ( + private_key.private_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PrivateFormat.TraditionalOpenSSL, + encryption_algorithm=serialization.NoEncryption(), + ).decode("utf-8").strip(), + protocol["device-activation"]["activation-record"]["DeviceCertificate"].decode( + "utf-8" + ).strip(), + ) + +if __name__ == "__main__": + private_key, cert = generate_push_cert() + print(private_key) + print(cert) \ No newline at end of file diff --git a/apns.py b/apns.py new file mode 100644 index 0000000..08503cb --- /dev/null +++ b/apns.py @@ -0,0 +1,101 @@ +from __future__ import annotations + +class Fields: + @staticmethod + def from_bytes(data: bytes) -> Fields: + fields = {} + + while len(data) > 0: + field = data[0] + length = int.from_bytes(data[1:3], "big") + value = data[3:3 + length] + + fields[field] = value + + data = data[3 + length:] + + return Fields(fields) + + def __init__(self, fields: dict[int, bytes]): + self.fields = fields + + def to_bytes(self) -> bytes: + buffer = bytearray() + + for field, value in self.fields.items(): + buffer.append(field) + buffer.extend(len(value).to_bytes(2, "big")) + buffer.extend(value) + + return buffer + + # Debug formating + def __str__(self) -> str: + return f"{self.fields}" + +# Define number to command name mapping +COMMANDS = { + 0x7: "Connect", + 0x8: "ConnectResponse", + 0x9: "PushTopics", + 0x0A: "PushNotification", +} + +class Payload: + @staticmethod + def from_stream(stream) -> Payload|None: + command = int.from_bytes(stream.read(1), "big") + if command == 0: + return None # We reached the end of the stream + length = int.from_bytes(stream.read(4), "big") + fields = Fields.from_bytes(stream.read(length)) + + return Payload(command, fields) + + @staticmethod + def from_bytes(data: bytes) -> Payload: + # Convert it to bytes for cleaner printing + data = bytes(data) + command = data[0] + length = int.from_bytes(data[1:5], "big") + fields = Fields.from_bytes(data[5:5 + length]) + + return Payload(command, fields) + + def __init__(self, command: int, fields: Fields): + self.command = command + self.fields = fields + + def to_bytes(self) -> bytes: + buffer = bytearray() + + buffer.append(self.command) + + fields = self.fields.to_bytes() + + buffer.extend(len(fields).to_bytes(4, "big")) + buffer.extend(fields) + + return buffer + + # Debug formating + def __str__(self) -> str: + return f"{COMMANDS[self.command]}: {self.fields}" + +if __name__ == "__main__": + import courier + sock = courier.connect() + payload = Payload(7, Fields({2: 0x01.to_bytes()})) + sock.write(payload.to_bytes()) + print("recieved: ", Payload.from_stream(sock)) + print("recieved: ", Payload.from_stream(sock)) + sock.close() +# with socket.create_connection((COURIER_HOST, COURIER_PORT)) as sock: +# with context.wrap_socket(sock, server_hostname=COURIER_HOST) as ssock: +# payload = Payload(7, Fields({2: 0x01.to_bytes()})) +# #print(payload) +# #print(payload.to_bytes()) +# #print(Payload.from_bytes(payload.to_bytes())) +# ssock.write(payload.to_bytes()) +# print("recieved: ", Payload.from_stream(ssock)) +# print("recieved: ", Payload.from_stream(ssock)) \ No newline at end of file diff --git a/courier.py b/courier.py new file mode 100644 index 0000000..cdc667c --- /dev/null +++ b/courier.py @@ -0,0 +1,47 @@ +import albert +import tlslite +import socket + +COURIER_HOST = "10-courier.push.apple.com" +COURIER_PORT = 5223 +#ALPN = [b"apns-security-v2"] +ALPN = None + +# Check if we have already generated a push certificate +# If not, generate one +def _setup_push_cert(): + try: + with open("push.key", "r") as f: + private_key = f.read() + with open("push.crt", "r") as f: + cert = f.read() + except FileNotFoundError: + private_key, cert = albert.generate_push_cert() + with open("push.key", "w") as f: + f.write(private_key) + with open("push.crt", "w") as f: + f.write(cert) + + return private_key, cert + +def connect(): + private_key, cert = _setup_push_cert() + + # Connect to the courier server + sock = socket.create_connection((COURIER_HOST, COURIER_PORT)) + # Wrap the socket in TLS + sock = tlslite.TLSConnection(sock) + # Parse the certificate and private key + cert = tlslite.X509CertChain([tlslite.X509().parse(cert)]) + private_key = tlslite.parsePEMKey(private_key, private=True) + # Handshake with the server + sock.handshakeClientCert(cert, private_key, alpn=ALPN) + + return sock + +if __name__ == "__main__": + sock = connect() + sock.write(b"Hello World!") + print(sock.read()) + sock.close() +