Kodax

رفع الصلاحيات (Privilege Escalation) — الاختراق الأخلاقي

رفع الصلاحيات - Privilege Escalation

ما هو؟

الانتقال من صلاحيات محدودة إلى صلاحيات أعلى (root/admin).

الأنواع

Vertical (عمودي)

user → root/admin

الحصول على صلاحيات أعلى

Horizontal (أفقي)

user1 → user2

الوصول لحساب بنفس المستوى

Linux Privilege Escalation

SUID Binaries

# البحث عن ملفات SUID
find / -perm -4000 2>/dev/null

# استغلال نسخة قديمة من sudo
sudo -l

Cron Jobs

# فحص cron
cat /etc/crontab
ls -la /etc/cron.*/

Writable Files

# ملفات قابلة للكتابة
find / -writable 2>/dev/null

Kernel Exploits

# معرفة إصدار النواة
uname -a

Windows Privilege Escalation

Service Misconfigurations

- Unquoted service paths

- Weak service permissions

Token Impersonation

- SeImpersonatePrivilege

- Potato attacks

UAC Bypass

- تجاوز User Account Control

Registry Keys

- AlwaysInstallElevated

أدوات مساعدة

- LinPEAS/WinPEAS: فحص آلي

- GTFOBins: قائمة استغلالات Linux

- LOLBAS: قائمة Windows

# أدوات فحص Privilege Escalation
import os
import subprocess
import platform

class PrivEscChecker:
    """
    فحص نقاط ضعف Privilege Escalation
    ⚠️ للاستخدام الأخلاقي فقط!
    """
    
    def __init__(self):
        self.findings = []
        self.os_type = platform.system()
    
    def check_linux_suid(self):
        """
        فحص ملفات SUID في Linux
        """
        print("\n🔍 فحص ملفات SUID...")
        
        dangerous_suids = [
            '/usr/bin/find', '/usr/bin/vim', '/usr/bin/python',
            '/usr/bin/perl', '/usr/bin/ruby', '/usr/bin/awk',
            '/usr/bin/nmap', '/usr/bin/less', '/usr/bin/more',
            '/usr/bin/nano', '/usr/bin/cp', '/usr/bin/mv',
        ]
        
        # محاكاة نتائج
        found_suids = [
            '/usr/bin/passwd',
            '/usr/bin/sudo',
            '/usr/bin/find',  # هذا خطير!
        ]
        
        print("  ملفات SUID:")
        for suid in found_suids:
            status = "🔴 خطير!" if suid in dangerous_suids else "✅ عادي"
            print(f"    {suid} - {status}")
            
            if suid in dangerous_suids:
                self.findings.append({
                    "type": "SUID",
                    "file": suid,
                    "severity": "HIGH",
                    "note": "يمكن استغلاله لرفع الصلاحيات"
                })
    
    def check_sudo_permissions(self):
        """
        فحص صلاحيات sudo
        """
        print("\n🔍 فحص صلاحيات sudo...")
        
        # محاكاة نتائج sudo -l
        sudo_permissions = [
            "(ALL) NOPASSWD: /usr/bin/vim",
            "(ALL) /usr/bin/apt-get",
        ]
        
        dangerous_commands = ['vim', 'vi', 'nano', 'less', 'more', 'awk', 
                             'find', 'python', 'perl', 'ruby', 'bash']
        
        for perm in sudo_permissions:
            is_dangerous = any(cmd in perm for cmd in dangerous_commands)
            is_nopasswd = "NOPASSWD" in perm
            
            if is_dangerous and is_nopasswd:
                status = "🔴 خطير جداً!"
                self.findings.append({
                    "type": "SUDO",
                    "permission": perm,
                    "severity": "CRITICAL"
                })
            elif is_dangerous:
                status = "🟡 يحتاج مراجعة"
            else:
                status = "✅"
            
            print(f"    {perm} - {status}")
    
    def check_writable_files(self):
        """
        فحص ملفات مهمة قابلة للكتابة
        """
        print("\n🔍 فحص ملفات قابلة للكتابة...")
        
        important_files = [
            '/etc/passwd',
            '/etc/shadow',
            '/etc/sudoers',
            '/etc/crontab',
        ]
        
        # محاكاة
        writable = ['/etc/crontab']
        
        for f in important_files:
            if f in writable:
                print(f"    🔴 {f} - قابل للكتابة!")
                self.findings.append({
                    "type": "WRITABLE_FILE",
                    "file": f,
                    "severity": "CRITICAL"
                })
            else:
                print(f"    ✅ {f} - محمي")
    
    def check_cron_jobs(self):
        """
        فحص Cron Jobs
        """
        print("\n🔍 فحص Cron Jobs...")
        
        # محاكاة cron jobs
        cron_jobs = [
            {"schedule": "*/5 * * * *", "user": "root", "command": "/opt/backup.sh"},
            {"schedule": "0 0 * * *", "user": "www-data", "command": "/var/www/cleanup.py"},
        ]
        
        for job in cron_jobs:
            print(f"    [{job['user']}] {job['schedule']} {job['command']}")
            
            # فحص إذا كان الملف قابل للكتابة (محاكاة)
            if job['command'] == "/opt/backup.sh":
                print(f"      🔴 الملف قابل للكتابة ويعمل بصلاحيات {job['user']}!")
                self.findings.append({
                    "type": "CRON",
                    "job": job,
                    "severity": "HIGH"
                })
    
    def check_kernel_version(self):
        """
        فحص إصدار النواة
        """
        print("\n🔍 فحص إصدار النواة...")
        
        # محاكاة
        kernel = "Linux 4.15.0-generic"
        print(f"    النواة: {kernel}")
        
        # قائمة إصدارات معرضة (مبسطة)
        vulnerable_kernels = ["4.15.0", "3.13.0", "2.6.32"]
        
        for vk in vulnerable_kernels:
            if vk in kernel:
                print(f"    🔴 إصدار قديم! قد يكون معرض لثغرات")
                self.findings.append({
                    "type": "KERNEL",
                    "version": kernel,
                    "severity": "HIGH"
                })
                break
        else:
            print(f"    ✅ الإصدار حديث نسبياً")
    
    def generate_report(self):
        """
        توليد تقرير
        """
        print("\n" + "=" * 60)
        print("  📋 تقرير فحص Privilege Escalation")
        print("=" * 60)
        
        if not self.findings:
            print("  ✅ لم يُعثر على نقاط ضعف واضحة")
            return
        
        severity_order = {"CRITICAL": 0, "HIGH": 1, "MEDIUM": 2, "LOW": 3}
        sorted_findings = sorted(self.findings, key=lambda x: severity_order.get(x['severity'], 4))
        
        for finding in sorted_findings:
            emoji = "🔴" if finding['severity'] == "CRITICAL" else "🟡" if finding['severity'] == "HIGH" else "🟢"
            print(f"\n  {emoji} [{finding['severity']}] {finding['type']}")
            for key, value in finding.items():
                if key not in ['type', 'severity']:
                    print(f"      {key}: {value}")
    
    def run_all_checks(self):
        """
        تشغيل كل الفحوصات
        """
        print("\n🔒 فحص Privilege Escalation")
        print("=" * 60)
        
        self.check_linux_suid()
        self.check_sudo_permissions()
        self.check_writable_files()
        self.check_cron_jobs()
        self.check_kernel_version()
        
        self.generate_report()


# تشغيل
checker = PrivEscChecker()
checker.run_all_checks()
اختبر فهمك