Skip to content

Instantly share code, notes, and snippets.

@notjuliee
Last active November 9, 2017 04:48
Show Gist options
  • Save notjuliee/8a9a7f6e580d75203b044195b6e21564 to your computer and use it in GitHub Desktop.
Save notjuliee/8a9a7f6e580d75203b044195b6e21564 to your computer and use it in GitHub Desktop.
Dump all SMS/MMS messages from Android to a JSON file (Requires root)
#!/bin/bash
set -e
echo "Starting SMS Backup"
echo "Downloading MMS/SMS DB"
adb shell su -c "cp /data/user_de/0/com.android.providers.telephony/databases/mmssms.db /sdcard/SMS_BAK.db"
adb pull /sdcard/SMS_BAK.db in.db &> /dev/null
adb shell rm /sdcard/SMS_BAK.db
echo "Parsing DB"
printf 'from sqlite3 import connect\nfrom json import dumps\nc = connect("in.db").cursor()\nc.execute("SELECT address FROM addr WHERE _id=1;")\nnum = c.fetchall()[0][0]\ndef getsms(i):\n\treturn {"date":i[1],"body":i[2],"to":i[0] if i[3]==2 else num,"from":num if i[3]==2 else i[0]}\nc.execute("SELECT address,date,body,type FROM sms;")\nout={"num":num,"msgs":[getsms(i) for i in c.fetchall()]}\nopen("sms.json","w+").write(dumps(out))' | python
rm in.db
echo "SMS Dumped to sms.json"
#!/usr/bin/env python3
from sys import argv
from json import loads
from copy import deepcopy
from os import get_terminal_size, remove
from subprocess import run
dat = loads(open(argv[1]).read())
num = dat["num"]
wid = get_terminal_size()[0]
cnvs = {}
for msg in dat["msgs"]:
if msg["from"] == num:
frm = msg["to"]
else:
frm = msg["from"]
if frm not in cnvs:
cnvs[frm] = list()
cnvs[frm].append(msg)
print("Found {} conversations:".format(len(cnvs)))
cnvids = [i for i in cnvs.keys()]
for x, i in enumerate(cnvids):
print("{}) {}".format(x, i))
cid = cnvids[int(input("\nSelect conversation to read: "))]
out = []
for msg in cnvs[cid]:
if msg["from"] == num:
out.append("{}{}".format(" " * (wid - len(msg["body"])), msg["body"]))
else:
out.append(msg["body"])
open("_SMS_TMP.txt", 'w+').write("\n".join(out))
run(["less", "_SMS_TMP.txt"])
remove("_SMS_TMP.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment