LoRaWAN: session keys
City warehouses operate on a LoRaWAN 1.0.3 (OTAA, EU868) network. The provider's provisioning table (provisioning.csv) accidentally ended up in a public storage: the DevEUI, AppEUI and AppKey of 50 devices. At that time the gateway logged the traffic in the Semtech UDP protocol (gateway.jsonl): each line is either rxpk (uplink) or txpk (downlink); data is a base64-encoded PHYPayload. Both are in lorawan.zip.
Notes: the DevEUI/AppEUI entries in the CSV are big-endian, while over the air they are little-endian. The network encrypts the Join Accept with the AES decrypt operation (the device decrypts it with encrypt), and MIC = the 4 bytes of AES-CMAC(AppKey, MHDR | AppNonce | NetID | DevAddr | DLSettings | RxDelay | [CFList]). Session keys: NwkSKey = aes128_encrypt(AppKey, 0x01 | AppNonce | NetID | DevNonce | pad), 0x02 for AppSKey (pad = zeros up to 16 bytes; fields in over-the-air order). The Join Accept is sent in the RX1 window, 5 seconds after the Join Request (tmst in microseconds). The uplink MIC and FRMPayload encryption follow LoRaWAN 1.0.3 §4.4 / §4.3.3 (the B0 and Ai blocks, 32-bit FCnt).
Devices sometimes send a Join Request that goes unanswered, and some rejoin (a new DevAddr, new keys, FCnt from 0). The device whose name is exactly vault-door-01 (a warehouse door controller) sends application data in fragments on FPort 42: each fragment is 0x42 | indeks (1 bayt) | jami bo'laklar (1 bayt) | 8 bayt matn. Fragments may arrive out of order, be re-sent (the same FCnt) and be spread across different sessions. Another device with a similar name (...-staging) also sends test text in the same format.
Task: join the vault-door-01 fragments to obtain the flag: BAYROQ{...} (the last 16 characters are lowercase hex).