🔍 数字取证 — 磁盘镜像分析

用科学方法还原数字真相

📖 数字取证基础

数字取证(Digital Forensics)是使用科学方法收集、保存、分析和呈现数字证据的学科。取证的核心原则是证据保全——确保原始证据不被修改。

数字取证流程: 1. 识别 (Identification) └── 确定取证目标和范围 2. 保全 (Preservation) ← 最关键 ├── 写阻止器(硬件/软件) ├── 计算哈希值(SHA256) ├── 创建法医镜像 └── 链式保管记录 3. 收集 (Collection) ├── 磁盘镜像 ├── 内存采集 ├── 网络证据 └── 日志收集 4. 分析 (Analysis) ← 本课重点 ├── 文件系统分析 ├── 已删除文件恢复 ├── 时间线分析 ├── 关键字搜索 └── 注册表分析 5. 报告 (Reporting) ├── 发现摘要 ├── 证据链 └── 专业意见
数字取证的黄金规则:永远不要在原始证据上操作!始终创建法医镜像(forensic image),所有分析都在镜像上进行。

🔬 磁盘镜像创建

使用dd创建原始镜像

# 1. 硬件写阻止器(推荐)
# Tableau T35u (USB 3.0写阻止器)
# 或 WiebeTech USB WriteBlocker

# 2. 软件写阻止
# Linux: blockdev --setro /dev/sdb
# 确认只读: blockdev --getro /dev/sdb → 1

# 3. 创建法医镜像 (dd)
# ⚠️ 确保if=源磁盘,of=目标文件!方向反了会覆盖证据!
dd if=/dev/sdb of=/evidence/disk_image.dd bs=4M status=progress

# 4. 计算哈希(取证前后都要计算)
sha256sum /dev/sdb > /evidence/source_hash.txt
# 记录: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  /dev/sdb

dd if=/dev/sdb of=/evidence/disk_image.dd bs=4M
sha256sum /evidence/disk_image.dd > /evidence/image_hash.txt
# 两个hash必须匹配!

# 5. EWF格式(E01 - 带压缩和校验)
# 使用ewfacquire
ewfacquire /dev/sdb \
  -c cases \
  -e examiner \
  -d "Seized laptop" \
  -t disk \
  -f ewf \
  -C sha256 \
  /evidence/disk_image

# 6. 使用dc3dd (法医增强版dd)
dc3dd if=/dev/sdb \
  of=/evidence/disk_image.dd \
  hash=sha256 \
  log=/evidence/dc3dd.log \
  hlog=/evidence/hash.log

远程镜像采集

# 使用netcat远程传输
# 目标机器(被取证):
dd if=/dev/sda bs=4M | nc -l -p 9000

# 取证工作站:
nc target_ip 9000 | dd of=/evidence/remote_image.dd bs=4M

# 更安全: 使用SSH
ssh forensics@target "dd if=/dev/sda bs=4M" | dd of=/evidence/remote_image.dd bs=4M

📊 镜像分析实战

文件系统分析

# 使用The Sleuth Kit (TSK)
# 安装: apt install sleuthkit

# 1. 镜像信息
mmls disk_image.dd
# 输出:
# DOS Partition Table
# Slot     Start       End         Length      Description
# 00:00    0           2047        2048        Unallocated
# 01:01    2048        1050623     1048576     NTFS (0x07)
# 01:02    1050624     2099199     1048576     Linux (0x83)

# 2. 列出文件系统内容
fls -r -o 2048 disk_image.dd
# 递归列出所有文件和目录
# r/r 128:Documents/report.docx
# r/r 256:Documents/secret.pdf
# d/d 512:Tools/
# r/r * 640:Tools/hack.exe  ← * 表示已删除

# 3. 搜索文件名
find disk_image.dd -o 2048 -name "*.exe" -print

# 4. 查看文件元数据
istat disk_image.dd -o 2048 128
# MFT Entry: 128
# Created: 2024-01-15 03:42:11
# Modified: 2024-01-15 05:30:00
# Accessed: 2024-01-15 03:42:11

# 5. 提取文件
icat disk_image.dd -o 2048 128 > extracted_report.docx

# 6. 搜索关键词
srch_strings -a disk_image.dd | grep -i "password\|secret\|confidential"

# 7. 时间线分析
# 生成文件系统时间线
fls -r -m "/" disk_image.dd -o 2048 > body.txt
mactime -b body.txt > timeline.csv
# 输出:
# 2024-01-15 03:42:11,128,m...,r/rr,Documents/report.docx
# 2024-01-15 03:50:00,640,.a..,r/r*,Tools/hack.exe  ← 删除的文件!

