惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
V
Visual Studio Blog
P
Privacy International News Feed
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
T
Threatpost
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
小众软件
小众软件
L
LINUX DO - 最新话题
C
Cisco Blogs
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
罗磊的独立博客
V
V2EX
博客园 - Franky
P
Proofpoint News Feed
SecWiki News
SecWiki News
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
PCI Perspectives
PCI Perspectives
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题

LY 的博客

AX1800 路由器折腾记录 ZJUCTF 2025 writeup 将 iPad 与 Apple Pencil 作为数位屏使用 Linux 修改 WiFi 监管数据 提取上交 jAccount 动态口令(TOTP) 机械革命无界 15X Pro 暴风雪使用 Arch Linux 基于 RawDisk,将双系统挂载为虚拟机 Linux 自制鼠标指针 Windows 使用 USB/IP 通过网络共享 USB 设备
使用硬件密钥解锁 KeePass 数据库
LY · 2025-06-22 · via LY 的博客

该教程适用于YubiKey、CanoKey等支持GPG的物理密钥,目标是在Linux系统上使用简短的PIN而非较长的数据库密钥解锁KeePass数据库。

准备

本文使用的环境是 Arch Linux,KDE桌面环境,KeePassXC。

首先你需要在硬件密钥中生成一个GPG密钥,该过程已有很多教程,此处不再赘述。

使用gpg -Kgpg --card-status,获得主密钥的ID,或智能卡中任一子密钥的ID,形如69D6E8DCB9E4117864368CA0EE4FB075119CE61F7C4464C89E529178,以下统一以前者代替,请根据实际修改。

执行read -s password && echo -n "$password" | gpg --encrypt --armor -r 69D6E8DCB9E4117864368CA0EE4FB075119CE61F > ENCRYPTED_PASS_FILE.gpg,输入数据库密钥后生成其加密副本。此处的--armor仅用于以ASCII格式保存文件,若省略则以二进制格式保存。

将以下文件保存为unlock_keepassxc_with_key.sh,使用chmod a+x unlock_keepassxc_with_key.sh赋予可执行权限。

#!/usr/bin/env bash
# https://young-lord.github.io/posts/keepass-security-key

# Configuration
ENCRYPTED_PASS_FILE="/path/to/ENCRYPTED_PASS_FILE.gpg"
DATABASE_FILE="/path/to/keepass_database.kdbx"

unlock_with_secrutiy_key() {
    echo "Please tap your key."
    # idk why terminal doesn't show in KDE autostart, so the kind notice is useless.

    local decrypted_pass
    if decrypted_pass=$(gpg --quiet --decrypt "$ENCRYPTED_PASS_FILE" 2>/dev/null); then
        nohup keepassxc --pw-stdin "$DATABASE_FILE" <<<"$decrypted_pass" >/dev/null 2>&1 &
        disown
        return 0
    else
        echo "Failed to decrypt password using security key."
        return 1
    fi
}

manual_unlock() {
    nohup keepassxc >/dev/null 2>&1 &
    disown
}

# Main execution
if gpg --card-status >/dev/null 2>&1; then
    unlock_with_secrutiy_key || manual_unlock
else
    manual_unlock
fi

在KeePassXC的设置中,关闭“系统运行时自动启动 KeePassXC”,打开“在应用程序启动时最小化窗口”。

对于KDE,在 System Settings -> System -> Autostart -> Add New -> Login Script 中添加该脚本。对于GNOME、Xfce等其他桌面环境,对应配置位置可能不同。

对于其他操作系统、原版KeePass,本方式经微调后亦可使用。

如果一切顺利,在插入硬件密钥的条件下,下次登录时,即会弹出输入OpenPGP PIN的窗口,输入PIN后,KeePassXC即在后台自动解锁。

缺点

目前仅可在开机(用户首次登录)时调用该脚本解锁,若重新锁定数据库,仍然需要输入数据库密码解锁。

该脚本未对内存中已解密的数据库密钥进行销毁等处理,可能存在泄露风险。