Extracting WhatsApp messages from database on Android

Prerequisites

Activate developer mode and enable USB debugging. Connect the phone via USB to a computer that has adb installed.

Create unencrypted backup

We temporarily downgrade to a version of the WhatsApp app that still supports adb backup. This way, we do not need to root our phone.

adb devices
adb shell getprop ro.build.version.sdk
adb shell am force-stop com.whatsapp
adb shell pm path com.whatsapp
adb shell cp {whatsapp_apk_path} /data/local/tmp/WhatsAppbackup.apk  # TODO also cp the two other files?
adb ls /data/local/tmp
adb shell pm uninstall -k com.whatsapp
adb reboot
adb devices
adb install -r -g --bypass-low-target-sdk-block LegacyWhatsApp.apk
adb shell am start -n com.whatsapp/.Main  # ignore any warnings you get
adb backup -f whatsapp.ab com.whatsapp
adb shell pm uninstall -k com.whatsapp
adb shell pm install /data/local/tmp/WhatsAppbackup.apk  # or just reinstall from Play Store

Extract backup

Using Android Backup Extractor (ABE)

curl https://github.com/YuvrajRaghuvanshiS/WhatsApp-Key-Database-Extractor/blob/master/bin/abe.jar -o abe.jar
# or via <https://github.com/nelenkov/android-backup-extractor/releases/tag/latest>
java -jar abe.jar unpack whatsapp.ab whatsapp.tar {backup_password}
tar -xf whatsapp.tar
mkdir extract
cp apps/com.whatsapp/f/key extract/key  # encryption key
cp apps/com.whatsapp/db/msgstore.db extract/msgstore.db  # decrypted messages
cp apps/com.whatsapp/db/wa.db extract/wa.db  # decrypted contact info

View backup

git clone https://github.com/absadiki/whatsapp-msgstore-viewer
cd whatsapp-msgstore-viewer
conda create -n venv-whatsapp python=3.9
conda activate venv-whatsapp
pip install -r requirements.txt
python main.py