欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

macos 通过 script 脚本 删除 钥匙串中 关于邮箱的密码 自己亲自做的 macos 通过 script 脚本 删除 钥匙串中 关于所有 wifi 的密码 AG 通义 有大用 有大大用 有大大大用


下面是我自己亲自做的 OK OOK 有大用  有大大用 有大大大用

#!/bin/bash
#
while :; do
       security find-generic-password -l Exchange
       if [ $? = 0 ];then
              security delete-generic-password -l Exchange
    else
       break;
       fi
done


(/usr/bin/osascript -e  'display dialog "已清理全部邮箱的密码!无须重启,打开outlook或macos的自带邮箱,即需要重新输密码" buttons {"OK"}   default button "OK"')

 


macos  通过shell脚本 删除  钥匙串 中 Exchange 条目 OK   有大用 

您可以使用security delete-generic-password命令通过shell脚本删除 macOS 钥匙串中的 Exchange 条目。以下是一个示例脚本:

bash

#!/bin/bash # 删除指定名称的钥匙串条目(这里以Exchange为例) security delete-generic-password -l Exchange

您可以将以上代码保存为一个.sh文件,然后在终端中运行该脚本,即可删除名为“Exchange”的钥匙串条目。您可以根据需要修改 "Exchange" 部分来指定要删除的条目名称。注意,在运行脚本时,系统可能会要求您输入管理员密码以确认删除操作。

请确保您具有合适的权限来运行这个脚本,以及谨慎操作,因为删除后将无法恢复。








macos  通过shell脚本 删除  钥匙串 中 Exchange 条目


要通过shell脚本删除钥匙串中的Exchange条目,可以使用以下命令:

bash

security delete-internet-password -s "Exchange" -a "username"

这条命令将会删除钥匙串中标签为"Exchange",账号为"username"的条目。你可以将上面的命令保存到一个shell脚本文件中,并运行它来删除Exchange条目。如果有多个Exchange条目需要删除,可以使用循环结构来批量删除。





可以看看    /node-admin/20909




security  find-generic-password  -l Exchange 详解,示例有多个结果

实际上,security find-generic-password 命令默认情况下每次只会返回与查询条件匹配的第一个结果。但是,如果你期望查找所有匹配 "Exchange" 标签的密码项并希望查看是否有多个结果,你不能直接依赖 find-generic-password 命令来一次性列出所有匹配项,因为该命令设计为返回单个结果或无结果。

解决方案

要处理可能存在的多个匹配项,你需要采取一种变通的方法,比如编写一个脚本循环查询或利用其他工具来遍历Keychain数据库。但请注意,这通常涉及到更高级的使用场景,并且可能需要对Keychain的底层结构有一定的理解。

示例脚本思路(非直接命令):

你可以考虑编写一个简单的bash脚本来尝试查找所有匹配特定标签的条目。以下是一个概念性的示例脚本框架,它尝试通过标签搜索,并打印出所有匹配的服务名称。注意,这仅作为一个概念展示,实际应用时需要考虑更多边界情况和安全措施。

Bash
1    #!/bin/bash
2    
3    # 指定标签
4    targetLabel="Exchange"
5
6    # 使用security命令查找所有匹配的条目,但不直接输出密码
7    # 通过grep过滤出包含服务名称的行,以便进一步处理
8    security find-generic-password -l "$targetLabel" | grep "acct"
9
10    # 注意:此示例不会直接显示密码,而是展示了如何循环或进一步处理每个匹配项
11    # 若要处理每个匹配项(例如获取密码或更多详情),你可能需要更复杂的逻辑

注意事项

  • 安全性:处理和显示Keychain中的敏感信息时务必小心,确保脚本或命令只在受控和安全的环境中运行。

  • 实现细节:上述脚本仅作为思路展示,实际实现时需根据具体需求调整,并考虑如何有效且安全地处理和展示结果。

  • 权限:执行这类脚本或命令可能需要管理员权限或用户的明确授权。

由于直接使用 security find-generic-password 命令难以直接处理多结果输出,上述方法提供了探索和管理多个匹配项的一种方式。在处理实际问题时,请根据具体情况选择合适的方法,并确保操作符合安全规范。







security   find-internet-password 命令详解


security find-internet-password 是一个在 macOS 操作系统中用于通过命令行访问钥匙串(Keychain)的实用工具,特别用于查找存储的互联网密码项。这个命令对于自动化脚本、调试或获取忘记的登录凭证非常有用。以下是对该命令的一个基本详解:

基本语法

Bash
1    security find-internet-password [-g] [-a account_name] [-s service_name] [-c creator_code] [-l label] [-w] [-D domain] [-r protocol] [-t type] [-C class] [-Z zone] [-U user] [-P prompt]

