feat(protect): 签名构建跳过整文件 CRC(flags.bit1)
- 壳 bootstrap6.c:flags bit1=已签名时跳过 crcFile 比对(Authenticode 会追加证书表并改校验和,与整文件 CRC 互斥) - pepatch:PatchSigned/PatchAutoSigned,签名构建写 flags|=2 且 crcFile=0 - Runner:PE 任务带证书 → 走签名构建,注入后调 osslsigncode 重签,Note 记录结果 - 重编 x64/x86 blob(10000/8384 字节)并替换 assets - Wine 回归:普通 exit0/F、签名标记 exit0/F、未签名+PE头篡改 exit13/3
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -65,6 +65,11 @@ func DetectArch(target []byte) (string, error) {
|
||||
|
||||
// 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) {
|
||||
if arch == "" {
|
||||
return nil, info, fmt.Errorf("缺少目标架构")
|
||||
}
|
||||
@@ -72,16 +77,21 @@ func Patch(target, cfg []byte, arch string) (out []byte, info Info, err error) {
|
||||
if err != nil {
|
||||
return nil, info, err
|
||||
}
|
||||
return patchWith(target, blob, dll, cfg)
|
||||
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 PatchAutoSigned(target, cfg []byte, name string, signed bool) (out []byte, info Info, err error) {
|
||||
det := DetectFile(target, name)
|
||||
if det.Kind != KindPE {
|
||||
return nil, info, unsupportedFor(det)
|
||||
}
|
||||
return Patch(target, cfg, det.Arch)
|
||||
return PatchSigned(target, cfg, det.Arch, signed)
|
||||
}
|
||||
|
||||
// Info 打包结果摘要
|
||||
@@ -98,7 +108,7 @@ type Info struct {
|
||||
Note string // 非 PE 的补充说明(例如 APK 重签信息)
|
||||
}
|
||||
|
||||
func patchWith(raw, blob, dll, cfg []byte) ([]byte, Info, error) {
|
||||
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 {
|
||||
@@ -161,7 +171,11 @@ func patchWith(raw, blob, dll, cfg []byte) ([]byte, Info, error) {
|
||||
binary.LittleEndian.PutUint32(hdr[20:], uint32(len(cfg)))
|
||||
binary.LittleEndian.PutUint32(hdr[24:], entryRVA)
|
||||
binary.LittleEndian.PutUint32(hdr[28:], uint32(len(blob)))
|
||||
binary.LittleEndian.PutUint32(hdr[32:], 0) // flags
|
||||
flags := uint32(0)
|
||||
if signed {
|
||||
flags |= 2 // bit1:已签名构建,壳侧跳过整文件 CRC
|
||||
}
|
||||
binary.LittleEndian.PutUint32(hdr[32:], flags)
|
||||
|
||||
var sec bytes.Buffer
|
||||
sec.Write(hdr)
|
||||
@@ -209,7 +223,11 @@ func patchWith(raw, blob, dll, cfg []byte) ([]byte, Info, error) {
|
||||
skipAt := int(newFileOff) + crcOff
|
||||
binary.LittleEndian.PutUint32(out[skipAt:], 0)
|
||||
binary.LittleEndian.PutUint32(out[skipAt+4:], 0)
|
||||
crcF := crcSkip(out[:skipAt], out[skipAt+8:])
|
||||
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)
|
||||
|
||||
@@ -276,7 +294,24 @@ func (r *Runner) RunWithTask(task *model.ProtectTask, srcPath, outPath string, c
|
||||
} else if det.Kind == KindAPK {
|
||||
out, info, perr = patchAPK(src, cfg, filepath.Dir(outPath))
|
||||
} else {
|
||||
out, info, perr = PatchAuto(src, cfg, filepath.Base(srcPath))
|
||||
// PE:任务带证书 → 走「签名构建」(flags.bit1,crcFile 留空),注入完成后再 Authenticode 重签
|
||||
signed := det.Kind == KindPE && task != nil && task.SignProfileID != nil
|
||||
out, info, perr = PatchAutoSigned(src, cfg, filepath.Base(srcPath), signed)
|
||||
if perr == nil && signed {
|
||||
var mat *Material
|
||||
mat, perr = LoadMaterial(*task.SignProfileID)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user