"""Verify every uncompressed 64 KiB payload hash in an MSIX block map.""" import base64 import hashlib import json from pathlib import Path import sys import zipfile from xml.etree import ElementTree as ET from urllib.parse import unquote package=Path(sys.argv[1]);count=0 with zipfile.ZipFile(package) as archive: names={unquote(name):name for name in archive.namelist()} tree=ET.fromstring(archive.read('AppxBlockMap.xml')) for file in tree: name=file.attrib['Name'].replace('\\','/') with archive.open(names[name]) as payload: size=0;whole=hashlib.sha256() for block in file: if block.tag.endswith('FileHash'): assert base64.b64encode(whole.digest()).decode()==block.attrib['Hash'],name continue chunk=payload.read(65536);size+=len(chunk) whole.update(chunk) assert base64.b64encode(hashlib.sha256(chunk).digest()).decode()==block.attrib['Hash'],name assert not payload.read(1),name assert size==int(file.attrib['Size']),name count+=1 assert 'AppxSignature.p7x' in archive.namelist(),'Development package is unsigned' print(json.dumps({'package':str(package),'payloads':count,'blockHashesValid':True, 'sha256':hashlib.sha256(package.read_bytes()).hexdigest()},indent=2))