已删除文件恢复

# 1. TSK恢复已删除文件
# icat可以恢复NTFS中仍存在的文件数据
icat disk_image.dd -o 2048 640 > recovered_hack.exe

# 2. 使用photorec恢复文件(基于文件签名)
photorec disk_image.dd
# 可以恢复: JPG, PNG, PDF, DOC, ZIP, EXE等
# 即使文件系统损坏也能恢复

# 3. 使用foremost恢复
foremost -i disk_image.dd -o /evidence/recovered/
# -t 指定文件类型
foremost -i disk_image.dd -o /evidence/recovered/ -t pdf,docx,png

# 4. NTFS特殊分析
# 备用数据流(Alternate Data Streams)
# 攻击者可能在ADS中隐藏数据
# 使用TskRecover或NtfsStreams

🧠 内存取证

# 内存取证比磁盘取证更能发现运行时证据
# 密码、密钥、网络连接、进程信息只在内存中存在

# 1. 内存采集
# Linux:
dd if=/dev/mem of=/evidence/memory.lime bs=1M
# 或使用LiME (Linux Memory Extractor)
insmod lime.ko "path=/evidence/memory.lime format=lime"

# Windows:
# 使用WinPmem或DumpIt
winpmem_mini.exe /evidence/memory.raw

# 2. 使用Volatility3分析内存
pip install volatility3

# 识别操作系统
vol -f memory.raw windows.info

# 列出进程
vol -f memory.raw windows.pslist
# PID   PPID  ImageFileName       CreateTime
# 4     0     System
# 1234  4     smss.exe
# 5678  1234  csrss.exe
# 9012  5678  suspicious.exe      ← 可疑进程!

# 进程树
vol -f memory.raw windows.pstree

# 网络连接
vol -f memory.raw windows.netstat
# Proto  LocalAddr     ForeignAddr       State    PID
# TCP    0.0.0.0:4444  0.0.0.0:0         LISTEN   9012  ← 反弹shell!
# TCP    10.0.0.5:49152 185.x.x.x:80     ESTAB    9012  ← C2连接!

# 提取进程内存
vol -f memory.raw windows.memmap --pid 9012 --dump

# 提取DLL列表
vol -f memory.raw windows.dlllist --pid 9012

# 注册表分析
vol -f memory.raw windows.registry.hivelist
vol -f memory.raw windows.registry.printkey \
  --key "Software\Microsoft\Windows\CurrentVersion\Run"

# 哈希提取
vol -f memory.raw windows.hashdump

# 3. 时间线分析
vol -f memory.raw windows.timeliner

🛠️ Autopsy GUI取证平台

# Autopsy - 开源数字取证平台
# 基于The Sleuth Kit的图形化界面

# 安装
apt install autopsy

# 或下载: https://www.sleuthkit.org/autopsy/

# 使用流程:
# 1. 创建新案件
#    Case Name: Incident-2024-0115
#    Case Number: IR-2024-0115
#    Examiner: Security Team

# 2. 添加数据源
#    → 选择磁盘镜像文件
#    → 选择镜像类型(DD/E01/VMware)

# 3. 配置Ingest模块
#    ✅ Recent Activity (最近活动)
#    ✅ Hash Lookup (哈希查询)
#    ✅ File Type Identification (文件类型)
#    ✅ Keyword Search (关键词搜索)
#    ✅ Email Parser (邮件解析)
#    ✅ Encryption Detection (加密检测)
#    ✅ Extension Mismatch (扩展名不匹配)
#    ✅ Exif Parser (EXIF元数据)

# 4. 分析发现
#    → 按文件类型浏览
#    → 搜索关键词
#    → 查看已删除文件
#    → 查看Web历史
#    → 时间线视图

# 命令行批量分析:
autopsy-cli -c case_name -d disk_image.dd --ingest all

📊 取证报告模板

# 数字取证报告

## 1. 案件信息
- 案件编号: IR-2024-0115
- 取证人员: Security Team
- 日期: 2024-01-15
- 描述: 服务器入侵事件取证

## 2. 证据清单
| 编号 | 证据 | 哈希(SHA256) | 采集时间 |
|------|------|-------------|----------|
| E-001 | disk_image.dd | abc123... | 2024-01-15 10:00 |
| E-002 | memory.raw | def456... | 2024-01-15 10:05 |
| E-003 | network.pcap | 789abc... | 2024-01-15 09:55 |

## 3. 关键发现
### 3.1 初始入侵
- 时间: 2024-01-15 03:42:11
- 方式: SSH暴力破解(root)
- 来源IP: 185.220.101.x
- 证据: auth.log + 进程树

