系统学习 Windows 学习路径:从 CMD 到 PowerShell、系统网络,再到 WSL 与 Server

certutil

管理证书存储、计算文件哈希、做 Base64/十六进制编解码,并执行 CA 查询与维护。

适用场景

排查证书链、导入或删除证书、校验下载文件哈希、临时编解码,或在证书颁发机构(CA)服务器上查看 CA 信息、发布 CRL、撤销证书。证书存储和 CA 操作会影响全机信任与签发体系,先读影响范围再执行。

常用命令

# 1. 只读:查看机器和当前用户的证书存储;-user 才看用户存储
C:\> certutil -store Root
C:\> certutil -store My
C:\> certutil -user -store My

# 2. 只读:查看单个证书详情、验证证书链
C:\> certutil -store My <thumbprint>
C:\> certutil -dump C:\certs\server.cer
C:\> certutil -verify C:\certs\server.cer

# 3. 计算文件哈希;不写算法时默认 SHA1,完整性校验应显式用 SHA256
C:\> certutil -hashfile C:\ISO\ubuntu.iso SHA256
PS C:\> Get-FileHash C:\ISO\ubuntu.iso -Algorithm SHA256

# 4. Base64 编解码;certutil -encode 会加 BEGIN/END 头和换行
C:\> certutil -encode C:\Temp\payload.bin C:\Temp\payload.b64
C:\> certutil -decode C:\Temp\payload.b64 C:\Temp\payload.out
C:\> certutil -encodehex C:\Temp\payload.bin C:\Temp\payload.hex

# 5. 影响范围:向 LocalMachine 的 Root 存储导入根证书,等于信任该 CA 签发的所有证书;删除根证书会让依赖它的 TLS/签名验证失败
# 备份/回滚:先用 certutil -store Root <thumbprint> C:\backup\root.cer 导出备份;回滚用 -addstore Root 重新导入
C:\> certutil -addstore Root C:\certs\corp-root.cer
C:\> certutil -delstore Root <thumbprint>

# 6. 影响范围:导入 PFX 会把证书和私钥写入存储;默认是机器存储,-user 才是当前用户
# 备份/回滚:先保留原 PFX;误导入用 -delstore 删除,私钥证书可用 Export-PfxCertificate 备份
C:\> certutil -importPFX My C:\certs\server.pfx
C:\> certutil -user -importPFX My C:\certs\client.pfx
PS C:\> $pwd = Read-Host -AsSecureString 'PFX 密码'
PS C:\> Export-PfxCertificate -Cert Cert:\LocalMachine\My\<thumbprint> -FilePath C:\backup\server.pfx -Password $pwd

# 7. URL 缓存与下载;certutil 常被滥用为下载工具,只对可信 URL 使用
# 影响范围:* delete 清除当前用户 WinINET 缓存;提权运行清的是管理员账户缓存,不是原用户
C:\> certutil -urlcache * delete
C:\> certutil -urlcache -split -f https://example.com/file.zip C:\Temp\file.zip

# 8. CA 操作;-ping 和 -CAInfo 只读,-revoke 和 -crl 会改变签发与吊销状态
# 影响范围:错误撤销会进入 CRL,客户端将拒绝该证书;发布错误 CRL 可能造成大面积验证失败
# 备份/回滚:先在 CA 上备份 CA 数据库和私钥;撤销前核对序列号,1 表示密钥泄露,错误撤销只能换发证书
C:\> certutil -config "CA01\Corp-CA" -ping
C:\> certutil -config "CA01\Corp-CA" -CAInfo
C:\> certutil -config "CA01\Corp-CA" -revoke <serial> 1
C:\> certutil -config "CA01\Corp-CA" -crl

易错点

相关条目

参考链接

读完自测

合上页面,先回答,再展开对照。

  1. certutil -store Root、certutil -user -store My 和 -addstore Root 分别操作哪些存储?哪些需要管理员权限?
  2. 为什么不应直接删除证书存储里的根证书?删除前怎样备份和回滚?
  3. certutil -hashfile 的默认算法是什么?脚本为什么不应解析 certutil 的文本输出?
答案
  1. 前两个分别查看机器 Root 存储、当前用户 My 存储;-addstore Root 写入机器 Root 存储,需要管理员权限,加 -user 才写入用户存储。
  2. 删除根证书会让依赖该根的 TLS、签名和客户端认证失败;删除前用 certutil -store Root <thumbprint> <文件> 导出证书(带私钥用 Export-PfxCertificate),回滚用 -addstore 重新导入。
  3. 默认 SHA1;应显式用 SHA256。certutil 输出随系统语言变化,解析本地化文本不可靠,需要对象结果时改用 Get-FileHash。
微信小程序码:扫码打开命令速查小程序

小程序版

微信扫码,随时查命令