feat: 接入 iOSGuard LLVM 混淆工具链与 IPA 加固门禁,按分支策略兼容旧版本打包
- 新增 toolchain_manager:构建前自愈校验 LLVM opt 工具链,按 Xcode 版本与渠道隔离 obj_cache - readoor31/readoor31OtherPay Archive 透传 IG_DIST_CHANNEL/IG_OBF_MODE/IG_ALL_GUARD_OFF/IG_RELEASE_SEED - Archive 前核对分发渠道编译条件:上架包缺失 IG_APPSTORE_DISTRIBUTION 即阻断,非上架包带该条件仅告警,未接入 anti_tamper 的分支跳过 - 导出后执行 IPA 加固门禁审计,失败阻断上传;持久化 dSYM、混淆映射、诊断事件码与审计报告 - 混淆是否执行改由分支 AutoPacking/build_policy.json 声明:force_obfuscation=true 强制,否则按任务勾选,兼容旧分支/旧 Tag;恢复打包页混淆勾选框 - 分支未自带混淆脚本时回退服务端脚本并告警;同步更新服务端 L1 混淆脚本 - .gitignore 忽略 iosguard_cache 与 artifacts 运行时目录;README 补充分支打包策略说明
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
SD 鉴权 X-CT=3.2 私钥 CI 加密脚本
|
||||
|
||||
在 Xcode Run Script Build Phase 中调用(仿照同目录 generate_image_whitelist.py 的模式),
|
||||
构建期读取仓库里的原始 PEM 私钥 AutoPacking/sd_priv_ios_v1.pem,用 AES-256-GCM 加密后
|
||||
直接写入当前 target 的 app bundle(${TARGET_BUILD_DIR}/${WRAPPER_NAME}/encrypted_private_key.bin),
|
||||
不经过 Xcode 的 Copy Bundle Resources,早于 CodeSign 执行即可随包一起签名(同 whitelist 脚本的做法)。
|
||||
|
||||
产出格式为 nonce(12字节) + ciphertext + tag(16字节) 拼接,与 Swift 侧 CryptoKit
|
||||
AES.GCM.SealedBox(combined:) 的输入格式完全一致,App 运行时解密见
|
||||
readoor/3.0/Network/RDSDPrivateKeyManager.swift。
|
||||
|
||||
PBKDF2 密钥派生参数必须和 Swift 侧完全一致,见该文件里的
|
||||
pbkdf2Password / pbkdf2Salt / pbkdf2Iterations 常量。
|
||||
|
||||
依赖:cryptography(第三方 Python 包,标准库不自带 AES-GCM)。这是方案文档 §8 记录的一个已知
|
||||
风险,已在本次实现中实测确认:macOS 系统自带的 /usr/bin/openssl 是 LibreSSL 3.3.6,其 enc
|
||||
子命令对 -aes-256-gcm 直接报 "bad decrypt",openssl 命令行方案不可行;改用 cryptography 包
|
||||
经端到端验证(Xcode Run Script Build Phase 实际跑通、产出的密文能正确解密回原始 PEM)确认可行,
|
||||
但需要在每台构建机(含 iOSBuildServer)上为 Xcode 实际调用的 python3(通常是
|
||||
/usr/bin/python3,Xcode 自带的 3.9)显式安装该依赖:
|
||||
/usr/bin/python3 -m pip install --user cryptography
|
||||
"""
|
||||
|
||||
import hashlib
|
||||
import os
|
||||
import sys
|
||||
|
||||
try:
|
||||
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
||||
except ImportError:
|
||||
sys.stderr.write(
|
||||
"[sd_v32] 缺少 cryptography 包,无法加密私钥。\n"
|
||||
"[sd_v32] 请在构建机上执行: /usr/bin/python3 -m pip install --user cryptography\n"
|
||||
"[sd_v32] (macOS 系统自带 openssl 是 LibreSSL,不支持 aes-256-gcm,已确认不可用作替代方案)\n"
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
# 必须和 RDSDPrivateKeyManager.swift 里的同名常量保持一致
|
||||
PBKDF2_PASSWORD = b"readoor_sd_v32_ci_key_2026"
|
||||
PBKDF2_SALT = b"readoor_sd_v32_salt_fixed"
|
||||
PBKDF2_ITERATIONS = 100_000
|
||||
AES_KEY_LENGTH = 32 # AES-256
|
||||
NONCE_LENGTH = 12 # 与 CryptoKit AES.GCM 默认 nonce 长度一致
|
||||
|
||||
|
||||
def derive_key() -> bytes:
|
||||
return hashlib.pbkdf2_hmac(
|
||||
"sha256", PBKDF2_PASSWORD, PBKDF2_SALT, PBKDF2_ITERATIONS, dklen=AES_KEY_LENGTH
|
||||
)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
srcroot = os.environ.get("SRCROOT")
|
||||
if not srcroot:
|
||||
# 允许手动执行时退回脚本所在目录向上两级推断项目根目录
|
||||
srcroot = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
|
||||
private_key_path = os.path.join(srcroot, "AutoPacking", "sd_priv_ios_v1.pem")
|
||||
if not os.path.isfile(private_key_path):
|
||||
sys.stderr.write(f"[sd_v32] 未找到私钥文件: {private_key_path}\n")
|
||||
return 1
|
||||
|
||||
with open(private_key_path, "rb") as f:
|
||||
plaintext = f.read()
|
||||
|
||||
key = derive_key()
|
||||
nonce = os.urandom(NONCE_LENGTH)
|
||||
ciphertext_with_tag = AESGCM(key).encrypt(nonce, plaintext, None)
|
||||
combined = nonce + ciphertext_with_tag
|
||||
|
||||
target_build_dir = os.environ.get("TARGET_BUILD_DIR")
|
||||
wrapper_name = os.environ.get("WRAPPER_NAME")
|
||||
if target_build_dir and wrapper_name:
|
||||
# Xcode Build Phase 场景:直接写进当前 target 的 app bundle,早于 CodeSign 执行
|
||||
output_dir = os.path.join(target_build_dir, wrapper_name)
|
||||
else:
|
||||
# 手动执行场景:写到项目根目录,方便本地验证加解密是否正确
|
||||
output_dir = srcroot
|
||||
|
||||
output_path = os.path.join(output_dir, "rd_glyph_lut_v2.dat")
|
||||
for stale_name in ("rd_net_cfg_v1.dat", "encrypted_private_key.bin"):
|
||||
stale_path = os.path.join(output_dir, stale_name)
|
||||
if os.path.isfile(stale_path):
|
||||
os.remove(stale_path)
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
with open(output_path, "wb") as f:
|
||||
f.write(combined)
|
||||
|
||||
print(f"[sd_v32] asset written: {output_path}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -72,9 +72,9 @@ def scan_bundled_images(app_bundle_path, executable_path):
|
||||
seen = set()
|
||||
|
||||
executable_name = Path(executable_path).name if executable_path else Path(app_bundle_path).stem
|
||||
main_key = (executable_name, executable_path or executable_name)
|
||||
main_key = (executable_name, executable_name)
|
||||
seen.add(main_key)
|
||||
known.append({'name': executable_name, 'path': executable_path or executable_name})
|
||||
known.append({'name': executable_name, 'path': executable_name})
|
||||
|
||||
for root, dirs, files in os.walk(app_bundle_path):
|
||||
dirs.sort()
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -44,7 +44,12 @@ def build_canonical_symbol_tables(
|
||||
swift_signatures.update(method_data["swift_signatures"])
|
||||
|
||||
canonical = {
|
||||
"swift_symbols": sorted(swift_symbols),
|
||||
"swift_symbols": [
|
||||
name for name in sorted(swift_symbols)
|
||||
if not already_obfuscated_re.match(name)
|
||||
and name not in symbol_whitelist
|
||||
and not name.startswith(symbol_prefix_whitelist)
|
||||
],
|
||||
"objc_symbols": [
|
||||
name for name in sorted(objc_symbols)
|
||||
if any(name.startswith(prefix) for prefix in objc_symbol_prefixes)
|
||||
@@ -107,13 +112,24 @@ def build_type_mappings_from_canonical_table(canonical_tables: dict, salt: str,
|
||||
return mapping_swift, mapping_objc
|
||||
|
||||
|
||||
def build_method_mapping_from_canonical_table(canonical_tables: dict, salt: str, *, build_unique_mapping, method_prefix: str) -> dict:
|
||||
def build_method_mapping_from_canonical_table(canonical_tables: dict, salt: str, *, build_unique_mapping, method_prefix: str, build_method_entries=None) -> dict:
|
||||
swift_signature_mapping = canonical_tables.get("swift_signatures", {})
|
||||
direct_methods = list(canonical_tables.get("direct_methods", []))
|
||||
objc_selectors = list(canonical_tables.get("objc_selectors", []))
|
||||
|
||||
if build_method_entries is not None:
|
||||
return build_method_entries(
|
||||
direct_methods=direct_methods,
|
||||
objc_selectors=objc_selectors,
|
||||
swift_signatures=swift_signature_mapping,
|
||||
salt=salt,
|
||||
)
|
||||
|
||||
ordered_names = []
|
||||
seen = set()
|
||||
for bucket in (
|
||||
canonical_tables.get("direct_methods", []),
|
||||
canonical_tables.get("objc_selectors", []),
|
||||
direct_methods,
|
||||
objc_selectors,
|
||||
sorted(swift_signature_mapping.keys()),
|
||||
):
|
||||
for name in bucket:
|
||||
@@ -125,12 +141,12 @@ def build_method_mapping_from_canonical_table(canonical_tables: dict, salt: str,
|
||||
combined_tokens = build_unique_mapping(ordered_names, salt, prefix=method_prefix)
|
||||
method_mapping = {
|
||||
name: combined_tokens[name]
|
||||
for name in canonical_tables.get("direct_methods", [])
|
||||
for name in direct_methods
|
||||
if name in combined_tokens
|
||||
}
|
||||
method_mapping.update({
|
||||
name: combined_tokens[name]
|
||||
for name in canonical_tables.get("objc_selectors", [])
|
||||
for name in objc_selectors
|
||||
if name in combined_tokens
|
||||
})
|
||||
|
||||
|
||||
@@ -59,6 +59,8 @@ def _discover_files_via_os_walk(scan_dirs: list, allowed_exts: tuple, exclude_pa
|
||||
|
||||
|
||||
def _discover_files_via_rg(scan_dirs: list, allowed_exts: tuple, exclude_path_fragments, skip_dir_names: tuple = (), basename_prefixes: tuple = ()) -> list:
|
||||
if not scan_dirs:
|
||||
return []
|
||||
rg = shutil.which("rg")
|
||||
if not rg:
|
||||
return []
|
||||
@@ -89,6 +91,8 @@ def _discover_files_via_rg(scan_dirs: list, allowed_exts: tuple, exclude_path_fr
|
||||
|
||||
|
||||
def discover_files(scan_dirs: list, allowed_exts: tuple, exclude_path_fragments, skip_dir_names: tuple = (), basename_prefixes: tuple = ()) -> list:
|
||||
if not scan_dirs:
|
||||
return []
|
||||
files = _discover_files_via_rg(
|
||||
scan_dirs,
|
||||
allowed_exts,
|
||||
|
||||
@@ -23,7 +23,7 @@ STAMP="$(date +%Y%m%d_%H%M%S)"
|
||||
BACKUP_DIR="${DERIVED_DIR}/rd_obf_backup_${STAMP}"
|
||||
MARKER_FILE="${DERIVED_DIR}/rd_obf_marker_${TARGET_NAME:-app}.env"
|
||||
MAP_DIR="${ROOT}/obfuscation_maps"
|
||||
MAP_PATH="${MAP_DIR}/${TARGET_NAME:-app}_${STAMP}.json"
|
||||
MAP_PATH="${MAP_DIR}/symbol_map_${TARGET_NAME:-app}_${STAMP}.json"
|
||||
METHOD_MAP_PATH="${MAP_DIR}/method_map_${TARGET_NAME:-app}_${STAMP}.json"
|
||||
PRE_SUCCESS=0
|
||||
|
||||
|
||||
@@ -1,14 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# Scan repository for residual obfuscated tokens like RdXXXXXX and fail if any found.
|
||||
# Scan repository for residual obfuscated tokens (both legacy Rd/YMH/ymh<hex> and curated natural CamelCase stems)
|
||||
# Exits 0 when clean, non-zero when tokens are present.
|
||||
|
||||
ROOT="${1:-$(cd "$(dirname "$0")/../../.." && pwd)}"
|
||||
|
||||
echo "[obf-check] Scanning for residual obfuscated tokens in ${ROOT}"
|
||||
|
||||
EXCLUDE=("./Pods" "./.git" "./obfuscation_maps" "./build_output" "./DerivedData" "./readoorTests")
|
||||
EXCLUDE=(
|
||||
"./Pods"
|
||||
"./.git"
|
||||
"./obfuscation_maps"
|
||||
"./build_output"
|
||||
"./DerivedData"
|
||||
"./readoorTests"
|
||||
"./iOSGuardIntegration"
|
||||
"./Factorys"
|
||||
"./AutoPacking"
|
||||
)
|
||||
|
||||
GREP_EXCLUDE_ARGS=()
|
||||
for e in "${EXCLUDE[@]}"; do
|
||||
@@ -16,15 +26,23 @@ for e in "${EXCLUDE[@]}"; do
|
||||
done
|
||||
|
||||
FOUND=0
|
||||
TOKEN_SUFFIX_RE='(?:[a-f0-9]{6}|[a-f0-9]{8}|[a-f0-9]{10})'
|
||||
TOKEN_SUFFIX_RE='([a-f0-9]{6}|[a-f0-9]{8}|[a-f0-9]{10})'
|
||||
STEM_GROUP='(Breeze|Birch|Canopy|Cobalt|Harbor|Kestrel|Linden|Lumen|Meadow|Meridian|Osprey|Prism|Solace|Summit|Timber|Zephyr)'
|
||||
NATURAL_TYPE_RE="\\b${STEM_GROUP}[A-Z][a-z]+([A-Z][a-z]+)+\\b"
|
||||
NATURAL_METHOD_RE="\\b[a-z]+${STEM_GROUP}[A-Z][A-Za-z]+\\b"
|
||||
|
||||
echo "[obf-check] Looking for token pattern '\b(?:Rd|YMH)${TOKEN_SUFFIX_RE}\b' in source files..."
|
||||
if grep -R --line-number -E "\\b(?:Rd|YMH)${TOKEN_SUFFIX_RE}\\b" --include='*.swift' --include='*.m' --include='*.mm' --include='*.h' "${ROOT}" "${GREP_EXCLUDE_ARGS[@]}"; then
|
||||
echo "[obf-check] Looking for legacy token pattern '\\b(Rd|YMH|ymh)${TOKEN_SUFFIX_RE}\\b' in source files..."
|
||||
if grep -R --line-number -E "\\b(Rd|YMH|ymh)${TOKEN_SUFFIX_RE}\\b" --include='*.swift' --include='*.m' --include='*.mm' --include='*.h' "${GREP_EXCLUDE_ARGS[@]}" "${ROOT}"; then
|
||||
FOUND=1
|
||||
fi
|
||||
|
||||
echo "[obf-check] Looking for natural CamelCase obfuscated tokens in source files..."
|
||||
if grep -R --line-number -E "${NATURAL_TYPE_RE}|${NATURAL_METHOD_RE}" --include='*.swift' --include='*.m' --include='*.mm' --include='*.h' "${GREP_EXCLUDE_ARGS[@]}" "${ROOT}"; then
|
||||
FOUND=1
|
||||
fi
|
||||
|
||||
echo "[obf-check] Looking for obfuscated header imports like \"(Rd|YMH)XXXXXX.h\"..."
|
||||
if grep -R --line-number -E "#import\\s+\"(?:Rd|YMH)${TOKEN_SUFFIX_RE}\\.h\"" --include='*.pch' --include='*.h' --include='*.m' --include='*.mm' "${ROOT}" "${GREP_EXCLUDE_ARGS[@]}"; then
|
||||
if grep -R --line-number -E "#import\\s+\"(Rd|YMH)${TOKEN_SUFFIX_RE}\\.h\"" --include='*.pch' --include='*.h' --include='*.m' --include='*.mm' "${GREP_EXCLUDE_ARGS[@]}" "${ROOT}"; then
|
||||
FOUND=1
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user