参数说明

  • -g:(global)全局选项,表示要显示找到的密码。如果不加此选项,命令可能不会直接显示密码,而是提供其他详细信息。

  • -a account_name: 指定要查找的账户名。

  • -s service_name: 指定要查找的服务名,通常这是网站或应用程序的名称。

  • -c creator_code: 指定创建者的代码,这个选项在现代 macOS 中不太常用。

  • -l label: 设置钥匙串条目的标签,有时用于区分同服务下的不同登录。

  • -w: 表示要显示密码。结合 -g 使用时可以直接输出密码。

  • -D domain: 指定域名,比如公司内部的网络服务可能需要此参数。

  • -r protocol: 指定协议类型,如 httphttps 等。

  • -t type: 密码项的类型,通常默认为 internet password,所以这个选项在使用 find-internet-password 时不是必需的。

  • -C class: 密钥链项的类别。

  • -Z zone: 密钥链区域,通常与域有关。

  • -U user: 指定用户,某些情况下可能需要。

  • -P prompt: 自定义提示信息,当需要用户输入密码(如解锁钥匙串)时显示。

实际例子

假设你想查找并显示已保存的 Gmail 账户密码:

Bash
1    security find-internet-password -g -s "Gmail" -a "your_email@example.com"

在这个命令中,-g 用于显示密码,-s 后面跟服务名(这里是 "Gmail"),-a 后面跟账户名。执行此命令后,你可能需要输入管理员密码来解锁钥匙串,之后密码会直接显示在终端中。

请注意,使用这些命令时应谨慎,因为它们可以暴露敏感信息。确保在理解每个参数的作用后再进行操作。














find-internet-password  命令用法 

B

Before you start scripting: You better educate students how to use the keychain, since they are local admins, and will probably add many passwords into Keychains.
在开始编写脚本之前:最好教学生如何使用钥匙串,因为他们是本地管理员,并且可能会将许多密码添加到钥匙串中。

But the basic bash command to manage the keychain is '
但是管理钥匙串的基本 bash 命令是”
security' and the option you are looking for is 'find-internet-password' or 'delete-internet-password' but the command's output is not easy to understand, nor parse easily. See these blogs I found (some may be outdated, but they look similar to my experience) Blog one or Blog two

AppleScript support for scripting the keychain is gone since 10.7, but some 3rd party add ons have some options, but the last update is for 10.9.1 so status for these tools
自 10.7 以来,AppleScript 对钥匙串脚本的支持已消失,但某些第三方附加组件有一些选项,但上次更新是针对 10.9.1 的,因此这些工具处于状态
 on macOS Siera is unkown.
自 10.7 以来,AppleScript 对钥匙串脚本的支持已消失,但一些第三方附加组件有一些选项,但上次更新是针对 10.9.1 的,因此 macOS Siera 上这些工具的状态是未知的。

from the man page for the security command:
从 security 命令的手册页中:

find-internet-password [-h] [-a account] [-s server] [options...] [-g]
     [keychain...]
            Find an internet password item.

            -a account      Match account string
            -c creator      Match creator (four-character code)
            -C type         Match type (four-character code)
            -d securityDomain
                            Match securityDomain string
            -D kind         Match kind string
            -j comment      Match comment string
            -l label        Match label string
            -p path         Match path string
            -P port         Match port number
            -r protocol     Match protocol (four-character code)
            -s server       Match server string
            -t authenticationType
                            Match authenticationType (four-character code)
            -g              Display the password for the item found
            -w              Display the password(only) for the item found

and then 'security delete-internet-password' with the proper options will delete the wrong keys
然后“安全删除-互联网密码”与正确的选项将删除错误的密钥


来自  https://community.jamf.com/t5/jamf-pro/script-to-delete-keychain-entry/m-p/184414








Manage Keychains with Commands
∟ 使用命令管理钥匙串

This section provides a quick introduction on how to manage keychains with command line tools.
本节简要介绍了如何使用命令行工具管理钥匙串。

© 2024 Dr. Herong Yang. All rights reserved.
© 2024 年,Herong Yang 博士。保留所有权利。

find-internet-password  用法 


security 命令用法 

Keychains stored in Keychain Access can also be managed from command line tools.
还可以管理存储在“钥匙串访问”中的钥匙串
从命令行工具。

macOS offers the "security" command that allows you to manage keychains:
macOS 提供“security”命令,可让您管理
钥匙扣:

herong$ man security

NAME
  security -- Command line interface to keychains and Security framework

SYNOPSIS
  security [-hilqv] [-p prompt] [command] [command_options] [command_args]

DESCRIPTION
  A simple command line interface which lets you administer keychains, 
  manipulate keys and certificates, and do just about anything the 
  Security framework is capable of from the command line.

  Here is a complete list of the options available:

    -h If no arguments are specified, show a list of all commands.  
       If argument are provided, show usage for the specified command.

    -i Run security in interactive mode.
  ...

