原 classic 是 Direct2D 自绘壳(shell/classic/shell5.cpp、shell6.cpp),里面
login_mode、FetchAppInfo、卡密、billing_type 全是 0 处匹配 —— 它没有
「读 /info 再改界面」这套逻辑,界面永远固定成「账号+密码+注册」。
而它偏偏是面板皮肤的默认值(NormalizeTheme("")、ShellThemeOrder[0]、
Runner 和 BuildCfgThemedVer 的兜底都指向它),等于默认就把界面配置废掉。
## 做法
照搬原 Direct2D 壳的配色,用 DuiLib 重写一套 classic 皮肤:
#1E1E1E 卡片/主背景 #252527 标题栏 #252526 输入框
#3F3F46 边框/输入框边 #0E639C 主按钮 #0A4E7A 按下
#E6E6E6 主文字 #9D9D9D 次要文字 #0078D4 聚焦
做法是把 dark 皮肤的 XML 按上面这张表做颜色替换(11 种色全部命中),
布局和控件沿用 DuiLib 那套 —— 也就是拿到 classic 的观感 + DuiLib 的功能。
(skins_v4.h 里加了 kSkin_classic,kSkins 注册为 470x436。)
## 选壳逻辑
AssetsThemed 不再给 classic 开后门:
改动前:theme == "classic" → shell64.bin (Direct2D 老壳)
改动后:所有主题 → shell64_dui.bin (DuiLib 壳)
老产物不受影响(已经加过壳的照跑),HasVfy 仍然认 .vfy 节。
shell64.bin / shell32.bin 留在 assets 里没删,要回退直接改回来即可。
## 实测
- 64 位壳 907264 → 924160 字节、32 位 1045504 → 1059328(多出的是 classic 皮肤)
- 用 classic 主题加壳产物 1378816 字节,wine 跑起来:
深灰底 + 蓝色主按钮(VS Code 观感)、4 个 tab「登录/我的设备/卡密充值/找回绑定」、
单个「卡密」输入框、「卡密登录」按钮、无黑框错位
578 lines
19 KiB
Go
578 lines
19 KiB
Go
// Package protect:服务端给 PE 注入验证壳(改入口 + 内嵌壳 DLL + 双层 CRC32)
|
||
//
|
||
// 产物结构:原 exe 追加 .load 节 = [48B 头][引导 blob][壳 DLL][配置 JSON]
|
||
// 入口指向 .load 内的引导代码;引导代码反射加载 DLL(零落盘)。
|
||
package protect
|
||
|
||
import (
|
||
"bytes"
|
||
"debug/pe"
|
||
"embed"
|
||
"encoding/binary"
|
||
"fmt"
|
||
"hash/crc32"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
"time"
|
||
"verification-platform-backend/internal/model"
|
||
)
|
||
|
||
//go:embed assets/blob64.bin assets/blob32.bin assets/shell64.bin assets/shell32.bin assets/shell64_dui.bin assets/shell32_dui.bin
|
||
var assets embed.FS
|
||
|
||
const (
|
||
hdrSize = 48
|
||
crcOff = 36
|
||
secName = ".load"
|
||
legacySec = ".vfy" // 旧产物用 .vfy,检测时一并识别(新产物一律写 .load)
|
||
shellVer = 8
|
||
secAttrib = 0x60000020 // CODE | EXECUTE | READ
|
||
)
|
||
|
||
// ShellThemes 可选的验证界面皮肤。classic = 原 Direct2D 壳,其余是 DuiLib 壳
|
||
var ShellThemes = map[string]string{
|
||
"classic": "经典(Direct2D·不支持按配置切卡密/隐藏注册)",
|
||
"dark": "深色 · 竖排紧凑",
|
||
"light": "浅色 · 办公风",
|
||
"industrial": "工业蓝 · 设备授权",
|
||
"compact": "极简横幅 · 单行激活",
|
||
"split": "左右分栏 · 品牌竖栏",
|
||
"card": "宽扁卡片 · 双列说明",
|
||
"terminal": "终端风 · 命令行",
|
||
}
|
||
|
||
// ShellThemeOrder 主题的展示顺序(报错文案、前端下拉都按这个来)
|
||
// dark 排第一:面板下拉默认选第一项,而 classic 不支持按配置切换登录方式
|
||
var ShellThemeOrder = []string{"dark", "light", "industrial", "compact", "split", "card", "terminal", "classic"}
|
||
|
||
// ShellThemeList 返回「键=显示名」的有序切片,给前端/文档用
|
||
func ShellThemeList() []map[string]string {
|
||
out := make([]map[string]string, 0, len(ShellThemeOrder))
|
||
for _, k := range ShellThemeOrder {
|
||
out = append(out, map[string]string{"key": k, "label": ShellThemes[k]})
|
||
}
|
||
return out
|
||
}
|
||
|
||
// NormalizeTheme 校验并归一化主题名
|
||
func NormalizeTheme(theme string) string {
|
||
t := strings.ToLower(strings.TrimSpace(theme))
|
||
if t == "" {
|
||
// 默认给 dark 而不是 classic:classic 是 Direct2D 老壳,源码里没有
|
||
// login_mode / FetchAppInfo 这套逻辑(login_mode、卡密、billing_type
|
||
// 全是 0 处匹配),界面永远固定成「账号+密码+注册」——跟着应用配置
|
||
// 切卡密、隐藏注册、控制充值 tab 全都不会生效。
|
||
return "dark"
|
||
}
|
||
if _, ok := ShellThemes[t]; ok {
|
||
return t
|
||
}
|
||
return ""
|
||
}
|
||
|
||
// Assets 取出指定架构的 blob 与壳 DLL(classic 版)
|
||
func Assets(arch string) (blob, dll []byte, err error) {
|
||
return AssetsThemed(arch, "classic")
|
||
}
|
||
|
||
// AssetsThemed 按主题取壳。DuiLib 壳 x64 / x86 各有一份构建
|
||
// (shell64_dui.bin / shell32_dui.bin),两边的皮肤完全一致。
|
||
//
|
||
// 注意:classic 现在也走 DuiLib 壳。原来它指向 shell64.bin / shell32.bin
|
||
// 那套 Direct2D 壳,但那套壳里没有 login_mode / FetchAppInfo 逻辑,
|
||
// 界面永远固定成「账号+密码+注册」,跟着应用配置切卡密登录、隐藏注册、
|
||
// 控制充值 tab 全都不生效。现在已经用 DuiLib 按原配色(VS Code 暗色系)
|
||
// 重做了一套 classic 皮肤,走和其他 7 套一样的链路。
|
||
// 老产物(.vfy 节)不受影响,HasVfy 仍然认 .vfy。
|
||
func AssetsThemed(arch, theme string) (blob, dll []byte, err error) {
|
||
var bp, dp string
|
||
switch arch {
|
||
case "x64":
|
||
bp, dp = "assets/blob64.bin", "assets/shell64_dui.bin"
|
||
case "x86":
|
||
bp, dp = "assets/blob32.bin", "assets/shell32_dui.bin"
|
||
default:
|
||
return nil, nil, fmt.Errorf("不支持的架构: %s", arch)
|
||
}
|
||
blob, err = assets.ReadFile(bp)
|
||
if err != nil {
|
||
return nil, nil, err
|
||
}
|
||
dll, err = assets.ReadFile(dp)
|
||
if err != nil {
|
||
return nil, nil, err
|
||
}
|
||
return blob, dll, nil
|
||
}
|
||
|
||
// HasVfy 判断 PE 里是不是已经有壳节(.load,或旧产物的 .vfy)。
|
||
// 二次加壳会在文件里留下两个壳节:旧壳的 cfg 指向老服务器、白占几百 KB,
|
||
// 目标机跳的却是新节,排查时极易被旧头误导,所以入口直接挡掉。
|
||
func HasVfy(target []byte) bool {
|
||
f, err := pe.NewFile(bytes.NewReader(target))
|
||
if err != nil {
|
||
return false
|
||
}
|
||
defer f.Close()
|
||
for _, s := range f.Sections {
|
||
if strings.HasPrefix(s.Name, secName) || strings.HasPrefix(s.Name, legacySec) {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// DetectArch 兼容旧调用:只接受 PE,返回 x64/x86
|
||
func DetectArch(target []byte) (string, error) {
|
||
info := DetectFile(target, "")
|
||
if info.Kind != KindPE {
|
||
return "", unsupportedFor(info)
|
||
}
|
||
if info.Arch == "" {
|
||
return "", fmt.Errorf("无法识别的 PE 架构")
|
||
}
|
||
return info.Arch, nil
|
||
}
|
||
|
||
// Patch 把验证壳注入 target(按类型分发;目前只有 PE 具备注入实现)
|
||
func Patch(target, cfg []byte, arch string) (out []byte, info Info, err error) {
|
||
return PatchSigned(target, cfg, arch, false)
|
||
}
|
||
|
||
// PatchSigned signed=true 时写 flags.bit1 并留空 crcFile(供 Authenticode 重签)
|
||
func PatchSigned(target, cfg []byte, arch string, signed bool) (out []byte, info Info, err error) {
|
||
return PatchSignedThemed(target, cfg, arch, signed, "classic")
|
||
}
|
||
|
||
// PatchSignedThemed 同 PatchSigned,并指定验证界面主题
|
||
func PatchSignedThemed(target, cfg []byte, arch string, signed bool, theme string) (out []byte, info Info, err error) {
|
||
if arch == "" {
|
||
return nil, info, fmt.Errorf("缺少目标架构")
|
||
}
|
||
if theme == "" {
|
||
theme = "dark"
|
||
}
|
||
blob, dll, err := AssetsThemed(arch, theme)
|
||
if err != nil {
|
||
return nil, info, err
|
||
}
|
||
return patchWith(target, blob, dll, cfg, signed)
|
||
}
|
||
|
||
// PatchAuto 自动识别类型后注入;非 PE 返回 *UnsupportedError
|
||
func PatchAuto(target, cfg []byte, name string) (out []byte, info Info, err error) {
|
||
return PatchAutoSigned(target, cfg, name, false)
|
||
}
|
||
|
||
// PatchAutoSigned 自动识别类型并注入,signed 仅对 PE 有效
|
||
func PatchAutoSignedThemed(target, cfg []byte, name string, signed bool, theme string) (out []byte, info Info, err error) {
|
||
arch, err := DetectArch(target)
|
||
if err != nil {
|
||
return nil, info, err
|
||
}
|
||
out, info, err = PatchSignedThemed(target, cfg, arch, signed, theme)
|
||
if err != nil {
|
||
return nil, info, err
|
||
}
|
||
info.Arch = arch
|
||
return out, info, nil
|
||
}
|
||
|
||
func PatchAutoSigned(target, cfg []byte, name string, signed bool) (out []byte, info Info, err error) {
|
||
det := DetectFile(target, name)
|
||
switch det.Kind {
|
||
case KindPE:
|
||
return PatchSigned(target, cfg, det.Arch, signed)
|
||
case KindELF:
|
||
return PatchELF(target, cfg)
|
||
}
|
||
return nil, info, unsupportedFor(det)
|
||
}
|
||
|
||
// Info 打包结果摘要
|
||
type Info struct {
|
||
Arch string
|
||
EntryOld uint64
|
||
EntryNew uint64
|
||
SecRVA uint64
|
||
SecSize uint32
|
||
CrcVfy uint32
|
||
CrcFile uint32
|
||
OutSize int
|
||
OriginalSource int
|
||
Note string // 非 PE 的补充说明(例如 APK 重签信息)
|
||
}
|
||
|
||
func patchWith(raw, blob, dll, cfg []byte, signed bool) ([]byte, Info, error) {
|
||
var info Info
|
||
f, err := pe.NewFile(bytes.NewReader(raw))
|
||
if err != nil {
|
||
return nil, info, fmt.Errorf("解析 PE: %w", err)
|
||
}
|
||
is64 := f.Machine == pe.IMAGE_FILE_MACHINE_AMD64
|
||
is32 := f.Machine == pe.IMAGE_FILE_MACHINE_I386
|
||
if !is64 && !is32 {
|
||
return nil, info, fmt.Errorf("不支持的架构 machine=0x%x", f.Machine)
|
||
}
|
||
info.Arch = map[bool]string{true: "x64", false: "x86"}[is64]
|
||
info.OriginalSource = len(raw)
|
||
|
||
peOff := int(binary.LittleEndian.Uint32(raw[0x3C:0x40])) + 4 + 20
|
||
var numSections uint16
|
||
var sizeOfHeaders, fileAlign, sectAlign, entryRVA uint32
|
||
if is64 {
|
||
oh := f.OptionalHeader.(*pe.OptionalHeader64)
|
||
numSections, sizeOfHeaders = f.FileHeader.NumberOfSections, oh.SizeOfHeaders
|
||
fileAlign, sectAlign, entryRVA = oh.FileAlignment, oh.SectionAlignment, oh.AddressOfEntryPoint
|
||
} else {
|
||
oh := f.OptionalHeader.(*pe.OptionalHeader32)
|
||
numSections, sizeOfHeaders = f.FileHeader.NumberOfSections, oh.SizeOfHeaders
|
||
fileAlign, sectAlign, entryRVA = oh.FileAlignment, oh.SectionAlignment, oh.AddressOfEntryPoint
|
||
}
|
||
if fileAlign == 0 {
|
||
fileAlign = 0x200
|
||
}
|
||
if sectAlign == 0 {
|
||
sectAlign = 0x1000
|
||
}
|
||
|
||
var maxEnd, maxRVAEnd uint32
|
||
for _, s := range f.Sections {
|
||
if end := s.Offset + s.Size; end > maxEnd {
|
||
maxEnd = end
|
||
}
|
||
for _, e := range []uint32{s.VirtualAddress + s.VirtualSize, s.VirtualAddress + s.Size} {
|
||
if e > maxRVAEnd {
|
||
maxRVAEnd = e
|
||
}
|
||
}
|
||
}
|
||
newFileOff := align(maxEnd, fileAlign)
|
||
newRVA := align(maxRVAEnd, sectAlign)
|
||
|
||
shOff := uint32(peOff) + uint32(optSize(is64)) + uint32(numSections)*40
|
||
if shOff+40 > sizeOfHeaders {
|
||
return nil, info, fmt.Errorf("头部空间不足(SizeOfHeaders=0x%x)", sizeOfHeaders)
|
||
}
|
||
|
||
hdr := make([]byte, hdrSize)
|
||
copy(hdr[0:4], []byte("VFY4"))
|
||
binary.LittleEndian.PutUint32(hdr[4:], 4)
|
||
dllOff := uint32(hdrSize) + uint32(len(blob))
|
||
cfgOff := dllOff + uint32(len(dll))
|
||
binary.LittleEndian.PutUint32(hdr[8:], dllOff)
|
||
binary.LittleEndian.PutUint32(hdr[12:], uint32(len(dll)))
|
||
binary.LittleEndian.PutUint32(hdr[16:], cfgOff)
|
||
binary.LittleEndian.PutUint32(hdr[20:], uint32(len(cfg)))
|
||
binary.LittleEndian.PutUint32(hdr[24:], entryRVA)
|
||
binary.LittleEndian.PutUint32(hdr[28:], uint32(len(blob)))
|
||
flags := uint32(0)
|
||
if signed {
|
||
flags |= 2 // bit1:已签名构建,壳侧跳过整文件 CRC
|
||
}
|
||
binary.LittleEndian.PutUint32(hdr[32:], flags)
|
||
|
||
var sec bytes.Buffer
|
||
sec.Write(hdr)
|
||
sec.Write(blob)
|
||
sec.Write(dll)
|
||
sec.Write(cfg)
|
||
sectionData := sec.Bytes()
|
||
|
||
binary.LittleEndian.PutUint32(sectionData[crcOff:], 0)
|
||
binary.LittleEndian.PutUint32(sectionData[crcOff+4:], 0)
|
||
crcV := crcSkip(sectionData[:crcOff], sectionData[crcOff+8:])
|
||
binary.LittleEndian.PutUint32(sectionData[crcOff:], crcV)
|
||
|
||
padded := make([]byte, align(uint32(len(sectionData)), fileAlign))
|
||
copy(padded, sectionData)
|
||
|
||
out := make([]byte, 0, int(newFileOff)+len(padded))
|
||
out = append(out, raw...)
|
||
for uint32(len(out)) < newFileOff {
|
||
out = append(out, 0)
|
||
}
|
||
out = append(out, padded...)
|
||
|
||
sh := make([]byte, 40)
|
||
copy(sh[0:8], secName)
|
||
binary.LittleEndian.PutUint32(sh[8:], uint32(len(sectionData)))
|
||
binary.LittleEndian.PutUint32(sh[12:], newRVA)
|
||
binary.LittleEndian.PutUint32(sh[16:], uint32(len(padded)))
|
||
binary.LittleEndian.PutUint32(sh[20:], newFileOff)
|
||
binary.LittleEndian.PutUint32(sh[36:], secAttrib)
|
||
copy(out[shOff:], sh)
|
||
|
||
lfanew := int(binary.LittleEndian.Uint32(out[0x3C:0x40]))
|
||
binary.LittleEndian.PutUint16(out[lfanew+4+2:], numSections+1)
|
||
binary.LittleEndian.PutUint32(out[peOff+16:], newRVA+hdrSize)
|
||
newSizeOfImage := align(newRVA+uint32(len(sectionData)), sectAlign)
|
||
binary.LittleEndian.PutUint32(out[peOff+56:], newSizeOfImage)
|
||
curInit := binary.LittleEndian.Uint32(out[peOff+8:])
|
||
binary.LittleEndian.PutUint32(out[peOff+8:], curInit+uint32(len(padded)))
|
||
|
||
ckOff := peOff + 64
|
||
binary.LittleEndian.PutUint32(out[ckOff:], 0)
|
||
binary.LittleEndian.PutUint32(out[ckOff:], peChecksum(out))
|
||
|
||
skipAt := int(newFileOff) + crcOff
|
||
binary.LittleEndian.PutUint32(out[skipAt:], 0)
|
||
binary.LittleEndian.PutUint32(out[skipAt+4:], 0)
|
||
var crcF uint32
|
||
if !signed {
|
||
// 签名产物留空:Authenticode 会追加证书表,与整文件 CRC 互斥
|
||
crcF = crcSkip(out[:skipAt], out[skipAt+8:])
|
||
}
|
||
binary.LittleEndian.PutUint32(out[skipAt:], crcV)
|
||
binary.LittleEndian.PutUint32(out[skipAt+4:], crcF)
|
||
|
||
info.EntryOld = uint64(entryRVA)
|
||
info.EntryNew = uint64(newRVA) + hdrSize
|
||
info.SecRVA = uint64(newRVA)
|
||
info.SecSize = uint32(len(sectionData))
|
||
info.CrcVfy = crcV
|
||
info.CrcFile = crcF
|
||
info.OutSize = len(out)
|
||
return out, info, nil
|
||
}
|
||
|
||
// BuildCfg 生成壳配置(api 指向调用方所在站点)
|
||
func BuildCfg(appKey, appName, baseURL string) []byte {
|
||
return BuildCfgThemed(appKey, appName, baseURL, "classic")
|
||
}
|
||
|
||
// BuildCfgThemed 生成壳用的配置;theme 决定验证界面用哪套皮肤(DuiLib 壳读取)
|
||
func BuildCfgThemed(appKey, appName, baseURL, theme string) []byte {
|
||
return BuildCfgThemedVer(appKey, appName, baseURL, theme, "")
|
||
}
|
||
|
||
// BuildCfgThemedVer 额外带上目标程序的版本号。
|
||
// 只有登记了版本的程序才做自动更新比对;空版本号 = 壳不打扰用户。
|
||
func BuildCfgThemedVer(appKey, appName, baseURL, theme, ver string) []byte {
|
||
api := fmt.Sprintf("%s/api/v1/app/%s/login", strings.TrimRight(baseURL, "/"), appKey)
|
||
if theme == "" {
|
||
theme = "dark"
|
||
}
|
||
ver = strings.TrimSpace(ver)
|
||
cfg := fmt.Sprintf(`{"api":%q,"name":%q,"device_auto":1,"shell_ver":%d,"theme":%q,"ver":%q}`,
|
||
api, appName, shellVer, theme, ver)
|
||
return []byte(cfg)
|
||
}
|
||
|
||
// ---------- 任务存储与执行 ----------
|
||
type Runner struct {
|
||
Dir string
|
||
}
|
||
|
||
func NewRunner(dir string) *Runner {
|
||
if dir == "" {
|
||
dir = filepath.Join("data", "protect")
|
||
}
|
||
// 必须绝对:签名/打包工具是子进程,cwd 会被切到 workDir,
|
||
// 相对路径(默认 data/protect)在子进程里会解析到错误位置
|
||
// (实测 osslsigncode 报 Failed to read PKCS#12 file)。
|
||
if abs, err := filepath.Abs(dir); err == nil {
|
||
dir = abs
|
||
}
|
||
os.MkdirAll(dir, 0o755)
|
||
return &Runner{Dir: dir}
|
||
}
|
||
|
||
// Run 对已落盘的源文件加壳,返回产物路径与信息
|
||
func (r *Runner) Run(srcPath, outPath string, cfg []byte) (Info, error) {
|
||
return r.RunWithTask(nil, srcPath, outPath, cfg)
|
||
}
|
||
|
||
// RunWithTask 带任务上下文的执行:IPA 需要任务上的签名证书与壳 dylib
|
||
func (r *Runner) RunWithTask(task *model.ProtectTask, srcPath, outPath string, cfg []byte) (Info, error) {
|
||
src, err := os.ReadFile(srcPath)
|
||
if err != nil {
|
||
return Info{}, fmt.Errorf("读源文件: %w", err)
|
||
}
|
||
det := DetectFile(src, filepath.Base(srcPath))
|
||
var out []byte
|
||
var info Info
|
||
var perr error
|
||
if det.Kind == KindIPA {
|
||
var mat *Material
|
||
if task != nil && task.SignProfileID != nil {
|
||
mat, perr = LoadMaterial(*task.SignProfileID, task.CertIdentity)
|
||
if perr != nil {
|
||
return info, perr
|
||
}
|
||
defer mat.Close()
|
||
}
|
||
out, perr = SignIPA(src, mat, DylibForIOS(), filepath.Join(filepath.Dir(outPath), "ipa"))
|
||
if perr == nil {
|
||
info = Info{Arch: "universal", OriginalSource: len(src), OutSize: len(out),
|
||
Note: "IPA 已注入壳 dylib 并用你的证书重签"}
|
||
}
|
||
} else if det.Kind == KindAPK {
|
||
out, info, perr = patchAPK(src, cfg, filepath.Dir(outPath))
|
||
} else if det.Kind == KindPE && task != nil && IsDriverPE(src, filepath.Base(srcPath)) {
|
||
// 内核驱动(.sys / 子系统 NATIVE):绝不能塞用户态验证壳,注进去驱动直接加载失败。
|
||
// 驱动要的是 Authenticode 签名,所以这里只签名、不注入。
|
||
if task.SignProfileID == nil {
|
||
return info, fmt.Errorf("内核驱动只能签名、不能加密;请选择一张支持驱动签名用途的证书")
|
||
}
|
||
mat, merr := LoadMaterial(*task.SignProfileID, task.CertIdentity)
|
||
if merr != nil {
|
||
return info, merr
|
||
}
|
||
defer mat.Close()
|
||
var note string
|
||
out, note, perr = SignPE(src, mat, filepath.Join(filepath.Dir(outPath), "pe_sign"))
|
||
if perr != nil {
|
||
return info, perr
|
||
}
|
||
info = Info{Arch: det.Arch, OriginalSource: len(src), OutSize: len(out),
|
||
Note: "内核驱动仅签名(未注入验证壳)"}
|
||
if note != "" {
|
||
info.Note += "(" + note + ")"
|
||
}
|
||
} else {
|
||
// PE:任务带证书 → 走「签名构建」(flags.bit1,crcFile 留空),注入完成后再 Authenticode 重签
|
||
signed := det.Kind == KindPE && task != nil && task.SignProfileID != nil
|
||
// 兜底跟 NormalizeTheme 保持一致用 dark:classic 壳不支持按配置
|
||
// 切卡密登录/隐藏注册,落到它上面等于界面配置全部失效
|
||
theme := "dark"
|
||
if task != nil && task.ShellTheme != "" {
|
||
theme = task.ShellTheme
|
||
}
|
||
|
||
// 默认先用 DogProtect 保护原程序,再注入验证壳 —— 顺序不能反。
|
||
// DogProtect 会把被保护的节打包、清零 raw size;若先注入 .load,
|
||
// 壳运行时读不到自己的配置节,整个验证直接失效(vfyfix 只能修
|
||
// CRC,救不回被搬走的内容)。反过来先 DogProtect 再追加 .load
|
||
// 是可用的:运行时先跑验证壳,验过再跳回 DogProtect 解包原程序。
|
||
//
|
||
// 只对 PE 生效(APK/IPA/ELF/Mach-O 走各自的分支,DogProtect 不管)。
|
||
dogNote := ""
|
||
if det.Kind == KindPE && DogProtectAvailable() {
|
||
dogged, dn, derr := DogProtect(src, filepath.Join(filepath.Dir(outPath), "dog"))
|
||
if derr != nil {
|
||
// 保护是默认承诺,不能悄悄降级成「只打了验证壳」
|
||
err = fmt.Errorf("DogProtect 保护失败:%w", derr)
|
||
return info, err
|
||
}
|
||
src = dogged
|
||
dogNote = "DogProtect 已保护"
|
||
if dn != "" {
|
||
dogNote += "(" + dn + ")"
|
||
}
|
||
}
|
||
|
||
out, info, perr = PatchAutoSignedThemed(src, cfg, filepath.Base(srcPath), signed, theme)
|
||
if perr == nil {
|
||
// info 被 Patch 覆盖过,DogProtect 的备注重新拼回去
|
||
if dogNote != "" {
|
||
if info.Note != "" {
|
||
info.Note = dogNote + ";" + info.Note
|
||
} else {
|
||
info.Note = dogNote
|
||
}
|
||
}
|
||
}
|
||
if perr == nil && signed {
|
||
var mat *Material
|
||
mat, perr = LoadMaterial(*task.SignProfileID, task.CertIdentity)
|
||
if perr == nil {
|
||
defer mat.Close()
|
||
var note string
|
||
out, note, perr = SignPE(out, mat, filepath.Join(filepath.Dir(outPath), "pe_sign"))
|
||
if perr == nil {
|
||
info.Note = "已用你的证书做 Authenticode 重签"
|
||
if note != "" {
|
||
info.Note += "(" + note + ")"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if perr != nil {
|
||
err = perr
|
||
return info, err
|
||
}
|
||
if err := os.MkdirAll(filepath.Dir(outPath), 0o755); err != nil {
|
||
return info, err
|
||
}
|
||
if err := os.WriteFile(outPath, out, 0o644); err != nil {
|
||
return info, fmt.Errorf("写产物: %w", err)
|
||
}
|
||
return info, nil
|
||
}
|
||
|
||
// SaveSource 把上传的原始程序落盘
|
||
func (r *Runner) SaveSource(taskID uint, name string, data []byte) (string, error) {
|
||
d := filepath.Join(r.Dir, fmt.Sprintf("%d", taskID))
|
||
if err := os.MkdirAll(d, 0o755); err != nil {
|
||
return "", err
|
||
}
|
||
base := filepath.Base(name)
|
||
if base == "" || base == "." || base == string(filepath.Separator) {
|
||
base = "app.exe"
|
||
}
|
||
p := filepath.Join(d, "src_"+base)
|
||
if err := os.WriteFile(p, data, 0o644); err != nil {
|
||
return "", err
|
||
}
|
||
return p, nil
|
||
}
|
||
|
||
// OutPath 产物路径
|
||
func (r *Runner) OutPath(taskID uint, name string) string {
|
||
base := filepath.Base(name)
|
||
if base == "" {
|
||
base = "app.exe"
|
||
}
|
||
if i := strings.LastIndex(base, "."); i > 0 {
|
||
base = base[:i] + "_protected" + base[i:]
|
||
} else {
|
||
// 无扩展名(常见于 Linux 可执行)不加 .exe
|
||
base += "_protected"
|
||
}
|
||
return filepath.Join(r.Dir, fmt.Sprintf("%d", taskID), base)
|
||
}
|
||
|
||
func (r *Runner) Now() time.Time { return time.Now() }
|
||
|
||
// ---------- 内部工具 ----------
|
||
func align(v, a uint32) uint32 {
|
||
if a == 0 {
|
||
return v
|
||
}
|
||
return (v + a - 1) / a * a
|
||
}
|
||
|
||
func optSize(is64 bool) int {
|
||
if is64 {
|
||
return 240
|
||
}
|
||
return 224
|
||
}
|
||
|
||
func crcSkip(chunks ...[]byte) uint32 {
|
||
h := crc32.NewIEEE()
|
||
for _, c := range chunks {
|
||
h.Write(c)
|
||
}
|
||
return h.Sum32()
|
||
}
|
||
|
||
func peChecksum(data []byte) uint32 {
|
||
var sum uint64
|
||
n := len(data)
|
||
i := 0
|
||
for ; i+1 < n; i += 2 {
|
||
sum += uint64(binary.LittleEndian.Uint16(data[i:]))
|
||
sum = (sum & 0xffff) + (sum >> 16)
|
||
}
|
||
if i < n {
|
||
sum += uint64(data[i])
|
||
sum = (sum & 0xffff) + (sum >> 16)
|
||
}
|
||
sum = (sum & 0xffff) + (sum >> 16)
|
||
return uint32(sum) + uint32(n)
|
||
}
|