### 3.2 横向移动
- 时间: 2024-01-15 03:50:00
- 方式: Pass-the-Hash
- 目标: 10.0.0.10 (文件服务器)
- 证据: 内存中的NTLM hash + 网络连接

### 3.3 数据窃取
- 时间: 2024-01-15 04:15:00
- 方式: HTTP POST到外部服务器
- 数据量: 约2.3GB
- 证据: network.pcap + Web日志

### 3.4 持久化
- 方式: 注册表Run键 + SSH密钥
- 位置: HKCU\...\Run\svchost_update
- 证据: 注册表分析 + authorized_keys

## 4. 攻击时间线
2024-01-15 03:42  SSH暴力破解成功
2024-01-15 03:45  下载攻击工具
2024-01-15 03:50  横向移动到文件服务器
2024-01-15 04:00  发现并打包敏感数据
2024-01-15 04:15  数据外传
2024-01-15 04:20  安装后门持久化
2024-01-15 04:25  清理日志

## 5. IOC指标
- IP: 185.220.101.x
- 域名: evil-c2.com
- 文件: hack.exe (SHA256: xxx)
- 注册表: HKCU\...\Run\svchost_update
- SSH密钥: AAAA...
磁盘镜像分析 — 掌握数字取证的核心技能!你能创建法医镜像、分析文件系统、恢复已删除文件、执行内存取证,并生成专业取证报告。
命令已验证:dd / ewfacquire / mmls / fls / icat / foremost / volatility3 — 所有命令在取证工作站环境测试通过
思考题:
  1. 为什么取证镜像必须计算哈希?如果哈希不匹配意味着什么?
  2. E01格式相比原始DD镜像有什么优势?
  3. 内存取证比磁盘取证能发现哪些额外的证据?
  4. 如何证明取证的证据链(Chain of Custody)完整性?

📚 延伸阅读

🔧 取证自动化与AI辅助

自动化取证流程

# 使用ALEAPP (Android)
git clone https://github.com/abrignoni/ALEAPP.git
cd ALEAPP
python aleapp.py -i /evidence/android.tar.gz -o /evidence/output/

# 使用iLEAPP (iOS)
git clone https://github.com/abrignoni/iLEAPP.git
cd iLEAPP
python ileapp.py -i /evidence/ios_backup/ -o /evidence/output/

# KAPE (Kroll Artifact Parser)
# Windows快速取证收集
./kape.exe --target SANS_Triage --dest /evidence/kape_output/

# ERIS (Evidence Repository & Investigation System)
# 企业级证据管理系统

# 取证数据关联分析
# 使用GRR (Google Rapid Response)
pip install grr-api-client
# 远程取证收集
grr_console
> flow.SearchFileByName("/etc/shadow", client="C.123456789")

AI辅助取证分析

# 使用AI加速日志和证据分析

# 1. 大语言模型辅助时间线分析
# 将日志片段输入LLM,请求:
# "分析以下auth.log,识别攻击者的行为模式和时间线"

# 2. 自动化报告生成
# 使用模板 + 证据数据自动填充
python3 << 'EOF'
from jinja2 import Template

template = Template('''
# 取证分析报告
## 事件概述
- 时间范围: {{ start_time }} 至 {{ end_time }}
- 攻击源IP: {{ source_ip }}
- 受影响系统: {{ affected_systems }}

## 关键事件
{% for event in timeline %}
- [{{ event.time }}] {{ event.description }}
{% endfor %}

## IOC指标
{% for ioc in iocs %}
- {{ ioc.type }}: {{ ioc.value }}
{% endfor %}
''')

report = template.render(
    start_time="2024-01-15 03:42",
    end_time="2024-01-15 05:30",
    source_ip="185.220.101.x",
    affected_systems=["Web Server", "Domain Controller"],
    timeline=[...],
    iocs=[...]
)
print(report)
EOF

# 3. Volatility插件开发
# 自定义Volatility3插件分析特定恶意软件
import volatility.plugins
from volatility3.framework import interfaces

class CustomPlugin(interfaces.plugins.PluginInterface):
    """检测特定恶意软件的内存特征"""
    _required_framework = (2, 0, 0)
    
    @classmethod
    def get_requirements(cls):
        return []
    
    def run(self):
        # 自定义分析逻辑
        pass
参考资料:NIST SP 800-86 Guide to Integrating Forensic | The Sleuth Kit Documentation | Volatility3 Docs | ACPO Good Practice Guide | RFC 3227 Evidence Collection