Learning Record learning from practice

· security / python

Python keyring on Windows: CryptFile Backend, Listing Secrets, and Credential Manager

keyrings.cryptfile works on Windows. ModuleNotFoundError: No module named 'keyrings' only means the package is not installed:

python -m pip install keyrings.cryptfile

Using CryptFileKeyring on Windows

  1. It is not selected by default. kr.get_keyring() returns WinVaultKeyring (Windows Credential Manager, priority 5), which beats CryptFile’s 2.5. Select it explicitly:
    import keyring as kr
    from keyrings.cryptfile.cryptfile import CryptFileKeyring
    
    k = CryptFileKeyring()
    k.keyring_key = "your-master-password"  # otherwise prompted via getpass
    kr.set_keyring(k)
    kr.set_password("svc", "user", "secret")
    

    The encrypted file lives at %LOCALAPPDATA%\Python Keyring\cryptfile_pass.cfg.

  2. You usually don’t need it on Windows. WinVault is already encrypted storage and needs no master password. CryptFile fits headless Linux without SecretService. On Windows only, plain import keyring is enough.

Listing All Stored Secrets

The public keyring API is just get/set/delete_password and get_credential — there is no list function. To enumerate entries, read the backend’s storage directly.

WinVaultKeyring (Windows default)

from win32ctypes.pywin32 import win32cred  # pywin32-ctypes, installed with keyring on Windows

for c in win32cred.CredEnumerate(None, 0):
    if c["Type"] != win32cred.CRED_TYPE_GENERIC:
        continue
    pwd = c["CredentialBlob"].decode("utf-16-le") if c["CredentialBlob"] else ""
    print(c["TargetName"], c["UserName"], pwd)
  • This lists the entire Windows Credential Manager, not only entries written by keyring (OneDrive, Office, gh, …). Filter by TargetName.
  • If a service has more than one username, keyring stores the extra ones under a compound target username@service.
  • For names only, without passwords, run cmdkey /list.

CryptFileKeyring

Entries are stored in an INI file: section = escaped service, option = escaped username. Anything outside [A-Za-z0-9] is escaped as _XX hex (e.g. keyring-setting → keyring_2Dsetting). Enumerate the names, then decrypt with get_password:

import configparser
from keyrings.cryptfile.cryptfile import CryptFileKeyring
from keyrings.cryptfile.escape import unescape

k = CryptFileKeyring()
k.keyring_key = "your-master-password"

cfg = configparser.RawConfigParser()
cfg.optionxform = str  # keep username case; default lowercases option names
cfg.read(k.file_path)
for sec in cfg.sections():
    if unescape(sec) == "keyring-setting":  # master-password check, scheme, version
        continue
    for opt in cfg.options(sec):
        svc, user = unescape(sec), unescape(opt)
        print(svc, user, k.get_password(svc, user))

Both scripts print plaintext passwords — run them only in your own terminal and never log the output.

Windows Credential Commands

These show names and usernames only, not passwords:

Command Purpose
cmdkey /list List the current user’s saved credentials (target, type, user)
cmdkey /list:gh* Filter by target name, wildcards allowed
vaultcmd /list List credential vaults (“Windows Credentials”, “Web Credentials”)
vaultcmd /listcreds:"Windows Credentials" /all List entries in one vault
control keymgr.dll Open the Credential Manager GUI
rundll32.exe keymgr.dll,KRShowKeyMgr Open the legacy “Stored User Names and Passwords” dialog

To see plaintext passwords:

  • The CredEnumerate script above.
  • The CredentialManager PowerShell module (Install-Module CredentialManager):
    Get-StoredCredential -AsCredentialObject | Select-Object TargetName, UserName, Password
    
  • GUI: Credential Manager → expand a Generic Credential → Show (asks for your Windows password).

Delete with cmdkey /delete:<target> — this cannot be undone.