Kodax

أساسيات HTTP للأمن السيبراني — أساسيات الأمن السيبراني

بروتوكول HTTP

ما هو HTTP؟

HyperText Transfer Protocol

البروتوكول المستخدم لنقل صفحات الويب.

HTTP Methods (الطرق)

GET

- الاستخدام: طلب بيانات من الخادم

- آمن: لا يغيّر البيانات

- المعاملات: في URL (مرئية)

GET /users?id=123 HTTP/1.1

POST

- الاستخدام: إرسال بيانات للخادم

- غير آمن: يعدّل البيانات

- المعاملات: في Body (غير مرئية في URL)

POST /login HTTP/1.1
Content-Type: application/json

{"username": "admin", "password": "123"}

PUT, DELETE, PATCH

- تعديل وحذف البيانات

أجزاء الطلب (Request)

GET /index.html HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0
Cookie: session=abc123
Authorization: Bearer token

أجزاء الرد (Response)

HTTP/1.1 200 OK
Content-Type: text/html
Set-Cookie: session=xyz789
Content-Length: 1234

<html>...</html>

Status Codes

- 200: نجح

- 301/302: إعادة توجيه

- 400: خطأ من العميل

- 401: غير مصرّح

- 403: ممنوع

- 404: غير موجود

- 500: خطأ في الخادم

Headers مهمة للأمن

- Content-Security-Policy: منع XSS

- X-Frame-Options: منع Clickjacking

- Strict-Transport-Security: فرض HTTPS

- X-Content-Type-Options: منع MIME sniffing

HTTP vs HTTPS

- HTTP: نص عادي ⚠️

- HTTPS: مشفر بـ SSL/TLS ✅

# محاكاة طلبات HTTP في Python
import requests

# ===== GET Request =====
def make_get_request():
    """
    طلب GET بسيط
    """
    url = "https://jsonplaceholder.typicode.com/users/1"
    
    # إرسال الطلب
    response = requests.get(url)
    
    print(f"Status Code: {response.status_code}")
    print(f"Headers: {dict(response.headers)}")
    print(f"Content: {response.json()}")
    
    return response

# ===== POST Request =====
def make_post_request():
    """
    طلب POST لإرسال بيانات
    """
    url = "https://jsonplaceholder.typicode.com/posts"
    
    data = {
        "title": "عنوان المقال",
        "body": "محتوى المقال",
        "userId": 1
    }
    
    response = requests.post(url, json=data)
    
    print(f"Status: {response.status_code}")
    print(f"Response: {response.json()}")
    
    return response

# ===== تحليل Headers =====
def analyze_security_headers(url):
    """
    فحص headers الأمان في موقع
    """
    response = requests.get(url)
    headers = response.headers
    
    security_headers = {
        "Content-Security-Policy": headers.get("Content-Security-Policy"),
        "X-Frame-Options": headers.get("X-Frame-Options"),
        "Strict-Transport-Security": headers.get("Strict-Transport-Security"),
        "X-Content-Type-Options": headers.get("X-Content-Type-Options"),
        "X-XSS-Protection": headers.get("X-XSS-Protection"),
    }
    
    print(f"\n=== فحص Headers الأمان لـ {url} ===")
    for header, value in security_headers.items():
        status = "✅" if value else "❌"
        print(f"{status} {header}: {value or 'غير موجود'}")
    
    return security_headers

# ===== Cookies =====
def demo_cookies():
    """
    عرض توضيحي للـ Cookies
    """
    session = requests.Session()
    
    # الخادم يرسل Cookie
    response = session.get("https://httpbin.org/cookies/set/session/abc123")
    
    # الكوكيز المخزنة
    print(f"Cookies: {session.cookies.get_dict()}")
    
    # الطلب التالي يرسل Cookie تلقائياً
    response = session.get("https://httpbin.org/cookies")
    print(f"Server received: {response.json()}")

# تشغيل الأمثلة
# make_get_request()
# make_post_request()
# analyze_security_headers("https://google.com")
# demo_cookies()
اختبر فهمك