SiamCafe.net Blog
Cybersecurity

ModSecurity WAF Micro-segmentation

modsecurity waf micro segmentation
ModSecurity WAF Micro-segmentation | SiamCafe Blog
2026-02-18· อ. บอม — SiamCafe.net· 9,839 คำ

ModSecurity WAF + Micro-segmentation

ModSecurity WAF OWASP CRS Micro-segmentation SQL Injection XSS Lateral Movement Network Segmentation Production

Protection LayerToolProtects AgainstDirection
WAF (Layer 7)ModSecurity + OWASP CRSSQLi, XSS, CSRF, RCENorth-South (inbound)
Network Firewalliptables / nftablesPort scanning, unauthorized accessNorth-South
Micro-segmentationCalico / Cilium / iptablesLateral movementEast-West (internal)
Host Firewallufw / firewalldDirect host accessBoth
Container NetworkKubernetes NetworkPolicyPod-to-pod unauthorizedEast-West

ModSecurity Setup

# === ModSecurity Installation ===

# Install on Debian/Ubuntu with Nginx
# apt install libmodsecurity3 libmodsecurity-dev
# git clone https://github.com/SpiderLabs/ModSecurity-nginx
# # Compile Nginx with module:
# nginx -V 2>&1 | grep configure  # get existing flags
# ./configure --add-dynamic-module=../ModSecurity-nginx [existing flags]
# make modules
# cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules/

# nginx.conf
# load_module modules/ngx_http_modsecurity_module.so;
# server {
#     modsecurity on;
#     modsecurity_rules_file /etc/nginx/modsec/main.conf;
#     location / {
#         proxy_pass http://backend:8080;
#     }
# }

# /etc/nginx/modsec/main.conf
# Include /etc/nginx/modsec/modsecurity.conf
# Include /etc/nginx/modsec/crs/crs-setup.conf
# Include /etc/nginx/modsec/crs/rules/*.conf
# SecRuleEngine DetectionOnly  # Start with detection
# SecAuditLog /var/log/modsec/audit.log
# SecAuditLogFormat JSON

# Download OWASP CRS
# cd /etc/nginx/modsec
# git clone https://github.com/coreruleset/coreruleset crs
# cp crs/crs-setup.conf.example crs/crs-setup.conf

from dataclasses import dataclass

@dataclass
class CRSRule:
    rule_id: str
    category: str
    attacks: str
    severity: str

rules = [
    CRSRule("920xxx", "Protocol Enforcement",
        "HTTP Protocol violations, request smuggling",
        "Medium-High"),
    CRSRule("930xxx", "Local File Inclusion",
        "Path traversal, /etc/passwd, ../ attacks",
        "High"),
    CRSRule("931xxx", "Remote File Inclusion",
        "Remote URL in parameters, PHP include",
        "High"),
    CRSRule("932xxx", "Remote Code Execution",
        "Command injection, OS commands in input",
        "Critical"),
    CRSRule("941xxx", "XSS (Cross-Site Scripting)",
        "Script tags, event handlers, encoded XSS",
        "High"),
    CRSRule("942xxx", "SQL Injection",
        "SQL keywords, UNION SELECT, blind SQLi",
        "Critical"),
]

print("=== OWASP CRS Rules ===")
for r in rules:
    print(f"  [{r.rule_id}] {r.category} | Severity: {r.severity}")
    print(f"    Attacks: {r.attacks}")

Micro-segmentation

# === Network Micro-segmentation ===

# iptables rules for micro-segmentation
# Web Server (10.0.1.0/24) → App Server (10.0.2.0/24) only port 8080
# iptables -A FORWARD -s 10.0.1.0/24 -d 10.0.2.0/24 -p tcp --dport 8080 -j ACCEPT
# iptables -A FORWARD -s 10.0.1.0/24 -d 10.0.2.0/24 -j DROP
#
# App Server (10.0.2.0/24) → Database (10.0.3.0/24) only port 5432
# iptables -A FORWARD -s 10.0.2.0/24 -d 10.0.3.0/24 -p tcp --dport 5432 -j ACCEPT
# iptables -A FORWARD -s 10.0.2.0/24 -d 10.0.3.0/24 -j DROP
#
# Database (10.0.3.0/24) → deny all outbound except backup
# iptables -A FORWARD -s 10.0.3.0/24 -d 10.0.4.0/24 -p tcp --dport 22 -j ACCEPT
# iptables -A FORWARD -s 10.0.3.0/24 -j DROP

# Kubernetes NetworkPolicy
# apiVersion: networking.k8s.io/v1
# kind: NetworkPolicy
# metadata:
#   name: allow-app-to-db
# spec:
#   podSelector:
#     matchLabels: { app: database }
#   policyTypes: [Ingress]
#   ingress:
#     - from:
#         - podSelector:
#             matchLabels: { app: backend }
#       ports:
#         - protocol: TCP
#           port: 5432

@dataclass
class Segment:
    segment: str
    allowed_inbound: str
    allowed_outbound: str
    blocked: str
    tool: str

