def test_multi_entry_case_insensitive_search(self): kdb_contents = open_data_file('passwordmultientry.kdb').read() db = Database(kdb_contents, 'password') self.assertEqual(len(db.entries), 3) matches = db.fuzzy_search_by_title('mYtItlE') self.assertEqual(len(matches), 3) self.assertEqual(matches[0].title, 'mytitle') self.assertEqual(matches[1].title, 'mytitle') self.assertEqual(matches[2].title, 'mytitle')
def test_multi_entry_case_insensitive_search(self): kdb_contents = open_data_file('passwordmultientry.kdb').read() db = Database(kdb_contents, self.password) self.assertEqual(len(db.entries), 3) matches = db.fuzzy_search_by_title('mYtItlE') self.assertEqual(len(matches), 3) self.assertEqual(matches[0].title, 'mytitle') self.assertEqual(matches[1].title, 'mytitle') self.assertEqual(matches[2].title, 'mytitle')
def test_multi_entry_exact_search(self): # This particular kdb file has multiple entries with the title # "mytitle". kdb_contents = open_data_file('passwordmultientry.kdb').read() db = Database(kdb_contents, self.password) self.assertEqual(len(db.entries), 3) matches = db.fuzzy_search_by_title('mytitle') self.assertEqual(len(matches), 3) self.assertEqual(matches[0].title, 'mytitle') self.assertEqual(matches[1].title, 'mytitle') self.assertEqual(matches[2].title, 'mytitle')
def test_multi_entry_exact_search(self): # This particular kdb file has multiple entries with the title # "mytitle". kdb_contents = open_data_file('passwordmultientry.kdb').read() db = Database(kdb_contents, 'password') self.assertEqual(len(db.entries), 3) matches = db.fuzzy_search_by_title('mytitle') self.assertEqual(len(matches), 3) self.assertEqual(matches[0].title, 'mytitle') self.assertEqual(matches[1].title, 'mytitle') self.assertEqual(matches[2].title, 'mytitle')
def test_search_entry_by_title(self): db = Database(self.kdb_contents, 'password') entry = db.fuzzy_search_by_title('mytitle')[0] self.assertEqual(entry.title, 'mytitle') entry = db.fuzzy_search_by_title('myTITLE')[0] self.assertEqual(entry.title, 'mytitle') entry = db.fuzzy_search_by_title('mytle')[0] self.assertEqual(entry.title, 'mytitle') entry = db.fuzzy_search_by_title('badvalue') self.assertEqual(entry, [])
def test_fuzzy_search_ignore_groups(self): kdb_contents = open_data_file('passwordmultientry.kdb').read() db = Database(kdb_contents, 'password') # There are 3 entries in the db with 'mytitle' titles. # 1 of the entries is in the Backup group. If we # specify ignore_groups in our search, we should not # get the entry in the Backup group. matches = db.fuzzy_search_by_title('mytitle', ignore_groups=['Backup']) self.assertEqual(len(matches), 2) self.assertEqual(matches[0].title, 'mytitle') self.assertEqual(matches[1].title, 'mytitle') self.assertNotEqual(matches[0].group.group_name, 'Backup') self.assertNotEqual(matches[1].group.group_name, 'Backup')
def test_32byte_key(self): # keepassx has some special casing of key files if they're # 32 or 64 bytes long. kdb_contents = open_data_file('password32byte.kdb').read() key_file_contents = open_data_file('password32byte.key').read() db = Database(kdb_contents, 'password', key_file_contents) self.assertEqual(db.entries[0].group.group_name, 'Internet')
def test_fuzzy_search_ignore_groups(self): kdb_contents = open_data_file('passwordmultientry.kdb').read() db = Database(kdb_contents, self.password) # There are 3 entries in the db with 'mytitle' titles. # 1 of the entries is in the Backup group. If we # specify ignore_groups in our search, we should not # get the entry in the Backup group. matches = db.fuzzy_search_by_title('mytitle', ignore_groups=['Backup']) self.assertEqual(len(matches), 2) self.assertEqual(matches[0].title, 'mytitle') self.assertEqual(matches[1].title, 'mytitle') self.assertNotEqual(matches[0].group.group_name, 'Backup') self.assertNotEqual(matches[1].group.group_name, 'Backup')
def find_password(db_file, repository_name, kdb_password=None): if not kdb_password: kdb_password = getpass.getpass("Password for %s: " % db_file.name) try: with db_file.open("rb") as f: db = Database(f.read(), password=kdb_password.encode("utf8")) except InvalidPasswordError: print("Wrong password for unlocking .kdb file!!") return None, None try: entry = db.find_by_title(repository_name) if entry.group.group_name == "grenier": return kdb_password, entry.password else: print("Could not find kdb entry for grenier/%s!!!" % repository_name) return kdb_password, None except EntryNotFoundError as err: print(err) return kdb_password, None
def test_parse_entries_from_decrypted_data(self): db = Database(self.kdb_contents, 'password') self.assertEqual(len(db.entries), 1) entry = db.entries[0] self.assertEqual(entry.title, 'mytitle') self.assertEqual(entry.uuid, 'c4d301502050cd695e353b16094be4a7') self.assertEqual(entry.groupid, 1876827345) self.assertEqual(entry.url, 'myurl') self.assertEqual(entry.username, 'myusername') self.assertEqual(entry.password, 'mypassword') self.assertEqual(entry.notes, '') self.assertEqual(entry.creation_time, datetime(2012, 7, 14, 13, 17, 8))
def create_db(args): if 'KP_INSECURE_PASSWORD' in os.environ: # This env var is really intended for testing purposes. # No one should be using this var. password = os.environ['KP_INSECURE_PASSWORD'] else: password = getpass.getpass('Password: '******'s ok if no key file # was specified. key_file_contents = None db = Database(db_file.read(), password=password, key_file_contents=key_file_contents) return db
def test_find_entry_by_uuid(self): db = Database(self.kdb_contents, 'password') entry = db.find_by_uuid('c4d301502050cd695e353b16094be4a7') self.assertEqual(entry.uuid, 'c4d301502050cd695e353b16094be4a7')
def test_64byte_key_no_password(self): kdb_contents = open_data_file('passwordlesskey.kdb').read() key_file_contents = open_data_file('passwordlesskey.key').read() db = Database(kdb_contents, '', key_file_contents) self.assertEqual(db.entries[0].group.group_name, 'Internet')
def test_database_metadata(self): """Header is accessible from database object as the metadata attr.""" db = Database(self.kdb_contents, 'password') self.assertEqual(db.metadata.version, 0x30002)
arguments.password = getpass.getpass( 'Please insert database password: '******'utf-8') # Get key file content if arguments.key: # Open key file with open(arguments.key, 'rb') as file_key: key_file_content = file_key.read() else: # No key needed key_file_content = None # Process results if True: # Get passwords from database with open(arguments.database, 'rb') as file_database: database = Database(contents=file_database.read(), password=arguments.password, key_file_contents=key_file_content) # Get selected group only selected_group = [ group for group in database.groups if group.group_name == arguments.group ] if selected_group: # List entries for the selected group entries = [ entry for entry in database.entries if entry.group is selected_group[0] and entry.username and entry.password ] # Set socket and IMAP timeout socket.setdefaulttimeout(arguments.timeout)
def test_parse_entries_from_decrypted_data_with_key_file(self): kdb_contents = open_data_file('passwordkey.kdb').read() key_file_contents = open_data_file('passwordkey.key').read() db = Database(kdb_contents, 'password', key_file_contents) self.assertEqual(db.entries[0].group.group_name, 'Internet')
def test_parse_groups_from_decrypted_data(self): db = Database(self.kdb_contents, 'password') self.assertEqual(len(db.groups), 2) self.assertEqual(db.groups[0].group_name, 'Internet') self.assertEqual(db.groups[0].groupid, 1876827345) self.assertEqual(db.groups[0].level, 0)
def test_find_entry_by_title_does_not_exist(self): db = Database(self.kdb_contents, self.password) with self.assertRaises(EntryNotFoundError): db.find_by_title('badtitle')
def test_entries_can_be_grouped_by_groupid(self): db = Database(self.kdb_contents, 'password') self.assertEqual(db.entries[0].group.group_name, 'Internet')
def test_search_entry_with_typos(self): db = Database(self.kdb_contents, 'password') # 'le' has been transposed. entry = db.fuzzy_search_by_title('mytitel')[0] self.assertEqual(entry.title, 'mytitle')
def test_find_entry_does_not_exist(self): db = Database(self.kdb_contents, 'password') with self.assertRaises(EntryNotFoundError): entry = db.find_by_uuid('baduuid')
def test_find_entry_does_not_exist(self): db = Database(self.kdb_contents, 'password') with self.assertRaises(EntryNotFoundError): db.find_by_uuid('baduuid')
def test_find_entry_by_title(self): db = Database(self.kdb_contents, 'password') entry = db.find_by_title('mytitle') self.assertEqual(entry.title, 'mytitle')