def _build_from_blank(self, filename, password): if self._blank_kdb_file and os.path.isfile(self._blank_kdb_file): kdb = PyKeePass(self._blank_kdb_file, self._blank_kdb_file_password) kdb.filename = filename kdb.password = password kdb.keyfile = None else: kdb = create_database(filename, password) self.__tmd_kdb = kdb
def init_database(args): """Create database""" # from pykeepass.pykeepass import PyKeePass # ----- setup config ----- c = ConfigParser() if os.path.exists(args.config): c.read(args.config) if args.name is None: database_name = editable_input("Database name (no spaces)", "passhole") else: database_name = args.name if database_name in c.sections(): log.error( red("There is already a database named ") + bold(database_name) + red(" in ") + bold(args.config)) sys.exit(1) else: c.add_section(database_name) if not os.path.exists(args.config): c.set(database_name, 'default', 'True') # ----- database prompt ----- if args.name is None: database_path = editable_input("Desired database path", default_database.format(database_name)) else: database_path = args.database.format(database_name) # quit if database already exists if os.path.exists(database_path): log.error(red("Found database at ") + bold(database_path)) sys.exit(1) else: c.set(database_name, 'database', database_path) # ----- password prompt ----- if args.name is None: use_password = boolean_input("Password protect database?") if use_password: password = getpass(green('Password: '******'Confirm: ')) if not password == password_confirm: log.error(red("Passwords do not match")) sys.exit(1) else: password = None c.set(database_name, 'no-password', 'True') else: password = args.password if password is None: c.set(database_name, 'no-password', 'True') # ----- keyfile prompt ----- if args.name is None: use_keyfile = boolean_input("Use a keyfile?") if use_keyfile: keyfile = editable_input("Desired keyfile path", default_keyfile.format(database_name)) else: keyfile = None else: keyfile = args.keyfile # ----- create keyfile/database/config ----- # create keyfile if keyfile is not None: keyfile = realpath(expanduser(keyfile)) log.debug("Looking for keyfile at {}".format(keyfile)) if os.path.exists(keyfile): print("Found existing keyfile at {} Exiting".format( bold(keyfile))) sys.exit(1) print("Creating keyfile at " + bold(keyfile)) os.makedirs(dirname(keyfile), exist_ok=True) c.set(database_name, 'keyfile', keyfile) with open(keyfile, 'w') as f: log.debug("keyfile contents {}".format(keyfile_contents)) f.write(keyfile_contents.format( b64encode(os.urandom(32)).decode())) database_path = realpath(expanduser(database_path)) # create database print("Creating database at {}".format(bold(database_path))) os.makedirs(dirname(database_path), exist_ok=True) shutil.copy(template_database_file, database_path) from pykeepass import PyKeePass kp = PyKeePass(database_path, password='******') kp.password = password kp.keyfile = keyfile kp.save() config = realpath(expanduser(args.config)) # create config print("Creating config at {}".format(bold(config))) os.makedirs(dirname(config), exist_ok=True) with open(config, 'w') as f: c.write(f)
def main(input_file, target_file, password): def matches(query, card): try: if query is None: return True query = query.lower() if query in card['@title'].lower(): return True # for field in card['field']: # if query in field['@name'].lower(): # return True # if '#text' in field and query in field['#text'].lower(): # return True except: pass return False if len(sys.argv) == 2: search = sys.argv[1] else: search = None db = Decrypter(input_file, password) kp = PyKeePass('New3.kdbx', password='******') kp.root_group.name = "test_title" kp.password = password entries = [] try: xml = db.decrypt() xml = parseString(xml) except: print("cannot decrypt, maybe wrong password?") sys.exit(1) for node in xml.getElementsByTagName('card'): is_template = False if node.hasAttribute('template'): is_template = (node.attributes['template'].value == 'true') if not is_template: entries.append(Entry(node)) duplicate_check = set() for entry in entries: password = "" login = "" url = "" notes = "" # extract all types the fields and their value type_value = { entry.fields[x].type: entry.fields[x].value for x in entry.fields } # map the type of the field in safeincloud to the field type in pykeepass map_types = { "password": "******", "login": "******", "website": "url", "notes": "notes" } # username and password are required fields mapped_type_value = {"username": "", "password": ""} # add all other fields with the new names mapped_type_value.update({ map_types[x]: type_value[x] for x in type_value if x in map_types }) # ensure that the title is unique title = entry.title if title in duplicate_check: i = 2 while title + str(i) in duplicate_check: i = i + 1 title = title + str(i) assert title not in duplicate_check # add entry to keepass file kp.add_entry(kp.root_group, title, **mapped_type_value) # add title to duplicate check duplicate_check.add(title) kp.save(target_file) print("file successful mapped")
iv = hashlib.md5(key.encode()).digest() return hkey, iv if len(sys.argv) < 2: print("Modo de empleo: {} <input.geco>") sys.exit() input_file = sys.argv[1] output_file = 'db.kdbx' master = getpass.getpass("Contraseña maestra: ") aes = CustomAES() kp = PyKeePass('database.kdbx', password="******") kp.password = master groups = {} with open(input_file, 'r') as f: for p in f.readlines(): name, type, description, account, password, cypher_method, updated, expiration = p.split("|") expiration = datetime.datetime.fromtimestamp(float(expiration)) updated = datetime.datetime.fromtimestamp(float(updated)) name, type, description, account =\ map(lambda x: x.replace("\\n", "\n"), (name, type, description, account)) clear = aes.decrypt(password, master) if type and not type in groups: group = kp.add_group(kp.root_group, type)