Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python script for generating TOTP codes #1121

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 22 additions & 11 deletions docs/decrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,20 @@
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
from cryptography.hazmat.backends import default_backend
import cryptography

backend = default_backend()


def die(msg, code=1):
print(msg, file=sys.stderr)
exit(code)

def main():
parser = argparse.ArgumentParser(description="Decrypt an Aegis vault")
parser.add_argument("--input", dest="input", required=True, help="encrypted Aegis vault file")
parser.add_argument("--output", dest="output", default="-", help="output file ('-' for stdout)")
args = parser.parse_args()

# parse the Aegis vault file
with io.open(args.input, "r") as f:
data = json.load(f)
# parse and decrypt the Aegis vault file
def decrypt_db(file, password):

# ask the user for a password
password = getpass.getpass().encode("utf-8")
with io.open(file, "r") as f:
data = json.load(f)

# extract all password slots from the header
header = data["header"]
Expand Down Expand Up @@ -82,12 +78,27 @@ def main():
associated_data=None
)

db = db.decode("utf-8")
return db.decode("utf-8")


def main():
parser = argparse.ArgumentParser(description="Decrypt an Aegis vault")
parser.add_argument("--input", dest="input", required=True, help="encrypted Aegis vault file")
parser.add_argument("--output", dest="output", default="-", help="output file ('-' for stdout)")
args = parser.parse_args()

# ask the user for a password
password = getpass.getpass().encode("utf-8")

# parse and decrypt the Aegis vault file
db = decrypt_db(args.input, password)

if args.output != "-":
with io.open(args.output, "w") as f:
f.write(db)
else:
print(db)


if __name__ == "__main__":
main()
48 changes: 48 additions & 0 deletions docs/generate_otp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3

# example usage: ./scripts/decrypt.py --input ./app/src/test/resources/com/beemdevelopment/aegis/importers/aegis_encrypted.json --entryname Mason
# password: test

import argparse
import getpass
import json
import sys

import pyotp

import decrypt


def main():
parser = argparse.ArgumentParser(description="Decrypt an Aegis vault and generate an OTP code")
parser.add_argument("--input", dest="input", required=True, help="encrypted Aegis vault file")
parser.add_argument("--entryname", dest="entryname", required=True,
help="name of the entry for which you want to generate the OTP code")
args = parser.parse_args()

password = getpass.getpass().encode("utf-8")

db = decrypt.decrypt_db(args.input, password)

entries = json.loads(db)
entries_found = []

for entry in entries['entries']:
name = entry.get('name', '')

# Looks also for substrings
if args.entryname.lower() in name.lower():
entries_found.append(entry)

for entry in entries_found:
if entry.get('type', '') == 'totp':
totp = pyotp.TOTP(entry['info']['secret'], interval=entry['info']['period'])
print("Entry %s - issuer %s - TOTP generated: %s" % (
entry.get('name', ''), entry.get('issuer', ''), totp.now()))
else:
print("OTP type not supported: %s" % entry.get('type', ''))
sys.exit(2)


if __name__ == '__main__':
main()