
ملاحظات وسكربتات تطوير استغلال نواة لينكس لتحديات CTF، تغطي استخراج 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 ..
قم بتجميع الاستغلال، وأضفه إلى 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 بحيث يكون حجم كل تخصيص حتى 0x1000، وتكون منتهية بـ NULL ويجب أن تكون كذلك.
للقراءة الاعتباطية:
استبدل next و m_ts بحيث يقرأ من مؤشر next المستبدل.
للكتابة الاعتباطية:
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);