当前位置:首页 > 操作系统 > 正文内容

windows在cygwin64下运用acme.sh批量签发Let's Encrypt的ssl证书,并用powershell重新分配iis证书

邻居的猫3个月前 (12-09)操作系统1237

运用条件

本脚本是在运用阿里云Windows服务器的条件,假如运用其他dns服务,请参看acme.sh的dns相关文档

装备好cygwin64、acme.sh并装备好阿里云账户,openssl最好也装置上

cygwin64装备参阅

acme.sh装备

openssl参阅,增加-certpbe PBE-SHA1-3DES -keypbe PBE-SHA1-3DES -nomac 是为了应对pfx输入密钥不正确

终究途径便是项目途径

一、装置cygwin64

1. 下载

cygwin64官网下载
cygwin64百度云
假如windows server 08R2发动装置程序失利,请运用cmd运转

setup-x86_64.exe --allow-unsupported-windows --site http://ctm.crouchingtigerhiddenfruitbat.org/pub/cygwin/circa/64bit/2024/01/30/231215 --no-verify

其他老旧体系请参阅cygwin64官网网页的How can I install the last Cygwin version for an old, unsupported Windows答复
cygwin64下载示例

2. 非老旧体系装备镜像点

假如不是老久体系 ,则能够考虑运用阿里云镜像

阿里云镜像点: https://mirrors.aliyun.com
非老体系参阅镜像点

3. 装置包参阅列表

curl
cron
bzip2
wget
gcc-core
gcc-g++
make
openssh
openssl
lynx
dos2unix
vim

4. Windows装备环境变量

在Path后加;C:\cygwin64\bin;C:\cygwin64\usr\local\bin
cygwin64装备环境变量

5. 假如遇到VIM不能张贴仿制

翻开cygwin64桌面图标Cygwin64 Terminal,进入操控台
cd /home
vim .vimrc
i键进入修正形式,键入以下内容

set mouse=c
syntax on

esc键退出修正形式,输入:wq即可保存文件

shift+ins 是cygwin64操控台下的张贴键

二、装置acme.sh

1. 经过curl指令直接装置acme.sh

curl -k https://raw.githubusercontent.com/acmesh-official/acme.sh/master/acme.sh | sh -s -- --install-online -m  [email protected]

假如报错如图
acme.ssh ssl证书报错
输入以下,使curl疏忽大局ssl认证,成功后 ,再去经过curl装置acme.sh试试

echo insecure > ~/.curlrc

2. 假如过程二.1成功则不需要看此条

1)直接去git下载acme.sh的源码

acme.sh git资源
acme.sh 百度云
示例
运用解压软件如7z将acme.sh-3.0.7.tar.gz解压两次放在/usr/download目录下,偏重命名为acme.sh,如图
目录示例

2)装置acme.sh

切换到cygwin64操控台

cd /usr/download/acme.sh
./acme.sh --install -m [email protected]

3. 装备acme.sh

0)装置完成后从头加载 Bash

source ~/.bashrc

1)装备主动更新

acme.sh --upgrade --auto-upgrade

2)切换至Letsencrypt

acme.sh --set-default-ca --server letsencrypt

3)装备DNSApi,这儿是参阅阿里云的dnsapi,其他请参阅acme.sh的dns相关文档

cd /home/Administrator/.acme.sh
vim account.conf
i键进入修正形式,输入阿里云帐号的AccessKey相关内容

export Ali_Key="LTAIsadfd8J9qs4fxxxxxxxxxx"
export Ali_Secret="Xp3adgfNDOW0CJcPLKoUwqxxxxxxxxxx"

esc键退出修正形式,输入 :wq 保存文件并退出

三、预备powershell脚本

PowerShell 脚本,运用前,更改履行战略

关于履行战略:
https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.4#powershell-execution-policies

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

以下是PowerShell脚本,假如乱码,请将文件保存为GB2312字符文件

1.acme主动请求证书脚本,请保存在cygwin64批量主动签发证书.ps1文件中

