performing-file-carving-with-foremost

使用 Foremost 的文件头/文件尾签名雕刻技术,从磁盘镜像和未分配空间中恢复文件,无论文件系统状态如何均可提取证据。

9 stars

Best use case

performing-file-carving-with-foremost is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

使用 Foremost 的文件头/文件尾签名雕刻技术,从磁盘镜像和未分配空间中恢复文件,无论文件系统状态如何均可提取证据。

Teams using performing-file-carving-with-foremost should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/performing-file-carving-with-foremost/SKILL.md --create-dirs "https://raw.githubusercontent.com/killvxk/cybersecurity-skills-zh/main/skills/performing-file-carving-with-foremost/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/performing-file-carving-with-foremost/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How performing-file-carving-with-foremost Compares

Feature / Agentperforming-file-carving-with-foremostStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

使用 Foremost 的文件头/文件尾签名雕刻技术,从磁盘镜像和未分配空间中恢复文件,无论文件系统状态如何均可提取证据。

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# 使用 Foremost 执行文件雕刻

## 适用场景
- 从未分配磁盘空间或损坏的文件系统中恢复文件时
- 从格式化或擦除的存储介质中提取证据时
- 文件系统元数据不可用但原始数据扇区包含证据时
- 调查需要从原始镜像中恢复特定文件类型时
- 作为基于文件系统恢复的补充,以最大化证据提取时

## 前置条件
- 取证工作站上已安装 Foremost
- 原始(dd)格式的取证磁盘镜像
- 充足的输出存储空间(可能大于源文件)
- 针对特定文件类型的自定义 foremost.conf(可选)
- 了解目标文件类型的文件签名(魔术字节)
- Scalpel 作为性能关键型雕刻的替代工具

## 工作流程

### 步骤 1:安装和配置 Foremost

```bash
# 安装 Foremost
sudo apt-get install foremost

# 验证安装
foremost -V

# 查看默认配置
cat /etc/foremost.conf

# 默认 foremost.conf 支持:
# jpg, gif, png, bmp - 图像格式
# avi, exe, mpg, wav - 媒体文件和可执行文件
# riff, wmv, mov, pdf - 文档和视频
# ole (doc/xls/ppt), zip, rar - Office 文件和压缩包
# htm, cpp, java - 文本/代码文件

# 为额外文件类型创建自定义配置
cp /etc/foremost.conf /cases/case-2024-001/custom_foremost.conf

# 添加自定义文件签名
cat << 'EOF' >> /cases/case-2024-001/custom_foremost.conf
# 调查自定义新增内容
# 格式:扩展名  区分大小写  最大大小  文件头  文件尾
    docx    y    10000000    \x50\x4b\x03\x04    \x50\x4b\x05\x06
    xlsx    y    10000000    \x50\x4b\x03\x04    \x50\x4b\x05\x06
    pptx    y    10000000    \x50\x4b\x03\x04    \x50\x4b\x05\x06
    sqlite  y    50000000    \x53\x51\x4c\x69\x74\x65\x20\x66\x6f\x72\x6d\x61\x74
    pst     y    500000000   \x21\x42\x44\x4e
    eml     y    1000000     \x46\x72\x6f\x6d\x3a    \x0d\x0a\x0d\x0a
    evtx    y    50000000    \x45\x6c\x66\x46\x69\x6c\x65
EOF
```

### 步骤 2:对磁盘镜像运行 Foremost

```bash
# 雕刻所有支持的文件类型(基本模式)
foremost -t all \
   -i /cases/case-2024-001/images/evidence.dd \
   -o /cases/case-2024-001/carved/foremost_all/

# 仅雕刻特定文件类型
foremost -t jpg,png,pdf,doc,xls,zip \
   -i /cases/case-2024-001/images/evidence.dd \
   -o /cases/case-2024-001/carved/foremost_targeted/

# 使用自定义配置
foremost -c /cases/case-2024-001/custom_foremost.conf \
   -i /cases/case-2024-001/images/evidence.dd \
   -o /cases/case-2024-001/carved/foremost_custom/

# 从特定分区偏移量开始雕刻
# 首先,查找分区信息
mmls /cases/case-2024-001/images/evidence.dd
# 然后仅从未分配空间雕刻
# 使用 blkls 提取未分配空间
blkls -o 2048 /cases/case-2024-001/images/evidence.dd \
   > /cases/case-2024-001/unallocated.dd

foremost -t all \
   -i /cases/case-2024-001/unallocated.dd \
   -o /cases/case-2024-001/carved/foremost_unalloc/

# 详细模式以查看进度
foremost -v -t all \
   -i /cases/case-2024-001/images/evidence.dd \
   -o /cases/case-2024-001/carved/foremost_verbose/ 2>&1 | \
   tee /cases/case-2024-001/carved/foremost_log.txt

# 间接模式(处理标准输入)
dd if=/cases/case-2024-001/images/evidence.dd bs=512 skip=2048 | \
   foremost -t jpg,pdf -o /cases/case-2024-001/carved/foremost_pipe/
```

