Skip to content

Instantly share code, notes, and snippets.

@bhdouglass
Created August 26, 2019 04:23
Show Gist options
  • Save bhdouglass/649fbd0b6825e6787e513323b672ff73 to your computer and use it in GitHub Desktop.
Save bhdouglass/649fbd0b6825e6787e513323b672ff73 to your computer and use it in GitHub Desktop.
Convert Google Authenticator database to Authenticator-ng Config

Convert Google Authenticator database to Authenticator-ng Config

This script converts the sqlite database form the Google Authenticator app to the config file for Authenticator-ng.

Usage

./convert.py -i /path/to/database -o /path/to/output/config

Google authenticator Database

On android devices the database is located at /data/data/com.google.android.apps.authenticator2/databases/databases

Reference

Google Authenticator account schema

CREATE TABLE accounts ( _id INTEGER PRIMARY KEY, email TEXT NOT NULL, secret TEXT NOT NULL, counter INTEGER DEFAULT 0, type INTEGER, provider INTEGER DEFAULT 0, issuer TEXT DEFAULT NULL, original_name TEXT DEFAULT NULL );

Authenticator-ng format:

[%7Be0af62c9-2f2b-401e-b9af-3287f31a4011%7D] account=Test counter=0 pinLength=6 secret=aaaaaaaaaaaaaaaa timeStep=30 type=totp

#!/usr/bin/env python3
import sqlite3
from uuid import uuid4
from argparse import ArgumentParser
def main():
parser = ArgumentParser()
parser.add_argument(
'--input',
'-i',
help='Path to a Google Authenticator sqlite database file',
required=True,
)
parser.add_argument(
'--output',
'-o',
help='Path output an Authenticator-ng config file',
default=None,
)
args = parser.parse_args()
db = sqlite3.connect(args.input)
cursor = db.cursor()
cursor.execute('SELECT * FROM accounts')
accounts = cursor.fetchall()
config = ''
for account in accounts:
accountType = 'totp'
if account[4] != 0:
accountType = 'hotp'
accountName = account[1]
if account[6]:
accountName = '{}: {}'.format(account[6], account[1])
accountConfig = '''
[%7B{uuid}%7D]
account={account}
counter={counter}
pinLength={pin_length}
secret={secret}
timeStep={time_step}
type={type}
'''.format(
uuid=uuid4(),
account=accountName,
counter=account[3],
pin_length=6,
secret=account[2],
time_step=30,
type=accountType,
)
config += accountConfig
db.close()
if args.output:
with open(args.output, 'w') as f:
f.write(config)
else:
print(config)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment