OTA manifest and signature verification
An OTA (Over-the-Air) update package of an IoT gateway was intercepted: ota_bundle.zip. Inside the archive there are device.cfg, two manifests (manifest.json and manifest.old.json) and three firmware images (images/fw_a.bin, images/fw_b.bin, images/fw_c.bin).
Before applying an update, the device performs a two-stage check:
1. Manifest authenticity (HMAC-SHA256): the hmac field of the manifest is verified using the key ota_psk (an ASCII string) in device.cfg. The canonical text over which the HMAC is computed is built from the following lines (separated by \n, file names in alphabetical order):<version>\nimages/fw_a.bin:<size>:<sha256>\nimages/fw_b.bin:<size>:<sha256>\nimages/fw_c.bin:<size>:<sha256>
2. Image integrity (SHA-256): only the images/fw_*.bin file that truly matches the sha256 hash inside the manifest whose HMAC signature turned out to be correct is genuine (the other two images are corrupted or are decoy images belonging to an old manifest).
Each fw_*.bin image starts with a 24-byte SAMOTA1\0 header: struct.unpack('<8sIIII', data[:24]) → (magic, code_offset, code_size, cfg_offset, cfg_size). Decrypt BAYROQ{...} by XOR-ing the cfg section (data[cfg_offset : cfg_offset + cfg_size]) of the genuine image periodically with the first 16 bytes of the ota_psk string. (Flag format: BAYROQ{OTA_MANIFEST_<16 kichik hex>}).