fix_nlb_security_groups.sh
3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
# NLB 安全组修复脚本
# 确保 NLB 安全组只允许 VPC 内部访问
set -e
echo "=== NLB 安全组修复脚本 ==="
# VPC CIDR
VPC_CIDR="10.0.0.0/16"
echo "VPC CIDR: $VPC_CIDR"
# 需要修复的安全组列表
SG_IDS=("sg-041e4cdf3325feef6" "sg-04a74a6ad79bc99e8")
# 需要修复的端口
PORTS=(80 443)
for SG_ID in "${SG_IDS[@]}"; do
    echo ""
    echo "=== 处理安全组: $SG_ID ==="
    
    # 获取安全组名称
    SG_NAME=$(aws ec2 describe-security-groups \
        --group-ids $SG_ID \
        --query 'SecurityGroups[0].GroupName' \
        --output text)
    echo "安全组名称: $SG_NAME"
    
    # 检查并修复每个端口的规则
    for PORT in "${PORTS[@]}"; do
        echo "检查端口 $PORT 的规则..."
        
        # 检查是否存在公网访问规则 (0.0.0.0/0)
        PUBLIC_RULE_EXISTS=$(aws ec2 describe-security-groups \
            --group-ids $SG_ID \
            --query "SecurityGroups[0].IpPermissions[?ToPort==\`$PORT\` && FromPort==\`$PORT\` && IpProtocol==\`tcp\`].IpRanges[?CidrIp==\`0.0.0.0/0\`]" \
            --output text)
        
        # 检查是否存在 VPC 内网访问规则
        VPC_RULE_EXISTS=$(aws ec2 describe-security-groups \
            --group-ids $SG_ID \
            --query "SecurityGroups[0].IpPermissions[?ToPort==\`$PORT\` && FromPort==\`$PORT\` && IpProtocol==\`tcp\`].IpRanges[?CidrIp==\`$VPC_CIDR\`]" \
            --output text)
        
        # 如果存在公网规则,删除它
        if [ ! -z "$PUBLIC_RULE_EXISTS" ]; then
            echo "⚠️  发现公网访问规则,删除端口 $PORT 的公网访问权限..."
            aws ec2 revoke-security-group-ingress \
                --group-id $SG_ID \
                --protocol tcp \
                --port $PORT \
                --cidr 0.0.0.0/0
            echo "✅ 已删除端口 $PORT 的公网访问规则"
        else
            echo "✅ 端口 $PORT 没有公网访问规则"
        fi
        
        # 如果不存在 VPC 内网规则,添加它
        if [ -z "$VPC_RULE_EXISTS" ]; then
            echo "➡️  添加端口 $PORT 的 VPC 内网访问规则..."
            aws ec2 authorize-security-group-ingress \
                --group-id $SG_ID \
                --protocol tcp \
                --port $PORT \
                --cidr $VPC_CIDR
            echo "✅ 已添加端口 $PORT 的 VPC 内网访问规则"
        else
            echo "✅ 端口 $PORT 已有 VPC 内网访问规则"
        fi
    done
done
echo ""
echo "=== 修复完成,验证安全组规则 ==="
# 验证修复结果
for SG_ID in "${SG_IDS[@]}"; do
    echo ""
    echo "安全组: $SG_ID"
    echo "------------------------------------------------------------"
    
    # 获取安全组详细信息
    SG_NAME=$(aws ec2 describe-security-groups \
        --group-ids $SG_ID \
        --query 'SecurityGroups[0].GroupName' \
        --output text)
    
    echo "安全组名称: $SG_NAME"
    echo ""
    
    # 显示入站规则
    echo "入站规则:"
    aws ec2 describe-security-groups \
        --group-ids $SG_ID \
        --query 'SecurityGroups[0].IpPermissions[?ToPort==`80` || ToPort==`443`]' \
        --output table
done
echo ""
echo "=== 安全组修复总结 ==="
echo "✅ 所有 NLB 安全组已修复为只允许 VPC 内部访问"
echo "✅ 已移除所有公网访问规则 (0.0.0.0/0)"
echo "✅ 已确保所有必要端口 (80, 443) 都有 VPC 内网访问规则"
echo ""
echo "当前配置符合 Internal NLB 的安全最佳实践"