Skip to main content
 首页 » 编程设计

python之是否可以在 Firefox 上列出已安装的证书

2025年02月15日8lautakyan007

我需要获取/列出安装在 Mozilla Firefox 上的所有证书。我想知道是否可以使用 Selenium webdriver 来管理它。

我找到了 this answer以及存储证书的文件:

%appdata%/Mozilla/Firefox/<user.profile>/cert8.db 

但是我无法解析这种文件格式。那么,是否可以使用 Selenium 在 Firefox 上安装所有证书?

请您参考如下方法:

您可以使用Mozilla 的certutil 工具读取数据库。请注意,如果您在命令提示符下运行 certutil,您将运行 Windows certutil,而不是 Mozilla 的。

要运行 Mozilla 的 certutil,您需要从其存储库下载网络安全服务 (NSS):

https://ftp.mozilla.org/pub/security/nss/releases/

但是 NSS 包需要 NSPR dll 才能正常运行。不知道为什么从 NSPR v4.6.2 开始所有包都只是源包,没有所需的 dll,所以直接转到 v4.6.1 链接并下载丢失的 dll 压缩包。

http://ftp.mozilla.org/pub/nspr/releases/v4.6.1/

将 certutil.exe 和 dll 放在同一个文件夹中后,运行以下命令:

certutil.exe -L -d %appdata%\Mozilla\Firefox\Profiles\<profile_folder_name_here> 

地点:

  • -L:列出所有证书
  • -d:指定包含证书和 key 数据库文件的数据库目录

有关其他 certutil 命令,请参见此处:

https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/tools/NSS_Tools_certutil

一个Python脚本示例是这样的:

import subprocess 
import os 
 
ff_prof_path = '{}\\Mozilla\\Firefox\\Profiles\\'.format(os.environ['APPDATA']) 
ff_prof_path = '{}{}'.format(ff_prof_path, os.listdir(ff_prof_path)[0]) 
result = subprocess.run('certutil -L -d {}'.format(ff_prof_path), stdout=subprocess.PIPE) 
print(result.stdout.decode('utf-8'))