Skip to content

Instantly share code, notes, and snippets.

@dautovri
Created March 7, 2025 11:31
Show Gist options
  • Select an option

  • Save dautovri/0457dd64c5f1dd34a47b27bd71039959 to your computer and use it in GitHub Desktop.

Select an option

Save dautovri/0457dd64c5f1dd34a47b27bd71039959 to your computer and use it in GitHub Desktop.
Apple Guides decoding
#!/usr/bin/env python3
import base64
import re
import binascii
import sys
import urllib.parse
from datetime import datetime
from dataclasses import dataclass
from typing import List, Optional
try:
from google.protobuf.internal import decoder
HAS_PROTOBUF = True
except ImportError:
HAS_PROTOBUF = False
print("Warning: google.protobuf not installed. Using fallback parsing method.")
@dataclass
class AppleGuideLocation:
maps_id: str
index: int
raw_bytes: bytes
@dataclass
class AppleGuide:
name: str
locations: List[AppleGuideLocation]
raw_bytes: bytes
def extract_apple_maps_id(data: bytes) -> str:
"""Extract Apple Maps ID from binary data"""
# Convert to hex and format according to observed pattern
hex_str = binascii.hexlify(data).decode('ascii').upper()
return 'I' + hex_str
def decode_apple_guide_fallback(encoded_data: bytes) -> AppleGuide:
"""Decode the guide data using pattern matching"""
data = encoded_data
# Extract guide name
name_tag = data[0]
name_length = data[1]
guide_name = data[2:2+name_length].decode('utf-8')
# Starting position after the name
pos = 2 + name_length
# Find location entries
locations = []
idx = 1
while pos < len(data):
# Look for location entry pattern (typically starts with 0x12)
if pos + 1 < len(data) and data[pos] == 0x12:
entry_tag = data[pos]
entry_length = data[pos+1] # Length of this entry
pos += 2
# Find the recurring marker "Irk0Q" (or similar)
marker_pos = pos
while marker_pos < pos + entry_length - 4:
if data[marker_pos:marker_pos+3] == b'\x08\xae\x4d':
# Found marker, now extract the ID data that follows
id_start = marker_pos + 3
id_data = data[id_start:pos+entry_length]
# Extract the Apple Maps ID
if len(id_data) >= 8:
# Take last 8 bytes for the ID
maps_id_bytes = id_data[-8:]
maps_id = extract_apple_maps_id(maps_id_bytes)
locations.append(AppleGuideLocation(
maps_id=maps_id,
index=idx,
raw_bytes=id_data
))
idx += 1
break
marker_pos += 1
pos += entry_length
else:
# Skip unrecognized byte
pos += 1
return AppleGuide(name=guide_name, locations=locations, raw_bytes=encoded_data)
def decode_apple_guide_protobuf(encoded_data: bytes) -> AppleGuide:
"""Decode using Protocol Buffers if available"""
if not HAS_PROTOBUF:
raise RuntimeError("Protocol Buffers library not available")
# This is a placeholder implementation
# A complete implementation would use the proper protobuf schema
# For now, fallback to the pattern-matching approach
return decode_apple_guide_fallback(encoded_data)
def decode_apple_guides_url(url: str) -> Optional[AppleGuide]:
"""Decode an Apple Guides URL"""
# Extract the 'ug' parameter from the URL
matches = re.search(r'[?&]ug=([^&]+)', url)
if not matches:
print(f"No 'ug' parameter found in URL: {url}")
return None
encoded_parameter = matches.group(1)
# URL decode the parameter
url_decoded = urllib.parse.unquote(encoded_parameter)
# Base64 decode the parameter
try:
binary_data = base64.b64decode(url_decoded)
print(f"Successfully decoded {len(binary_data)} bytes")
except Exception as e:
print(f"Error decoding base64 data: {e}")
return None
# Try protocol buffer parsing first, fall back if needed
if HAS_PROTOBUF:
try:
return decode_apple_guide_protobuf(binary_data)
except Exception as e:
print(f"Protocol buffer parsing failed: {e}")
# Fallback to manual parsing
return decode_apple_guide_fallback(binary_data)
def print_guide(guide: AppleGuide) -> None:
"""Print a formatted representation of the guide"""
print(f"Guide Name: {guide.name}")
for location in guide.locations:
print(f" Apple Maps ID {location.index} : {location.maps_id}")
def main():
if len(sys.argv) > 1:
url = sys.argv[1]
else:
url = input("Enter Apple Guides URL: ")
print(f"Processing URL: {url}")
encoded_param = re.search(r'[?&]ug=([^&]+)', url).group(1)
decoded_param = urllib.parse.unquote(encoded_param)
print(f"Decoded URL parameter: {decoded_param}")
guide = decode_apple_guides_url(url)
if guide:
print_guide(guide)
if __name__ == "__main__":
# Format date and time as YYYY-MM-DD HH:MM:SS
print(f"Current Date and Time (UTC - YYYY-MM-DD HH:MM:SS formatted): {datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"Current User's Login: {sys.argv[2] if len(sys.argv) > 2 else 'unknown'}")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment