可以使用secret-tool来查找现有的seahorse密码吗?

我在网上找到了一篇关于如何从命令行中获取Gnome Keyring密码的帖子(如何在终端中获取密钥环中的密码以供脚本使用?),并发现secret-tool是解决方法。使用secret-tool可以很好地存储密码,但我似乎无法弄清楚如何查找使用seahorse存储的现有密码,这正是我真正希望做到的。我不知道需要使用哪些属性和值来查询它们,而且man手册对这些可能是什么也没有明确说明。

2你想将它们恢复为纯文本吗?我希望这是不可能的。如果确实可能,那么请更改您用来存储密码的系统。 - terdon
4我非常确定这就是secret-tool和seahorse的目的。我已经将一些密码存储在seahorse的登录部分(因此它们在我登录后可用;仍然加密在磁盘上,但使用我的用户凭据解密)。 这是我从这个例子中得出的结论(这是我在许多搜索中找到的唯一一个例子)[http://www.mankier.com/1/secret-tool#Examples]。 - Randoogle
2顺便说一句,这不是一个“海马密码”,而是存储密码的gnome-keyring,而seahorse是访问这个钥匙链的图形用户界面。 - Jeno
4个回答

我做了一个实用工具lssecret,它可以列出使用libsecret的所有项目,包括所有GNOME Keyring项目。看起来libsecret会自动为每个项目添加一个键值对,但Seahorse在图形界面中不显示它们。以下是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: cueball@xkcd.com
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添加到钥匙链中的密码。描述为"Test Password",实际密码为"Tr0ub4dor&3"。 2. 使用命令secret-tool store --label="secret-tool item" "my key" "my value"添加的项目,然后在提示中输入"s3cret-t00l"。 3. 用于自动解锁gnome keyring中的ssh密钥的密码。它是通过命令行中的ssh-add添加的,然后在首次使用时选中了"登录时自动解锁此密钥"框。 4. 通过在NetworkManager连接编辑器中选择"仅为此用户存储密码"来添加的wifi网络密码。 正如您所见,使用Seahorse添加到钥匙链中的密码具有"xdg:schema"和"org.gnome.keyring.Note"的键值对。您可以使用secret-tool search --all xdg:schema org.gnome.keyring.Note列出所有这些密码。

1啊,这很有趣!但是如何唯一地识别一个特定的密码呢?我不明白如何通过标签进行搜索/查找,这似乎有些奇怪... - Ibrahim
2源代码存档中还有一个工具:https://gitlab.gnome.org/GNOME/gnome-keyring/-/blob/master/pkcs11/secret-store/dump-keyring0-format.c - Frederick Nord
如果X无法启动,似乎无法正常工作,这也是我一开始遇到的问题所在。"由于未知原因无法获取秘密服务" "没有X11 $DISPLAY,无法自动启动D-Bus" - Adam Barnes
那是一款很棒的工具。不幸的是,我根本无法阅读这些秘密,出现了错误在路径 /org/freedesktop/secrets/collection/login/13 下没有此类秘密项目。事实上,当我尝试使用GNOME Seahorse时,也会得到完全相同的错误。看起来libsecret存在问题,或者可能某些发行版已对libsecret进行了加固。人们正在讨论有关钥匙链问题的内容,请参考https://gitlab.gnome.org/GNOME/seahorse/-/issues/157和https://gitlab.gnome.org/GNOME/gnome-keyring/-/issues/91。 - Mitch McMabers

好的,在进一步调查后,看起来海马不会为存储的密码分配任何属性(我很确定secret-tool找不到没有任何属性可查询的密码)。我认为这是因为,在创建一个密码后,我查看属性->详细信息,发现没有详细信息(即属性)。然而,我在海马中找到了其他具有详细信息的密码,并且可以使用这些属性在secret-tool中查找它们。
解决方案似乎是使用secret-tool创建密码,为以后的唯一查找分配适当的属性。由于它们都在后台使用gnome-keyring,所以只要你在secret-tool中创建密码,它就会立即出现在海马中。
Gnome-keyring似乎非常乐意创建多个具有完全相同标签的密码(只要属性不完全相同,如果完全相同则会替换),所以一旦你重新创建了密码,你可以从海马中删除旧的无用密码。
$ 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就会将其存储在那里。

1有人知道如何使用 secret-tool 来存储/检索密码,而不是使用默认的钥匙链吗? - Alex Spurling

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

secret-tool lookup server "ownCloud"


根据Salim的回答,我编写了一个笨拙的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"