Skip to content

Instantly share code, notes, and snippets.

@apiraino
Created January 23, 2018 21:19
Show Gist options
  • Save apiraino/db364a32bc0e6fb2694163fa460e2d6c to your computer and use it in GitHub Desktop.
Save apiraino/db364a32bc0e6fb2694163fa460e2d6c to your computer and use it in GitHub Desktop.
Convert XML file from Firefox (pre-Quantum) extension --> Keepass import CSV
#!/usr/bin/env python3
# Convert XML file from the firefox (pre-Quantum) extension
# into a CSV suitable to be imported into keepassxc
# Notes:
# - Python3 only!
# - password must be exported in plain text
# - requires defusedxml package ("pip install defusedxml")
import csv
import os
import sys
from urllib.parse import urlsplit
from defusedxml import ElementTree as ET
if len(sys.argv) != 3:
print('Usage: python3 pwd_exporter.py source_file.xml dest_file.csv')
exit(0)
if not os.path.exists(sys.argv[1]):
print('Sorce file {} does not exist'.format(sys.argv[1]))
exit(0)
IFILE = sys.argv[1]
OFILE = sys.argv[2]
csv_fields = ['Group', 'Title', 'Username', 'Password', 'URL', 'Notes']
if os.path.exists(OFILE):
os.unlink(OFILE)
tree = ET.parse(IFILE)
with open(OFILE, 'w') as fp:
writer = csv.DictWriter(fp, csv_fields, extrasaction='ignore',
quoting=csv.QUOTE_ALL)
writer.writeheader()
count = 0
for e in tree.findall('.//entry'):
host = e.get('host')
_, netloc, _, _, _ = urlsplit(host)
title = netloc.replace('www.', '') or 'no title guessed'
user = e.get('user')
pwd = e.get('password')
record = {
'Group': 'imported_logins',
'Title': title,
'URL': host,
'Username': user,
'Password': pwd,
}
writer.writerow(record)
count += 1
print('DONE: converted {} passwords'.format(count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment