π§© File Carving & Data Recovery β Advanced Techniques
File carving adalah teknik merekonstruksi file dari raw data tanpa metadata filesystem β berguna ketika filesystem corrupt, file sudah dihapus, atau Anda hanya punya blok mentah (dd image) tanpa tabel partisi. Ini adalah teknik paling vital dalam digital forensic karena file yang dihapus sering menyimpan bukti paling penting β malware yang sudah dibersihkan, dokumen yang dihapus attacker, atau flag yang ditinggalkan di unallocated space. Dari hierarchy-digital-evidence-acquisition, ini ada di Level 4 β active persistent storage dan Level 3 β transient files.
Prinsip Emas Carving
βDeleted β Gone.β File yang dihapus tidak hilang β hanya inode/entrinya yang ditandai sebagai tersedia. Data fisik tetap ada di media sampai ditimpa file baru. Dengan tool carving yang tepat, file yang βdihapusβ 5 tahun lalu pun bisa direcover β selama belum ditimpa.
Daftar Isi
- Teori Dasar β Bagaimana File Bisa Di-recover
- File Signature β Magic Bytes
- Carving Tools β Benchmark & Use Case
- Advanced Carving β Fragmented File Recovery
- SSD & TRIM β Musuh Besar Carving
- Recovery by File Type
- Anti-Forensics & Counter-Measures
- Carving in CTF Context
- Cheat Sheet β Command Matrix
Teori Dasar β Bagaimana File Bisa Di-recover
Struktur File di Disk
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β $MFT Entry (alokasi file) β
β βββββββββββ ββββββββββββ ββββββββββββ βββββββββββββββ β
β β Header β β Filename β β Metadata β β Data Runs β β
β β (0x46) β β Namaku.txtβ β size, ts β β cluster 100ββ β
β βββββββββββ ββββββββββββ ββββββββββββ ββββββββ¬βββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββ
β
ββββββββββββββββββββββββββββββββ
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Cluster 100-105 (Data Run) β
β ββββββββ¬βββββββ¬βββββββ¬βββββββ¬βββββββ¬βββββββ β
β βHDR βDATA βDATA βDATA βDATA βDATA β β
β β0xFFDBβNamakuβ... β... β... β... β β
β ββββββββ΄βββββββ΄βββββββ΄βββββββ΄βββββββ΄βββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Saat file dihapus:
- $MFT entry ditandai
not in use(byte pertama = 0x00) - Data cluster tidak dihapus β hanya ditandai sebagai tersedia (available)
- File Name di Recycle Bin dipindahkan, tapi data asli tetap di cluster asli
- SSD TRIM β cluster benar-benar dikosongkan (diisi 0x00)
Konsep Carving
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Unallocated Space (raw bytes) β
β ββββββ¬βββββ¬βββββ¬βββββ¬βββββ¬βββββ¬βββββ¬βββββ¬βββββ¬ββββββ
β β0xFFβ0xD8β0xFFβ0xE0β0x00β0x10β0x4Aβ0x46β... β0xFFββ
β β β ^^^^^^^ JPEG Header ^^^^^^^^^^ β β0xD9ββ
β β β ββββββββββ JPEG Data βββββββββββββ β ββ
β ββββββ΄βββββ΄βββββ΄βββββ΄βββββ΄βββββ΄βββββ΄βββββ΄βββββ΄ββββββ
β β
β Carving: cari magic bytes β baca sampai footer β
β β extract cluster β file utuh β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Carving tidak menggunakan metadata filesystem. Ia hanya mencari pola bytes (magic number) di raw data, lalu mengikuti struktur file untuk membaca seluruh konten hingga footer.
Carving vs File System Recovery
| Aspek | File System Recovery | File Carving |
|---|---|---|
| Metode | Rekonstruksi $MFT / inode table | Cari magic bytes |
| Butuh metadata | Ya β butuh entry utuh | Tidak |
| File terfragmentasi | Bisa recover (ada di metadata data runs) | Sulit β hanya contiguous |
| SSD TRIM | Tidak bisa | Tidak bisa |
| File overwrite | Tidak bisa | Sebagian β file rusak |
| Tools | TestDisk, R-Studio, GetDataBack | foremost, scalpel, photorec |
File Signature β Magic Bytes
Universal Signatures
Setiap format file punya magic bytes β byte pertama yang unik. Carving tool menggunakan database signature ini.
# Signatures built-in di foremost:
/etc/foremost.conf
# Signatures built-in di scalpel:
/etc/scalpel/scalpel.conf
# Cari sendiri β cek header file
xxd file.jpg | head -1
# Output: 00000000: ffd8 ffe0 0010 4a46 4946 0001 ... β JPEGMagic Bytes Reference (Paling Sering di CTF)
| Format | Magic Bytes (Hex) | Offset | Footer (Hex) |
|---|---|---|---|
| JPEG | FF D8 FF E0 atau FF D8 FF E1 | 0 | FF D9 |
| PNG | 89 50 4E 47 0D 0A 1A 0A | 0 | 49 45 4E 44 AE 42 60 82 |
| GIF | 47 49 46 38 37 61 atau 38 39 61 | 0 | 00 3B |
| BMP | 42 4D | 0 | β |
| TIFF | 49 49 2A 00 atau 4D 4D 00 2A | 0 | β |
25 50 44 46 (%PDF) | 0 | 25 25 45 4F 46 (%%EOF) | |
| ZIP | 50 4B 03 04 | 0 | 50 4B 05 06 (EOCD) |
| RAR | 52 61 72 21 1A 07 00 | 0 | β |
| 7z | 37 7A BC AF 27 1C | 0 | β |
| GZIP | 1F 8B 08 | 0 | β |
| BZ2 | 42 5A 68 | 0 | β |
| ELF | 7F 45 4C 46 (.ELF) | 0 | β |
| RIFF (AVI/WAV) | 52 49 46 46 xx xx xx xx 41 56 49 | 0 | β |
| MP3 (ID3v2) | 49 44 33 (ID3) | 0 | β |
| MP4 | 00 00 00 18 66 74 79 70 | 0 | β |
| DOCX/XLSX (ZIP) | 50 4B 03 04 14 00 06 00 | 0 | 50 4B 05 06 |
| Mach-O | FE ED FA CE atau FE ED FA CF | 0 | β |
Custom Signature β Cari Flag Manual
# Cari string CTF/flag di raw image
strings disk.dd | grep -i "CTF\|flag\|KEY\|secret"
# Cari di unallocated space khusus
# Gunakan blkls dari Sleuth Kit
blkls -s disk.dd > unallocated.raw
strings unallocated.raw | grep -i "CTF\|flag"
# Cari magic bytes manual dengan xxd
xxd disk.dd | grep "ffd8 ffe0" # Cari JPEG header di rawCarving Tools β Benchmark & Use Case
Foremost (Tier 1 β Default)
Kelebihan: Cepat, default signatures built-in, mudah. Kekurangan: Tidak bisa fragmented file recovery, konfigurasi terbatas.
# Basic carving β cari semua tipe file
foremost -i disk.dd -o output/
# Carving dengan tipe spesifik β cuma JPEG + PDF
foremost -t jpeg,pdf -i disk.dd -o output/
# Verbose + quiet mode
foremost -v -q -i disk.dd -o output/
# Config file
foremost -c /etc/foremost.conf -i disk.dd -o output/Output structure:
output/
βββ audit.txt # Log carving: file ditemukan, size, offset
βββ jpg/ # Recovered JPEG files
β βββ 00000001.jpg
β βββ 00000002.jpg
β βββ ...
βββ pdf/
βββ zip/
βββ ...
Scalpel (Tier 2 β More Configurable)
Kelebihan: Konfigurasi signature sangat detail, bisa custom header/footer. Kekurangan: Config file perlu diedit dulu β tidak langsung jalan.
# Edit /etc/scalpel/scalpel.conf β uncomment tipe file yang dicari
# Contoh:
# jpeg y 200000000 \xff\xd8\xff\xe0\x00\x10\x4a\x46\x49\x46\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xfe\x00\x3c\x43\x52\x45\x41\x54\x4f\x52\x3a\x20\x47\x64\x6a\x70\x65\x67\x20\x76\x65\x72\x20\x31\x2e\x30\x0a\xff\xdb\x00\x43\x00\x03\x02\x02\x03\x02\x02\x03\x03\x03\x03\x04\x03\x03\x04\x05\x08\x05\x05\x04\x04\x05\x0a\x07\x07\x06\x08\x0c\x0a\x0c\x0c\x0b\x0a\x0b\x0b\r\x0e\x12\x10\x0d\x0e\x11\x0e\x0b\x0b\x10\x16\x10\x11\x13\x14\x15\x15\x15\x0c\x0f\x17\x18\x16\x14\x18\x12\x14\x15\x14
# Jalankan carving
scalpel -i disk.dd -o output/PhotoRec (Tier 1 β Most File Types)
Kelebihan: Mendukung 480+ file signature, bisa photRec dari partisi yang tidak dikenal. Kekurangan: Interactive (bukan CLI murni), lambat untuk file besar.
# PhotoRec adalah interactive β harus pilih disk, partisi, output
sudo photorec /path/to/disk.dd
# Pilih file type β [File Opt] β Select all
# Pilih destination β [Search]Bulk Extractor (Tier 2 β Feature Extraction)
Kelebihan: Mengekstrak feature bukan file β URL, email, credit card, hash, DNA sequences. Sampai ribuan feature per detik. Kekurangan: Tidak menghasilkan file utuh β hanya ekstrak feature.
bulk_extractor -o output/ disk.dd
# Output: url.txt, email.txt, telephone.txt, etc.
# Di CTF: cari flag.txt, base64.txt
# Cari flag di hasil bulk_extractor
grep -i "CTF\|flag" output/*.txtddrescue (Tier 2 β Damaged Media)
Kelebihan: Bisa recover data dari disk yang mulai rusak (bad sector). Kekurangan: Butuh waktu lama β mencoba baca ulang sector berkali-kali.
# Clone disk yang rusak β log bad sector ke mapfile
ddrescue -d -r3 /dev/sdb disk_image.dd rescue.log
# Retry bad sector dengan backtrack
ddrescue -d -r3 -R /dev/sdb disk_image.dd rescue.logAdvanced Carving β Fragmented File Recovery
4.1 The Fragmentation Problem
Contiguous file (mudah):
ββββββ¬βββββ¬βββββ¬βββββ¬βββββ¬βββββ¬βββββ¬βββββ¬βββββ¬βββββ
βHDR βD1 βD2 βD3 βD4 βD5 βD6 βD7 βD8 βD9 β β utuh
ββββββ΄βββββ΄βββββ΄βββββ΄βββββ΄βββββ΄βββββ΄βββββ΄βββββ΄βββββ
Fragmented file (sulit):
ββββββ¬βββββ¬βββββ¬βββββ¬βββββ¬βββββ¬βββββ¬βββββ¬βββββ¬βββββ
βHDR βD1 β OTHER β FILE βD2 βD3 βD4 βD5 β
ββββββ΄βββββ΄βββββ΄βββββ΄βββββ΄βββββ΄βββββ΄βββββ΄βββββ΄βββββ
^^^^^^^^^^^ data file lain menyela
Fragmented file adalah tantangan terbesar carving. Sebagian besar tool hanya bisa recover file yang contiguous (blok datanya berurutan). File terfragmentasi butuh teknik khusus:
4.2 Bifragment Gap Carving (BGC)
Untuk file yang terpecah menjadi 2 bagian dengan celah di tengah:
# Tool: smartmontools + custom script
# Atau gunakan scalpel dengan konfigurasi multi-segment
# Prinsip: cari header β baca N bytes β cek apakah footer tercapai
# Jika tidak β ada gap β cari sisa setelah gapBGC hanya untuk 2 fragment β untuk 3+ fragment, carving sangat sulit bahkan dengan tool mahal.
4.3 Object-Based Carving
Bukan cari header/footer file, tapi cari struktur internal file. Contoh untuk ZIP:
# ZIP central directory (EOCD) β bisa recover file ZIP walaupun terfragmentasi
# Tools: zip, unzip, custom Python parser
# ZIP file walau fragmented bisa di-recover kalau EOCD-nya utuh
# EOCD ada di END file β mencari dari offset terakhir disk
# Foremost bisa recover ZIP fragmented dengan config khusus
# Edit /etc/foremost.conf β maksimum carving size dinaikkan4.4 Machine Learning Carving (Advanced)
Beberapa tool baru pakai ML untuk mendeteksi perbatasan file β tapi jarang di CTF karena resource heavy. Relevan untuk penelitian forensik.
SSD & TRIM β Musuh Besar Carving
Bagaimana TRIM Bekerja
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β TRIM Command Sequence: β
β β
β 1. File dihapus β
β 2. OS kirim TRIM ke SSD controller β
β ("cluster 100-200 sudah tidak dipakai") β
β 3. SSD controller kosongkan cluster tersebut β
β (diisi 0x00 secara internal) β
β 4. Data ASLI HILANG PERMANEN β tidak bisa direcover β
β (berbeda dari HDD yang hanya tandai sebagai kosong) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
HDD vs SSD:
| Aspek | HDD | SSD |
|---|---|---|
| File dihapus β bisa carving? | β Ya β data tetap ada | β Tidak β TRIM hapus instan |
| Format cepat β bisa carving? | β Ya | β TRIM triggered |
| Overwrite β data hilang? | β Bisa recover 1-2 overwrite | β Sekali overwrite = hilang |
| File slack | β Ada β data file sebelumnya | β Tidak ada (SSD nulis per page) |
Mitigasi TRIM β Forensic Write Blocker
# Write blocker HARDWARE (Tableau, WiebeTech)
# Mencegah SSD menerima TRIM command dari OS
# Atau software write blocker:
# Linux: mount -o ro,noatime,nodiratime
# Gunakan dd dengan oflag=direct,noatime
dd if=/dev/sdb of=/evidence/ssd.dd bs=4M conv=noerror,sparse iflag=fullblock,noatimeDi CTF: Jika soal memberikan file .dd atau .e01, biasanya TRIM sudah tidak relevan karena image sudah diambil sebelum analisis. Tapi awareness penting β jangan kaget kalau soal dengan SSD image tidak punya file carved dari unallocated space.
Recovery by File Type
1. Gambar (JPEG / PNG / GIF)
# Foremost β signature already included
foremost -t jpeg -i disk.dd -o carved_jpg/
# Cek metadata gambar yang di-recover
exiftool carved_jpg/00000001.jpg
# Cek steganografi β gambar bisa nampung hidden data!
zsteg carved_jpg/00000001.png
steghide extract -sf carved_jpg/00000001.jpg
# Cari flag dalam gambar β strings juga
strings carved_jpg/*.jpg | grep -i "CTF\|flag"CTF Pattern: Gambar sering dijadikan container steganografi β cek LSB, metadata, dan embedded ZIP.
2. Dokumen (PDF)
# Foremost β PDF signature default
foremost -t pdf -i disk.dd -o carved_pdf/
# Ekstrak teks
pdftotext carved_pdf/00000001.pdf output.txt
# Cari metadata
pdfinfo carved_pdf/00000001.pdf
# Cari embedded file dalam PDF
pdfdetach -list carved_pdf/00000001.pdf
pdfdetach -save 1 carved_pdf/00000001.pdf3. Archive (ZIP / RAR)
# ZIP carving β butuh EOCD (End of Central Directory) intact
foremost -t zip -i disk.dd -o carved_zip/
# Coba buka semua file yang di-recover
for f in carved_zip/*.zip; do
unzip -l "$f" 2>/dev/null && echo "=== $f OK ===" || echo "=== $f CORRUPT ==="
done4. Office Documents (DOCX / XLSX β Actually ZIP)
DOCX/XLSX adalah ZIP yang berisi XML. Carving = ZIP carving.
# Setelah di-recover sebagai ZIP:
unzip recovered.docx -d docx_extracted/
cat docx_extracted/word/document.xml | grep -i "CTF\|flag"
# Atau parse content langsung
strings recovered.docx | grep -i "CTF\|flag"5. Executable (EXE / ELF)
# PE (Windows) β magic bytes: MZ (4D 5A)
foremost -t exe -i disk.dd -o carved_exe/
# ELF (Linux) β magic bytes: 7F 45 4C 46
# Tidak ada pada foremost default β tambahkan ke config:
echo -e "elf\ty\t200000000\t\\x7f\x45\x4c\x46" >> /etc/foremost.conf6. Database (SQLite)
# SQLite magic bytes: 53 51 4C 69 74 65 20 66 6F 72 6D 61 74 20 33 00
# Tidak di default foremost β tambahkan di config:
echo -e "sqlite\ty\t200000000\t\\x53\x51\x4c\x69\x74\x65\x20\x66\x6f\x72\x6d\x61\x74\x20\x33\x00" >> /etc/foremost.conf
# Setelah di-recover:
sqlite3 recovered.db ".tables"
sqlite3 recovered.db "SELECT * FROM flag;"Anti-Forensics & Counter-Measures
Anti-Forensics Techniques
| Teknik | Efek | Cara Kerja |
|---|---|---|
| Secure Deletion (shred, srm) | Overwrite file dengan random data | Baca file β tulis 0xFF β hapus β data asli hilang |
| DoD 5220.22-M | 7-pass overwrite | 3x random + 1x complement + 3x random β standar militer |
| Disk Wiping (DBAN, nwipe) | Hapus seluruh disk | Write 0x00/0xFF/random ke semua sector |
| TRIM & NVMe Format | Instant wipe SSD | NVMe format command hapus seluruh NAND dalam detik |
| File Shredding | Fragmentasi + rename | Potong file kecil-kecil, rename random, sebar ke berbagai folder |
| Metadata Wiping | Hapus exif, timestamp, $MFT entry | exiftool -all= atau SetMACE |
| Steganography | Sembunyiin file di media lain | Data dikodekan di LSB gambar/audio β tidak ketahuan carving |
| Encryption | File terlihat random | Data encrypted tidak punya magic bytes β carving tidak mendeteksi |
Counter Anti-Forensics
# 1. Cari file shredding β cari file tiny (1-5 KB) dalam jumlah besar
find recovered/ -type f -size -5k | wc -l
# 2. Cari hidden stream (NTFS ADS)
# NTFS alternate data stream
find . -type f -exec stream_query {} \;
# 3. Cari file dengan nama random
ls output/ | grep -E "^[a-z0-9]{20,}\."
# 4. Cek slack space β data sisa dari file sebelumnya
# Menggunakan blkls
blkls -s disk.dd > slack.raw
strings slack.raw | grep -i "CTF\|flag"
# 5. Cari file terenkripsi β detect entropy tinggi
# Gunakan binwalk entropy analysis
binwalk -E disk.dd
# Output: entropy plot β puncak tinggi = compressed/encrypted dataCarving in CTF Context
Flowchart Soal Carving
Dapet file image (.dd/.e01/.img)
β
βββ Cek tipe image: file image.dd
βββ Cek strings dulu: strings image.dd | grep -i "CTF\|flag"
βββ Mount (jika bisa): mount -o loop,ro image.dd /mnt
β βββ Cari flag langsung di filesystem
β
βββ Mount gagal atau tidak ada file? β Carving
β
βββ blkls -s image.dd > unallocated.raw (unallocated space)
βββ foremost -i unallocated.raw -o carved/
βββ Cek hasil: ls carved/*/
β βββ Ada file? β cari flag di file yang di-recover
β βββ Tidak ada? β cek dengan scalpel / photorec
β
βββ Cari hidden data
βββ bulk_extractor
βββ binwalk -Me
βββ strings + grep
Cold Start β Jika Baru Dapat Soal Carving
1. strings image.dd | grep -i "CTF\|flag"
β
Jika dapat β submit. Selesai.
2. file image.dd + strings + exiftool
β Cek tipe, metadata, deskripsi
3. foremost -T -i image.dd -o output/
β Cek output: file apa yang muncul? Gambar? ZIP? PDF?
4. strings output/*/* | grep -i "CTF\|flag"
β Cari flag di semua file yang di-recover
5. binwalk -Me image.dd
β Cari embedded file yang tidak ketahuan foremost
6. bulk_extractor -o bulk/ image.dd
β Cari pattern: base64, hex, URL, email
Jika 1-6 tidak dapat flag β cari hidden sector, slack space, atau NTFS ADS.
Cheat Sheet β Command Matrix
# === CARVING QUICK START ===
foremost -T -i image.dd -o output/ # Carving semua tipe
foremost -t jpeg,pdf,zip -i image.dd -o out/ # Tipe spesifik
scalpel -i image.dd -o output/ # Scalpel (edit config dulu)
photorec image.dd # Interactive
# === UNALLOCATED SPACE ===
blkls -s image.dd > unallocated.raw # Extract unallocated
blkls -f image.dd > slack.raw # Extract slack space
# === FILE ANALYSIS ===
file recovered.jpg # Cek tipe file
strings recovered.jpg | grep -i "flag" # Cari string dalam file
exiftool recovered.jpg # Metadata
binwalk -Me recovered.jpg # Embedded file
# === HIDDEN DATA ===
bulk_extractor -o bulk/ image.dd # Feature extraction
binwalk -E image.dd # Entropy analysis
# === CUSTOM SIGNATURE ===
# Add custom magic bytes to foremost config:
echo -e "sqlite\ty\t200000000\t\\x53\\x51\\x4c\\x69\\x74\\x65" >> /etc/foremost.conf
# === DISK CLONE ===
dd if=/dev/sdb of=image.dd bs=4M status=progress
dcfldd if=/dev/sdb of=image.dd bs=4M hash=sha256
ddrescue -d -r3 /dev/sdb image.dd rescue.log # Damaged media
# === FILE FOUND β VERIFY ===
sha256sum recovered/*
file recovered/*
strings recovered/* | grep -i "CTF\|flag"Cross-Link
- Atlas Digital Evidence β hierarchy-digital-evidence-acquisition
- Windows Forensics (artifact extraction) β windows-forensics-artifact-analysis
- Network Forensics (carving dari PCAP) β network-forensics-pcap-analysis
- Memory Forensics (Volatility) β memory-forensics-volatility-deepdive
- CTF Methodology (Forensic Flow) β ctf-competition-methodology-strategy
- Tool Arsenal β ctf-tool-arsenal-universal
- Data Recovery (existing) β data-recovery
- Master Index β master-index
File Carving & Data Recovery Β· Deleted β Gone Β· Magic Bytes = Kunci Β· TRIM = Musuh SSD Β· Foremost + Scalpel + PhotoRec = Holy Trinity Β· Strings Dulu, Carving Kemudian Β· Fragmented File = Tantangan Besar