segments = [
    Segment("Web Server (DMZ)",
        "Internet port 443 only",
        "App Server port 8080 only",
        "Database, Internal Services, other Web Servers",
        "iptables + ModSecurity WAF"),
    Segment("App Server",
        "Web Server port 8080 only",
        "Database port 5432, Cache port 6379",
        "Internet, other App Servers, Management",
        "iptables + Calico NetworkPolicy"),
    Segment("Database",
        "App Server port 5432 only",
        "Backup Server port 22 only",
        "Internet, Web Server, other Databases",
        "iptables + host firewall"),
    Segment("Management",
        "Admin VPN port 22/443 only",
        "All internal (monitoring, config)",
        "Internet direct, untrusted networks",
        "VPN + firewall + MFA"),
]

print("=== Micro-segmentation ===")
for s in segments:
    print(f"  [{s.segment}] Tool: {s.tool}")
    print(f"    In: {s.allowed_inbound}")
    print(f"    Out: {s.allowed_outbound}")
    print(f"    Blocked: {s.blocked}")

Production Hardening

# === Production Checklist ===

@dataclass
class HardeningStep:
    step: str
    action: str
    verify: str
    priority: str

hardening = [
    HardeningStep("WAF Prevention Mode",
        "เปลี่ยน SecRuleEngine DetectionOnly เป็น On",
        "ทดสอบด้วย OWASP ZAP ว่า Attack ถูกบล็อก",
        "Critical"),
    HardeningStep("Anomaly Threshold Tuning",
        "ปรับ Anomaly Score Threshold ตาม Application",
        "ดู False Positive Rate < 0.1%",
        "High"),
    HardeningStep("Segment All Tiers",
        "แบ่ง Web App DB แยก Segment มี Firewall Rule",
        "Verify ด้วย nmap ว่าข้าม Segment ไม่ได้",
        "Critical"),
    HardeningStep("Log Centralization",
        "ส่ง ModSecurity Audit Log ไป SIEM (ELK, Splunk)",
        "ดู Dashboard ว่ามี Log เข้ามาครบ",
        "High"),
    HardeningStep("Rate Limiting",
        "ตั้ง Rate Limit ใน Nginx + ModSecurity",
        "ทดสอบ DDoS simulation ว่าบล็อกได้",
        "High"),
    HardeningStep("TLS Configuration",
        "TLS 1.2+ เท่านั้น Strong Cipher Suites HSTS",
        "testssl.sh หรือ SSL Labs Grade A+",
        "Critical"),
]

print("=== Production Hardening ===")
for h in hardening:
    print(f"  [{h.priority}] {h.step}")
    print(f"    Action: {h.action}")
    print(f"    Verify: {h.verify}")

เคล็ดลับ

แนวทางป้องกันภัยไซเบอร์สำหรับองค์กรไทย

ภัยคุกคามทางไซเบอร์ในปี 2026 มีความซับซ้อนมากขึ้น Ransomware ยังคงเป็นภัยอันดับหนึ่ง โดยผู้โจมตีใช้ AI ช่วยสร้าง Phishing Email ที่แนบเนียนขึ้น องค์กรควรมี Multi-Layered Security ตั้งแต่ Perimeter Defense ด้วย Next-Gen Firewall Endpoint Protection ด้วย EDR Solution และ Network Detection and Response

การฝึกอบรมพนักงานเป็นสิ่งสำคัญที่สุด เพราะ Human Error เป็นสาเหตุหลักของการรั่วไหลข้อมูล ควรจัด Security Awareness Training อย่างน้อยไตรมาสละครั้ง ทำ Phishing Simulation ทดสอบพนักงาน และมี Incident Response Plan ที่ชัดเจน ฝึกซ้อมเป็นประจำ

สำหรับกฎหมาย PDPA ของไทย องค์กรต้องมี Data Protection Officer แจ้งวัตถุประสงค์การเก็บข้อมูลอย่างชัดเจน ขอ Consent ก่อนใช้ข้อมูลส่วนบุคคล มีมาตรการรักษาความปลอดภัยที่เหมาะสม และแจ้งเหตุ Data Breach ภายใน 72 ชั่วโมง

ModSecurity คืออะไร

Open Source WAF Apache Nginx Module SQL Injection XSS CSRF OWASP CRS SecRule Audit Log Detection Prevention Mode

Micro-segmentation คืออะไร

แบ่ง Network ระดับ Workload Lateral Movement Firewall Rule Host Container Policy East-West WAF North-South Calico Cilium

ติดตั้ง ModSecurity อย่างไร

libmodsecurity3 Nginx Module OWASP CRS GitHub modsecurity.conf crs-setup.conf DetectionOnly Log False Positive Prevention Nikto ZAP

ลด False Positive อย่างไร

Detection Mode Audit Log SecRuleRemoveById SecRuleUpdateTargetById Anomaly Scoring Threshold Whitelist IP URL Custom Rule Test Deploy

สรุป

ModSecurity WAF OWASP CRS Micro-segmentation iptables Calico NetworkPolicy SQL Injection XSS Lateral Movement Production Hardening

📖 บทความที่เกี่ยวข้อง

Helm Chart Template Micro-segmentationอ่านบทความ → ModSecurity WAF Cost Optimization ลดค่าใช้จ่ายอ่านบทความ → ModSecurity WAF Progressive Deliveryอ่านบทความ → ModSecurity WAF Container Orchestrationอ่านบทความ →

📚 ดูบทความทั้งหมด →