### 步骤 3:使用 Scalpel 进行高性能雕刻

```bash
# 安装 Scalpel(基于 Foremost 的更快替代工具)
sudo apt-get install scalpel

# 编辑 Scalpel 配置(取消注释所需文件类型)
cp /etc/scalpel/scalpel.conf /cases/case-2024-001/scalpel.conf
# 在配置中取消注释目标文件类型对应的行

# 运行 Scalpel
scalpel -c /cases/case-2024-001/scalpel.conf \
   -o /cases/case-2024-001/carved/scalpel/ \
   /cases/case-2024-001/images/evidence.dd

# 带文件大小限制的 Scalpel
# 编辑 scalpel.conf 设置适当的最大大小:
# jpg  y  5000000  \xff\xd8\xff  \xff\xd9
# pdf  y  20000000 %PDF  %%EOF
```

### 步骤 4:处理并验证雕刻的文件

```bash
# 查看 Foremost 审计报告
cat /cases/case-2024-001/carved/foremost_all/audit.txt

# audit.txt 包含:
# - 每种类型找到的文件数
# - 起始和结束偏移量
# - 文件大小

# 验证雕刻的文件
python3 << 'PYEOF'
import os
import subprocess
from collections import defaultdict

carved_dir = '/cases/case-2024-001/carved/foremost_all/'
stats = defaultdict(lambda: {'total': 0, 'valid': 0, 'invalid': 0, 'size': 0})

for subdir in os.listdir(carved_dir):
    subdir_path = os.path.join(carved_dir, subdir)
    if not os.path.isdir(subdir_path) or subdir == 'audit.txt':
        continue

    for filename in os.listdir(subdir_path):
        filepath = os.path.join(subdir_path, filename)
        if not os.path.isfile(filepath):
            continue

        ext = subdir
        filesize = os.path.getsize(filepath)
        stats[ext]['total'] += 1
        stats[ext]['size'] += filesize

        # 使用 file 命令验证文件类型
        result = subprocess.run(['file', '--brief', filepath], capture_output=True, text=True)
        file_type = result.stdout.strip()

        if 'data' in file_type.lower() or 'empty' in file_type.lower():
            stats[ext]['invalid'] += 1
        else:
            stats[ext]['valid'] += 1

print("=== 雕刻文件验证 ===\n")
print(f"{'类型':<10} {'总数':<8} {'有效':<8} {'无效':<10} {'总大小':<15}")
print("-" * 55)
for ext in sorted(stats.keys()):
    s = stats[ext]
    size_mb = s['size'] / (1024*1024)
    print(f"{ext:<10} {s['total']:<8} {s['valid']:<8} {s['invalid']:<10} {size_mb:>10.1f} MB")

# 删除零字节文件
for subdir in os.listdir(carved_dir):
    subdir_path = os.path.join(carved_dir, subdir)
    if os.path.isdir(subdir_path):
        for filename in os.listdir(subdir_path):
            filepath = os.path.join(subdir_path, filename)
            if os.path.isfile(filepath) and os.path.getsize(filepath) == 0:
                os.remove(filepath)
PYEOF

# 对所有有效雕刻文件计算哈希
find /cases/case-2024-001/carved/foremost_all/ -type f ! -name "audit.txt" \
   -exec sha256sum {} \; > /cases/case-2024-001/carved/carved_file_hashes.txt

# 与已知恶意哈希数据库比对
# 与 NSRL 已知良性数据库比对以过滤
```

### 步骤 5:检查并归档证据文件

