需求:打出来的产物默认带 DogProtect 保护,不要每次手动两步。 ## 顺序(关键) 必须 **先 DogProtect、后注入验证壳**,反过来不行: src → dogprotect_con → PatchAutoSignedThemed(追加 .load)→ [可选重签] DogProtect 会把要保护的节打包、把 SizeOfRawData/PointerToRawData 清零。 若先注入 .load 再跑 DogProtect,.load 的内容被搬进打包节、磁盘上只剩一个 raw size = 0 的空节,验证壳运行时读不到自己的配置 —— 整个验证静默失效。 实测过这个反向顺序,vfyfix 只会报「节区间异常: off=0 raw=...」, 它重算的是 CRC,救不回被搬走的内容。 ## 实现 - :封装 dogprotect_con 调用 (DOGPROTECT_DIR 定位、180s 超时、退出码和产物双重校验、 从输出里捞 Output file size 做备注、尾部报错带回任务 message) - 加壳主流程:KindPE 且加壳器可用时先跑 DogProtect, 产物再进 Patch。失败直接让任务失败 —— 保护是默认承诺, 不能悄悄降级成「只打了验证壳」 - 只对 PE 生效;APK/IPA/ELF/Mach-O 走各自原分支 ## 镜像 dogprotect_con 是 glibc 程序且要求 GLIBC_2.38+ / GLIBCXX_3.4.32+: alpine 的 gcompat 跑不起来,bookworm 的 glibc 2.36 也差一截。 runtime 基础镜像 alpine → debian:trixie-slim(glibc 2.41),实测能跑。 配套改动: - apk → apt;openjdk17 → trixie 的 openjdk-21(apktool 兼容) - osslsigncode 用 trixie 现成包,删掉源码编译的 signer-builder 阶段 - entrypoint 的 su-exec(alpine 专有)→ util-linux 的 setpriv - 加壳器三个文件拷到 /app/tools/dogprotect/(必须同目录,-rpath,$ORIGIN) ## 实测 - Linux 直接调用:310784 → 6140928 字节,4.3 秒,输出 「Output file size is 6140928 bytes (1975%)」 - 完整链路(先 dog 后 verify):7195136 字节,wine 跑起来验证界面正常、 tab 是「登录/我的设备/卡密充值/找回绑定」、输入框「卡密」、无黑块错位 注意:随源码带的是 TestLicense(TestUser),换正式授权替换 docker/dogprotect/DogProtectLicense.ini 即可,代码不用动。
148 lines
4.7 KiB
Go
148 lines
4.7 KiB
Go
package protect
|
||
|
||
import (
|
||
"bytes"
|
||
"context"
|
||
"fmt"
|
||
"os"
|
||
"os/exec"
|
||
"path/filepath"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
// DogProtect 相关的可执行文件位置。容器里通过 Dockerfile 拷到 /app/tools/dogprotect/,
|
||
// 本地开发时可以指到 /root/work/vmprotect/bin/64/release/。
|
||
const (
|
||
dogToolDirEnv = "DOGPROTECT_DIR"
|
||
dogToolDirDflt = "/app/tools/dogprotect"
|
||
dogBinaryName = "dogprotect_con"
|
||
dogSDKLibName = "libDogProtectSDK64.so"
|
||
dogTimeoutSec = 180 // 大一点的 PE 虚拟化要几十秒,留足余量
|
||
)
|
||
|
||
// DogProtectAvailable 报告加壳器在不在。面板可以据此决定是否显示「已有保护」。
|
||
func DogProtectAvailable() bool {
|
||
dir := dogToolDir()
|
||
if dir == "" {
|
||
return false
|
||
}
|
||
if _, err := os.Stat(filepath.Join(dir, dogBinaryName)); err != nil {
|
||
return false
|
||
}
|
||
if _, err := os.Stat(filepath.Join(dir, dogSDKLibName)); err != nil {
|
||
return false
|
||
}
|
||
return true
|
||
}
|
||
|
||
func dogToolDir() string {
|
||
if d := strings.TrimSpace(os.Getenv(dogToolDirEnv)); d != "" {
|
||
return d
|
||
}
|
||
if _, err := os.Stat(dogToolDirDflt); err == nil {
|
||
return dogToolDirDflt
|
||
}
|
||
return ""
|
||
}
|
||
|
||
// DogProtect 用 DogProtect 保护一个 PE,返回保护后的字节。
|
||
//
|
||
// 必须先跑它、再注入验证壳,顺序不能反:
|
||
// DogProtect 会把它要保护的节打包并清零 SizeOfRawData/PointerToRawData,
|
||
// 如果先注入验证壳(.load 节),再跑 DogProtect,.load 的内容就被搬进
|
||
// 打包节里、磁盘上留下一个 raw size = 0 的空节 —— 验证壳运行时读不到
|
||
// 自己的配置,直接失效(vfyfix 也只能修 CRC,救不了被搬走的内容)。
|
||
//
|
||
// 反过来(先 DogProtect、后注入 .load)实测可行:DogProtect 的产物仍是
|
||
// 合法 PE,Patch 能在它上面正常追加 .load 节,运行时会先跑验证壳、
|
||
// 验证通过后再跳回 DogProtect 的启动代码去解包原程序。
|
||
//
|
||
// 不支持的文件类型(非 PE)返回 errUnsupportedForDog,由调用方决定跳过。
|
||
func DogProtect(src []byte, workDir string) (out []byte, note string, err error) {
|
||
if !DogProtectAvailable() {
|
||
return nil, "", fmt.Errorf("DogProtect 未就绪(找不到 %s)", dogBinaryName)
|
||
}
|
||
if len(src) == 0 {
|
||
return nil, "", fmt.Errorf("源文件为空")
|
||
}
|
||
|
||
dir := dogToolDir()
|
||
if err := os.MkdirAll(workDir, 0o755); err != nil {
|
||
return nil, "", fmt.Errorf("准备工作目录: %w", err)
|
||
}
|
||
|
||
inPath := filepath.Join(workDir, "dog_in.exe")
|
||
outPath := filepath.Join(workDir, "dog_out.exe")
|
||
// 用独立的临时文件,避免上一次失败留下的半成品被当成结果
|
||
_ = os.Remove(outPath)
|
||
|
||
if err := os.WriteFile(inPath, src, 0o644); err != nil {
|
||
return nil, "", fmt.Errorf("写临时输入: %w", err)
|
||
}
|
||
defer os.Remove(inPath)
|
||
defer os.Remove(outPath)
|
||
|
||
ctx, cancel := context.WithTimeout(context.Background(), dogTimeoutSec*time.Second)
|
||
defer cancel()
|
||
|
||
cmd := exec.CommandContext(ctx, filepath.Join(dir, dogBinaryName), inPath, outPath)
|
||
// 加壳器链接时是 -Wl,-rpath,$ORIGIN,同目录有 .so 就够;
|
||
// 再显式给一份 LD_LIBRARY_PATH,免得 $(ORIGIN) 在某些环境解析不到
|
||
cmd.Env = append(os.Environ(), "LD_LIBRARY_PATH="+dir)
|
||
|
||
var stdout, stderr bytes.Buffer
|
||
cmd.Stdout = &stdout
|
||
cmd.Stderr = &stderr
|
||
|
||
runErr := cmd.Run()
|
||
combined := stdout.String() + stderr.String()
|
||
|
||
if ctx.Err() == context.DeadlineExceeded {
|
||
return nil, "", fmt.Errorf("DogProtect 超时(>%ds)", dogTimeoutSec)
|
||
}
|
||
if runErr != nil {
|
||
return nil, "", fmt.Errorf("DogProtect 失败: %v|%s", runErr, tailLines(combined, 6))
|
||
}
|
||
// 退出码 0 也可能没产出(比如授权过期会在 stdout 里报错但退 0)
|
||
if _, statErr := os.Stat(outPath); statErr != nil {
|
||
return nil, "", fmt.Errorf("DogProtect 未生成产物|%s", tailLines(combined, 6))
|
||
}
|
||
|
||
buf, err := os.ReadFile(outPath)
|
||
if err != nil {
|
||
return nil, "", fmt.Errorf("读 DogProtect 产物: %w", err)
|
||
}
|
||
if len(buf) == 0 {
|
||
return nil, "", fmt.Errorf("DogProtect 产物为空")
|
||
}
|
||
|
||
// 从输出里捞一行增长信息做备注("Output file size is X bytes (Y%)")
|
||
for _, line := range strings.Split(combined, "\n") {
|
||
if strings.Contains(line, "Output file size") {
|
||
note = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), "[Information]"))
|
||
break
|
||
}
|
||
}
|
||
return buf, note, nil
|
||
}
|
||
|
||
// tailLines 取输出末尾几行,用于把加壳器的报错带回任务 message。
|
||
func tailLines(s string, n int) string {
|
||
s = strings.TrimSpace(s)
|
||
if s == "" {
|
||
return "(无输出)"
|
||
}
|
||
lines := strings.Split(s, "\n")
|
||
if len(lines) > n {
|
||
lines = lines[len(lines)-n:]
|
||
}
|
||
out := make([]string, 0, len(lines))
|
||
for _, l := range lines {
|
||
if t := strings.TrimSpace(l); t != "" {
|
||
out = append(out, t)
|
||
}
|
||
}
|
||
return strings.Join(out, " / ")
|
||
}
|