Stack Shellcode
Last updated
Last updated
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Stack shellcode ni mbinu inayotumika katika binary exploitation ambapo mshambuliaji anaandika shellcode kwenye stack ya programu iliyo hatarini na kisha kubadilisha Instruction Pointer (IP) au Extended Instruction Pointer (EIP) ili kuelekeza kwenye eneo la shellcode hii, na kusababisha itekelezwe. Hii ni njia ya jadi inayotumika kupata ufikiaji usioidhinishwa au kutekeleza amri zisizo na mipaka kwenye mfumo wa lengo. Hapa kuna muhtasari wa mchakato, ikiwa ni pamoja na mfano rahisi wa C na jinsi unavyoweza kuandika exploit inayolingana kwa kutumia Python na pwntools.
Tuanze na mfano rahisi wa programu ya C iliyo hatarini:
Huu programu ina udhaifu wa overflow ya buffer kutokana na matumizi ya kazi ya gets()
.
Ili kuunda programu hii huku ukizima ulinzi mbalimbali (ili kuiga mazingira yenye udhaifu), unaweza kutumia amri ifuatayo:
-fno-stack-protector
: Inazima ulinzi wa stack.
-z execstack
: Inafanya stack kuwa executable, ambayo ni muhimu kwa kutekeleza shellcode iliyohifadhiwa kwenye stack.
-no-pie
: Inazima Position Independent Executable, ikifanya iwe rahisi kutabiri anwani ya kumbukumbu ambapo shellcode yetu itakuwa.
-m32
: Inakusanya programu kama executable ya 32-bit, mara nyingi hutumiwa kwa urahisi katika maendeleo ya exploit.
Hapa kuna jinsi unavyoweza kuandika exploit katika Python ukitumia pwntools kufanya shambulio la ret2shellcode:
This script constructs a payload consisting of a NOP slide, the shellcode, and then overwrites the EIP with the address pointing to the NOP slide, ensuring the shellcode gets executed.
The NOP slide (asm('nop')
) is used to increase the chance that execution will "slide" into our shellcode regardless of the exact address. Adjust the p32()
argument to the starting address of your buffer plus an offset to land in the NOP slide.
ASLR inapaswa kuzuiliwa ili anwani iwe ya kuaminika katika utekelezaji tofauti au anwani ambapo kazi itahifadhiwa haitakuwa sawa kila wakati na unahitaji kuvuja ili kubaini wapi kazi ya ushindi imepakiwa.
Stack Canaries inapaswa pia kuzuiliwa au anwani ya kurudi ya EIP iliyovunjika haitafuatiwa kamwe.
NX kinga ya stack itazuia utekelezaji wa shellcode ndani ya stack kwa sababu eneo hilo halitakuwa la kutekelezeka.
64bit, ASLR with stack address leak, write shellcode and jump to it
32 bit, ASLR with stack leak, write shellcode and jump to it
32 bit, ASLR with stack leak, comparison to prevent call to exit(), overwrite variable with a value and write shellcode and jump to it
arm64, no ASLR, ROP gadget to make stack executable and jump to shellcode in stack
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)