```bash
# 从雕刻图像中提取元数据(EXIF 数据,包括 GPS)
exiftool -r -csv /cases/case-2024-001/carved/foremost_all/jpg/ \
   > /cases/case-2024-001/analysis/carved_image_metadata.csv

# 在雕刻文档中搜索关键字
find /cases/case-2024-001/carved/foremost_all/pdf/ -name "*.pdf" -exec pdftotext {} - \; 2>/dev/null | \
   grep -iE '(confidential|secret|password|account|ssn|credit.card)' \
   > /cases/case-2024-001/analysis/keyword_hits_pdf.txt

# 生成图像缩略图以便快速审查
mkdir -p /cases/case-2024-001/carved/thumbnails/
find /cases/case-2024-001/carved/foremost_all/jpg/ -name "*.jpg" -exec \
   convert {} -thumbnail 200x200 /cases/case-2024-001/carved/thumbnails/{} \; 2>/dev/null

# 创建证据目录
python3 << 'PYEOF'
import os, hashlib, csv, subprocess

catalog = []
carved_dir = '/cases/case-2024-001/carved/foremost_all/'

for subdir in sorted(os.listdir(carved_dir)):
    subdir_path = os.path.join(carved_dir, subdir)
    if not os.path.isdir(subdir_path):
        continue
    for filename in sorted(os.listdir(subdir_path)):
        filepath = os.path.join(subdir_path, filename)
        if not os.path.isfile(filepath):
            continue
        size = os.path.getsize(filepath)
        sha256 = hashlib.sha256(open(filepath, 'rb').read()).hexdigest()
        file_type = subprocess.run(['file', '--brief', filepath], capture_output=True, text=True).stdout.strip()

        catalog.append({
            'filename': filename,
            'type': subdir,
            'size': size,
            'sha256': sha256,
            'file_description': file_type[:100]
        })

with open('/cases/case-2024-001/analysis/carved_file_catalog.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=['filename', 'type', 'size', 'sha256', 'file_description'])
    writer.writeheader()
    writer.writerows(catalog)

print(f"已创建包含 {len(catalog)} 个文件的证据目录")
PYEOF
```

## 关键概念

| 概念 | 描述 |
|------|------|
| 文件雕刻(File carving) | 通过在原始数据中搜索已知的文件头/文件尾字节序列来恢复文件 |
| 文件签名(File signature) | 标识文件类型的开头(文件头)或结尾(文件尾)的唯一字节模式 |
| 未分配空间(Unallocated space) | 未分配给任何文件的磁盘扇区;雕刻的主要目标 |
| 文件碎片化(Fragmentation) | 文件数据存储于非连续扇区时,使雕刻更加复杂 |
| 文件头文件尾雕刻 | 提取已知文件起始和结束签名之间的数据 |
| 误报(False positives) | 雕刻数据匹配文件签名但内容损坏或无关 |
| 松弛空间(Slack space) | 文件最后分配簇末尾的未使用字节 |
| 扇区对齐 | 文件通常从扇区边界开始,提高雕刻精度 |

## 工具与系统

| 工具 | 用途 |
|------|------|
| Foremost | 最初为美国空军 OSI 开发的文件头文件尾雕刻工具 |
| Scalpel | 支持可配置签名的高性能文件雕刻工具 |
| PhotoRec | 支持 300+ 格式的基于签名的文件恢复工具 |
| bulk_extractor | 从原始数据中提取特征(邮箱、URL、信用卡号) |
| blkls | Sleuth Kit 工具,从磁盘镜像中提取未分配空间 |
| mmls | 用于识别雕刻目标的分区表显示工具 |
| ExifTool | 从雕刻的图像和文档文件中提取元数据 |
| hashdeep | 对雕刻文件目录进行递归哈希计算 |

## 常见场景

**场景 1:恢复已删除的证据文档**
针对用 blkls 提取的未分配空间运行 Foremost,目标文件类型为 doc、pdf、xlsx,验证雕刻文档,搜索与案件相关的关键字,归档并哈希所有可恢复文档,作为证据提交。

**场景 2:从格式化介质中恢复图像**
从格式化 U 盘镜像中雕刻 JPEG、PNG、GIF、BMP,提取包含 GPS 坐标和相机信息的 EXIF 元数据,生成缩略图以便快速视觉审查,识别与证据相关的图像,记录恢复链。

**场景 3:从损坏的 PST 文件中恢复电子邮件**
使用带有 PST 和 EML 签名的自定义 foremost.conf,从损坏的 Outlook 数据文件中雕刻邮件制品,尝试在查看器中打开雕刻的 PST 片段,提取单独的 EML 邮件,搜索相关通信内容。

**场景 4:金融调查的数据库恢复**
配置 Foremost 从未分配空间中雕刻 SQLite 数据库,恢复已删除的应用程序数据库,查询恢复的数据库中的财务记录,与已知交易数据交叉比对,记录调查发现以用于起诉。

## 输出格式

