> 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/ru/web/ssti/basic-ruby-server-side-template-injection.md).

# Базовая инъекция шаблонов на стороне сервера Ruby

### Базовая инъекция шаблона на стороне сервера

Приложение использует шаблоны ERB без защиты, что делает его уязвимым к **Инъекция шаблона на стороне сервера (SSTI)**./ Цель — выполнить произвольный код для **удалить файл** `morale.txt` расположенный в домашнем каталоге Карлоса:/ `/home/carlos/morale.txt`.

#### 1. Определение уязвимого параметра

Мы находим `message` параметр в приложении, который **отражает наш ввод** непосредственно на странице. / Это поле будет использоваться для внедрения кода ERB.

<figure><img src="/files/8aa38bf885a96a8e4dbf52b146cc3edfbc221213" alt=""><figcaption></figcaption></figure>

#### 2. Проверка выполнения кода (тест с 7/\*7)

Для подтверждения SSTI внедряется простое выражение Ruby:

```ruby
<%= 7*7 %>
```

Если уязвимость присутствует, вывод будет показывать:

<figure><img src="/files/524690d5312c1229f8106f0d14910d7a463fc694" alt="" width="563"><figcaption></figcaption></figure>

Это доказывает, что выражение Ruby вычисляется в шаблоне на стороне сервера.

#### 3. Прочитать конфиденциальный файл: `/etc/passwd`

После подтверждения выполнения попробуйте прочитать системный файл, чтобы проверить доступ к файловой системе:

```ruby
<%= File.open('/etc/passwd').read %>
```

<figure><img src="/files/bf36de2114040d697b334aa29fc644553bbb7a2a" alt=""><figcaption></figcaption></figure>

Содержимое `/etc/passwd` затем возвращается в ответе, подтверждая, что вы можете читать файлы на сервере.

#### 4. Удалить Карлоса `morale.txt` Файл

Чтобы завершить лабораторную работу, выполните системную команду для удаления запрошенного файла:

```ruby
<%= system('rm /home/carlos/morale.txt') %>
```

Эта инъекция запускает `rm` серверную команду и удаляет `morale.txt` файл в `/home/carlos/` каталог.


---

# 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://hacking-notes.jord4n.pro/ru/web/ssti/basic-ruby-server-side-template-injection.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.
