-
-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encryption support #9
Comments
I had a look at the encryption for crypt14. The encrypted database seems to consist of a header (about 190 bytes), the actual encrypted SQLite database and a footer (of 32 bytes). How can I determine/calculate these trailing bytes? I read that for crypt12 it is 20 bytes (md5 hash and the last two digits of your account). |
There is no footer in .crypt14-15 files. (edit: false, see below) |
You are absolutely right. I investigated this further and found that the footer came from the compression (probably different settings for zlib). The footer is irrelevant. |
Overview of what needs to be done:
I'm going by memory so I might be wrong. The code is the truth. |
Hi ElDavoo, I had some time to investigate this a bit further. It seems that the last 32 bytes of the crypt14 file are not needed for the decryption/extraction. Just shrink your crypt14 file by 32 bytes and then decrypt it with your algorithm. You will end up with the same sqlite database. Another way of looking at it is the following. Compare the following:
Here is some code that you can use to check this. Of course you need to first identify the offset and the IV (here 191 and 67). You could use your script with the parameter --no-protobuf.
Note, if you use re-encrypted_msgstore.db.crypt14 in WhatsApp to restore your database then it will fail. Those 32 bytes seem to make the difference. Here is a guide that I follow in order to restore from a local backup. https://www.quora.com/If-I-have-both-a-Google-Drive-and-a-local-backup-of-WhatsApp-chats-how-do-I-ensure-WhatsApp-picks-up-the-local-version-to-restore-when-I-reinstall This means it makes no sense to implement the encryption unless we know the definition of those last 32 bytes. I tried various hashes with no luck. |
Interesting findings, thanks. |
Yes, this how I see the situation. The last 32 bytes of the crypt14 file are probably either a checksum or an encrypted checksum. But those bytes are not needed to unpack the sqilte database. It would be great if you can confirm this. I only analysed one database. |
I also looked at zstandard (a zlib implementation from Meta) in order to see whether this is a better fit than zlib with level 1 compression.... no luck here. Also I found/confirmed that the bytes 33 to 36 counted from the end of the decrypted file coincide with the adler32 checksum....another indication that zlib ends there... and the last 32 bytes are something else.. |
The last 16 bytes of the backup are the MD5 of the encrypted file, including the header. In crypt12 files, the user'jid was also taken into account (re-added at the end as a suffix in clear (the last four bytes)) |
To make things further complicated, it seems like the checksum is not added in all the backups files (stickers and wallpapers), but the script has no knowledge of which file is decrypting (whatsapp knows it thanks to the file name), so I won't make the checksum check an error and will add an explanation |
Ok, now the scripts check the MD5 at the end of the file. You should now be able to generate a file that both the script and the official app should mark as correct by just appending the checksum of everything you have written. While your tests found out you can delete up to 32 bytes, i'm pretty sure there are no further footer information - just the md5 |
Thank you so much for all the changes. I only found the time to read your changes. I am not sure yet whether and when I can test the encryption. |
No worries, we took months to even notice there was a checksum at the end |
I gave it a shot this morning with the re-encryption. I used the md5 sum of the header and the encrypted sqlite database as the footer (like you found out). Unfortunately, the restore process in WhatsApp with this re-encrypted file failed. I noticed however, that I got a little further than before. When you use a file without a footer, the restore process stops right at the beginning (at 0%). Now, with this new md5 footer the restore process starts, but then stops at 24%. |
24%? strange |
You side a progress bar. It is not my estimate. And then I used the official backup to restore. It run from 0 to 24%. Paused a little and then jumped immediately to 100%. |
only way we can understand what's going on is seeing the application logs/logcat |
My phone is not rooted so I need to see how I can get access to the logs. A pro could potentially also decompile the app itself. I will probably compare some backup files by looking at bytes "-32 to -17". Maybe I can see something there. And also try some hashes.... Unfortunately, I have to go now. Maybe I find some time tonight, |
I had no luck so far with my analysis of the "missing" bytes, i.e. decrypted_data[-32:-16]. However, I again analysed decrypted_data[-36:-32]. And here I confirmed for 16 different crypt14 database files that this is the adler32 checksum. You can see that by comparing int.from_bytes(decrypted_data[-36:-32], "big") with zlib.adler32(decompressed_data). |
This issue was annoying me so much and I could not let go. Finally, I found the solution. Only just by chance. The crypt14 file consists of a header (as you know it), the encrypted compressed database, an authentication tag and finally the md5 checksum of all bytes before. Here, the tag is 16 bytes long. Or put differently use |
Excellent work! I will read what is an auth tag tomorrow (since I do not know) then i will implement the verification in the script (which should be easy to do). So you are able to perfectly re-encrypt now? |
Yes. I somehow came across the following post which triggered my eureka moment. |
The decrypt script is very large, more than 800 lines. Moreover, as you can guess, the encrypt script would share lots of code with the decrypt script. So i'm afraid that I need to let go of the "one autonomous python file that can do everything on its own" and split the project in more files |
@georg-lam btw, did you know that you can just feed an unencrypted backup to whatsapp and it should restore it? |
I tried that but it did not work for me. Do you have any instructions for it? At the moment I am doing it the following way: |
No. You should just copy the msgstore.db file in the database folder, it should just work. If it doesn't, myth busted. |
First experimental encryptor pushed into the encryption branch. I could recreate an identical msgstore.db.crypt15 file. However, there is a minor problem and a major one: |
For the zlib stream you could play around with the compression level and the wbit parameter. For the crypt14 files level = 1 was the correct setting. Yes, I agree you need a reference encrypted file. Otherwise it is too complicated. |
Yes. @georg-lam Congrats, nice work for both of you. If I'm not mistaken, you managed to perfectly re-encrypt a crypt14 file, correct ? |
Yes, bit for bit. |
@georg-lam What code have you used? The encrypt.py in the encryption branch does not seem to have crypt14 support. |
As pointed out above you can either construct the header from scratch (more difficult) or use an existing one. So do the following. |
@courious875 support for crypt12 encryption implement in da3bca7 . |
This comment was marked as off-topic.
This comment was marked as off-topic.
I didn't manage to create an identical zlib stream. However, Whatsapp accepted my modified crypt15 without complaining :) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Encryption support officially pushed, in "beta". Might still keep this issue open until it's final, but unpinning it. |
@ElDavoo Do you mean on a rooted phone putting it into the /data folder, or for restoring a backup on a non-rooted phone? Some user on reddit also recently stated that they were not able to restore an unencrypted msgstore.db on a non-rooted phone, and I was wondering on this. |
I was talking about the latter, but I later found out it's false |
I really don't have the time and the need to do that, but now that the file formats are documented and almost 100% parsed, it is now definitely possible to create an encrypter. If someone else wants to do that I will happily accept a PR.
The text was updated successfully, but these errors were encountered: