
CTF चुनौतियों के लिए Linux कर्नेल एक्सप्लॉइट विकास नोट्स और स्क्रिप्ट्स, जिनमें initramfs निष्कर्षण, msg_msg/ldt_struct दुरुपयोग, तथा विशेषाधिकार वृद्धि के लिए व्यावहारिक शोषण तकनीकें शामिल हैं।
CPIO संग्रह से फ़ाइल निकालें
#!/bin/sh
mkdir fs
cd fs
cp ../initramfs.cpio.gz ./initramfs.cpio.gz
gunzip ./initramfs.cpio.gz
cpio -idm < ./initramfs.cpio
rm initramfs.cpio
cd ..
एक्सप्लॉइट को संकलित (compile) करें, उसे fs में जोड़ें, और चलाएँ।
#!/bin/sh
gcc -w -o exploit -static exploit.c -pthread -lrt &&\
# musl-gcc -w -s -static -o3 exploit.c -o exploit -masm=intel &&\
mv exploit ./fs/ &&\
cd fs &&\
find . -print0 | cpio --owner root --null -ov --format=newc | gzip -9 > ../initramfs.cpio.gz &&\
cd .. &&\
# gunzip -f initramfs.cpio.gz &&\
./run.sh
ext4 फाइलसिस्टम संग्रह के मामले में
mount ./initramfs.cpio.gz ./fs/
0x20 आकार की संरचना, जिसमें copy_to_user कॉल के लिए कोई जाँच नहीं होती
struct msg_msg {
struct list_head m_list;
long m_type;
size_t m_ts; /* message text size */
struct msg_msgseg *next;
void *security;
/* the actual message follows immediately */
};
उपयोगकर्ता संदेश msg_msg संरचना के ठीक बाद 0x1000 - 0x30 तक संग्रहीत होता है; उसके बाद struct msg_msgseg *next में संग्रहीत चंक्स (chunks) की एकल-लिंक्ड सूची होती है, जिसका प्रत्येक आवंटन आकार 0x1000 तक होता है, और यह NULL से समाप्त होना चाहिए।
मनमाना पठन (arbitrary read) के लिए:
next और m_ts को इस प्रकार अधिलेखित (overwrite) करें कि उसे अधिलेखित next पॉइंटर से पढ़ना पड़े।
मनमाना लेखन (arbitrary write) के लिए:
msgsnd() // Userland
do_msgsnd() // Kernel land
load_msg()
alloc_msg() // Allocate all the necessary chunks
copy_from_user() // Race here to replace `struct msg_msgseg *next` before its used to copy userdata. Maybe use userfaultfd ;)
msg_msg do_msgsnd load_msg copy_msg
cpu_set_t cpu_set;
CPU_ZERO(&cpu_set);
CPU_SET(0,&cpu_set);
ret=sched_setaffinity(0,sizeof(cpu_set),&cpu_set);