如何使用aliyun-cli进行服务器的安全组配置?
美国、香港服务器
如何使用aliyun-cli进行服务器的安全组配置?
06-05 来源:
前置:已装好 aliyun-cli、配置完 RAM 子账号 AK、aliyun configure配置完成
固定规范:
优先级:1~20 黑名单拒绝|21~40 运维白名单|41~70 内网业务|71~99 公网业务
入站ingress、出站egress;放行Allow、拉黑Refuse
一、环境变量简写(替换成你自己参数)
bash
运行
# 改成你的参数
export REGION=cn-shanghai
export SG_ID=sg-xxxxxxxx
export ADMIN_IP=110.xx.xx.xx/32 # 你本地公网IP
export INNER_CIDR=192.168.0.0/16 # VPC内网段
二、查看安全组现有规则(加固第一步,先导出核对)
bash
运行
# 查看入方向规则
aliyun ecs DescribeSecurityGroupAttribute \
--RegionId $REGION \
--SecurityGroupId $SG_ID \
--Direction ingress
# 查看出方向规则
aliyun ecs DescribeSecurityGroupAttribute \
--RegionId $REGION \
--SecurityGroupId $SG_ID \
--Direction egress
输出里PermitId:sgperm-xxx是规则 ID,删除规则需要。
三、删除高危旧规则(查到高危就删)
bash
运行
aliyun ecs RevokeSecurityGroup \
--RegionId $REGION \
--SecurityGroupId $SG_ID \
--PermitId sgperm-xxxx
需要删掉:0.0.0.0/0放开3389/22/1433/445、全端口-1/-1全网放行规则。
四、CLI 批量新建加固规则
1、黑名单:恶意 IP 全端口拒绝(优先级 5,1~20 区间)
bash
运行
# BLOCK_IP替换攻击IP
BLOCK_IP=185.xx.xx.xx/32
aliyun ecs AuthorizeSecurityGroup \
--RegionId $REGION \
--SecurityGroupId $SG_ID \
--IpProtocol all \
--PortRange -1/-1 \
--SourceCidrIp $BLOCK_IP \
--Policy Refuse \
--Priority 5 \
--Description "黑名单拦截扫描IP"
2、运维放行:Windows3389 仅自己公网 IP(优先级 30)
bash
运行
aliyun ecs AuthorizeSecurityGroup \
--RegionId $REGION \
--SecurityGroupId $SG_ID \
--IpProtocol tcp \
--PortRange 3389/3389 \
--SourceCidrIp $ADMIN_IP \
--Policy Allow \
--Priority 30 \
--Description "仅自用IP远程RDP"
3、内网数据库 1433,只放行内网(优先级 55)
bash
运行
aliyun ecs AuthorizeSecurityGroup \
--RegionId $REGION \
--SecurityGroupId $SG_ID \
--IpProtocol tcp \
--PortRange 1433/1433 \
--SourceCidrIp $INNER_CIDR \
--Policy Allow \
--Priority 55 \
--Description "内网访问SQL1433"
4、公网 80/443 全网放行(优先级 80、85)
bash
运行
# 80
aliyun ecs AuthorizeSecurityGroup \
--RegionId $REGION \
--SecurityGroupId $SG_ID \
--IpProtocol tcp \
--PortRange 80/80 \
--SourceCidrIp 0.0.0.0/0 \
--Policy Allow \
--Priority 80
# 443
aliyun ecs AuthorizeSecurityGroup \
--RegionId $REGION \
--SecurityGroupId $SG_ID \
--IpProtocol tcp \
--PortRange 443/443 \
--SourceCidrIp 0.0.0.0/0 \
--Policy Allow \
--Priority 85
五、出站规则配置(默认全放行)
bash
运行
aliyun ecs AuthorizeSecurityGroupEgress \
--RegionId $REGION \
--SecurityGroupId $SG_ID \
--IpProtocol all \
--PortRange -1/-1 \
--DestCidrIp 0.0.0.0/0 \
--Policy Allow \
--Priority 100
六、常用运维指令
临时拉黑新攻击 IP
bash
运行
NEW_ATTACK_IP=xxx.xxx.xxx.xxx/32
aliyun ecs AuthorizeSecurityGroup --RegionId $REGION --SecurityGroupId $SG_ID --IpProtocol all --PortRange -1/-1 --SourceCidrIp $NEW_ATTACK_IP --Policy Refuse --Priority 10
修改已有规则 IP / 优先级(需要 PermitId)
bash
运行
aliyun ecs ModifySecurityGroupRule \
--RegionId $REGION \
--SecurityGroupId $SG_ID \
--PermitId sgperm-xxx \
--SourceCidrIp 新IP/32 \
--Priority 32
七、加固完成复核
bash
运行
aliyun ecs DescribeSecurityGroupAttribute --RegionId $REGION --SecurityGroupId $SG_ID --Direction ingress > sg_rule_back.json
导出规则备份留存。
三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快