การทดสอบและตรวจสอบ AWS Security Groups (SG) ให้ครอบคลุม
AWS Security Group (SG) ทำหน้าที่เป็น Virtual Firewall แบบ Stateful ควบคุม Inbound/Outbound Traffic ของ EC2, RDS, ELB, ENI และทรัพยากรอื่นๆ ใน VPC การทดสอบต้องครอบคลุมทั้ง การตั้งค่า (Configuration) และ การทำงานจริง (Runtime Behavior)
1. การตรวจสอบการตั้งค่า (Configuration Audit) - Static Analysis
ใช้ AWS Console / CLI
# ดู SG ทั้งหมด
aws ec2 describe-security-groups --query 'SecurityGroups[*].[GroupId,GroupName,Description,VpcId]'
# ดูรายละเอียดกฎ Inbound/Outbound
aws ec2 describe-security-groups --group-ids sg-xxxxxxxx --query 'SecurityGroups[0].{Inbound:IpPermissions,Outbound:IpPermissionsEgress}'
# ค้นหา SG ที่เปิด 0.0.0.0/0 บน Port อันตราย
aws ec2 describe-security-groups --filters Name=ip-permission.cidr,Values=0.0.0.0/0 --query 'SecurityGroups[?contains(IpPermissions[].FromPort, `22`) || contains(IpPermissions[].FromPort, `3389`) || contains(IpPermissions[].FromPort, `3306`)].[GroupId,GroupName]'
ใช้ AWS Config Rules (Managed Rules)
restricted-ssh - ตรวจ SG ที่เปิด Port 22 จาก 0.0.0.0/0
restricted-common-ports - ตรวจ Port อันตรายทั่วไป (RDP, DB, etc.)
security-group-rule-description - บังคับให้มี Description
vpc-sg-open-only-to-authorized-ports - Custom Rule ตรวจ Port ที่อนุญาต
เครื่องมือ Open Source สำหรับ Audit
- Prowler (AWS Security Best Practices Assessment) - รวม SG Checks
- ScoutSuite - Multi-cloud Security Auditing
- CloudSploit - Security Configuration Monitoring
- cfn-nag / checkov - IaC Scanning (CloudFormation/Terraform)
2. การทดสอบการทำงานจริง (Runtime / Functional Testing)
การทดสอบ Inbound Rules (จากภายนอก/เครือข่ายอื่น)
- จาก Instance อื่นใน VPC เดียวกัน (Same VPC, Different SG):
# Test TCP Port
nc -zv
# หรือ
telnet
# HTTP/HTTPS
curl -I https://:
curl -I https://: -k
# Test UDP (ยากกว่า)
nc -zuv
# หรือใช้ iperf3 (TCP/UDP throughput test)
- จาก Internet (Public IP / ALB / NLB): ใช้เครื่องมือภายนอก หรือรันจาก Instance ใน Public Subnet
- จาก VPC Peering / Transit Gateway / VPN / Direct Connect: ทดสอบ Cross-VPC/On-prem Connectivity
การทดสอบ Outbound Rules (Egress Filtering)
# จาก Instance ที่อยู่ใน SG นั้น
# Test DNS (Port 53 UDP/TCP)
nslookup google.com
dig @8.8.8.8 google.com
# Test HTTPS Egress (Port 443)
curl -I https://aws.amazon.com
# Test Specific Destination (เช่น S3 Endpoint, API Endpoint)
curl -I https://s3..amazonaws.com
# Test Blocked Port (ควรล้มเหลว/Timeout)
nc -zv
# ควรได้ Connection timeout (ไม่ใช่ Connection refused)
3. การทดสอบขั้นสูง (Advanced Testing)
VPC Reachability Analyzer
- บริการ AWS Native: ตรวจสอบ Path Connectivity ระหว่าง Source/Destination
- ระบุ: Source (ENI/SG/Subnet), Destination (ENI/SG/Subnet/IP), Protocol/Port
- รายงาน: Reachable / Not Reachable พร้อมรายละเอียด Component ที่บล็อก (SG, NACL, Route Table, ENI)
- ใช้ได้ทั้ง Console และ CLI:
aws ec2 create-network-insights-path
VPC Flow Logs Analysis
- เปิด Flow Logs ส่งไป CloudWatch Logs / S3 / Kinesis Firehose
- Query ด้วย CloudWatch Logs Insights / Athena:
# หา Traffic ที่ถูก Reject โดย SG
filter (action = "REJECT" and protocol = 6)
| stats count() by srcaddr, dstaddr, dstport, pkt-src-aws-service, pkt-dst-aws-service
| sort count desc
Automated Testing ใน CI/CD Pipeline
- ใช้ Terratest (Go) หรือ AWS CDK/CloudFormation Tests
- Deploy Test Resources → Run Connectivity Tests → Assert Results → Cleanup
- ตัวอย่าง: Deploy EC2 ใน SG → Test Port 443 Inbound Pass, Port 22 Inbound Fail
4. Checklist สำหรับ Security Group Review
| หมวดหมู่ | รายการตรวจสอบ |
| Least Privilege | เปิด Port/Protocol/IP Range เท่าที่จำเป็นจริงๆ เท่านั้น |
| No 0.0.0.0/0 on Sensitive Ports | Port 22 (SSH), 3389 (RDP), 3306 (MySQL), 5432 (Postgres), 27017 (Mongo), 6379 (Redis) |
| Source = SG ID (ไม่ใช่ CIDR) | อ้างอิง SG อื่น (sg-xxxx) แทน IP Range สำหรับ Internal Traffic |
| Outbound Restriction | จำกัด Egress เฉพาะที่จำเป็น (Default Allow All อันตราย) |
| Description บนทุก Rule | อธิบายวัตถุประสงค์ Owner Ticket Reference |
| Naming Convention | sg-<env>-<app>-<tier>-<role> (เช่น sg-prod-web-alb-https) |
| Unused SG Cleanup | ลบ SG ที่ไม่มี ENI/Resource ผูกใช้งาน |
| Port Range Minimization | ระบุ Port เดียว (From=To=443) ไม่ใช่ Range กว้าง |
5. การแก้ไขปัญหาที่พบบ่อย (Troubleshooting)
| อาการ | สาเหตุที่เป็นไปได้ | วิธีตรวจสอบ/แก้ |
| Timeout (ไม่ใช่ Refused) | SG/NACL Block, Route Table Missing, ENI Issue | Reachability Analyzer, Flow Logs, Check NACL (Stateless!) |
| Connection Refused | Port เปิดใน SG แต่ Service ไม่ Listen / Firewall OS Block | ss -tlnp / netstat -an บน Target Instance |
| Intermittent | ASG Scaling, ENI Change, SG Rule Change, Network Glitch | CloudTrail Lookup (ModifySecurityGroupRules), Flow Logs Time-series |
| Cross-Region/VPC Fail | Peering/TGW Route Missing, SG ไม่อนุญาต Peer CIDR | Check Route Tables, SG Source = Peer SG / CIDR |
สรุปแนวทางปฏิบัติที่ดีที่สุด (Best Practices)
- IaC First: จัดการ SG ผ่าน Terraform/CloudFormation/CDK เท่านั้น ห้ามแก้ Console โดยตรง
- Default Deny: สร้าง SG ใหม่ = Deny All → เปิดเฉพาะที่ต้องการ
- Reference SG IDs: ใช้ SG-to-SG Reference แทน CIDR สำหรับ Internal
- Automate Audit: Prowler/Config Rules รันทุกวัน + Alert Slack/Email
- Test After Change: รันทดสอบ Connectivity ทุกครั้งที่แก้ SG (Manual หรือ Automated)
- Document & Review: Review SG ทุกไตรมาส ลบ Rule/Groups ที่ไม่ใช้