# 设定bash别号,假如体系装了wsl或许抵触,运用别号强制运用cygwin64指令
Set-Alias -Name bash C:\cygwin64\bin\bash.exe
# 设定openssl别号,假如体系装了wsl或许抵触,运用别号强制运用cygwin64指令
Set-Alias -Name openssl C:\cygwin64\bin\openssl.exe
# 公共证书备份途径,必须带盘符
$commonPath = "E:\cert"
# cygwin64用户途径,必须带盘符,Administrator是计算机装置cygwin64的用户名,不同状况姓名不同
$userPath = "C:\Cygwin64\home\Administrator"
# cygwin64内部用户途径
$cygwinUserPath = "/home/Administrator"
# pfx文件密钥
$pfxPassword = "dgfdgsdfg"

# 证书在以下列表中增加即可
$data = @(
    [pscustomobject]@{
        # 要请求的域名
        domain = "buy.test.com";
        # 项目途径,可空
        path   = "D:\Web\Main"
    },
    [pscustomobject]@{
        domain = "go.test.com";
    },
    [pscustomobject]@{
        domain = "*.test.com";
    }
)

# 假如公共途径不存在,那么创立,假如途径已存在,不影响指令持续履行
if (![System.IO.Directory]::Exists($commonPath)) {
    md $commonPath
}