Commands supported by "security" arre:
“security” arre支持的命令:

herong$ security -h 

help                      Show all commands, or show usage for a command.
list-keychains            Display or manipulate the keychain search list.
list-smartcards           Display available smartcards.
default-keychain          Display or set the default keychain.
login-keychain            Display or set the login keychain.
create-keychain           Create keychains and add them to the search list.
delete-keychain           Delete keychains and remove them from the search list.
lock-keychain             Lock the specified keychain.
unlock-keychain           Unlock the specified keychain.
set-keychain-settings     Set settings for a keychain.
set-keychain-password     Set password for a keychain.
show-keychain-info        Show the settings for keychain.
dump-keychain             Dump the contents of one or more keychains.
create-keypair            Create an asymmetric key pair.
add-generic-password      Add a generic password item.
add-internet-password     Add an internet password item.
add-certificates          Add certificates to a keychain.
find-generic-password     Find a generic password item.
delete-generic-password   Delete a generic password item.
find-internet-password    Find an internet password item.
delete-internet-password  Delete an internet password item.
find-key                  Find keys in the keychain
set-key-partition-list    Set the partition list of a key.
find-certificate          Find a certificate item.
find-identity             Find an identity (certificate + private key).
delete-certificate        Delete a certificate from a keychain.
delete-identity           Delete an identity (certificate + private key) from a keychain.
set-identity-preference   Set the preferred identity to use for a service.
get-identity-preference   Get the preferred identity to use for a service.
create-db                 Create a db using the DL.
export                    Export items from a keychain.
import                    Import items into a keychain.
export-smartcard          Export items from a smartcard.
cms                       Encode or decode CMS messages.
install-mds               Install (or re-install) the MDS database.
add-trusted-cert          Add trusted certificate(s).
remove-trusted-cert       Remove trusted certificate(s).
dump-trust-settings       Display contents of trust settings.
trust-settings-export     Export trust settings.
trust-settings-import     Import trust settings.
verify-cert               Verify certificate(s).
authorize                 Perform authorization operations.
authorizationdb           Make changes to the authorization policy database.
execute-with-privileges   Execute tool with privileges.
leaks                     Run /usr/bin/leaks on this process.
error                     Display a descriptive message for the given error code(s).
smartcards                Enable, disable or list disabled smartcard tokens.
translocate-create        Create a translocation point for the provided path
translocate-policy-check  Check whether a path would be translocated.
translocate-status-check  Check whether a path is translocated.
translocate-original-path Find the original path for a translocated path.
...

You can list all keychains using "security list-keychains" command. Note that this command does not return the hidden "System Roots" keychain: /System/Library/Keychains/SystemRootCertificates.keychain.
您可以使用以下命令列出所有钥匙串
“security list-keychains”命令。
请注意,此命令不会返回
隐藏的“系统根”钥匙串:
/System/Library/Keychains/SystemRootCertificates.keychain。

herong$ security list-keychains
    "/Users/herong/Library/Keychains/login.keychain-db"
    "/Library/Keychains/System.keychain"

You can dump all items from a given keychain using the "security dump-keychain" command.
您可以使用
“security dump-keychain”命令。

herong$ security dump-keychain \
  /Users/herong/Library/Keychains/login.keychain-db > login_dump.txt

herong$ security dump-keychain \
  /Library/Keychains/System.keychain > system_dump.txt

herong$ more login_dump.txt

keychain: "/Library/Keychains/System.keychain"
version: 256
class: "genp"
attributes:
    0x00000007 <blob>="NETGEAR49"
    0x00000008 <blob>=<NULL>
    "acct"<blob>="NETGEAR49"
    "cdat"<timedate>=0x32303139303932383...  "20190928211737Z\000"
    "crtr"<uint32>=<NULL>
    "cusi"<sint32>=<NULL>
    "desc"<blob>="AirPort network password"
    "gena"<blob>=<NULL>
    "icmt"<blob>=<NULL>
    "invi"<sint32>=<NULL>
    "mdat"<timedate>=0x32303139303932383...  "20190928211737Z\000"
    "nega"<sint32>=<NULL>
    "prot"<blob>=<NULL>
    "scrp"<sint32>=<NULL>
    "svce"<blob>="AirPort"
    "type"<uint32>=<NULL>
    ...
...

You can dump different types of security items cross all keychains using different commands:
您可以在所有钥匙串中转储不同类型的安全项目
使用不同的命令:

herong$ security find-key > key.txt 
herong$ security find-certificate -a > certificate.txt 
herong$ security find-generic-password > generic-password.txt 
herong$ security find-internet-password > internet-password.txt 

You can decrypt and display an application password with a given password account name. In the following example, "NETGEAR49" is the account name of my Wi-Fi modem.
您可以解密并显示应用程序密码
使用给定的密码帐户名。
在以下示例中,“NETGEAR49”是我的帐户名称
Wi-Fi 调制解调器。

