0

After using Keepass for several years, an idea came to me that would be nice to implement. There are over 300 records in my database, and it would be nice to know which of those records are still good. Setting the expires flag on the records would help remind me to check if records are valid and potentially encourage me to change my password. Is there a way to look through the entire database and do the following to each record?

  1. Check if the expires flag is set.
  2. If it is not set, add X number of years to the creation (or modification) time.
  3. Set the expires flag.
  4. Set the expires time to the value calculated in step 2.
  5. Continue to the next record in the database.

A reply was received from Paul saying, "Not with KeePass, but you can use KPScript and some PowerShell." He included a link to #1318 Change Expiration Date from List, but my inexperience with the tools he recommended has only left me with more questions. Could anyone provide further assistance? (1)

Addendum

After some further research, the ability to export the database in a XML format was discovered. Would it be easier to parse the XML file, alter the data in that form, and then create a new database by importing the results? If using the suggestions already made would be too difficult, XML processing could potentially be an easier path to take.

0

Yes, it is possible to set the “expires” time based on “creation” time. By using a program, the process can be automated. Change the global constants (KPSCRIPT, DATABASE, and PASSWORD) to values corresponding to your system before running. In this particular example, the expiry time is set to be five years after the last modification date. The program below was adapted from Paul's answer.

#! /usr/bin/env python3
import datetime
import subprocess
import uuid


KPSCRIPT = r'C:\Program Files (x86)\KeePass Password Safe 2\KPScript.exe'
DATABASE = r'C:\Users\Stephen Paul Chappel\Documents\Database.kdbx'
PASSWORD = r'password'


def main():
    """Get KeePass entries, check each one, and change if required."""
    for line in ps(KPSCRIPT, '-c:ListEntries', DATABASE, '-pw:' + PASSWORD):
        key, value, *pair = line.split(': ', 1) + [None]
        if pair:
            if key == 'UUID':
                reference = uuid.UUID(value)
            elif key == 'TLM':
                tlm = datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S')
            elif key == 'EXP':
                if not {'False': False, 'True': True}[value]:
                    # Add 5 years to the last modification
                    # time for the expiry date and format.
                    te = tlm.replace(tlm.year + 5)
                    te = te.strftime('%Y-%m-%dT%H:%M:%S')
                    ps(
                        KPSCRIPT,
                        '-c:EditEntry',
                        DATABASE,
                        '-pw:' + PASSWORD,
                        '-refx-UUID:' + reference.hex,
                        '-setx-Expires:True',
                        '-setx-ExpiryTime:' + te
                    )
                del reference, tlm


def ps(*args):
    """Provide an interface for writing commands like those in PowerShell."""
    return subprocess.run(
        args,
        timeout=1,
        check=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        universal_newlines=True
    ).stdout.splitlines()


if __name__ == '__main__':
    main()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.