如何识别和删除未使用的 apt 密钥?

如何识别和删除未使用的 apt 密钥?

在典型的安装中,会添加几个适当的 GPG 密钥,无论是用于 PPA 还是其他来源,但后来都未被使用。

在 GUI(软件属性)中很难识别哪些密钥实际上用于哪些存储库。

有没有一种简单的方法来识别哪些键被使用,以便可以删除所有其他键?

在我看来,这存在一些安全隐患。如果存储库所有者丢失了私钥并更新存储库以使用新密钥,那么很多人仍然安装着旧的(不可信的)密钥,对吗?

答案1

删除未使用的密钥非常简单,但就像添加密钥一样,您负责做功课并决定哪些密钥不再使用并可以删除。

首先列出您当前拥有的 apt 密钥sudo apt-key list

一旦确定不再需要的密钥,您可以直接使用 将其删除sudo apt-key del KEYID

使用 list 命令,您通常能够看到每个密钥是什么,特别是如果它来自 ppa,因为它通常有一个 uid,其中包含类似“Launchpad PPA for John”的内容。因此,如果您从源中删除了该 ppa 并且不再使用它,您可以安全地将其删除。

有时您可能需要快速谷歌一下才能知道特定密钥的来源,例如,mono 密钥的 uid 为“Xamarin Public Jenkins”,如果您谷歌一下 xamarin,就可以看到该密钥的来源。此外,如果您不确定,您可以随时返回 bash 历史记录并找到您添加的密钥。

man apt-key那里有更多信息和其他命令。

答案2

我编写了一个脚本,可以自动识别(并可选择删除)未使用的 GPG 密钥。

#!/bin/bash
#------------------------------------
# Script that identifies and optionally removes unused trusted gpg keys.
# These keys are usually added to install software from non-standard repositories (e.g. PPAs)
# TODO:
# - what if source repo is not reachable
# - dunno why but the delete doesn't actually delete keys anymore ¯\_(ツ)_/¯
# - dunno why but slack key gets detected as unused even if slack is on the system ¯\_(ツ)_/¯
#------------------------------------

usage() { 
    echo "Usage: $(basename "$0") [-b] [-d] [-h]" 1>&2
    echo "prints to stdout the unused keys in the format <keyid> <userid>" 1>&2
    echo "-b, backups trusted.gpg and trusted.gpg.d/ appending .bak (requires superuser)" 1>&2
    echo "-d, deletes unused keys (requires superuser)" 1>&2
    echo "-h, shows this help" 1>&2
}

backup=0
delete=0

while getopts ":bdh" o; do
    case "${o}" in
        b)
            backup=1    
            ;;
        d)
            delete=1
            ;;
        h)
            usage
            exit 0
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done
shift $((OPTIND-1))

# create directory in /tmp with random name
tmpdir=$(mktemp -d)
#echo "storing tmp files in $tmpdir" 1>&2

# get list of sources
grep -h ^deb /etc/apt/sources.list /etc/apt/sources.list.d/*.list > "${tmpdir}"/sources

# get list of releases files
# first sed expression removes content between square brackets (such as architecture specifications)
# awk is used to get only the source url and the distro we are using
# the 2 subsequent sed expressions are used piece everything together and generate the url to the Release.gpg file we need
# sort and uniq are used to eliminate duplicate entries
sed -e "s/\[.*\] //" "${tmpdir}/sources" | awk '{print $2" "$3}' | sed -E -e "s/\/? /\/dists\//" | sed -e "s/$/\/Release.gpg/" | sort | uniq > "${tmpdir}"/releases

# for each source, compute the keyid and save it in a file
while read -r url; do
    domain=$(echo $url | awk -F/ '{print $3}')
    #echo "processing ${url}"
    wget -q -T 10 -O "${tmpdir}"/"${domain}"_Release.gpg "$url"
    gpg --list-packets "${tmpdir}"/"${domain}"_Release.gpg | grep "keyid" | grep -Eo "[0-9A-F]{16}" >> "${tmpdir}"/sourcekeyidstemp
done <"${tmpdir}"/releases

# remove duplicate entries
sort "${tmpdir}"/sourcekeyidstemp | uniq > "${tmpdir}"/sourcekeyids

# for each trusted gpg key, extract his keyid and userid
gpg --list-packets /etc/apt/trusted.gpg | grep -A 8 "public key packet" | grep -E "keyid:|user ID" | grep -oE "[0-9A-F]{16}|\".*\""  | awk '{(getline tmp); print $0,tmp}' > "${tmpdir}"/trustedkeys

for f in /etc/apt/trusted.gpg.d/*.gpg; do
    gpg --list-packets "$f" | grep -A 8 "public key packet" | grep -E "keyid:|user ID" | grep -oE "[0-9A-F]{16}|\".*\""  | awk '{(getline tmp); print $0,tmp}' >> "${tmpdir}"/trustedkeys
done

# for each trusted gpg key, check if in use
touch "${tmpdir}"/unusedkeys
while read -r line; do
    keyid=$(echo "$line" | cut -d "\"" -f 1)
    userid=$(echo "$line" | cut -d "\"" -f 2)
    check=$(grep ${keyid} "${tmpdir}"/sourcekeyids)
    if [ -z "$check" ]; then
        echo "${keyid} ${userid}" >> "${tmpdir}"/unusedkeys
    fi 
done <"${tmpdir}"/trustedkeys

# backup gpg keys 
if [ $backup -eq 1 ]; then
    sudo cp /etc/apt/trusted.gpg /etc/apt/trusted.gpg.bak
    sudo cp -r /etc/apt/trusted.gpg.d/ /etc/apt/trusted.gpg.d.bak
fi

# delete unused gpg keys
if [ $delete -eq 1 ]; then
    while read -r line; do
        keyid=$(echo "$line" | cut -d "\"" -f 1)
        sudo apt-key del "$keyid" >/dev/null 
    done <"${tmpdir}"/unusedkeys
    echo "deleted keys:"
fi

# print unused keys
cat "${tmpdir}"/unusedkeys

它相当笨重,但可以工作。可能存在错误,使用时请自担风险。

该脚本的工作流程如下:

  • 从在线存储库获取 Release.gpg 文件,提取所有 apt 源的 KEYID
  • 提取所有本地信任密钥的 KEYID。
  • 检查本地是否存在某些 KEYID,而从在线来源提取的 KEYID 中不存在。这些是未使用的密钥。

编辑:固定链接编辑:将代码粘贴在这里,因为我厌倦了修复链接

相关内容