KEEPASS

De Wiki Clusterlab.com.br
Revisão de 15h09min de 31 de outubro de 2019 por Damato (discussão | contribs) (→‎Code)
Ir para navegação Ir para pesquisar

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 = []
        data.append(
            borda + "USERNAME"
            + separator + "PASSWORD"
            + separator + "TITLE"
            + separator + "PATH"
            + separator + "PATH_LEN"
            + separator + "NOTES"
            + separator + "NOTES_LEN"
            + borda
        )
        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 + ".")
                print(e)
            else:
                for i in data:
                    f.writelines(i + "\n")
                print("Extraction completed for  "  + self.dbfile + ".")
if __name__ == '__main__':
    class main:
        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