#!/usr/bin/env python3 """PS3 PKG file extractor using Python. Decrypts and extracts PS3 PKG files using RAP keys from NoPayStation database. """ import struct import hashlib from pathlib import Path from Crypto.Cipher import AES from Crypto.Util.Padding import unpad class PS3PKGExtractor: """Extract PS3 PKG files.""" # PS3 PKG constants PKG_MAGIC = 0x7F504B47 # "\x7FPKG" def __init__(self, pkg_path: Path, rap_key: str = None): """Initialize extractor. Args: pkg_path: Path to .pkg file rap_key: RAP key as hex string (from NoPayStation) """ self.pkg_path = Path(pkg_path) self.rap_key = rap_key def read_pkg_header(self): """Read PKG file header. Returns: dict with header info """ with open(self.pkg_path, 'rb') as f: # Read header (0x80 bytes) header = f.read(0x80) # Parse header magic = struct.unpack('>I', header[0:4])[0] if magic != self.PKG_MAGIC: raise ValueError(f"Invalid PKG magic: {hex(magic)}") pkg_type = struct.unpack('>H', header[4:6])[0] pkg_info = struct.unpack('>H', header[6:8])[0] header_size = struct.unpack('>I', header[8:12])[0] item_count = struct.unpack('>I', header[12:16])[0] total_size = struct.unpack('>Q', header[16:24])[0] data_offset = struct.unpack('>Q', header[24:32])[0] data_size = struct.unpack('>Q', header[32:40])[0] # Content ID at offset 0x30 content_id = header[0x30:0x67].decode('ascii', errors='ignore').rstrip('\x00') return { 'magic': magic, 'type': pkg_type, 'header_size': header_size, 'item_count': item_count, 'total_size': total_size, 'data_offset': data_offset, 'data_size': data_size, 'content_id': content_id, } def extract_content_id(self): """Extract Content ID from PKG. Returns: str: Content ID (e.g., "UP0102-NPUB31552_00-...") """ info = self.read_pkg_header() return info['content_id'] def extract_title_id(self): """Extract Title ID from Content ID. Returns: str: Title ID (e.g., "NPUB31552") """ content_id = self.extract_content_id() # Content ID format: UP0102-NPUB31552_00-... # Extract the part after first dash, before underscore parts = content_id.split('-') if len(parts) >= 2: title_id = parts[1].split('_')[0] return title_id return None def test_pkg_reading(): """Test PKG file reading.""" import sys if len(sys.argv) < 2: print("Usage: python ps3_pkg_extractor.py /path/to/file.pkg") sys.exit(1) pkg_path = sys.argv[1] print(f"Reading PKG: {pkg_path}\n") extractor = PS3PKGExtractor(pkg_path) try: info = extractor.read_pkg_header() print("PKG Header Info:") print("=" * 60) print(f"Magic: {hex(info['magic'])}") print(f"Type: {hex(info['type'])}") print(f"Header Size: {info['header_size']} bytes") print(f"Item Count: {info['item_count']}") print(f"Total Size: {info['total_size']:,} bytes ({info['total_size'] / (1024**2):.1f} MB)") print(f"Data Offset: {info['data_offset']}") print(f"Data Size: {info['data_size']:,} bytes") print(f"Content ID: {info['content_id']}") title_id = extractor.extract_title_id() print(f"Title ID: {title_id}") except Exception as e: print(f"Error: {e}") import traceback traceback.print_exc() if __name__ == '__main__': test_pkg_reading()