mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
Translated ['src/hardware-physical-access/firmware-analysis/README.md',
This commit is contained in:
parent
ba9d630259
commit
002e9d406c
@ -769,6 +769,7 @@
|
||||
- [Ret2vDSO](binary-exploitation/rop-return-oriented-programing/ret2vdso.md)
|
||||
- [SROP - Sigreturn-Oriented Programming](binary-exploitation/rop-return-oriented-programing/srop-sigreturn-oriented-programming/README.md)
|
||||
- [SROP - ARM64](binary-exploitation/rop-return-oriented-programing/srop-sigreturn-oriented-programming/srop-arm64.md)
|
||||
- [Synology Encrypted Archive Decryption](hardware-physical-access/firmware-analysis/synology-encrypted-archive-decryption.md)
|
||||
- [Array Indexing](binary-exploitation/array-indexing.md)
|
||||
- [Chrome Exploiting](binary-exploitation/chrome-exploiting.md)
|
||||
- [Integer Overflow](binary-exploitation/integer-overflow.md)
|
||||
|
||||
@ -1,14 +1,20 @@
|
||||
# Firmware Analysis
|
||||
# 固件分析
|
||||
|
||||
{{#include ../../banners/hacktricks-training.md}}
|
||||
|
||||
## **Introduction**
|
||||
## **介绍**
|
||||
|
||||
### 相关资源
|
||||
|
||||
{{#ref}}
|
||||
synology-encrypted-archive-decryption.md
|
||||
{{#endref}}
|
||||
|
||||
固件是使设备正常运行的基本软件,通过管理和促进硬件组件与用户交互的软件之间的通信。它存储在永久内存中,确保设备在开机时能够访问重要指令,从而启动操作系统。检查和可能修改固件是识别安全漏洞的关键步骤。
|
||||
|
||||
## **Gathering Information**
|
||||
## **收集信息**
|
||||
|
||||
**收集信息**是理解设备构成和所使用技术的关键初步步骤。此过程涉及收集以下数据:
|
||||
**收集信息**是理解设备构成及其使用技术的关键初步步骤。此过程涉及收集以下数据:
|
||||
|
||||
- CPU架构和运行的操作系统
|
||||
- 引导加载程序的具体信息
|
||||
@ -19,14 +25,14 @@
|
||||
- 架构和流程图
|
||||
- 安全评估和已识别的漏洞
|
||||
|
||||
为此,**开源情报(OSINT)**工具是不可或缺的,同时通过手动和自动审查过程分析任何可用的开源软件组件也很重要。像[Coverity Scan](https://scan.coverity.com)和[Semmle’s LGTM](https://lgtm.com/#explore)这样的工具提供免费的静态分析,可以用来发现潜在问题。
|
||||
为此,**开源情报(OSINT)**工具是不可或缺的,同时对任何可用的开源软件组件进行手动和自动审查也是非常重要的。像[Coverity Scan](https://scan.coverity.com)和[Semmle’s LGTM](https://lgtm.com/#explore)这样的工具提供免费的静态分析,可以用来发现潜在问题。
|
||||
|
||||
## **Acquiring the Firmware**
|
||||
## **获取固件**
|
||||
|
||||
获取固件可以通过多种方式进行,每种方式的复杂程度不同:
|
||||
|
||||
- **直接**从源头(开发者、制造商)
|
||||
- **根据**提供的说明**构建**
|
||||
- **根据**提供的说明进行**构建**
|
||||
- **从**官方支持网站**下载**
|
||||
- 利用**Google dork**查询查找托管的固件文件
|
||||
- 直接访问**云存储**,使用像[S3Scanner](https://github.com/sa7mon/S3Scanner)这样的工具
|
||||
@ -35,11 +41,11 @@
|
||||
- 在设备通信中**嗅探**更新请求
|
||||
- 识别并使用**硬编码的更新端点**
|
||||
- 从引导加载程序或网络**转储**
|
||||
- 在所有其他方法失败时,**拆卸并读取**存储芯片,使用适当的硬件工具
|
||||
- 在所有其他方法失败时,使用适当的硬件工具**拆卸并读取**存储芯片
|
||||
|
||||
## Analyzing the firmware
|
||||
## 分析固件
|
||||
|
||||
现在你**拥有固件**,你需要提取有关它的信息,以了解如何处理它。你可以使用的不同工具有:
|
||||
现在你**拥有固件**,你需要提取有关它的信息,以了解如何处理它。你可以使用的不同工具包括:
|
||||
```bash
|
||||
file <bin>
|
||||
strings -n8 <bin>
|
||||
@ -105,7 +111,7 @@ $ dd if=DIR850L_REVB.bin bs=1 skip=1704084 of=dir.squashfs
|
||||
|
||||
`$ jefferson rootfsfile.jffs2`
|
||||
|
||||
- 对于带有 NAND 闪存的 ubifs 文件系统
|
||||
- 对于带 NAND 闪存的 ubifs 文件系统
|
||||
|
||||
`$ ubireader_extract_images -u UBI -s <start_offset> <bin>`
|
||||
|
||||
@ -117,7 +123,7 @@ $ dd if=DIR850L_REVB.bin bs=1 skip=1704084 of=dir.squashfs
|
||||
|
||||
### 初步分析工具
|
||||
|
||||
提供了一组命令用于对二进制文件(称为 `<bin>`)进行初步检查。这些命令有助于识别文件类型、提取字符串、分析二进制数据以及理解分区和文件系统的细节:
|
||||
提供了一组命令用于初步检查二进制文件(称为 `<bin>`)。这些命令有助于识别文件类型、提取字符串、分析二进制数据以及理解分区和文件系统的细节:
|
||||
```bash
|
||||
file <bin>
|
||||
strings -n8 <bin>
|
||||
@ -128,7 +134,7 @@ fdisk -lu <bin> #lists partitions and filesystems, if there are multiple
|
||||
```
|
||||
为了评估图像的加密状态,使用 `binwalk -E <bin>` 检查 **entropy**。低熵表明缺乏加密,而高熵则表示可能存在加密或压缩。
|
||||
|
||||
对于提取 **embedded files**,推荐使用 **file-data-carving-recovery-tools** 文档和 **binvis.io** 进行文件检查。
|
||||
对于提取 **embedded files**,推荐使用 **file-data-carving-recovery-tools** 文档和 **binvis.io** 进行文件检查的工具和资源。
|
||||
|
||||
### 提取文件系统
|
||||
|
||||
@ -188,25 +194,25 @@ sudo apt-get install qemu qemu-user qemu-user-static qemu-system-arm qemu-system
|
||||
|
||||
### 完整系统仿真
|
||||
|
||||
像 [Firmadyne](https://github.com/firmadyne/firmadyne)、[Firmware Analysis Toolkit](https://github.com/attify/firmware-analysis-toolkit) 等工具,促进完整固件仿真,自动化过程并帮助动态分析。
|
||||
像 [Firmadyne](https://github.com/firmadyne/firmadyne)、[Firmware Analysis Toolkit](https://github.com/attify/firmware-analysis-toolkit) 等工具,促进了完整固件仿真,自动化了过程并帮助进行动态分析。
|
||||
|
||||
## 实践中的动态分析
|
||||
|
||||
在此阶段,使用真实或仿真的设备环境进行分析。保持对操作系统和文件系统的 shell 访问至关重要。仿真可能无法完美模拟硬件交互,因此需要偶尔重新启动仿真。分析应重新访问文件系统,利用暴露的网页和网络服务,并探索引导加载程序漏洞。固件完整性测试对于识别潜在后门漏洞至关重要。
|
||||
在这个阶段,使用真实或仿真的设备环境进行分析。保持对操作系统和文件系统的 shell 访问是至关重要的。仿真可能无法完美模拟硬件交互,因此需要偶尔重新启动仿真。分析应重新访问文件系统,利用暴露的网页和网络服务,并探索引导加载程序漏洞。固件完整性测试对于识别潜在后门漏洞至关重要。
|
||||
|
||||
## 运行时分析技术
|
||||
|
||||
运行时分析涉及在其操作环境中与进程或二进制文件交互,使用 gdb-multiarch、Frida 和 Ghidra 等工具设置断点,并通过模糊测试和其他技术识别漏洞。
|
||||
运行时分析涉及在其操作环境中与进程或二进制文件交互,使用工具如 gdb-multiarch、Frida 和 Ghidra 设置断点,并通过模糊测试和其他技术识别漏洞。
|
||||
|
||||
## 二进制利用和概念验证
|
||||
|
||||
为识别的漏洞开发 PoC 需要对目标架构和低级语言编程有深入理解。嵌入式系统中的二进制运行时保护很少见,但如果存在,可能需要使用如返回导向编程(ROP)等技术。
|
||||
为识别的漏洞开发 PoC 需要对目标架构和低级语言编程有深入理解。嵌入式系统中的二进制运行时保护很少见,但在存在时,可能需要使用如返回导向编程(ROP)等技术。
|
||||
|
||||
## 准备好的操作系统用于固件分析
|
||||
|
||||
像 [AttifyOS](https://github.com/adi0x90/attifyos) 和 [EmbedOS](https://github.com/scriptingxss/EmbedOS) 这样的操作系统提供预配置的固件安全测试环境,配备必要的工具。
|
||||
操作系统如 [AttifyOS](https://github.com/adi0x90/attifyos) 和 [EmbedOS](https://github.com/scriptingxss/EmbedOS) 提供了预配置的固件安全测试环境,配备必要的工具。
|
||||
|
||||
## 准备好的操作系统分析固件
|
||||
## 准备好的操作系统用于分析固件
|
||||
|
||||
- [**AttifyOS**](https://github.com/adi0x90/attifyos):AttifyOS 是一个旨在帮助您对物联网(IoT)设备进行安全评估和渗透测试的发行版。它通过提供一个预配置的环境,加载所有必要的工具,节省了您大量时间。
|
||||
- [**EmbedOS**](https://github.com/scriptingxss/EmbedOS):基于 Ubuntu 18.04 的嵌入式安全测试操作系统,预装固件安全测试工具。
|
||||
|
||||
@ -0,0 +1,162 @@
|
||||
# Synology PAT/SPK 加密档案解密
|
||||
|
||||
{{#include ../../banners/hacktricks-training.md}}
|
||||
|
||||
## 概述
|
||||
|
||||
多个 Synology 设备(DSM/BSM NAS, BeeStation 等)以 **加密的 PAT / SPK 档案** 形式分发其固件和应用程序包。这些档案可以通过仅使用公共下载文件在 *离线* 状态下解密,得益于嵌入在官方提取库中的硬编码密钥。
|
||||
|
||||
本页面逐步记录了加密格式的工作原理以及如何完全恢复每个包内的明文 **TAR**。该过程基于 Synacktiv 在 2024 年 Pwn2Own 爱尔兰期间进行的研究,并在开源工具 [`synodecrypt`](https://github.com/synacktiv/synodecrypt) 中实现。
|
||||
|
||||
> ⚠️ `*.pat`(系统更新)和 `*.spk`(应用程序)档案的格式完全相同——它们仅在选择的硬编码密钥对上有所不同。
|
||||
|
||||
---
|
||||
|
||||
## 1. 获取档案
|
||||
|
||||
固件/应用程序更新通常可以从 Synology 的公共门户下载:
|
||||
```bash
|
||||
$ wget https://archive.synology.com/download/Os/BSM/BSM_BST150-4T_65374.pat
|
||||
```
|
||||
## 2. 转储 PAT 结构(可选)
|
||||
|
||||
`*.pat` 镜像本身是一个 **cpio 包**,其中嵌入了多个文件(引导加载程序、内核、rootfs、软件包……)。 免费工具 [`patology`](https://github.com/sud0woodo/patology) 方便用于检查该封装:
|
||||
```bash
|
||||
$ python3 patology.py --dump -i BSM_BST150-4T_65374.pat
|
||||
[…]
|
||||
$ ls
|
||||
DiskCompatibilityDB.tar hda1.tgz rd.bin packages/ …
|
||||
```
|
||||
对于 `*.spk`,您可以直接跳到第 3 步。
|
||||
|
||||
## 3. 提取 Synology 解压库
|
||||
|
||||
真正的解密逻辑位于:
|
||||
|
||||
* `/usr/syno/sbin/synoarchive` → 主要 CLI 包装器
|
||||
* `/usr/lib/libsynopkg.so.1` → 从 DSM UI 调用包装器
|
||||
* `libsynocodesign.so` → **包含加密实现**
|
||||
|
||||
这两个二进制文件都存在于系统根文件系统 (`hda1.tgz`) **和** 压缩的 init-rd (`rd.bin`) 中。如果您只有 PAT,可以通过以下方式获取它们:
|
||||
```bash
|
||||
# rd.bin is LZMA-compressed CPIO
|
||||
$ lzcat rd.bin | cpio -id 2>/dev/null
|
||||
$ file usr/lib/libsynocodesign.so
|
||||
usr/lib/libsynocodesign.so: ELF 64-bit LSB shared object, ARM aarch64, …
|
||||
```
|
||||
## 4. 恢复硬编码的密钥 (`get_keys`)
|
||||
|
||||
在 `libsynocodesign.so` 中,函数 `get_keys(int keytype)` 简单地返回请求的归档系列的两个 128 位全局变量:
|
||||
```c
|
||||
case 0: // PAT (system)
|
||||
case 10:
|
||||
case 11:
|
||||
signature_key = qword_23A40;
|
||||
master_key = qword_23A68;
|
||||
break;
|
||||
|
||||
case 3: // SPK (applications)
|
||||
signature_key = qword_23AE0;
|
||||
master_key = qword_23B08;
|
||||
break;
|
||||
```
|
||||
* **signature_key** → Ed25519 公钥,用于验证归档头。
|
||||
* **master_key** → 根密钥,用于推导每个归档的加密密钥。
|
||||
|
||||
您只需为每个 DSM 主要版本转储这两个常量一次。
|
||||
|
||||
## 5. 头结构与签名验证
|
||||
|
||||
`synoarchive_open()` → `support_format_synoarchive()` → `archive_read_support_format_synoarchive()` 执行以下操作:
|
||||
|
||||
1. 读取魔数 (3 字节) `0xBFBAAD` **或** `0xADBEEF`。
|
||||
2. 读取小端 32 位 `header_len`。
|
||||
3. 读取 `header_len` 字节 + 下一个 **0x40 字节的 Ed25519 签名**。
|
||||
4. 遍历所有嵌入的公钥,直到 `crypto_sign_verify_detached()` 成功。
|
||||
5. 使用 **MessagePack** 解码头,得到:
|
||||
```python
|
||||
[
|
||||
data: bytes,
|
||||
entries: [ [size: int, sha256: bytes], … ],
|
||||
archive_description: bytes,
|
||||
serial_number: [bytes],
|
||||
not_valid_before: int
|
||||
]
|
||||
```
|
||||
`entries` 后续允许 libarchive 在解密每个文件时进行完整性检查。
|
||||
|
||||
## 6. 派生每个归档的子密钥
|
||||
|
||||
从 MessagePack 头中包含的 `data` blob:
|
||||
|
||||
* `subkey_id` = 小端 `uint64` 在偏移量 0x10
|
||||
* `ctx` = 偏移量 0x18 处的 7 字节
|
||||
|
||||
32 字节的 **stream key** 通过 libsodium 获得:
|
||||
```c
|
||||
crypto_kdf_derive_from_key(kdf_subkey, 32, subkey_id, ctx, master_key);
|
||||
```
|
||||
## 7. Synology的自定义 **libarchive** 后端
|
||||
|
||||
Synology捆绑了一个修补过的libarchive,当魔术值为`0xADBEEF`时,它会注册一个假的"tar"格式:
|
||||
```c
|
||||
register_format(
|
||||
"tar", spk_bid, spk_options,
|
||||
spk_read_header, spk_read_data, spk_read_data_skip,
|
||||
NULL, spk_cleanup, NULL, NULL);
|
||||
```
|
||||
### spk_read_header()
|
||||
```
|
||||
- Read 0x200 bytes
|
||||
- nonce = buf[0:0x18]
|
||||
- cipher = buf[0x18:0x18+0x193]
|
||||
- crypto_secretstream_xchacha20poly1305_init_pull(state, nonce, kdf_subkey)
|
||||
- crypto_secretstream_xchacha20poly1305_pull(state, tar_hdr, …, cipher, 0x193)
|
||||
```
|
||||
解密后的 `tar_hdr` 是一个 **经典的 POSIX TAR 头**。
|
||||
|
||||
### spk_read_data()
|
||||
```
|
||||
while (remaining > 0):
|
||||
chunk_len = min(0x400000, remaining) + 0x11 # +tag
|
||||
buf = archive_read_ahead(chunk_len)
|
||||
crypto_secretstream_xchacha20poly1305_pull(state, out, …, buf, chunk_len)
|
||||
remaining -= chunk_len - 0x11
|
||||
```
|
||||
每个 **0x18-byte nonce** 被添加到加密块之前。
|
||||
|
||||
一旦所有条目被处理,libarchive 生成一个完全有效的 **`.tar`**,可以使用任何标准工具解压。
|
||||
|
||||
## 8. 使用 synodecrypt 解密所有内容
|
||||
```bash
|
||||
$ python3 synodecrypt.py SynologyPhotos-rtd1619b-1.7.0-0794.spk
|
||||
[+] found matching keys (SPK)
|
||||
[+] header signature verified
|
||||
[+] 104 entries
|
||||
[+] archive successfully decrypted → SynologyPhotos-rtd1619b-1.7.0-0794.tar
|
||||
|
||||
$ tar xf SynologyPhotos-rtd1619b-1.7.0-0794.tar
|
||||
```
|
||||
`synodecrypt` 自动检测 PAT/SPK,加载正确的密钥并应用上述完整链。
|
||||
|
||||
## 9. 常见陷阱
|
||||
|
||||
* **不要** 交换 `signature_key` 和 `master_key` – 它们的用途不同。
|
||||
* **nonce** 在每个块(头部和数据)的密文 *之前*。
|
||||
* 最大加密块大小为 **0x400000 + 0x11**(libsodium 标签)。
|
||||
* 为一个 DSM 版本创建的档案可能在下一个版本中切换到不同的硬编码密钥。
|
||||
|
||||
## 10. 额外工具
|
||||
|
||||
* [`patology`](https://github.com/sud0woodo/patology) – 解析/转储 PAT 档案。
|
||||
* [`synodecrypt`](https://github.com/synacktiv/synodecrypt) – 解密 PAT/SPK/其他。
|
||||
* [`libsodium`](https://github.com/jedisct1/libsodium) – XChaCha20-Poly1305 secretstream 的参考实现。
|
||||
* [`msgpack`](https://msgpack.org/) – 头部序列化。
|
||||
|
||||
## 参考文献
|
||||
|
||||
- [Extraction of Synology encrypted archives – Synacktiv (Pwn2Own IE 2024)](https://www.synacktiv.com/publications/extraction-des-archives-chiffrees-synology-pwn2own-irlande-2024.html)
|
||||
- [synodecrypt on GitHub](https://github.com/synacktiv/synodecrypt)
|
||||
- [patology on GitHub](https://github.com/sud0woodo/patology)
|
||||
|
||||
{{#include ../../banners/hacktricks-training.md}}
|
||||
@ -1,14 +1,14 @@
|
||||
# 命令注入
|
||||
# Command Injection
|
||||
|
||||
{{#include ../banners/hacktricks-training.md}}
|
||||
|
||||
## 什么是命令注入?
|
||||
|
||||
**命令注入** 允许攻击者在托管应用程序的服务器上执行任意操作系统命令。因此,应用程序及其所有数据可能会完全被破坏。这些命令的执行通常使攻击者能够获得对应用程序环境和底层系统的未经授权的访问或控制。
|
||||
**命令注入** 允许攻击者在托管应用程序的服务器上执行任意操作系统命令。因此,应用程序及其所有数据可能会完全被妥协。这些命令的执行通常使攻击者能够获得对应用程序环境和底层系统的未经授权的访问或控制。
|
||||
|
||||
### 上下文
|
||||
|
||||
根据 **输入被注入的位置**,您可能需要在命令之前 **终止引用上下文**(使用 `"` 或 `'`)。
|
||||
根据 **输入被注入的位置**,您可能需要在命令之前 **终止引用上下文**(使用 `"` 或 `'`)。
|
||||
|
||||
## 命令注入/执行
|
||||
```bash
|
||||
@ -75,7 +75,7 @@ vuln=echo PAYLOAD > /tmp/pay.txt; cat /tmp/pay.txt | base64 -d > /tmp/pay; chmod
|
||||
```
|
||||
### 基于时间的数据外泄
|
||||
|
||||
逐字符提取数据:
|
||||
逐字符提取数据:
|
||||
```
|
||||
swissky@crashlab▸ ~ ▸ $ time if [ $(whoami|cut -c 1) == s ]; then sleep 5; fi
|
||||
real 0m5.007s
|
||||
@ -87,7 +87,7 @@ real 0m0.002s
|
||||
user 0m0.000s
|
||||
sys 0m0.000s
|
||||
```
|
||||
### 基于DNS的数据外泄
|
||||
### DNS 基于数据外泄
|
||||
|
||||
基于工具 `https://github.com/HoLyVieR/dnsbin`,也托管在 dnsbin.zhack.ca
|
||||
```
|
||||
@ -117,6 +117,28 @@ powershell C:**2\n??e*d.*? # notepad
|
||||
../linux-hardening/bypass-bash-restrictions/
|
||||
{{#endref}}
|
||||
|
||||
### Node.js `child_process.exec` 与 `execFile`
|
||||
|
||||
在审计 JavaScript/TypeScript 后端时,您经常会遇到 Node.js `child_process` API。
|
||||
```javascript
|
||||
// Vulnerable: user-controlled variables interpolated inside a template string
|
||||
const { exec } = require('child_process');
|
||||
exec(`/usr/bin/do-something --id_user ${id_user} --payload '${JSON.stringify(payload)}'`, (err, stdout) => {
|
||||
/* … */
|
||||
});
|
||||
```
|
||||
`exec()` 生成一个 **shell** (`/bin/sh -c`),因此任何对 shell 有特殊意义的字符(反引号、`;`、`&&`、`|`、`$()` 等)在用户输入与字符串连接时将导致 **命令注入**。
|
||||
|
||||
**缓解措施:** 使用 `execFile()`(或不带 `shell` 选项的 `spawn()`),并将 **每个参数作为单独的数组元素** 提供,以便不涉及 shell:
|
||||
```javascript
|
||||
const { execFile } = require('child_process');
|
||||
execFile('/usr/bin/do-something', [
|
||||
'--id_user', id_user,
|
||||
'--payload', JSON.stringify(payload)
|
||||
]);
|
||||
```
|
||||
真实案例:*Synology Photos* ≤ 1.7.0-0794 通过一个未经身份验证的 WebSocket 事件被利用,该事件将攻击者控制的数据放入 `id_user` 中,随后嵌入到 `exec()` 调用中,实现了 RCE(Pwn2Own 爱尔兰 2024)。
|
||||
|
||||
## 暴力破解检测列表
|
||||
|
||||
{{#ref}}
|
||||
@ -125,7 +147,9 @@ https://github.com/carlospolop/Auto_Wordlists/blob/main/wordlists/command_inject
|
||||
|
||||
## 参考文献
|
||||
|
||||
- [https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection)
|
||||
- [https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection)
|
||||
- [https://portswigger.net/web-security/os-command-injection](https://portswigger.net/web-security/os-command-injection)
|
||||
- [Extraction of Synology encrypted archives – Synacktiv 2025](https://www.synacktiv.com/publications/extraction-des-archives-chiffrees-synology-pwn2own-irlande-2024.html)
|
||||
|
||||
{{#include ../banners/hacktricks-training.md}}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user