Ghidra Playbook
Ghidra เป็น reverse engineering suite ฟรีจาก NSA ที่ใช้แก้โจทย์ reversing ได้ครบ คู่มือนี้เป็น end-to-end: ตั้ง project, การ navigate, คีย์ลัดทั้งหมด, การ rename/retype ให้ decompiler อ่านง่าย, เทคนิคแก้โจทย์ CTF (หา flag check, patch), และ scripting
1. เริ่มต้น + import
- 1สร้าง Non-Shared Project → ตั้งชื่อ
- 2File → Import File → เลือก binary (Ghidra ระบุ format/arch อัตโนมัติ)
- 3ดับเบิลคลิกไฟล์เปิด CodeBrowser → ตอบ Yes ให้ auto-analyze
- 4รอ analysis เสร็จ (มุมขวาล่าง) — Ghidra หาฟังก์ชัน/string/xref ให้
- 5เริ่มที่ Symbol Tree → Functions → main (หรือ entry ถ้าไม่มี main)
2. หน้าต่างหลัก + navigate
- Listing (กลาง): disassembly (assembly)
- Decompiler (ขวา): pseudo-C — อ่านง่ายสุด (เปิดด้วย Window → Decompiler ถ้าไม่เห็น)
- Symbol Tree (ซ้าย): functions, labels, imports, exports
- Data Type Manager (ซ้ายล่าง): struct/type
- Defined Strings (Window → Defined Strings): string ทั้งหมด
- double-click ชื่อฟังก์ชัน/ตัวแปร = กระโดดไป definition; Alt+Left = ย้อนกลับ
3. คีย์ลัดสำคัญ
| คีย์ | ทำอะไร |
|---|---|
| L | rename ตัวแปร/ฟังก์ชัน |
| Ctrl+L | retype (เปลี่ยน data type) |
| ; | ใส่ comment |
| X | ดู cross-references (ใครเรียก/ใช้) |
| G | goto address/label |
| Alt+← / Alt+→ | ย้อนกลับ/ไปข้างหน้า (navigation history) |
| Ctrl+Shift+E | หา string / search memory |
| F | สร้างฟังก์ชัน (ถ้า Ghidra ไม่ detect) |
| double-click | ตามไป definition / xref |
4. ทำ decompiler ให้อ่านง่าย
decompiler output ดิบๆ อ่านยาก (ชื่อ local_28, uVar1) การ rename + retype ทีละนิดทำให้โค้ดอ่านเข้าใจขึ้นมาก — เป็นหัวใจของการ reverse ฟังก์ชันซับซ้อน
- rename ตัวแปร (L) ตามที่เข้าใจ เช่น local_28 → user_input
- retype (Ctrl+L) ตัวแปรให้ตรง (เช่น int → char*, หรือ apply struct)
- rename ฟังก์ชัน (L) ที่ Ghidra ตั้ง FUN_xxxxxx ตามหน้าที่
- ใส่ comment (;) จุดที่เข้าใจแล้ว กันลืม
- สร้าง struct ใน Data Type Manager ถ้าโปรแกรมใช้ struct
- ทำซ้ำเรื่อยๆ — โค้ดจะค่อยๆ อ่านรู้เรื่องขึ้น
5. เทคนิคแก้โจทย์ CTF
- หา flag check: Defined Strings หา 'Correct'/'Wrong' → X ดู xref → ไปฟังก์ชันที่เทียบ
- reverse algorithm: อ่าน decompiler ของ check function → เข้าใจการ transform → เขียน solver ย้อนกลับ (Python)
- strcmp/memcmp: หา call เหล่านี้ → argument ที่สองมัก hardcode flag/password
- patch: Listing → คลิกขวา instruction → Patch Instruction (เช่นเปลี่ยน JNZ→JZ ข้าม check) → Export Program
- xor/encode loop: เจอ loop ที่ xor → ดู key → decode เอง
- ใช้คู่ dynamic: อ่าน Ghidra เข้าใจโครงสร้าง → gdb ยืนยันค่า runtime (ดูหัวข้อ Dynamic Analysis)
6. Scripting (Ghidra Script)
Ghidra มี Script Manager (Window → Script Manager) รัน script Python/Java เพื่อ automate — เช่น deobfuscate, patch หลายจุด, ดึงข้อมูล ใช้ currentProgram, getFunctionAt(), ฯลฯ ผ่าน Flat API; เปิด Python console ได้ที่ Window → Python
7. Quick Reference
- import → auto-analyze → Symbol Tree → main
- คีย์: L rename, Ctrl+L retype, X xref, G goto, ; comment
- Ctrl+Shift+E = หา string (เริ่มทุกโจทย์ที่นี่)
- ทำอ่านง่าย: rename+retype ตัวแปร/ฟังก์ชันทีละนิด
- flag check: string 'Correct' → X xref → reverse algorithm
- patch: คลิกขวา instruction → Patch Instruction → Export
- scripting: Script Manager (Python/Java) automate
🧭 จับมือทำทีละขั้น (มีแค่ Kali) + ถ้าติดไปไหนต่อ
สมมติได้ binary มาจากโจทย์ reverse engineering มีแค่ Kali ทำตามนี้ทีละขั้นเพื่อเปิดดูและหา flag
- 1เช็คว่ามี Ghidra ไหม: เปิดเมนู Kali หรือรัน `ghidraRun` (ถ้าไม่มี `sudo apt install ghidra -y`)
- 2ก่อนเปิด Ghidra ลองดูข้อมูลเบื้องต้นก่อนเสมอ: `file ./binary` และ `strings ./binary | grep -i flag`
- 3เปิด Ghidra → File → New Project (Non-Shared) → ตั้งชื่อ
- 4File → Import File → เลือก binary → กด OK (Ghidra เดา format/arch ให้อัตโนมัติ)
- 5ดับเบิลคลิกไฟล์ที่ import → ขึ้นถาม 'Analyze?' → กด Yes ให้ auto-analyze แล้วรอจนแถบสถานะมุมขวาล่างหยุดวิ่ง
- 6เปิด Window → Defined Strings (หรือ Ctrl+Shift+E) หาคำว่า flag/Correct/Wrong/password
- 7เจอ string ที่น่าสงสัย → double-click แล้วกด X (cross-reference) ดูว่าใครเรียกใช้ string นี้
- 8กระโดดไปฟังก์ชันที่เรียก → ดู Decompiler (หน้าต่างขวา) อ่าน pseudo-C
- 9อ่านไม่รู้เรื่อง → rename ตัวแปร (กด L) และ retype (Ctrl+L) ทีละตัวจนโค้ดเริ่มมีความหมาย
- 10เข้าใจ algorithm แล้ว → เขียน Python solver ย้อนกลับ หรือถ้าแค่ต้องข้าม check → คลิกขวา instruction → Patch Instruction
- 11ไม่แน่ใจค่า runtime จริง → เปิด gdb ควบคู่ยืนยันค่า (dynamic analysis) แล้วกลับมาเทียบกับที่อ่านใน Ghidra
| ขั้นตอน/งาน | เครื่องมือใน Kali | ติดตั้งเพิ่ม (ถ้าไม่มี) | เครื่องมือออนไลน์ |
|---|---|---|---|
| ดูข้อมูลไฟล์เบื้องต้น | file, strings | - | - |
| decompile/reverse หลัก | ghidra | - | dogbolt.org (เทียบหลาย decompiler) |
| ยืนยัน runtime | gdb, gdb-peda/pwndbg | - | - |
| decompiler ทางเลือก | - | IDA Free, Binary Ninja (personal) | dogbolt.org |
| เขียน solver ย้อนกลับ | python3 | - | cyberchef (ลอง transform เร็วๆ) |
| ดู symbol/import เร็วๆ | objdump, readelf, nm | - | - |
| scripting automate | Ghidra Script Manager (Python/Java) | - | - |
หัวข้อที่เชื่อมโยง
โน้ตของฉัน
ยังไม่มีโน้ตสำหรับหัวข้อนี้