log show 是 macOS 统一日志核心命令,支持按时间、子系统、进程等精准筛选结构化日志,用于深度审计;关键在于围绕目标行为构建 --predicate 查询逻辑,并结合 --info --debug、--style syslog 或 JSON 等输出格式提升可分析性。
log show 是 macos 统一日志(unified logging)系统的核心命令,适合用于导出结构化日志进行深度审计——它不依赖图形界面,可精准按时间、子系统、进程、关键词和日志级别筛选,输出结果可直接用于分析、比对或自动化处理。
关键不是“导出全部”,而是“导出有效线索”。macOS 日志量极大,盲目导出全量日志既低效又难分析。真正有效的审计,是围绕目标行为(如权限变更、文件访问尝试、异常登录、进程启动)构建可验证的日志查询逻辑。
? 锁定审计目标,再选对应谓词
审计前先明确你要查什么,不同目标对应不同 --predicate 写法:
-
查全盘访问授权行为(含弹窗、允许、拒绝)
log show \ --predicate 'subsystem == "com.apple.tccd" AND (eventMessage contains "full disk access" OR eventMessage contains "allow" OR eventMessage contains "deny")' \ --last 30d \ --style syslog > ~/Desktop/fda-audit-$(date +%Y%m%d).txt
-
查沙盒拦截的越权文件操作(如读取用户文档被拒)
log show \ --predicate 'subsystem == "com.apple.sandboxd" AND eventMessage contains "deny" AND eventMessage contains "file-" ' \ --last 7d \ --info --debug > ~/Desktop/sandbox-denials.txt
-
查内核层文件系统异常(openat/mmap 失败、权限校验失败)
log show \ --predicate 'process == "kernel" AND (eventMessage contains "VFS" OR eventMessage contains "openat" OR eventMessage contains "mmap")' \ --level error \ --last boot > ~/Desktop/kernel-fs-errors.txt
-
查特定 App 的完整行为链(如 Slack 访问通讯录+读取桌面)
log show \ --predicate '(eventMessage contains "Slack" OR subsystem contains "com.slack.Slack") AND (eventMessage contains "addressbook" OR eventMessage contains "Desktop")' \ --last 14d > ~/Desktop/slack-access-trace.txt
✅ 提示:
--info --debug能捕获更详细的上下文(如调用堆栈、PID、bundle ID),对溯源至关重要;--style syslog输出传统时间戳格式,方便用awk/grep进一步处理。
? 导出策略:兼顾可读性与可分析性
-
轻量文本摘要(人工快速浏览)
log show --predicate 'level >= error' --last 24h | grep -E "(denied|failed|panic|crash)" > ~/Desktop/errors-summary.txt
-
结构化 JSON(供脚本解析或导入 SIEM)
log show --predicate 'subsystem == "com.apple.securityd"' --last 7d --style json > ~/Desktop/securityd-events.json
-
归档为 .logarchive(保留原始二进制上下文,Console 可双击打开)
log collect --last 7d --output ~/Desktop/audit-weekly-$(date +%Y%m%d).logarchive
⚠️ 注意:
log collect需要全盘访问权限,首次运行会弹窗授权;.logarchive不是纯文本,但支持log show --archive二次查询。
? 审计辅助:结合 TCC 数据库与时间锚点
日志本身不记录“谁在什么时候点了‘允许’”,但能记录 tccd 的决策动作。要确认授权是否真实发生,需交叉验证:
-
查看
/Library/Application Support/com.apple.TCC/TCC.db的修改时间:stat /Library/Application\ Support/com.apple.TCC/TCC.db | grep "Modify"
若某次
tccd日志中出现Granting full disk access to com.xxx,而TCC.db修改时间紧随其后(秒级差),即可认定该授权已落地。 -
检查数据库内容(需先关闭 SIP 或使用
sudo sqlite3):sudo sqlite3 "/Library/Application Support/com.apple.TCC/TCC.db" \ "SELECT service, client, allowed, last_modified FROM access WHERE service = 'kTCCServiceFullDiskAccess';"
? 注意事项:避免常见误操作
-
--last 7days❌ 错误写法 → 正确是--last 7d -
log show默认只查当前用户会话日志;如需系统级事件(如launchd启动服务),需在管理员终端中运行 -
eventMessage contains "password"类搜索可能漏检——因 Unified Logging 默认对敏感字段做隐私脱敏,实际日志中可能显示为<private> - 日志滚动策略默认保留约 14 天(取决于磁盘空间),长期审计建议配合
log collect定期归档
不复杂但容易忽略。


















