KEEPASS: mudanças entre as edições

De Wiki Clusterlab.com.br
Ir para navegação Ir para pesquisar
(Criou página com '=Preparations= <syntaxhighlight lang=bash> virtualenv -p $(which python3) kp source kp/bin/activate cd kp touch kp.py chmod +x kp.py pip install pykeepass </syntaxhighlight> =...')
 
 
(9 revisões intermediárias pelo mesmo usuário não estão sendo mostradas)
Linha 1: Linha 1:
=Preparations=
=Preparations=
<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
virtualenv -p $(which python3) kp
$ virtualenv -p $(which python3) kp
source kp/bin/activate
$ source kp/bin/activate
cd kp
$ cd kp
touch kp.py
$ touch kp.py
chmod +x kp.py
$ chmod +x kp.py
pip install pykeepass
$ pip install pykeepass
$ pip install argparse
</syntaxhighlight>
</syntaxhighlight>
=Code=
=Code=
<syntaxhighlight lang=python>
<syntaxhighlight lang=python>
#!/usr/bin/env python
#!/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")
</syntaxhighlight>
=Running=
<syntaxhighlight lang=bash>
$ ./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


from pykeepass import PyKeePass


with PyKeePass('all-my-secrets.kdbx', password='StrongPass') as kp:
$ ./kp.py  --dbfile all-my-secrets.kdbx --dbkey 'MyVeryStrongP@ssw0rd' | tee output.csv
    for entry in kp.entries:
</syntaxhighlight >
        grupo = str(entry.group).replace("\n", "")
        path = str(entry.path).replace("\n", "")
        title = str(entry.title).replace("\n", "")
        notes = str(entry.notes).replace("\n", "")
        username = str(entry.username).replace("\n", "")
        password = str(entry.password).replace("\n", "")
        print(grupo + ";" + path + ";" + title + ";" + notes + ";" + username + ";" + password)
</syntaxhighlight>

Edição atual tal como às 15h11min 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 = []
        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