Reversing SMART Health Cards
TLDR: Data stored in the SMART Health Cards is digitally signed but it is not encrypted. Anyone who scan the QR code will be able to retrieve your full name, date of birth and information on your vaccination (including the date and location).
Some people in Quebec started receiving proof of vaccination against COVID-19 in the format of a QR code. There was not a huge amount of details on the code at the time of the writing.
So, is it a good idea to print your proof of vaccination on a T-shirt? I guess it’s time to analyse one.
High Level Analysis
Note: We will be analysing this fictive QR code in this post.
The first step is to read the data stored within the QR code (I used this online decoder, but you should use a local QR reader if you want to read your own code).
The decoded data starts with the prefix
shc:/ followed by a blob of digits.
shc mean? Doing a simple google search for
shc:/ QR code returns a known framework.
Analysing the SHC Framework
Here’s a simplified version of the general flow to create QR code:
- Create a JWS payload with the patient information.
- Sign the JWS.
- Transform JWS into a numeric QR.
- Generate the QR code.
We will only focus on the step 2 and 3 since the others are implicit.
signJws function will stringify, deflate and then sign the client data.
Looking at the RFC 7515 for the JWS Compact Serialization, it shows that there are three sections: header, payload and signature. Each section is base64url encoded but no other transformation (such as encryption) is done on the data.
The last step before generating the QR code is to transform the JWS to a numeric QR. The
toNumeric function is a bit complicated but in the end, it only stores each character of the JWS in two-digits format and the prefix
shc:/ with the chunk index. In our example, there’s only one chunk so it will return an array that looks like this:
There are two questions that remain in the aforementioned code:
- Why are we subtracting 45 to the decimal value of each char?
- What does
.flatMap((c) => [Math.floor(c/10), c % 10])do?
If we look at an example, the decimal value of the character
z is 122. When subtracting 45, it will become 77. It’s now possible to store the value
z on two digits instead of three. But why 45 and not 42? It could have been 42 and all the characters used after base64url encoding the JWS would have been stored on two digits. They used 45 because
- is the smallest character used when encoding data with base64url.
flatMap section is used to left pad the values under 10 with a 0, thus storing them also on two digits. Fun fact, if they chose to subtract the decimal value by 35 instead of 45, they could have removed this code.
Personally, I would have stored the JWS in the hexadecimal format but they must have their reasons. ¯\_(ツ)_/¯
Now that we know each step used to generate the QR code, we can write a decoder.
Writing a decoder
First, we need to revert the changes from the
toNumericQr call. To do so, we will:
- Split all the digits in groups of two characters.
- Convert each group to an integer.
- Add 45 to retrieve to the original char code
- Cast it as a char.
Next, we can base64 decode all the parts of the JWS.
Finally, we can decompress the data section. Since deflateRaw was used to created raw data without a wrapper (header and adler32 crc) and the zlib module in Python does not support it, we need a workaround. Fortunately, it’s possible to pass the parameter
wbits=-15 to emulate a raw decompression.
Some interesting information can be found such as the full name and date of birth of the patient.
There’s also details on the vaccine received.
"display": "ABC General Hospital"
"lotNumber": "Lot #0000001"
Finally, the CVX code can then be used to find out which vaccine has been injected.
Since there’s some personal information in the QR code, you should share it only with trusted entities. Also, I would avoid to print it on a T-shirt. 😉
I expect an official tool to be released soon but the full code shown in this post is available here: https://github.com/marcan2020/shc-decoder-poc.
Another project exists to decode an SHC: https://github.com/fproulx/shc-covid19-decoder.
Update: I just found out a notebook that explains the SHC Framework in depth: health-cards-walkthrough.