herong$ security find-generic-password -a NETGEAR49 -g
(Enter admin password)

keychain: "/Library/Keychains/System.keychain"
version: 256
class: "genp"
attributes:
    0x00000007 <blob>="NETGEAR49"
    0x00000008 <blob>=<NULL>
    "acct"<blob>="NETGEAR49"
    ...

password: "3a6e....7c46"

You can also find and print a certificate with a given certificate subject name.
您还可以查找并打印证书
使用给定的证书使用者名称。

herong$ security find-certificate -c 'GeoTrust Global CA' -p 

-----BEGIN CERTIFICATE-----
MIIDVDCCAjygAwIBAgIDAjRWMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT
MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i
YWwgQ0EwHhcNMDIwNTIxMDQwMDAwWhcNMjIwNTIxMDQwMDAwWjBCMQswCQYDVQQG
...
5fEWCRE11azbJHFwLJhWC9kXtNHjUStedejV0NxPNO3CBWaAocvmMw==
-----END CERTIFICATE-----


Submit Your Comment:



来自  https://www.herongyang.com/Mac/Keychain-Access-Management-Commands.html  







macos 通过 脚本 删除 钥匙串中 关于邮箱的密码


您可以使用以下脚本来删除钥匙串中关于邮箱的密码:

bash

#!/bin/bash # Specify the email address for which you want to delete the password email_address="example@example.com" # Find the keychain item for the specified email address keychain_item=$(security find-internet-password -a $email_address) # Delete the keychain item security delete-internet-password -a $email_address echo "Password for email $email_address deleted successfully"

请确保将脚本中的email_address变量替换为您要删除密码的邮箱地址。您可以将此脚本保存为delete_email_password.sh文件,并在终端中运行sh delete_email_password.sh来执行脚本。这样就可以删除钥匙串中关于邮箱的密码了。








macos 通过 脚本 删除 钥匙串中 关于所有 wifi 的密码


您可以使用以下脚本来删除钥匙串中所有 WiFi 密码:

bash

#!/bin/bash # Get a list of all keychain items for WiFi wifi_items=$(security find-generic-password -s "AirPort" -D "AirPort network password" -l) # Loop through each WiFi keychain item and delete it while IFS= read -r line; do    item_name=$(echo $line | awk '{print $1}')    security delete-generic-password -l $item_name done <<< "$wifi_items" echo "All WiFi passwords deleted successfully"

这个脚本会找到所有WiFi密码的keychain项并逐一删除它们。您可以将这个脚本保存为delete_wifi_passwords.sh文件,并在终端中运行sh delete_wifi_passwords.sh来执行脚本。这样就可以删除钥匙串中所有WiFi密码了。请注意,删除这些密码可能会导致您需要重新输入密码来连接相应的WiFi网络。






macos 通过 脚本 删除 钥匙串中 关于所有邮箱的密码

为了删除所有邮箱密码,您可以使用以下脚本:

bash

#!/bin/bash # Get a list of all keychain items for internet passwords email_items=$(security find-internet-password) # Loop through each email keychain item and delete it while IFS= read -r line; do    email_address=$(echo $line | grep "acct" | cut -d '"' -f 2)    security delete-internet-password -a $email_address done <<< "$email_items" echo "All email passwords deleted successfully"

这个脚本将查找所有邮件密码的keychain项并逐一删除它们。您可以将这个脚本保存为delete_email_passwords.sh文件,并在终端中运行sh delete_email_passwords.sh来执行脚本。这样就可以删除钥匙串中所有邮箱密码了。请注意,删除这些密码可能会导致您需要重新输入密码来访问相应的邮箱。






macos 通过 脚本 删除 钥匙串中 模糊匹配邮箱的密码


要通过脚本删除钥匙串中模糊匹配邮箱的密码,可以使用以下脚本:

bash

#!/bin/bash # Specify the partial email address to match partial_email="example" # Get a list of all keychain items for internet passwords email_items=$(security find-internet-password) # Loop through each email keychain item and delete it if the email address matches the partial string while IFS= read -r line; do    email_address=$(echo $line | grep "acct" | cut -d '"' -f 2)    if [[ $email_address == *$partial_email* ]]; then        security delete-internet-password -a $email_address    fi done <<< "$email_items" echo "Email passwords matching '$partial_email' deleted successfully"

这个脚本会找出所有邮件密码的keychain项,在删除之前检查是否存在于指定的部分电子邮件字符串中。您可以将这个脚本保存为delete_email_by_partial.sh文件,并在终端中运行sh delete_email_by_partial.sh来执行脚本。这样就可以删除钥匙串中模糊匹配邮箱的密码了。请根据需要修改partial_email变量中的部分电子邮件地址。





普通分类: