gpt4 book ai didi

python - 如何获取多个域的数据框中格式化的 ssl 信息?

转载 作者:行者123 更新时间:2023-12-04 22:41:14 28 4
gpt4 key购买 nike

如何在多个域的数据框中获取这些统计信息。例如:google.com、nyu.edu

预期输出:

name            expiry date                                   status 

google.com Wednesday, 12 August 2020 at 17:30:48 active
nyu.edu expired/No Cert expired/No Cert

试过这个:
import ssl, socket
import pandas as pd

hostname =['google.com','nyu.edu']
datas=[]

for i in hostname:

ctx = ssl.create_default_context()
with ctx.wrap_socket(socket.socket(), server_hostname=i) as s:
s.connect((i, 443))
cert = s.getpeercert()
issued_to = subject['commonName']
notAfter=cert['notAfter']

data=[{"name":issued_to},{"expiry":notAfter}]

datas.append(data)


df = pd.DataFrame(datas)


试过了,这无法确定哪些证书已过期,哪些未过期。

最佳答案

如果您使用 getpeercert()如果验证失败,您将无法在输出中检索证书(例如,您可以使用 https://expired.badssl.com/ 进行测试)。

来自 this answer , 另一种方法是使用 getpeercert(True)获取证书的二进制输出并使用 OpenSSL 解析它。

在以下示例中,我添加了 expiry通过与当前日期比较来检查证书是否已过期的列:

import ssl, socket
import pandas as pd
from datetime import datetime
import OpenSSL.crypto as crypto

hostname = ['google.com','nyu.edu','expired.badssl.com']
datas = []
now = datetime.now()

for i in hostname:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
with ctx.wrap_socket(socket.socket(), server_hostname = i) as s:
s.connect((i, 443))
cert = s.getpeercert(True)
x509 = crypto.load_certificate(crypto.FILETYPE_ASN1,cert)
commonName = x509.get_subject().CN
notAfter = datetime.strptime(x509.get_notAfter().decode('ascii'), '%Y%m%d%H%M%SZ')
notBefore = datetime.strptime(x509.get_notBefore().decode('ascii'), '%Y%m%d%H%M%SZ')
datas.append({
"name": commonName,
"notAfter": notAfter,
"notBefore": notBefore,
"expired": (notAfter < now) or (notBefore > now)
})

df = pd.DataFrame(datas)
print(df)

输出 :
           name            notAfter           notBefore  expired
0 *.google.com 2020-08-12 12:00:48 2020-05-20 12:00:48 False
1 nyu.edu 2021-01-06 23:59:59 2019-01-07 00:00:00 False
2 *.badssl.com 2015-04-12 23:59:59 2015-04-09 00:00:00 True

请注意,我使用了 this post将时间戳格式的日期转换为日期时间

另外,如果你想测试无效证书,可以查看 https://badssl.com/对于可能出现特定验证错误的测试域列表

关于python - 如何获取多个域的数据框中格式化的 ssl 信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62251084/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com