欢迎各位兄弟 发布技术文章
这里的技术是共享的
security find-generic-password
命令在 macOS 上用于查找存储在钥匙串(Keychain)中的通用密码(Generic Passwords)。这个命令不会默认只查找一条记录,它会根据提供的参数(如服务名称、帐户名称等)来查找匹配的密码项。
如果你发现 find-generic-password
只返回了一条记录,那很可能是因为:
只有一个匹配的项:你的钥匙串中只有一个与该命令提供的参数匹配的通用密码项。
参数限制:你提供的参数(如 -s
、-a
等)非常具体,以至于只有一个项匹配。
权限问题:你可能没有足够的权限来查看所有的通用密码项。在 macOS 上,钥匙串访问权限是受限的,可能需要特定的权限或管理员身份才能访问某些密码项。
命令使用问题:你可能没有正确使用 find-generic-password
命令或其参数。确保你了解每个参数的含义,并提供了正确的值。
如果你想要查找所有的通用密码项(而不是仅根据特定参数查找),那么你可能需要使用其他方法,如 security dump-keychain
命令,并解析其输出来找到所有通用密码项。但请注意,dump-keychain
会输出钥匙串中的所有内容,包括证书、密钥和其他类型的密码项,因此你可能需要编写一些脚本来过滤出你感兴趣的通用密码项。
此外,请注意,从钥匙串中检索密码需要适当的权限,并且出于安全原因,密码本身可能不会在输出中直接显示。通常,你需要使用 -w
参数来请求密码的明文值(如果可用且你有权限)。
find-generic-password
仅显示第一个匹配项
Ask Question
Looking into using the command line tool for KeyChain. I am able to do a lot of the things through security; listing my multiple keychains, dumping them and setting defaults. Reading through tutorials and other postings I expect to find my passwords with
security find-generic-password test
But I get
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security:SecKeychainSearchCopyNext:在钥匙串中找不到指定的项目。
This won't work in my default keychain or login.keychain. However, I am able to find my passwords listed as 'internet' with find-internet-password command. Can anyone explain why or what I am doing wrong? Sites I've been reading is the man page and http://blog.macromates.com/2006/keychain-access-from-shell/,
ShareTravis
Generic passwords are identified by their Service and Account attributes. For example, to search for a password for account "bar" of the "foo" service, use
security find-generic-password -a foo -s bar -g
解释
The -g
option displays the value of the password at the end of the output.
The combination of service and account is guaranteed to uniquely identify a particular password. Other queries (comment, label, etc.) are possible, but they may match multiple passwords. find-generic-password
displays only the first single matching item, which limits its usefulness for such queries.
ShareKaroy Lorentey
What is the "Service" attribute? I don't see that label anywhere in the Keychain Access app on macOS Mojave 10.14.4. The metadata I see are "Name", "Kind", "Account", "Where", and "Comments". (I tried the obvious guess here, "Name", but that does not work.) – robenkleene
2Keychain Access uses the "Where" label for the Service attribute.
1Isn't -s
the service and -a
the account? Seems they are inverted in the example. – Adrian Maire
A more accurate and up to date answer would be to use -w
instead of -g
if you only need password. I've seen people using -g
and parsing output using awk/perl to get the password field, which is not needed (anymore). All you need to do is:
您需要做的就是:
security find-generic-password -a foo -s bar -w
解释
You may usefind-internet-password
instead of find-generic-password
command depending on where your password is stored in keychain.
ShareBabak Farrokhi
ShareImprove this answeranswered Jul 15, 2011 at 3:31Lily Ballard185k
1
1
security list-keychains # print all the keychains security default-keychain -s "<printed keychain using above command>" eg: security default-keychain "Users/myname/Library/Keychain/login-db"
解释
security delete-keychain "keychain name" # to delete the keychain
解释
ShareImprove this answeredited Jul 14, 2021 at 20:42Lee Irvine3,227 answered May 14, 2021 at 15:58
shubham rasal111
ShareImprove this answeranswered Sep 3, 2013 at 16:04Developer107
ShareImprove this answeranswered Dec 2, 2014 at 18:35Greg Wiley21
One minor issue I ran into with find-generic-password: if the service name (-s parameter) contains a period ('.') ('example.com', for example), I can run add-generic-password successfully but can't find the value with find-generic-password. – John McCarthy
Commented Apr 19, 2022 at 22:17