```
文件雕刻摘要:
  工具:Foremost 1.5.7
  来源:evidence.dd(500 GB)
  目标:未分配空间(234 GB)
  耗时:1 小时 45 分钟

  雕刻文件:
    jpg:    2,345 个文件(1.8 GB)- 有效:2,100 / 无效:245
    png:      234 个文件(456 MB)- 有效:210 / 无效:24
    pdf:      156 个文件(890 MB)- 有效:134 / 无效:22
    doc:       89 个文件(234 MB)- 有效:67 / 无效:22
    xls:       45 个文件(123 MB)- 有效:38 / 无效:7
    zip:       67 个文件(567 MB)- 有效:52 / 无效:15
    exe:       34 个文件(234 MB)- 有效:30 / 无效:4
    sqlite:    12 个文件(89 MB) - 有效:10 / 无效:2

  总文件数:2,982(恢复 3.4 GB)
  与证据相关:标记 45 个文件待审查
  审计日志:/cases/case-2024-001/carved/foremost_all/audit.txt
  文件目录:/cases/case-2024-001/analysis/carved_file_catalog.csv
```

Related Skills

recovering-deleted-files-with-photorec

9
from killvxk/cybersecurity-skills-zh

使用 PhotoRec 基于文件签名的数据雕刻(File Carving)引擎,从磁盘镜像和存储介质中恢复已删除文件,无论文件系统是否损坏。

performing-yara-rule-development-for-detection

9
from killvxk/cybersecurity-skills-zh

通过识别可执行文件中的唯一字节模式、字符串和行为指标,开发精准的 YARA 恶意软件检测规则,同时将误报率降至最低。

performing-wireless-security-assessment-with-kismet

9
from killvxk/cybersecurity-skills-zh

使用 Kismet 通过被动射频监控进行无线网络安全评估,检测流氓接入点(Rogue AP)、隐藏 SSID、弱加密和未授权客户端。

performing-wireless-network-penetration-test

9
from killvxk/cybersecurity-skills-zh

执行无线网络渗透测试,通过捕获握手包、破解 WPA2/WPA3 密钥、检测流氓接入点以及使用 Aircrack-ng 和相关工具测试无线网络分段,评估 WiFi 安全性。

performing-windows-artifact-analysis-with-eric-zimmerman-tools

9
from killvxk/cybersecurity-skills-zh

使用 Eric Zimmerman 的开源 EZ Tools 套件(包括 KAPE、MFTECmd、PECmd、LECmd、JLECmd 和 Timeline Explorer)执行全面的 Windows 取证制品分析,解析注册表 hive、预取文件、事件日志和文件系统元数据。

performing-wifi-password-cracking-with-aircrack

9
from killvxk/cybersecurity-skills-zh

在授权无线安全评估中捕获 WPA/WPA2 握手包,并使用 aircrack-ng、hashcat 和字典攻击进行离线密码破解, 以评估密码短语强度和无线网络安全状况。

performing-web-cache-poisoning-attack

9
from killvxk/cybersecurity-skills-zh

在授权安全测试期间,通过未纳入缓存键的头部和参数毒化缓存响应,利用 Web 缓存机制向其他用户投递恶意内容。

performing-web-cache-deception-attack

9
from killvxk/cybersecurity-skills-zh

通过利用 CDN 缓存层与源服务器之间的路径规范化差异,执行 Web 缓存欺骗攻击,从而缓存并获取敏感的已认证内容。

performing-web-application-vulnerability-triage

9
from killvxk/cybersecurity-skills-zh

使用 OWASP 风险评级方法论对 DAST/SAST 扫描器的 Web 应用程序漏洞发现进行分类,区分真阳性和假阳性,并确定修复优先级。

performing-web-application-scanning-with-nikto

9
from killvxk/cybersecurity-skills-zh

Nikto 是一款开源 Web 服务器和 Web 应用程序扫描器,可针对超过 7,000 个潜在危险文件/程序进行测试,检查超过 1,250 个服务器的过期版本,并识别超过 270 个服务器的版本特定问题。

performing-web-application-penetration-test

9
from killvxk/cybersecurity-skills-zh

遵循 OWASP Web 安全测试指南(WSTG)方法论,对 Web 应用程序执行系统化安全测试,识别认证、授权、 输入验证、会话管理和业务逻辑中的漏洞。测试人员以 Burp Suite 作为主要拦截代理,结合手动测试技术 发现自动化扫描器遗漏的缺陷。适用于 Web 应用渗透测试、OWASP 测试、应用安全评估或 Web 漏洞测试等请求场景。

performing-web-application-firewall-bypass

9
from killvxk/cybersecurity-skills-zh

使用编码技术、HTTP 方法操控、参数污染和载荷混淆绕过 Web 应用防火墙保护,将 SQL 注入、XSS 及其他攻击载荷穿透 WAF 检测规则。