KEEPASS: mudanças entre as edições
Ir para navegação
Ir para pesquisar
(→Code) |
(→Code) |
||
Linha 31: | Linha 31: | ||
try: | try: | ||
print("Starting Extraction of " + self.dbfile + ".") | |||
with PyKeePass(self.dbfile, password=self.dbkey) as kp: | with PyKeePass(self.dbfile, password=self.dbkey) as kp: | ||
for entry in kp.entries: | for entry in kp.entries: | ||
Linha 41: | Linha 42: | ||
data.append(borda + username + separator + password + separator + title + separator + path + separator + str(len(path)) + separator + notes + separator + str(len(notes)) + borda) | data.append(borda + username + separator + password + separator + title + separator + path + separator + str(len(path)) + separator + notes + separator + str(len(notes)) + borda) | ||
except FileNotFoundError: | except FileNotFoundError: | ||
print("DBFile " + self.dbfile + " Not Found") | print("DBFile " + self.dbfile + " Not Found.") | ||
exit(1) | exit(1) | ||
except construct.core.ChecksumError: | except construct.core.ChecksumError: | ||
print("Could not open " + self.dbfile) | print("Could not open " + self.dbfile + ". Probably bad master password.") | ||
exit(1) | exit(1) | ||
except AttributeError as e: | |||
print("Attribute error when exporting " + self.dbfile + ".") | |||
print(e) | |||
else: | else: | ||
try: | try: | ||
f= open(self.fileobject,"w+") | f= open(self.fileobject,"w+") | ||
except Exception as e: | except Exception as e: | ||
print("Could not create file " + self.fileobject) | print("Could not create file " + self.fileobject + ".") | ||
else: | else: | ||
for i in data: | for i in data: | ||
f.writelines(i + "\n") | f.writelines(i + "\n") | ||
print("Extraction completed for " + self.dbfile) | print("Extraction completed for " + self.dbfile + ".") | ||
Edição das 13h33min de 31 de outubro de 2019
Preparations
$ virtualenv -p $(which python3) kp
$ source kp/bin/activate
$ cd kp
$ touch kp.py
$ chmod +x kp.py
$ pip install pykeepass
$ pip install argparse
Code
#!/usr/bin/env python
from pykeepass import PyKeePass
import construct
import pyark
import argparse
class KeePass:
def __init__(self,dbfile, dbkey):
self.dbfile = dbfile
self.dbkey = dbkey
self.dbfilenopath = self.dbfile.split("/")[len(self.dbfile.split("/")) - 1]
self.dbfilename = self.dbfilenopath.split(".")[0]
self.fileobject = "output/" + self.dbfilename + ".csv"
def extract(self):
borda = "\""
separator = "\";\""
coding = "utf-8"
data = []
try:
print("Starting Extraction of " + self.dbfile + ".")
with PyKeePass(self.dbfile, password=self.dbkey) as kp:
for entry in kp.entries:
grupo = str(entry.group).replace("\n", "").encode(coding, errors="ignore").decode()
path = str(entry.path).replace("\n", "").encode(coding, errors="ignore").decode()
title = str(entry.title).replace("\n", "").encode(coding, errors="ignore").decode()
notes = str(entry.notes).replace("\n", "").encode(coding, errors="ignore").decode()
username = str(entry.username).replace("\n", "")
password = str(entry.password).replace("\n", "")
data.append(borda + username + separator + password + separator + title + separator + path + separator + str(len(path)) + separator + notes + separator + str(len(notes)) + borda)
except FileNotFoundError:
print("DBFile " + self.dbfile + " Not Found.")
exit(1)
except construct.core.ChecksumError:
print("Could not open " + self.dbfile + ". Probably bad master password.")
exit(1)
except AttributeError as e:
print("Attribute error when exporting " + self.dbfile + ".")
print(e)
else:
try:
f= open(self.fileobject,"w+")
except Exception as e:
print("Could not create file " + self.fileobject + ".")
else:
for i in data:
f.writelines(i + "\n")
print("Extraction completed for " + self.dbfile + ".")
parser = argparse.ArgumentParser(description='KeePass extractor, By D Amato')
parser.add_argument('--dbfile', action='store', help='dbfile to extract')
parser.add_argument('--dbkey', action='store', help='Secret to open dbfile')
parser.parse_args([])
args = parser.parse_args()
if args.dbfile is not None:
if args.dbkey is not None:
mykp = KeePass(dbfile=args.dbfile,dbkey=args.dbkey)
mykp.extract()
else:
print("Error. use --help for more information")
else:
print("Error. use --help for more information")
Running
$ ./kp.py --help
usage: kp.py [-h] [--dbfile DBFILE] [--dbkey DBKEY]
KeePass extractor, By D´Amato
optional arguments:
-h, --help show this help message and exit
--dbfile DBFILE dbfile to extract
--dbkey DBKEY Secret to open dbfile
$ ./kp.py --dbfile all-my-secrets.kdbx --dbkey 'MyVeryStrongP@ssw0rd' | tee output.csv