PF-Grivet/ULWrap
GitHub: PF-Grivet/ULWrap
封装 macOS Unified Log 查询的 Python 命令行工具,提供预设场景和检测分析引擎用于威胁狩猎。
Stars: 0 | Forks: 0
# ulwrap
用于查询 macOS Unified Log 的 CLI Python 工具,专注于威胁狩猎。
ulwrap 封装了原生的 `log show` 命令,提供了开箱即用的预设、过滤、颜色输出以及一个可检测可疑模式的分析引擎。
## 安装说明
```
git clone https://github.com/your-user/ulwrap.git
cd ulwrap
bash install.sh
```
需要 Python 3.8+ 和 macOS。无外部依赖。
安装完成后,可以在任意目录下使用 `ulwrap`。
## 使用说明
### 列出可用的预设
```
ulwrap --list-presets
```
### 使用预设运行查询
```
ulwrap --preset tcc --last 1h
ulwrap --preset keychain --last 30m
ulwrap --preset persistence --last 24h
```
### 查看预设的 predicate 而不执行
```
ulwrap --preset tcc --show-predicate
```
显示 predicate、描述和说明。这对于学习 Unified Log 的工作原理很有帮助。
### 使用自定义 predicate 进行自由查询
```
ulwrap --predicate 'process == "sshd"' --last 30m
ulwrap --predicate 'subsystem == "com.apple.network"' --last 1h
```
### 时间窗口
```
ulwrap --preset tcc --last 5m # 5 dernières minutes
ulwrap --preset tcc --last 1h # dernière heure
ulwrap --preset tcc --last 2d # 2 derniers jours
ulwrap --preset tcc --start "2025-01-15 09:00:00" --end "2025-01-15 12:00:00"
```
### 过滤结果
```
ulwrap --preset keychain --last 5m --filter "trustd"
```
仅显示包含该关键字 的条目。计数器会显示过滤后与总数的比率。
### 分析结果
```
ulwrap --preset tcc --last 1h --analyze
ulwrap --preset codesign --last 24h --analyze
```
`--analyze` 模式会根据检测规则扫描每个条目,并按严重程度显示警报:critical、high、medium、low。
### JSON 输出
```
ulwrap --preset tcc --last 1h --json
```
### 保存到文件
```
ulwrap --preset dns --last 1h --json --output results.json
ulwrap --preset tcc --last 1h --output results.txt
```
### 额外选项
```
ulwrap --preset tcc --last 1h --no-color # Désactiver les couleurs
ulwrap --preset tcc --last 1h --presets-file ~/my-presets.json # Fichier presets custom
```
## 可用的预设
### 基础预设
| 预设 | 描述 |
|--------|-------------|
| `tcc` | TCC (Transparency, Consent, and Control) 访问 |
| `dns` | 包含 pid 和调用进程的 DNS 解析 |
| `launchd` | launchd 服务活动 |
| `auth` | sudo 身份验证 |
| `gatekeeper` | 对可执行文件的 Gatekeeper 验证 |
### 威胁狩猎预设
| 预设 | 覆盖的威胁 | 描述 |
|--------|-------------------|-------------|
| `persistence` | 广告软件、后门、RATs | 添加或修改的 LaunchAgent/LaunchDaemon |
| `keychain` | 窃密木马 | 对密码钥匙串的访问 |
| `gatekeeper-bypass` | 木马、破解应用、钓鱼 | 绕过 quarantine 和 notarization |
| `xpc` | 后门、RATs | 进程间通信 |
| `installer` | 木马、广告软件、恶意广告 | 软件包安装 |
| `codesign` | 破解应用 | 代码签名验证 |
| `screen-capture` | RATs | 截屏和键盘监控 |
| `xprotect` | 恶意软件 | XProtect 扫描和检测 |
| `sip` | 后门、RATs | System Integrity Protection |
## 分析和检测规则
`--analyze` 模式使用了在 `presets/rules.json` 中定义的两个级别的规则:
### 通用规则 (所有预设)
- 从 `/tmp/` 或 `/var/tmp/` 启动的进程
- 从 Downloads 文件夹启动的进程
- 位于隐藏文件夹 (以点号开头) 中的进程
### 各预设专属规则
每个预设都有其特定的规则。示例:
- **tcc**:访问摄像头、麦克风、截屏、Full Disk Access
- **keychain**:由非 Apple 进程发起的访问
- **codesign**:无效的签名
- **xprotect**:检测恶意软件 (排除正常扫描)
- **sip**:SIP 被禁用或修改
### 添加自定义规则
编辑 `presets/rules.json`。每个规则具有以下结构:
```
{
"id": "mon-id-unique",
"name": "Nom de l'alerte",
"severity": "high",
"description": "Ce que ca signifie",
"conditions": [{"type": "msg_contains", "value": "texte a chercher"}]
}
```
要添加通用规则,请将其添加到 `general` 数组中。
要添加与特定预设相关的规则,请将其添加到 `preset > 预设名称` 下。
#### 条件类型
| 类型 | 参数 | 描述 |
|------|-----------|-------------|
| `msg_contains` | `value` | 消息包含此文本 |
| `msg_regex` | `value` | 消息匹配此正则表达式 |
| `path_contains` | `value` | 进程路径包含此文本 |
| `non_apple_process` | — | 该进程不是 Apple 二进制文件 |
| `hidden_path` | — | 该进程位于隐藏文件夹中 |
#### 排除误报
添加一个包含相同条件类型的 `exclude` 字段:
```
{
"id": "ma-regle",
"name": "Ma regle",
"severity": "medium",
"description": "Description",
"conditions": [{"type": "msg_regex", "value": "(detect|malware)"}],
"exclude": [{"type": "msg_regex", "value": "(Found scanner|activity)"}]
}
```
### 添加预设
编辑 `presets/presets.json`:
```
{
"mon-preset": {
"predicate": "process == \"monprocess\"",
"description": "Ce que ce preset surveille",
"notes": "Informations utiles",
"level": "info",
"threats": ["infostealers", "backdoors"]
}
}
```
## 访问级别
| 模式 | 可见内容 |
|------|----------------|
| Normal | 标准 log,部分数据会被隐藏 (``,DNS 主机名显示为 `mask.hash`) |
| `sudo ulwrap ...` | 所有内容均明文显示,包括标记为 `` 的数据 |
对于基础的威胁狩猎,normal 模式就足够了。
## 架构
```
ulwrap/
├── ulwrap.py # CLI entry point
├── install.sh # Script d'installation
├── core/
│ ├── runner.py # Execute la commande log native
│ ├── parser.py # Parse le JSON retourne par log
│ ├── formatter.py # Sortie human-readable, couleurs, filtrage, compteur
│ └── analyzer.py # Moteur d'analyse, charge rules.json
├── presets/
│ ├── presets.json # Presets avec predicates visibles
│ └── rules.json # Regles de detection (editables)
├── lib/
│ └── ulwrap.py # API Python reutilisable
└── tests/
├── test_parser.py
├── test_formatter.py
├── test_presets.py
└── test_runner.py
```
## 设计原则
predicate 始终可以通过 `--show-predicate` 查看。该工具不仅是辅助工具,还具有教育意义 —— 用户可以在使用中修改和学习。
标签:PB级数据处理, Python, 安全运维, 文档结构分析, 无后门, 逆向工具