الدرس 14 من 24

أمان الشبكة والبنية التحتية

مجموعات الأمان و Network ACLs

4 دقيقة للقراءة

مجموعات الأمان و Network ACLs (NACLs) توفر أمان شبكة متعدد الطبقات. فهم اختلافاتها والتكوين الصحيح أمر حاسم للدفاع في العمق.

مجموعات الأمان مقابل NACLs

الميزة مجموعات الأمان Network ACLs
المستوى Instance/المورد الشبكة الفرعية
الحالة Stateful Stateless
القواعد السماح فقط السماح والرفض
التقييم جميع القواعد تُقيَّم القواعد تُعالج بالترتيب
الافتراضي رفض كل الوارد السماح بالكل
الأفضل لـ تصفية مستوى التطبيق حظر مستوى الشبكة الفرعية

مجموعات أمان AWS

السلوك Stateful

مجموعات الأمان stateful—إذا سمحت بحركة مرور واردة، الرد مسموح تلقائياً:

قاعدة واردة: السماح بـ TCP 443 من 0.0.0.0/0
← حركة الرد مسموحة تلقائياً (لا حاجة لقاعدة صادرة)

أنماط مجموعات الأمان الآمنة

مجموعة أمان خادم الويب:

resource "aws_security_group" "web" {
  name        = "web-server-sg"
  description = "مجموعة أمان لخوادم الويب"
  vpc_id      = aws_vpc.main.id

  # HTTPS من موازن التحميل فقط
  ingress {
    description     = "HTTPS من ALB"
    from_port       = 443
    to_port         = 443
    protocol        = "tcp"
    security_groups = [aws_security_group.alb.id]
  }

  # لا SSH مباشر - استخدم Session Manager
  # ingress { ... port 22 } # تجنب

  egress {
    description     = "HTTPS لطبقة التطبيق"
    from_port       = 8080
    to_port         = 8080
    protocol        = "tcp"
    security_groups = [aws_security_group.app.id]
  }

  tags = {
    Name = "web-server-sg"
  }
}

مجموعة أمان قاعدة البيانات:

resource "aws_security_group" "database" {
  name        = "database-sg"
  description = "مجموعة أمان لقواعد البيانات"
  vpc_id      = aws_vpc.main.id

  # PostgreSQL من طبقة التطبيق فقط
  ingress {
    description     = "PostgreSQL من طبقة التطبيق"
    from_port       = 5432
    to_port         = 5432
    protocol        = "tcp"
    security_groups = [aws_security_group.app.id]
  }

  # لا وصول إنترنت صادر
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["10.0.0.0/16"]  # VPC فقط
  }
}

أخطاء مجموعات الأمان الشائعة

الخطأ المخاطر الحل
0.0.0.0/0 على SSH (22) هجمات القوة الغاشمة استخدم Session Manager أو bastion
0.0.0.0/0 على RDP (3389) ناقل برامج الفدية استخدم VPN أو bastion
0.0.0.0/0 على قواعد البيانات خرق البيانات طبقة التطبيق فقط
صادر 0.0.0.0/0 الكل تسريب البيانات حدد للمطلوب

Network ACLs (NACLs)

السلوك Stateless

NACLs هي stateless—يجب السماح صريحاً بكل من الوارد والصادر:

قاعدة واردة: السماح بـ TCP 443 من 0.0.0.0/0
قاعدة صادرة: السماح بـ TCP 1024-65535 إلى 0.0.0.0/0 (منافذ ephemeral للرد)

NACL للدفاع في العمق

استخدم NACLs لحظر الجهات الخبيثة المعروفة على مستوى الشبكة الفرعية:

resource "aws_network_acl" "private" {
  vpc_id     = aws_vpc.main.id
  subnet_ids = [aws_subnet.private.id]

  # حظر IPs خبيثة معروفة
  ingress {
    protocol   = "-1"
    rule_no    = 100
    action     = "deny"
    cidr_block = "192.0.2.0/24"  # نطاق محظور مثال
    from_port  = 0
    to_port    = 0
  }

  # السماح بحركة VPC
  ingress {
    protocol   = "-1"
    rule_no    = 200
    action     = "allow"
    cidr_block = "10.0.0.0/16"
    from_port  = 0
    to_port    = 0
  }

  # رفض كل الوارد الآخر
  ingress {
    protocol   = "-1"
    rule_no    = 32766
    action     = "deny"
    cidr_block = "0.0.0.0/0"
    from_port  = 0
    to_port    = 0
  }

  # السماح بكل الصادر لـ VPC
  egress {
    protocol   = "-1"
    rule_no    = 100
    action     = "allow"
    cidr_block = "10.0.0.0/16"
    from_port  = 0
    to_port    = 0
  }
}

Azure Network Security Groups

قواعد NSG

# إنشاء NSG
az network nsg create \
    --name web-nsg \
    --resource-group myRG

# السماح بـ HTTPS من موازن التحميل
az network nsg rule create \
    --nsg-name web-nsg \
    --resource-group myRG \
    --name AllowHTTPS \
    --priority 100 \
    --access Allow \
    --direction Inbound \
    --protocol Tcp \
    --destination-port-ranges 443 \
    --source-address-prefixes AzureLoadBalancer

# رفض SSH المباشر
az network nsg rule create \
    --nsg-name web-nsg \
    --resource-group myRG \
    --name DenySSH \
    --priority 200 \
    --access Deny \
    --direction Inbound \
    --protocol Tcp \
    --destination-port-ranges 22 \
    --source-address-prefixes Internet

مجموعات أمان التطبيقات (ASG)

تجميع VMs حسب الوظيفة بدلاً من IP:

# إنشاء ASGs
az network asg create --name web-asg --resource-group myRG
az network asg create --name db-asg --resource-group myRG

# قاعدة NSG باستخدام ASGs
az network nsg rule create \
    --nsg-name app-nsg \
    --resource-group myRG \
    --name AllowWebToDb \
    --priority 100 \
    --access Allow \
    --direction Inbound \
    --protocol Tcp \
    --destination-port-ranges 5432 \
    --source-asgs web-asg \
    --destination-asgs db-asg

قواعد جدار حماية GCP

قواعد جدار حماية VPC

# السماح بـ HTTPS من موازن التحميل
gcloud compute firewall-rules create allow-https-lb \
    --network=production-vpc \
    --allow=tcp:443 \
    --source-ranges=130.211.0.0/22,35.191.0.0/16 \
    --target-tags=web-server

# السماح بحركة المرور الداخلية
gcloud compute firewall-rules create allow-internal \
    --network=production-vpc \
    --allow=tcp,udp,icmp \
    --source-ranges=10.0.0.0/16

# رفض كل الوارد (ضمني، لكن الصريح أفضل)
gcloud compute firewall-rules create deny-all-ingress \
    --network=production-vpc \
    --action=DENY \
    --rules=all \
    --source-ranges=0.0.0.0/0 \
    --priority=65534

قائمة تدقيق مجموعات الأمان

الفحص الإجراء الأولوية
لا 0.0.0.0/0 على منافذ الإدارة إزالة أو تقييد حرج
قاعدة البيانات ليست مواجهة للإنترنت طبقة التطبيق فقط حرج
الصادر مقيد حدد قواعد الخروج عالي
القواعد غير المستخدمة محذوفة تدقيق وتنظيف متوسط
سجلات التدفق مفعلة راقب حركة المرور عالي
التوثيق محدث راجع ربع سنوياً متوسط

التالي، سنستكشف WAF، حماية DDoS، وخدمات أمان الحافة. :::

اختبار

الوحدة 4: أمان الشبكة والبنية التحتية

خذ الاختبار