# 获取公共证书备份途径在cygwin64环境下的途径
$cygwinCommonPath = "/cygdrive/" + $commonPath.Replace(":", "").Replace("\", "/");

# 重试次数
$retryCnt = 0

function IssueKey {
    param (
        [string]$currDomain,
        [string]$currPath,
        [bool]$force
    )
    # 假如重试次数大于2,那么退出当时函数
    if ($retryCnt -gt 2) {
        return
    }

    # 替换特别途径名
    $domain = $currDomain.Replace("*", "_")
    $cygDomain = $currDomain.Replace("*", "\*")

    # 设置履行指令后缀,这儿是acme.sh相关指令,修正dns api就在这儿
    $issueCmd = "--issue --dns dns_ali -d $($currDomain) --key-file $($cygwinCommonPath)/$($domain).key --fullchain-file $($cygwinCommonPath)/$($domain)_fullchain.cer"
    #假如是强制重发
    if ($force) {
        $issueCmd += " --force"
    }
    Write-Host 被履行的acme.sh后缀指令 $issueCmd
    bash --login -i -c "acme.sh $($issueCmd)"
    
    Write-Host 查看$currDomain key文件巨细和backup目录是否存在文件
    
    $commonFullChainPath = "$($commonPath)\$($domain)_fullchain.cer"
    $commonKeyPath = "$($commonPath)\$($domain).key"
    $commonPfxPath = "$($commonPath)\$($domain).pfx"
    $commonPemPath = "$($commonPath)\$($domain).pem"
    # cygwin环境下的目录
    $cygwinCertPath = "$($cygwinUserPath)/.acme.sh/$($cygDomain)_ecc"

    Write-Host 赋予权限
    bash --login -i -c "chmod -R g+rw $($cygwinCertPath)"

    Write-Host 复制Key、Fullchain文件到公共目录
    bash --login -i -c "cp -f $($cygwinCertPath)/$($cygDomain).key $($cygwinCommonPath)/$($domain).key"
    bash --login -i -c "cp -f $($cygwinCertPath)/fullchain.cer $($cygwinCommonPath)/$($domain)_fullchain.cer"

    # 判别绝对途径下证书文件是否存在,假如不存在直接强制从头生成证书
    Write-Host 第一次查看$commonKeyPath 文件是否存在
    if (![System.IO.File]::Exists($commonKeyPath)) {
        Write-Host 查看key.bak是否存在
        # 测验从备份中康复文件到原目录
        bash --login -i -c "cp -f $($cygwinCertPath)/backup/key.bak $($cygwinCertPath)/$($cygDomain).key"
        # 测验从原目录复制文件到公共目录
        bash --login -i -c "cp -f $($cygwinCertPath)/$($cygDomain).key $($cygwinCommonPath)/$($domain).key"
        Write-Host 第2次查看$commonKeyPath 文件是否存在
        if (![System.IO.File]::Exists($commonKeyPath)) {
            Write-Host 公共途径证书文件不存在 $commonKeyPath 行将强制从头请求
            # 重试次数+1
            $retryCnt += 1
            IssueKey -currDomain $currDomain -currPath $currPath -force $true
        }
    }

    Write-Host 第一次查看 $($domain).pfx 文件是否存在
    if (![System.IO.File]::Exists($commonPfxPath)) {
        Write-Host openssl转化pfx    
        # openssl 3.x 版别  
        openssl pkcs12 -export -certpbe PBE-SHA1-3DES -keypbe PBE-SHA1-3DES -nomac -out $commonPfxPath -inkey $commonKeyPath -in $commonFullChainPath -password pass:$pfxPassword
        # openssl 1.0 版别
        # openssl pkcs12 -export -out $commonPfxPath -inkey $commonKeyPath -in $commonFullChainPath -password pass:$pfxPassword
    }

    Write-Host 第2次查看 $($domain).pfx 文件是否存在
    if (![System.IO.File]::Exists($commonPfxPath)) {
        # 假如重试次数大于2,那么退出当时函数
        if ($retryCnt -gt 2) {
            return
        }
        else {
            # 重试次数+1
            $retryCnt += 1
            IssueKey -currDomain $currDomain -currPath $currPath -force $true
        }
    }

    # 假如pem格局文件不存在,那么运用openssl转化成pem格局
    if (![System.IO.File]::Exists($commonPemPath)) {
        Write-Host openssl转化pem  
        openssl pkcs12 -in $commonPfxPath -out $commonPemPath -nodes -password pass:$pfxPassword
    }

    # 假如目标path不为空且存在,将证书复制到项目途径下 
    if (![string]::IsNullOrEmpty($currPath)) {
        Write-Host 复制$domain 证书文件到项目目录
        Copy-Item -Path $commonKeyPath -Destination "$($currPath)\$($domain).key" -Force
        Copy-Item -Path $commonPfxPath -Destination "$($currPath)\$($domain).pfx" -Force
        Copy-Item -Path $commonPemPath -Destination "$($currPath)\$($domain).pem" -Force
    }
}

foreach ($curr in $data) {
    # 每次弄新的,就重置次数
    $retryCnt = 0

    try {
        # 登录到cygwin运用acme.sh签发证书,并将文件复制到公共证书目录,并转成pfx格局,暗码一致运用$pfxPassword
        Write-Host 地址 $curr.path
        Write-Host 签发 $curr.domain 证书
        IssueKey -currDomain $curr.domain -currPath $curr.path -force $false
    }
    catch {
        Write-Host "产生反常:$_"
        break
    }
}
# 履行完后退出
exit

2.iis分配证书脚本,请保存在iis批量从头分配证书.ps1文件中

# 运用前先将战略设置为不严厉 Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
# 确保证书有用的状况下再运转次脚本,将证书称号、证书目录、密钥放入以下数组
# 公共证书密钥
$pfxpassword = "dgfdgsdfg"
# 公共证书途径
$pfxCommandDir= "E:\cert"
# 域名
$domain="test.com"
# 服务器上的证书与端口映射联系
$data = @(
    [pscustomobject]@{subDomain = '*';port=443}
    [pscustomobject]@{subDomain = 'buy';port=8443}
    [pscustomobject]@{subDomain = 'go';port=7443}
)
$certRootStore = "LocalMachine"
$certStore = "My"
# 创立证书存储
$store = new-object System.Security.Cryptography.X509Certificates.X509Store($certStore, $certRootStore) 
$store.open("MaxAllowed")

# 开端循环数组操作
foreach ($element in $data) {
    $currDomain=$element.subDomain.Replace("*","_");
    $pfxPath = "$($pfxCommandDir)\$($currDomain).$($domain).pfx"
    Write-Host $pfxPath
    # 创立pfx目标
    try {
        $certificateObject = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($pfxPath, $pfxpassword)
        if (!$certificateObject.Verify()) {
            Write-Host $element.subDomain证书验证失利, 请查看相关装备
            break
        }
        else {
            Write-Host 证书验证成功
        }
        # 存储证书到个人
        if (!$store.Certificates.Contains($certificateObject)) {
            $store.Add($certificateObject)
            Write-Host 导入证书成功
        }
        $newThumbprint = $certificateObject.Thumbprint
        Write-Host 获取证书信息成功
        $guid = [System.Guid]::NewGuid()
        $applicationID = "{$($guid)}"
        $addr = "0.0.0.0:$($element.port)"
        Write-Host $addr $newThumbprint
        # netsh删去原有监听端口
        netsh http delete sslcert ipport=$addr
        # netsh增加端口
        netsh http add sslcert ipport=$addr certhash=$newThumbprint appid=$applicationID
        # 假如目标path不为空且存在,将证书复制到项目途径下 
        if (![string]::IsNullOrEmpty($element.path)) {
            $dest = "$($element.path)\$($currDomain).$($domain).pfx"
            Copy-Item -Path $pfxPath -Destination $dest -Force
            Write-Host 复制文件到项目目录成功
        }
    }
    catch {
        Write-Host "产生反常:$_"
        break
    }
}
# 封闭证书存储
$store.close() 

# 履行完后退出
exit

创立使命方案程序参阅

在惯例页面中,勾选“只在用户登录时运转”以及“运用最高权限”,保存即可

使命方案程序是依照次序履行的。

使命方案程序设置界面作用终究如图

以Windows Server 2008R2所示:
惯例界面
操作界面

扫描二维码推送至手机访问。

版权声明:本文由51Blog发布,如需转载请注明出处。

本文链接:https://www.51blog.vip/?id=603

分享给朋友:

“windows在cygwin64下运用acme.sh批量签发Let's Encrypt的ssl证书,并用powershell重新分配iis证书” 的相关文章

将计算机参加和脱离域

将计算机参加和脱离域

一:将核算机参加域 给核算机配IP ip地址随意填,只要和dns在同一个网段就行 首选dns填dns1的地址,备用dns填dns2的地址 在这台电脑(此电脑)点击特点,这样能进入核算体系 点击更改设置,更改域 输入更改的域名 输入dns1域管理员的暗码 二:查验核算机参加域的状况 翻开dns...

windows查看端口是否被占用,Windows系统下查看端口是否被占用的详细指南

1. 使用命令提示符(CMD): 打开命令提示符(CMD)。 输入 `netstat ano` 并按 Enter。这将显示所有当前活动的网络连接和它们所使用的端口号。 查找你感兴趣的端口号,然后查找与该端口关联的进程ID(PID)。 你可以使用 `tasklist | fin...

linux中mv,Linux mv 命令概述

在Linux中,`mv`(move)命令用于移动或重命名文件和目录。当你使用`mv`命令时,你可以将文件从一个目录移动到另一个目录,或者更改文件名。以下是`mv`命令的基本语法:```mv 源文件 目标文件```或者```mv 源文件/目录 目标目录```其中: `源文件`:你想要移动或重命名的...

linux改名,文件和目录的重命名

linux改名,文件和目录的重命名

在Linux中,你可以使用`mv`命令来重命名文件或目录。`mv`是move的缩写,但在这个上下文中,它也用于重命名。下面是`mv`命令的基本语法:```mv 原始文件名 新文件名```或者,如果你要重命名一个目录,可以使用:```mv 原始目录名 新目录名```例如,如果你想将一个名为`oldfi...

windows7系统修复,轻松解决常见问题

windows7系统修复,轻松解决常见问题

Windows 7 系统修复是一个比较复杂的过程,具体步骤如下:1. 准备启动盘: 首先,你需要准备一个 Windows 7 的启动盘。这可以通过下载 Windows 7 的 ISO 文件并使用工具(如 Windows USB/DVD Download Tool)创建启动盘来完成。2. 启动到修复模...

windows纸牌,经典电脑游戏的魅力与玩法解析

windows纸牌,经典电脑游戏的魅力与玩法解析

Windows纸牌是一款经典且广受欢迎的纸牌游戏,其历史可以追溯到1988年,由微软的一名暑期实习生Wes Cherry开发。这款游戏最早随Windows 3.0系统发布,旨在帮助用户熟悉鼠标操作,并逐渐成为Windows操作系统中不可或缺的一部分。 游戏规则Windows纸牌的基本规则如下:1....