From 37827eb8c747ce84ca8c13d7167a0e49965b48c8 Mon Sep 17 00:00:00 2001 From: zerorigin2019 Date: Mon, 16 Nov 2026 11:06:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=85=A5=E8=87=AA=E7=AD=BE=E5=90=8D?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=AD=BE=E5=90=8D=EF=BC=9A=E4=B8=BB=20exe?= =?UTF-8?q?=E3=80=81watchdog=E3=80=81=E5=AE=89=E8=A3=85=E5=8C=85=E4=B8=8E?= =?UTF-8?q?=E5=8D=B8=E8=BD=BD=E5=99=A8=E7=BB=9F=E4=B8=80=E7=AD=BE=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mygo.json 增加 windows.signCommand -> scripts/codesign/sign-file.ps1(PowerShell Set-AuthenticodeSignature,不依赖 Windows SDK)。新增 new-cert.ps1 生成 10 年期自签名证书、trust.ps1 导入当前用户 Root + TrustedPublisher(certutil 静默导入)。公钥 certs/ZeroOrigin-CodeSign.cer 进仓库,*.pfx 进 .gitignore。AGENTS.md / 架构文档 / README 同步签名流程与三条坑。 --- .gitignore | 3 ++ AGENTS.md | 35 +++++++++++--- README.md | 20 +++++++- certs/ZeroOrigin-CodeSign.cer | Bin 0 -> 1106 bytes docs/zero-origin-toolbox-architecture.md | 27 ++++++++++- mygo.json | 3 +- scripts/codesign/new-cert.ps1 | 57 +++++++++++++++++++++++ scripts/codesign/sign-file.ps1 | 52 +++++++++++++++++++++ scripts/codesign/trust.ps1 | 50 ++++++++++++++++++++ 9 files changed, 237 insertions(+), 10 deletions(-) create mode 100644 certs/ZeroOrigin-CodeSign.cer create mode 100644 scripts/codesign/new-cert.ps1 create mode 100644 scripts/codesign/sign-file.ps1 create mode 100644 scripts/codesign/trust.ps1 diff --git a/.gitignore b/.gitignore index 77c517f..2f3d69b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,9 @@ build/ *.exe *.dll +# 代码签名私钥(公钥 certs/*.cer 可以提交,.pfx 绝不进仓库) +*.pfx + # 本地配置与日志(运行时生成) config.json *.bak diff --git a/AGENTS.md b/AGENTS.md index 9a50401..5bab1a2 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -43,7 +43,10 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原 (含亮度 DDC/CI + gamma 兜底); `internal/assets` 托盘 PNG(go:embed);`internal/logging` slog;`internal/version` ldflags; - `resources/windows-amd64/` 随 exe 复制的平台资源(build.bat 编译出 watchdog.exe); - `assets/` 图标源文件;`mygo.json` mygo 应用清单;`bin/` 产物(gitignore);`portable/` 便携版。 + `assets/` 图标源文件;`mygo.json` mygo 应用清单;`bin/` 产物(gitignore);`portable/` 便携版; +- `scripts/` 发布与签名脚本:`publish-gitea.ps1`(上传 Gitea Release);`codesign/new-cert.ps1` + (生成自签名代码签名证书)、`codesign/sign-file.ps1`(mygo 的 `signCommand` 调用)、 + `codesign/trust.ps1`(把公钥证书导入当前用户信任存储);`certs/` 只放公钥证书。 ## 构建与验证 @@ -57,8 +60,17 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原 `%APPDATA%\mygo\update-keys\mygo-update.key`(**绝不进仓库**,用户需自行备份;`build.bat` 会读进 `MYGO_UPDATER_PRIVATE_KEY` 环境变量)。私钥缺失时 `go tool mygo build` 只构建、不产出更新包且不报错, 容易误以为已经发版(构建日志里有「已加载更新签名私钥」才是有签名的构建); +- 代码签名(Authenticode,自签名):`mygo.json` 的 `windows.signCommand` 指向 + `scripts/codesign/sign-file.ps1`,mygo 会依次签主 exe、资源里的 watchdog.exe、安装包与卸载器 + (`signWindowsResources` 只签未签名的 PE,所以 setup 里的卸载器也签上了)。证书由 + `scripts/codesign/new-cert.ps1` 生成(10 年,`CN=ZeroOrigin ToolBox`),公钥 + `certs/ZeroOrigin-CodeSign.cer` 进仓库、私钥备份在 `%APPDATA%\ZerOriginTool\codesign\` + (**绝不进仓库**,`.gitignore` 已忽略 `*.pfx`);新机器跑 `scripts/codesign/trust.ps1` 导入 + 当前用户的 Root + TrustedPublisher。自签名只让**导入过根证书的机器**不再提示「未知发布者」, + 对外分发无效(别人仍提示,SmartScreen 甚至更谨慎); - 发版顺序:改 `mygo.json` 的 `version` → 在 `CHANGELOG.md` 加同名 `## ` 小节(缺失时 mygo 报 - no notes)→ `build.bat quick` → `scripts/publish-gitea.ps1 -Owner zerorigin -Repo zerorigin-toolbox`; + no notes)→ `build.bat`(要安装版必须完整构建,`quick` 会跳过 setup)→ + `scripts/publish-gitea.ps1 -Owner zerorigin -Repo zerorigin-toolbox`; - 发布脚本为什么自己写:`go tool mygo build -upload` 只支持 GitHub Release(依赖 gh CLI)或 S3 桶, 自建 Gitea 没有内置支持。脚本用 Gitea REST API 把归档、delta、SHA256SUMS、安装版 setup 与便携版 zip 传到**固定的 `latest` Release**(新用户一个页面拿全部形态),同名 manifest **先删旧附件再传、 @@ -201,8 +213,8 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原 - 自更新有三条硬前提,缺一条就**静默不做后台检查**:`updates.publicKey` 非空、构建时注入了 feed (`updates.github` 或 `updates.url`,二者互斥)、`!IsDev()`,并且 exe 所在目录可写(`mygo.Updater.Enabled()`); - 发布源是自建 Gitea:`updates.url` = `https://git.zerorigin.cn/zerorigin/zerorigin-toolbox/releases/download/latest` - (2026-11-16 实测:TLS 正常、匿名可下载、单附件上限 100MB、`allowed_types` 含 `.gz`/`.json` 但**不含 - `.delta`**,所以 delta 上传会被拒、脚本直接跳过)。`updates.github` 天然写死 github.com + (2026-11-16 实测:TLS 正常、匿名可下载、单附件上限 100MB;`allowed_types` 白名单只约束 issue 类附件, + Release 附件连 `.exe` 与 `.delta` 都接受)。`updates.github` 天然写死 github.com (`cmd/mygo/updates.go:126`),国内网络常连接超时(实测 `dial tcp 20.6.7.17:443`),不要换回去; - 固定 tag `latest` 是 URL 的一部分:**改名等于换渠道**,已发出去的 exe 不会跟过来;Release `latest` 里的附件不能删,删了客户端就再也检查不到更新; @@ -222,8 +234,19 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原 - `build.bat` 读 `mygo.json` 的版本号必须 `Get-Content -Raw -Encoding UTF8`:不加会按 GBK 读中文, `ConvertFrom-Json` 报 `Invalid object passed in, ':' or '}' expected`,版本号取空 → 便携版 zip 被命名成 `0.0.0`。zip 名对不上发布脚本的过滤器就传不上去; -- Gitea 的 `allowed_types` 白名单只影响 issue 之类附件,**Release 附件实测接受 `.exe`** - (setup 安装包直接上传成功),不需要再套一层 zip;`.delta` 会被拒,脚本跳过它不影响更新; +- Gitea 的 `allowed_types` 白名单只影响 issue 之类附件,**Release 附件实测接受 `.exe` 与 `.delta`** + (setup 安装包、增量包都直接上传成功),不需要再套一层 zip;脚本里保留「delta 失败只警告」的容错; +- 自签名证书**不能用 mygo 的 `windows.certificate` 路径**:它固定给 signtool 传 + `/tr http://timestamp.digicert.com`(`cmd/mygo/winsign.go:79-82`,空字符串也会被填成该默认值), + DigiCert 不给自签名证书打时间戳,signtool 报错、整个构建失败。自签名必须走 `windows.signCommand` + 且不带 `/tr`;代价是证书 2036 年到期后旧签名失效,重新发证重签即可; +- 往 `Cert:\CurrentUser\Root` 导入证书**不要用 `Import-Certificate`**:它要交互式 UI 确认,脚本里 + 报 `UI is not allowed in this operation`,还会卡住等对话框;用 `certutil -user -addstore Root ` + (静默,见 `scripts/codesign/trust.ps1`); +- 本机没有 Windows SDK 与 signtool:签名用 PowerShell 内置的 `Set-AuthenticodeSignature`(.NET)即可, + 不要为签名去装几 GB 的 SDK;签名前必须先停掉正在跑的 exe,否则报「文件正被另一个进程使用」; +- 签名会改变 exe 内容:已发布的旧包与本地新包即使版本号相同也不一致,改签名后必须重新构建、 + 重新发布(delta 由 mygo 重新生成,不影响更新链路); - vet 对 crashguard.go / win32.go 的 unsafe.Pointer 警告是有意为之,不要"修复"。 ## 风格 diff --git a/README.md b/README.md index e2a776e..9642475 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,8 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原 1. 改 `mygo.json` 的 `version`; 2. 在 `CHANGELOG.md` 里加 `## <版本号>` 段(mygo 用它做更新窗口的发布说明); -3. `build.bat quick`(需要 `%APPDATA%\mygo\update-keys\mygo-update.key` 才会签名并产出更新包); +3. `build.bat`(完整构建才产出安装版;`quick` 会跳过 setup。需要 + `%APPDATA%\mygo\update-keys\mygo-update.key` 才会签名并产出更新包); 4. 发布: ```powershell @@ -29,3 +30,20 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原 ``` 脚本会先传归档与 delta,最后传 manifest,并先删除同名的旧 manifest 附件。 + +## 代码签名(自签名) + +构建产物(`ZerOriginTool.exe`、`watchdog.exe`、安装包与卸载器)都带 Authenticode 签名, +用于消除本机的「发布商:未知发布者」提示: + +- 公钥证书:[`certs/ZeroOrigin-CodeSign.cer`](certs/ZeroOrigin-CodeSign.cer); +- 新机器先导入信任(当前用户,无需管理员): + + ```powershell + powershell -NoProfile -ExecutionPolicy Bypass -File scripts\codesign\trust.ps1 + ``` + +- 私钥备份在 `%APPDATA%\ZerOriginTool\codesign\`(**不进仓库**,换机/重装前请备份)。 + +自签名只对**导入过这张根证书的机器**有效;对外分发仍会提示未知发布者,要彻底解决需要 +CA 签发的代码签名证书。 diff --git a/certs/ZeroOrigin-CodeSign.cer b/certs/ZeroOrigin-CodeSign.cer new file mode 100644 index 0000000000000000000000000000000000000000..6671301682387c398440fec23faae8d0720bb678 GIT binary patch literal 1106 zcmXqLV(~L*V%oNVnTe5!NkBMVr()d%hrNz>{sr$-T724omyJ`a&7xTReCx@qGc)fSt(^NJSFY|??ny02kw<$vgzwrm zXF6SsNbHxkw26_co%!(!oBgJhTblOCZf0crs<`F-TelC!eqC>v9xt2RZy$KIOWJV5 zuTOK{nPw<8yB0kCXZ-TYobvY@(q7g4yl=MEN2=d@d-DGVl^_31E4e=Owlr|ZZ7G~% zoF6}l!|`rY=~vwh7a`*^D}Bo{!TLhmdD6SC_BKB9-WKUBxaaX&Z;`C3d2Gqt2QD98 zUddLV?fh|^||Io&6_)>$IetC(F#`#kXU)Z%i?I zXX*G;j(sb3CsKGBGnUFfMj8a53NmrV?3x zM#ldvEX+*o4FzA6ik0T&yGHX9==D?2l@fhaLAIQuW| zbay~+6)+7mH%wkwA|I@9bY +[CmdletBinding()] +param( + [string]$Subject = 'CN=ZeroOrigin ToolBox, O=ZeroOrigin, C=CN', + [int]$Years = 10, + [switch]$Force +) + +$ErrorActionPreference = 'Stop' +$root = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) # 仓库根 +$cerDir = Join-Path $root 'certs' +$backupDir = Join-Path $env:APPDATA 'ZerOriginTool\codesign' +New-Item -ItemType Directory -Force -Path $cerDir, $backupDir | Out-Null + +$existing = Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert | + Where-Object { $_.Subject -eq $Subject -and $_.HasPrivateKey } +if ($existing -and -not $Force) { + "已存在同名证书:$($existing[0].Thumbprint)($($existing[0].NotAfter.ToString('yyyy-MM-dd')) 到期);要重建请加 -Force" + exit 0 +} + +$cert = New-SelfSignedCertificate ` + -Type CodeSigningCert ` + -Subject $Subject ` + -KeyUsage DigitalSignature ` + -KeyAlgorithm RSA ` + -KeyLength 3072 ` + -HashAlgorithm SHA256 ` + -CertStoreLocation Cert:\CurrentUser\My ` + -NotAfter (Get-Date).AddYears($Years) + +$cerPath = Join-Path $cerDir 'ZeroOrigin-CodeSign.cer' +Export-Certificate -Cert $cert -FilePath $cerPath -Force | Out-Null + +$password = -join ((48..57) + (65..90) + (97..122) | Get-Random -Count 24 | ForEach-Object { [char]$_ }) +$secure = ConvertTo-SecureString -String $password -AsPlainText -Force +$pfxPath = Join-Path $backupDir 'ZeroOrigin-CodeSign.pfx' +Export-PfxCertificate -Cert $cert -FilePath $pfxPath -Password $secure | Out-Null +Set-Content -Path (Join-Path $backupDir 'password.txt') -Value $password -Encoding ASCII + +"证书已生成" +" 主题 : $($cert.Subject)" +" 指纹 : $($cert.Thumbprint)" +" 到期 : $($cert.NotAfter.ToString('yyyy-MM-dd'))" +" 公钥证书 : $cerPath(可提交仓库,供其他机器导入)" +" pfx 备份 : $pfxPath(含私钥,绝不进仓库)" +" pfx 密码 : $password(同目录 password.txt 也有)" diff --git a/scripts/codesign/sign-file.ps1 b/scripts/codesign/sign-file.ps1 new file mode 100644 index 0000000..7bfdbeb --- /dev/null +++ b/scripts/codesign/sign-file.ps1 @@ -0,0 +1,52 @@ +#Requires -Version 5.1 +<# + 用本机自签名证书给一个 PE 文件做 Authenticode 签名。 + + mygo.json 的 windows.signCommand 指向本脚本:mygo build 会给主 exe、资源里的 + watchdog.exe、安装包和卸载器逐个调用它(%1 是文件路径,已带引号)。 + + 自签名证书没有 CA 时间戳服务,所以不加 RFC 3161 时间戳:证书到期后旧签名会失效, + 到那时重新发证并重签即可(自用场景够)。 + + 用法:powershell -NoProfile -ExecutionPolicy Bypass -File scripts\codesign\sign-file.ps1 <文件> +#> +[CmdletBinding()] +param( + [Parameter(Mandatory = $true, Position = 0)][string]$Path, + [string]$Subject = 'CN=ZeroOrigin ToolBox, O=ZeroOrigin, C=CN' +) + +$ErrorActionPreference = 'Stop' +if (-not (Test-Path -LiteralPath $Path)) { + "签名失败:文件不存在 $Path" + exit 1 +} + +$cert = Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert | + Where-Object { $_.Subject -eq $Subject -and $_.HasPrivateKey } | + Sort-Object NotAfter -Descending | + Select-Object -First 1 +if (-not $cert) { + "签名失败:找不到证书「$Subject」,先运行 scripts\codesign\new-cert.ps1" + exit 1 +} + +$leaf = Split-Path -Leaf $Path +$existing = Get-AuthenticodeSignature -LiteralPath $Path +if ($existing.SignerCertificate) { + "已签名,跳过:$leaf" + exit 0 +} + +try { + $sig = Set-AuthenticodeSignature -LiteralPath $Path -Certificate $cert -HashAlgorithm SHA256 +} catch { + "签名失败:$leaf — $($_.Exception.Message)" + exit 1 +} +if (-not $sig.SignerCertificate) { + "签名失败:$leaf — $($sig.StatusMessage)" + exit 1 +} +"已签名:$leaf($($sig.Status))" +exit 0 diff --git a/scripts/codesign/trust.ps1 b/scripts/codesign/trust.ps1 new file mode 100644 index 0000000..9a98934 --- /dev/null +++ b/scripts/codesign/trust.ps1 @@ -0,0 +1,50 @@ +#Requires -Version 5.1 +<# + 把自签名代码签名证书导入当前用户的「受信任的根证书颁发机构」与「受信任的发布者」。 + + 只影响当前用户、不需要管理员:导入后本机运行我们签名过的 exe 不再提示「未知发布者」。 + 换一台机器要再导一次;-Remove 可撤销。 + + 用法:powershell -NoProfile -ExecutionPolicy Bypass -File scripts\codesign\trust.ps1 [-CerPath <证书>] [-Remove] + + 为什么用 certutil 而不是 Import-Certificate:后者向 Root 存储导入要求交互式 UI 确认, + 在脚本里直接报 "UI is not allowed in this operation";certutil -user -addstore 是静默的。 +#> +[CmdletBinding()] +param( + [string]$CerPath, + [switch]$Remove +) + +$ErrorActionPreference = 'Stop' +if (-not $CerPath) { + $CerPath = Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) 'certs\ZeroOrigin-CodeSign.cer' +} +if (-not (Test-Path -LiteralPath $CerPath)) { + "找不到证书文件:$CerPath" + exit 1 +} + +$full = (Resolve-Path -LiteralPath $CerPath).Path +$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($full) +$thumb = $cert.Thumbprint + +foreach ($store in 'Root', 'TrustedPublisher') { + if ($Remove) { + $proc = Start-Process certutil -ArgumentList @('-user', '-delstore', $store, $thumb) -PassThru -NoNewWindow -Wait + } else { + $proc = Start-Process certutil -ArgumentList @('-user', '-addstore', $store, $full) -PassThru -NoNewWindow -Wait + } + if ($proc.ExitCode -ne 0) { + "操作失败:$store(certutil 退出码 $($proc.ExitCode))" + exit 1 + } +} + +if ($Remove) { + "已移除:$($cert.Subject)($thumb)" +} else { + "已信任:$($cert.Subject)($thumb)" + "到期:$($cert.NotAfter.ToString('yyyy-MM-dd'))" +} +exit 0