是否可以使用 secret-tool 查找现有的海马密码?

是否可以使用 secret-tool 查找现有的海马密码?

我找到了这篇关于从命令行获取 gnome 密钥环密码的帖子(如何从终端中的密钥环获取密码以用于脚本?),我发现 secret-tool 是最佳选择。使用 secret-tool 存储密码很棒,但我似乎无法弄清楚如何查找使用 seahorse 存储的现有密码,而这正是我真正希望做的。我不知道需要使用什么属性和值来查询它们,手册页对这些属性和值可能是什么的说明相当模糊。

答案1

我做了一个实用程序,秘密,列出使用 libsecret 的所有项目,包括所有 GNOME Keyring 项目。看起来 libsecret 会自动为每个项目添加一个键值对,但 Seahorse 不会在 GUI 中显示它们。以下是 lssecret 的一些示例输出:

Collection: Login

Item:   Test Password
Secret: Tr0ub4dor&3
Key:    xdg:schema
Value:  org.gnome.keyring.Note

Item:   secret-tool item
Secret: s3cret-t00l
Key:    my key
Value:  my value
Key:    xdg:schema
Value:  org.freedesktop.Secret.Generic

Item:   Unlock password for: [email protected]
Secret: correct horse battery staple
Key:    unique
Value:  ssh-store:/home/cueball/.ssh/id_rsa
Key:    xdg:schema
Value:  org.freedesktop.Secret.Generic

Item:   Network secret for GenericSSID/802-1x/password
Secret: S3cureW1f1Passw0rd
Key:    setting-name
Value:  802-1x
Key:    connection-uuid
Value:  6a762552-04b3-cd05-45ba-586688e0a029 
Key:    xdg:schema
Value:  org.freedesktop.NetworkManager.Connection
Key:    setting-key
Value:  password

GNOME Keyring 将每个密钥环作为集合存储在机密服务中。您可以在输出中看到默认密钥环 Login。这些项目代表:

  1. 使用 seahorse 添加到密钥环的密码。描述为“测试密码”,实际密码为“Tr0ub4dor&3”。
  2. 使用命令添加的项目secret-tool store --label="secret-tool item" "my key" "my value",然后在提示符中输入“s3cret-t00l”。
  3. ssh 密钥的密码,以便 gnome 密钥环可以自动解锁。它是使用命令行中的 ssh-add 添加的,然后在首次使用时选中“登录时自动解锁此密钥”框。
  4. 通过在 NetworkManager 连接编辑器中选择“仅为此用户存储密码”来添加的 wifi 网络密码。

如您所见,使用 Seahorse 添加到密钥环的密码具有“xdg:schema”和“org.gnome.keyring.Note”的键值对。您可以使用 列出它们secret-tool search --all xdg:schema org.gnome.keyring.Note

答案2

好吧,经过进一步调查,它看起来像海马不为存储的密码分配任何属性(我很确定 secret-tool 无法找到没有任何属性可查找的密码)。我相信这种情况的原因是,在 seahorse 中创建一个密码后,我查看了属性->详细信息,但没有详细信息(即属性)。不过,我在 seahorse 中找到了其他有详细信息的密码,我可以使用那些属性在 secret-tool 中查找它们。

解决方案似乎是使用 secret-tool 创建密码,分配适当的属性以便稍后进行唯一查找。由于它们都在后台使用 gnome-keyring,因此在 secret-tool 中创建新密码时,新密码就会出现在 seahorse 中。

Gnome-keyring 似乎非常乐意创建具有完全相同标签的多个密码(只是属性不完全相同,如果属性完全相同,它会替换它),因此一旦您重新创建了它,您就可以从 seahorse 中删除旧的无用的密码。

$ secret-tool store --label=DomainPassword user sjohnson domain some.domain
Password: thisismypa$$w0rd
$ secret-tool lookup user sjohnson domain some.domain
thisismypa$$w0rd

我最初不想使用 secret-tool 存储密码的原因是我不知道如何让它成为登录密码(登录后可用)通过 secret-tool 提供。它看起来像,只要“登录”是你的默认密钥库,secret-tool 会将其存储在那里。

答案3

我现在已经测试过了,我可以用这些参数恢复我的密码:

秘密工具查找服务器“ownCloud”

答案4

根据 Salims 的回答,我构建了一个笨拙的 bash 脚本:

outFile="keyRingExport.csv"

# Get all secrets from keyring
items=`secret-tool search --all xdg:schema org.gnome.keyring.Note`

# Clean exsisiting file
echo "">"$outFile"
currentPath="none"
currentLabel="none"
currentSecret="none"

while IFS= read -r line; do
  if [[ $line == "["* ]]; then
      # Trim Path
        currentPath=${line/\/org\/freedesktop\/secrets\/collection\//""}
  fi
  if [[ $line == "label"* ]]; then
      # Trim lable
      currentLabel=${line/label = /""}
      # CSV escape
      currentLabel="\"${currentLabel//\"/"\"\""}\""
  fi
  if [[ $line == "secret"* ]]; then
      # Trim secret
      currentSecret=${line/secret = /""}
      # CSV escape
      currentSecret="\"${currentSecret//\"/"\"\""}\""
      # write to file
      echo "$currentPath,$currentLabel,$currentSecret" >> "$outFile"

      # reset current variables
      currentPath="none"
      currentLabel="none"
      currentSecret="none"
  fi

done <<< "$items"

相关内容