Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sheeeng/561957fa22c20801d8c82862f71cc4be to your computer and use it in GitHub Desktop.
Save sheeeng/561957fa22c20801d8c82862f71cc4be to your computer and use it in GitHub Desktop.
Recovering Google Authenticator keys from Android device for backup
### Last tested February 7 2014 on a Galaxy S3 (d2att) running Cyanogenmod 11 nightly, with Google Authenticator 2.49.
### Device with Google Authenticator must have root.
### Computer requires Android Developer Tools and SQLite 3.
### Connect your device in USB debugging mode.
$ cd /tmp
$ adb root
$ adb pull /data/data/com.google.android.apps.authenticator2/databases/databases
$ sqlite3 ./databases "select * from accounts" > /Volumes/TRUECRYPT_ENCRYPTED_VOLUME/google_authenticator_backup.txt
$ rm ./databases
### If you look at the file, you see a pipe-delimited file with entries looking like the following.
### The X's mark the key.
1|Google:me@gmail.com|XXXXXXXXXXXXXXXXXXXXXXXX|0|0|0||
2|Google:me@domain.org|XXXXXXXXXXXXXXXXXXXXXXXX|0|0|0|Google|Google:me@domain.org
3|Dropbox:me@gmail.com|XXXXXXXXXXXXXXXXXXXXXXXX|0|0|0|Dropbox|Dropbox:me@gmail.com
### To restore the keys, you can key them in manually in Google Authenticator:
### Menu -> Set up account -> Enter provided key.
### Enter the key exactly as it appears, case sensitive, and choose Time-based.
@sheeeng
Copy link
Author

sheeeng commented Aug 31, 2016

Another useful tip from the comment in the topic.

import qrcode
import sqlite3
conn = sqlite3.connect('databases')
c = conn.cursor()

for idx, (email, secret, issuer) in enumerate(c.execute("SELECT email,secret,issuer FROM accounts").fetchall()):
    url = 'otpauth://totp/{}?secret={}&issuer={}'.format(email, secret, issuer)
    print url
    im = qrcode.make(url)
    im.save('{}.png'.format(idx))

@sheeeng
Copy link
Author

sheeeng commented Sep 15, 2016

Fix missing dependencies

Traceback (most recent call last):
  File "generate.py", line 1, in <module>
    import qrcode
ImportError: No module named qrcode
sudo pip install qrcode
Traceback (most recent call last):
  File "generate.py", line 9, in <module>
    im = qrcode.make(url)
  File "/Library/Python/2.7/site-packages/qrcode/main.py", line 11, in make
    return qr.make_image()
  File "/Library/Python/2.7/site-packages/qrcode/main.py", line 271, in make_image
    from qrcode.image.pil import PilImage
  File "/Library/Python/2.7/site-packages/qrcode/image/pil.py", line 8, in <module>
    import Image
ImportError: No module named Image
sudo pip install pillow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment