KEEPASS: mudanças entre as edições

De Wiki Clusterlab.com.br
Ir para navegação Ir para pesquisar
Sem resumo de edição
 
(6 revisões intermediárias pelo mesmo usuário não estão sendo mostradas)
Linha 12: Linha 12:
<syntaxhighlight lang=python>
<syntaxhighlight lang=python>
#!/usr/bin/env python
#!/usr/bin/env python
from pykeepass import PyKeePass
from pykeepass import PyKeePass
import construct
import pyark
import argparse
import argparse
class KeePass:
class KeePass:
Linha 19: Linha 20:
         self.dbfile = dbfile
         self.dbfile = dbfile
         self.dbkey = dbkey
         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):
     def extract(self):
         with PyKeePass(self.dbfile, password=self.dbkey) as kp:
         borda = "\""
            for entry in kp.entries:
        separator = "\";\""
                grupo = str(entry.group).replace("\n", "")
        coding = "utf-8"
                path = str(entry.path).replace("\n", "")
        data = []
                title = str(entry.title).replace("\n", "")
        data.append(
                notes = str(entry.notes).replace("\n", "")
            borda + "USERNAME"
                username = str(entry.username).replace("\n", "")
            + separator + "PASSWORD"
                password = str(entry.password).replace("\n", "")
            + separator + "TITLE"
                print("\"" + grupo + "\";\"" + path + "\";\"" + title + "\";\"" + notes + "\";\"" + username + "\";\"" + password + "\"")
            + 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>


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 t 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=
=Running=
<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
Linha 61: Linha 108:




$ ./kp.py  --dbfile all-my-secrets.kdbx --dbkey 'MyVeryStrongP@ssw0rd'
$ ./kp.py  --dbfile all-my-secrets.kdbx --dbkey 'MyVeryStrongP@ssw0rd' | tee output.csv
</syntaxhighlight >
</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