Files
verify/backend/cmd/vfyfix/main.go
Cmake 9e9a4b0674 feat(protect): vfyfix 重算 .vfy 壳头 CRC + 二次加壳实测结论
- cmd/vfyfix:按 protect.go 的算法(crcOff=36 / CRC32 IEEE / crcV 覆盖节内 [0,36)+[44,len) /
  crcFile 覆盖整文件跳过 8 字节)重算并写回,顺带重算 PE 校验和;-dry-run 只打印新旧值
  对未改动的单层壳产物,新旧值逐字节一致
- docs/double-pack.md:verify 壳与 DogProtect 二次加壳的真机结论 ——
  可行配置是「壳用 signed 模式(flags.bit1)→ 再 DogProtect」,顺序反过来引导交不过去;
  整文件 CRC 与二次加壳互斥;壳的 cfg 另有校验,离线改 api 会 exit=13
2026-11-23 18:25:25 +08:00

142 lines
4.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// vfyfix — 修正 .vfy 壳头里的两个 CRC(crcV / crcFile)
//
// 场景:先用 verify 的加壳链路打壳(signed=false,壳会校验整文件 CRC),再用别的工具
// (如 DogProtect)二次加壳。后者重写了 PE,壳一校验就退出。本工具在最后一步重算:
//
// .vfy 节内偏移 36 处:crcV(4 字节) = CRC32IEEE(节内 [0,36) + [44,size))
// .vfy 节内偏移 40 处:crcFile(4 字节)= CRC32IEEE(整文件,跳过这两个字段共 8 字节)
//
// 与 backend/internal/protect/protect.go 的算法逐字节一致(crcOff=36、crcSkip=crc32.IEEE)。
//
// 用法:
//
// vfyfix <file.exe> # 修正并写回
// vfyfix <file.exe> -dry-run # 只打印新旧值
//
// 注意:外层加壳时别开压缩(DogProtect 项目的 Options 里不要 cpPack=0x100),
// 否则 .vfy 节在文件里不是明文,壳运行时看到的内容与磁盘不一致,这里的重算就失去意义。
package main
import (
"debug/pe"
"encoding/binary"
"fmt"
"hash/crc32"
"os"
"strings"
)
const (
secName = ".vfy"
hdrSize = 48
crcOff = 36
)
func die(f string, a ...interface{}) {
fmt.Fprintf(os.Stderr, f+"\n", a...)
os.Exit(1)
}
func main() {
if len(os.Args) < 2 {
die("用法: vfyfix <file.exe> [-dry-run]")
}
path := os.Args[1]
dry := len(os.Args) > 2 && os.Args[2] == "-dry-run"
data, err := os.ReadFile(path)
if err != nil {
die("读取 %s 失败: %v", path, err)
}
f, err := pe.NewFile(strings.NewReader(string(data)))
if err != nil {
die("不是有效的 PE: %v", err)
}
defer f.Close()
var sec *pe.Section
for _, s := range f.Sections {
if strings.HasPrefix(s.Name, secName) {
sec = s
}
}
if sec == nil {
die("%s 里没有 %s 节(不是 verify 壳产物)", path, secName)
}
off := int(sec.Offset)
rawSize := int(sec.VirtualSize) // crcV 的覆盖长度 = 生成时的 len(sectionData)
if rawSize <= 0 || off+rawSize > len(data) {
rawSize = int(sec.Size) // 兜底:VirtualSize 异常时用原始大小减对齐填充
}
if off <= 0 || off+rawSize > len(data) || rawSize < hdrSize {
die("节区间异常: off=%d raw=%d file=%d", off, rawSize, len(data))
}
if string(data[off:off+4]) != "VFY4" {
die("壳头 magic 不是 VFY4(实际 %q)", data[off:off+4])
}
flags := binary.LittleEndian.Uint32(data[off+32:])
oldV := binary.LittleEndian.Uint32(data[off+crcOff:])
oldF := binary.LittleEndian.Uint32(data[off+crcOff+4:])
fmt.Printf("%s\n .vfy 文件偏移=0x%x 原始大小=%d flags=0x%x(bit1=%d)\n", path, off, rawSize, flags, (flags>>1)&1)
fmt.Printf(" 旧: crcV=%08x crcFile=%08x\n", oldV, oldF)
// ---- 重算 crcV:节内跳过 36..44 ----
h1 := crc32.NewIEEE()
h1.Write(data[off : off+crcOff])
h1.Write(data[off+crcOff+8 : off+rawSize])
newV := h1.Sum32()
// ---- 重算 crcFile:整文件跳过这两个字段 ----
skipAt := off + crcOff
copyForCRC := make([]byte, 0, len(data))
copyForCRC = append(copyForCRC, data[:skipAt]...)
copyForCRC = append(copyForCRC, data[skipAt+8:]...)
newF := crc32.ChecksumIEEE(copyForCRC)
fmt.Printf(" 新: crcV=%08x crcFile=%08x\n", newV, newF)
if newV == oldV && newF == oldF {
fmt.Println(" 已经是正确值,无需修改")
return
}
if dry {
fmt.Println(" -dry-run:未写入")
return
}
binary.LittleEndian.PutUint32(data[skipAt:], newV)
binary.LittleEndian.PutUint32(data[skipAt+4:], newF)
// ---- 顺手重算 PE 校验和(壳不查,但 Windows 工具链会看)----
peOff := int(binary.LittleEndian.Uint32(data[0x3c:0x40])) + 4 + 20
ckOff := peOff + 64
if ckOff+4 <= len(data) {
binary.LittleEndian.PutUint32(data[ckOff:], 0)
binary.LittleEndian.PutUint32(data[ckOff:], peChecksum(data))
}
if err := os.WriteFile(path, data, 0o644); err != nil {
die("写回失败: %v", err)
}
fmt.Println(" 已写回")
}
// peChecksum 与 protect.go 里的实现一致
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)
}