【问题标题】:certutil dump - expiration date p12 - powershellcertutil 转储 - 到期日期 p12 - powershell
【发布时间】:2020-03-09 03:41:24
【问题描述】:

我并不是真正的程序员,但我正在尝试修复一个 Powershell 脚本,它可以帮助我从 P12 证书文件中获取到期日期。

这是命令:

C:\>certutil.exe -dump c:\1\p_CERT.p12 证书:未加密 ================ 证书0 ================ ================ 开始嵌套级别 1 ================ 元素 0: 序列号:03g3 发行人:CN=COMPANY MY CA v2,O=Company SL,C=GL NotBefore: 2012-06-20 11:47 之后:2022-06-20 11:46 主题:CN=COMPANY MY CA v2, O=Company SL, C=GL 签名匹配公钥 根证书:主题匹配颁发者 证书哈希(sha1):1234124214214214214sdada122 ---------------- 结束嵌套级别 1 ---------------- 没有关键提供者信息 找不到用于解密的证书和私钥。 ================ 证书 1 ================ ================ 开始嵌套级别 1 ================ 元素 1: 序列号:2100 发行人:CN=COMPANY MY CA v2,O=Company SL,C=GL NotBefore: 2018-12-07 08:48 NotAfter: 2020-12-07 08:48 主题:CN=private_CERT + SERIALNUMBER=445566778899,O=OTHER_Company,C=GL 非根证书 证书哈希(sha1):1234423hhhshshhshsh444423232 ---------------- 结束嵌套级别 1 ---------------- 密钥容器 = PfxContainer 提供者 = PfxProvider 加密测试失败 CertUtil:-dump 命令成功完成。

有趣的是第二个部分,即带有 SERIALNUMBER 的“NotAfter: 2020-12-07 08:48”

使用这个作为灵感来源 (https://gist.github.com/banterCZ/9bd6aa1ab49995fdf018),感谢 banterCZ,我尝试了以下方法。但它不起作用,因为结果不是“NotAfter”字段。 有什么想法如何用脚本把这部分弄出来“NotAfter: 2020-12-07 08:48”?

########################################################
#
#       Check certificates inside a p12 certificate file
#
########################################################

[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True)]
    [string]$location,

    [Parameter(Mandatory=$True)]
    [int]$threshold
)

[System.Threading.Thread]::CurrentThread.CurrentCulture = "en-US"

$certutil="certutil.exe"
$certificate = Invoke-Expression "$certutil -dump '$location'"

foreach($line in $certificate){    
    if($line.Contains("Element 1:")){    
        $index = $line.Substring(0,20)
        write-host $index
        $dateAsString = $line | Select-String -Pattern 'NotAfter' | foreach {$_.groups[""].value}
        write-host $dateAsString
        #$expirationDate = [datetime]::parseexact($dateAsString,"ddd MMM dd HH:mm:ss yyyy",$null)
        break
    }
}

$now = ([System.DateTime]::Now)
$daysToExpire = [int]($expirationDate - $now).TotalDays

if ($threshold -lt $daysToExpire) {
    Write-Host "[OK] Certificate '$alias' expires in '$expirationDate' ($daysToExpire day(s) remaining)."
    exit 0
} elseif ($daysToExpire -lt 0) {
    Write-Host "[CRITICAL] Certificate $alias has already expired."
    exit 2
} else {
    Write-Host "[WARNING] Certificate '$alias' expires in '$expirationDate' ($daysToExpire day(s) remaining)."
    exit 1
}

谢谢!

【问题讨论】:

  • 尝试改用 Get-PfxData cmdlet
  • 当我运行 Get-PfxData 我在下面得到这个响应。这个 Get-PfxData 很可能需要证书的密码,如果 certutil 准确地转储了我在 OtherCertificates ----------------- {[Subject]... 之后的信息,我最好避免将其附加到脚本中

标签: windows powershell certificate


【解决方案1】:

感谢一位好伙伴,J.I.这个脚本是固定的吗? 我希望它可以帮助其他人。

它查找带有 SERIALNUMBER 的行,将后面的 1 行保存到一个变量中,依此类推。

########################################################
#
#       Check certificates inside a p12 file
#       J.I., banterCZ, trustbyte & stackoverflow
#
########################################################

[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True)]
    [string]$location,

    [Parameter(Mandatory=$True)]
    [string]$certserial,

    [Parameter(Mandatory=$True)]
    [int]$warning,

    [Parameter(Mandatory=$True)]
    [int]$critical

)

[System.Threading.Thread]::CurrentThread.CurrentCulture = "en-US"

$certutil="certutil.exe"
$certificate = $(Invoke-Expression "$certutil -dump '$location'")


$row = [array]::IndexOf($certificate,$certificate -match "$certserial")
$notbefore = $certificate[$row-1]
$notbefore = $notbefore.ToString().Replace(" NotAfter: ","")
$now = (Get-Date).tostring("yyyy-MM-dd HH:mm")

$date1 = get-date $notbefore
$date2 = get-date $now
$daysToExpire = [int]($date1-$date2).TotalDays


if ($daysToExpire -lt $critical) {
    Write-Host "[CRITICAL] Certificate '$location' expires in '$notbefore' ($daysToExpire day(s) remaining)."
    exit 2
} elseif ($daysToExpire -lt $warning) {
    Write-Host "[WARNING] Certificate '$location' expires in '$notbefore' ($daysToExpire day(s) remaining)."
    exit 1
} else {
    Write-Host "[OK] Certificate '$location' expires in '$notbefore' ($daysToExpire day(s) remaining)"
    exit 0
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多