heap题出题模板

食用方法:编译后用示例脚本开始调试,具体漏洞要在源码中启用

gcc -o init init.c
-Wl,–dynamic-linker=./ld-2.23.so
-Wl,-rpath=’$ORIGIN’
-L. -l:libc.so.6
-no-pie
-fno-stack-protector
-Wl,-z,norelro
-D_FORTIFY_SOURCE=0
-z noexecstack

源码:

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <malloc.h>

int *heap_list[16];
int heap_list_size[16];

void init() {
setvbuf(stdout,0,2,0);
setvbuf(stderr,0,2,0);
setvbuf(stdin,0,2,0);
memset(heap_list, 0, sizeof(heap_list));
memset(heap_list_size, 0, sizeof(heap_list_size));
}

void menu() {
printf("1. Add a new note\n");
printf("2. Delete a note\n");
printf("3. Edit a note\n");
printf("4. Check the note\n");
printf("5. Exit\n");
puts("Your choice: ");
}

void add_note()
{
for(int i=0; i<16; i++)
{
if(!heap_list[i])
{
char size[8];
char *note;
while(1)
{
printf("Size: ");
read(0, size, 4);
heap_list_size[i] = atoi(size);
if(heap_list_size[i] <=0)
{
puts("invalid size");
continue;
}
else
{
note = (char *)malloc(heap_list_size[i]);
heap_list[i] = (int *)note;
break;
}
if(heap_list_size[i] > 0x1500)
{
puts("Size is too big!");
exit(1);
}
}

// char buffer[size_num];
// memset(buffer, 0, sizeof(buffer));
if(!note)
{
puts("Memory allocation failed!");
exit(2);
}
printf("Content: ");
read(0, note, heap_list_size[i]);
printf("Note %d added successfully!\n",i);
// off by null
// strcpy(note, buffer);
// off by one
// read(0, buffer, size_num+1);
break;
}
else if(i >= 15)
{
puts("Heap is full!");
break;
}
}
}

void delete_note()
{
char buf[8];
int index_num;
printf("Index: ");
read(0, buf, 4);
index_num = atoi(buf);
if(index_num < 0 || index_num > 15)
{
puts("Invalid index");
return;
}
if(heap_list[index_num])
{
free(heap_list[index_num]);
heap_list[index_num] = NULL; // UAF
heap_list_size[index_num] = 0;//uaf
puts("Note deleted successfully!");
}
else
{
puts("Free corrupted!");
exit(3);
}
}

void edit_note()
{
puts("Please enter the index of the note you want to edit:");
char buf[8];
int index_num;
read(0, buf, 4);
index_num = atoi(buf);
if(index_num < 0 || index_num > 15|| heap_list[index_num] == NULL)
{
puts("Invalid index");
return;
}
else
{
size_t current_size = malloc_usable_size(heap_list[index_num]);
puts("Please enter the new content:");
read(0, heap_list[index_num], current_size-1);
((char *)heap_list[index_num])[current_size-1] = '\0';
}
// 堆溢出
// else
// {
// puts("Please enter the length of item name:");
// char size[8];
// int size_num;
// read(0, size, 4);
// size_num = atoi(size);
// if (size_num <= 0)
// {
// puts("Invalid size");
// return;
// }
// puts("Please enter the new content:");
// read(0, heap_list[index_num], size_num);
// puts("Note edited successfully!");
// }

// 堆溢出2
// else
// {
// char ch;
// int offset = 0;
// puts("Please enter the new content:");
// while (1)
// {
// if (read(0, &ch, 1) <= 0)
// break;
// if (ch == '\n')
// break;
// ((char *)heap_list[index_num])[offset++] = ch;
// }
// puts("Note edited successfully!");
// }
// }
//
// off-by-one:
// else
// {
// puts("Please enter the new content:");
// read(0, heap_list[index_num], size_num+1);
// puts("Note edited successfully!");
// }

// off-by-null:
// else
// {
// puts("Please enter the new content:");
// read(0, heap_list[index_num], heap_list_size[index_num]);
// ((char *)heap_list[index_num])[heap_list_size[index_num]] = '\0';
// puts("Note edited successfully!");
// }
}

void check_note()
{
puts("Please enter the index of the note you want to edit:");
char buf[8];
int index_num;
read(0, buf, 4);
index_num = atoi(buf);
if (index_num < 0 || index_num > 15|| heap_list[index_num] == NULL)
{
puts("Invalid index");
return;
}
else
{
printf("Note %d: %s\n", index_num, (char *)heap_list[index_num]);
}
}



int main()
{
init();
char choice[8];
printf("Welcome to the note management system!\n");
printf("Gift %p\n", &puts);
while (1)
{
menu();
read(0, choice, 8);
switch (atoi(choice))
{
case 1:
add_note();
break;
case 2:
delete_note();
break;
case 3:
edit_note();
break;
case 4:
check_note();
break;
case 5:
puts("Bye!");
exit(0);
break;
case 6:
for(int i=0; i<16; i++)
{
if(heap_list[i])
{
printf("heap_list[%d]: %p\n", i, heap_list[i]);
// printf("heap_list_size[%d]: %d\n", i, heap_list_size[i]);
}
}
break;
default:
puts("Invalid choice");
break;
}
}
return 0;
}


调试脚本

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
from pwn import *
context.log_level = 'debug'
context.arch = 'amd64'
r=process('./init')
libc=ELF('./libc.so.6')

# gdb.attach(r, 'b *addr')

heap_list=0x4040A0
def add(size, content):
r.sendlineafter(b'Your choice: ', b'1')
r.sendlineafter(b'Size: ', str(size))
r.sendafter(b'Content: ', content)

def delete(idx):
r.sendlineafter(b'Your choice: ', b'2')
r.sendafter(b'Index: ', str(idx))

def edit(idx,content):
r.sendlineafter(b'Your choice: ', b'3')
r.sendafter(b'edit:', str(idx))
r.recvuntil(b'content:')
r.sendline(content)

def show(idx):
r.sendlineafter(b'Your choice: ', b'4')
r.sendafter(b'edit:', str(idx))

def leak():
r.sendlineafter(b'Your choice: ', b'6')

r.recvuntil(b'0x')
puts_addr=int(r.recv(12),16)
log.success('puts_addr: ' + hex(puts_addr))
libc_base=puts_addr - libc.symbols['puts']
log.success('libc_base: ' + hex(libc_base))
malloc_hook=libc_base + libc.symbols['__malloc_hook']
log.success('__malloc_hook: ' + hex(malloc_hook))
#操作区





r.interactive()


heap题出题模板
http://example.com/2025/04/21/heap题出题模板/
发布于
2025年4月21日
许可协议