abdelatif2030/SecureCloud-Hardening
GitHub: abdelatif2030/SecureCloud-Hardening
基于PowerShell与AWS CLI的AWS EC2 Ubuntu实例生产级安全加固自动化方案。
Stars: 1 | Forks: 0
# 🔐 AWS EC2 安全加固项目
## 📋 目录
- [项目概述](#project-overview)
- [架构](#architecture)
- [前置条件](#prerequisites)
- [阶段 1 — 环境设置](#phase-1--environment-setup)
- [阶段 2 — 启动 EC2 实例](#phase-2--ec2-instance-launch)
- [阶段 3 — SSH 加固](#phase-3--ssh-hardening)
- [阶段 4 — UFW 防火墙](#phase-4--ufw-firewall)
- [阶段 5 — 监控与稳定性](#phase-5--monitoring--stability)
- [验证结果](#verification-results)
- [安全检查清单](#security-checklist)
- [使用的技术](#technologies-used)
## 项目概述
本项目演示了在 AWS 上对 Linux 服务器进行端到端安全加固的过程,涵盖:
- **SSH 加固** — 端口更改、仅密钥身份验证、Fail2ban 暴力破解保护
- **防火墙配置** — 包含默认拒绝入站、速率限制和 IP 阻止的 UFW
- **监控** — CloudWatch 代理发送 auth/syslog/UFW 日志,并配置 CPU 及状态警报
- **稳定性** — Swap 空间,自动安全更新
所有的配置与设置均是在 Windows 上通过 **PowerShell 7** 驱动,使用 **AWS CLI v2** 和 SSH 远程命令完成的——无需在控制台进行手动点击操作。
## 架构
```
┌─────────────────────────────────────────────────────┐
│ Windows Machine (Local) │
│ PowerShell 7+ ──► AWS CLI v2 ──► SSH Client │
└────────────────────────┬────────────────────────────┘
│ HTTPS / SSH (port 2222)
┌──────▼──────┐
│ Internet │
└──────┬──────┘
┌────────────▼────────────┐
│ AWS VPC / SG │
│ ┌─────────────────┐ │
│ │ EC2 Ubuntu │ │
│ │ t3.micro │ │
│ │ SSH :2222 │ │
│ │ UFW Firewall │ │
│ │ Fail2ban │ │
│ │ CloudWatch │ │
│ └─────────────────┘ │
└─────────────────────────┘
```
## 前置条件
| 工具 | 版本 | 用途 |
|------|---------|---------|
| PowerShell | 7+ | 主管理 Shell |
| AWS CLI | v2 | 配置 AWS 资源 |
| OpenSSH Client | 内置 | 连接到 EC2 |
```
# 安装 PowerShell 7
winget install --id Microsoft.PowerShell --source winget
# 安装 AWS CLI v2
Invoke-WebRequest -Uri https://awscli.amazonaws.com/AWSCLIV2.msi -OutFile AWSCLIV2.msi
Start-Process msiexec.exe -ArgumentList '/i AWSCLIV2.msi /quiet' -Wait
# 启用 OpenSSH Client (以管理员身份)
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
```
## 阶段 1 — 环境设置
配置 AWS 凭证:
```
aws configure
# AWS Access Key ID:
# AWS Secret Access Key:
# Default region name: us-east-1
# Default output format: json
# 验证
aws sts get-caller-identity
```
## 阶段 2 — 启动 EC2 实例
```
# 1. 创建密钥对
$keyName = 'hardening-key'
aws ec2 create-key-pair --key-name $keyName --query 'KeyMaterial' --output text `
| Out-File -Encoding ascii .\$keyName.pem
icacls .\$keyName.pem /inheritance:r /grant:r "$($env:USERNAME):R"
# 2. 创建安全组 (仅限您的 IP 访问 SSH)
$myIp = (Invoke-RestMethod https://checkip.amazonaws.com).Trim()
$vpcId = (aws ec2 describe-vpcs --query 'Vpcs[0].VpcId' --output text)
$sgId = (aws ec2 create-security-group --group-name 'hardening-sg' `
--description 'Hardened EC2 SG' --vpc-id $vpcId --query 'GroupId' --output text)
aws ec2 authorize-security-group-ingress --group-id $sgId `
--protocol tcp --port 22 --cidr "$myIp/32"
# 3. 启动 Ubuntu 22.04 实例
$amiId = (aws ec2 describe-images --owners 099720109477 `
--filters 'Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64*' `
'Name=state,Values=available' `
--query 'sort_by(Images,&CreationDate)[-1].ImageId' --output text)
$instanceId = (aws ec2 run-instances --image-id $amiId --instance-type t3.micro `
--key-name hardening-key --security-group-ids $sgId `
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=hardened-server}]' `
--query 'Instances[0].InstanceId' --output text)
aws ec2 wait instance-running --instance-ids $instanceId
$publicIp = (aws ec2 describe-instances --instance-ids $instanceId `
--query 'Reservations[0].Instances[0].PublicIpAddress' --output text)
```
## 阶段 3 — SSH 加固
应用到 `/etc/ssh/sshd_config` 的关键加固设置:
| 设置 | 值 | 用途 |
|---------|-------|---------|
| `Port` | `2222` | 非默认端口 |
| `PermitRootLogin` | `no` | 阻止 root 访问 |
| `PasswordAuthentication` | `no` | 仅限密钥登录 |
| `LoginGraceTime` | `30` | 限制登录窗口时间 |
| `MaxAuthTries` | `3` | 限制重试次数 |
| `X11Forwarding` | `no` | 禁用 X11 |
| `LogLevel` | `VERBOSE` | 详细的审计跟踪 |
```
# 远程推送加固配置,更新 SG 端口,安装 Fail2ban
ssh -i .\hardening-key.pem ubuntu@$publicIp 'sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup'
# 应用配置后,更新安全组
aws ec2 authorize-security-group-ingress --group-id $sgId --protocol tcp --port 2222 --cidr "$myIp/32"
aws ec2 revoke-security-group-ingress --group-id $sgId --protocol tcp --port 22 --cidr "$myIp/32"
```
**Fail2ban 配置** (`/etc/fail2ban/jail.local`):
```
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3
[sshd]
enabled = true
port = 2222
```
## 阶段 4 — UFW 防火墙
```
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 2222/tcp comment 'SSH Hardened'
sudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'
sudo ufw limit 2222/tcp comment 'SSH Rate Limit'
sudo ufw logging on
sudo ufw --force enable
```
**从运行中的实例验证 UFW 状态:**
```
Status: active
Logging: on (medium)
Default: deny (incoming), allow (outgoing), disabled (routed)
To Action From
-- ------ ----
2222/tcp LIMIT IN Anywhere # SSH Rate Limit
80/tcp ALLOW IN Anywhere # HTTP
443/tcp ALLOW IN Anywhere # HTTPS
```
## 阶段 5 — 监控与稳定性
### CloudWatch 代理
收集并发送至 AWS CloudWatch:
- `/var/log/auth.log` → 日志组 `/ec2/auth`
- `/var/log/syslog` → 日志组 `/ec2/syslog`
- `/var/log/ufw.log` → 日志组 `/ec2/ufw`
- 指标:CPU、内存、磁盘
### CloudWatch 警报(已验证激活)
| 警报 | 指标 | 阈值 | 状态 |
|-------|--------|-----------|-------|
| High-CPU | CPUUtilization | > 80% 持续 10 分钟 | ✅ OK |
| Instance-Status-Check | StatusCheckFailed | ≥ 1 持续 3 分钟 | ✅ OK |
### Swap 空间
```
Swap: 2.0Gi total, 0B used, 2.0Gi free
```
### 自动安全更新
```
sudo apt install -y unattended-upgrades
# 安全补丁自动重启计划于 UTC 时间 03:00
```
## 验证结果
从 PowerShell 执行的完整审计——所有检查均已通过:
```
[SSH port changed]: LISTEN 0 128 0.0.0.0:2222 (sshd,pid=2243)
[Root login disabled]: PermitRootLogin no
[Password auth off]: PasswordAuthentication no
[UFW enabled]: Status: active
[Fail2ban running]: active (1 jail: sshd)
[CW agent running]: active (running) since 2026-05-08
[Auto-updates on]: active (running) since 2026-05-08
```
每次 SSH 连接时显示的登录横幅:
```
**********************************************
* AUTHORIZED ACCESS ONLY *
* All activity is monitored and logged. *
**********************************************
```
## 安全检查清单
- [x] SSH 默认端口 22 已关闭
- [x] SSH 正在端口 2222 上运行
- [x] Root SSH 登录已禁用
- [x] 密码身份验证已禁用(仅限密钥)
- [x] SSH 登录宽限时间已限制(30秒)
- [x] 最大身份验证尝试次数已限制(3次)
- [x] 登录警告横幅已配置
- [x] Fail2ban 已激活(封禁 1 小时,3 次重试)
- [x] UFW 默认拒绝入站流量
- [x] SSH 上的 UFW 速率限制
- [x] UFW 日志已启用(medium)
- [x] CloudWatch 代理正在发送日志
- [x] CPU 警报已配置
- [x] 状态检查警报已配置
- [x] 自动安全更新已启用
- [x] 已配置 2GB Swap 空间
- [x] 包含 CloudWatchAgentServerPolicy 的 EC2 IAM 角色
## 使用的技术





| 层级 | 技术 |
|-------|-----------|
| 云服务提供商 | AWS (EC2, CloudWatch, IAM, VPC) |
| 操作系统 | Ubuntu 22.04.5 LTS |
| 管理 Shell | PowerShell 7 + AWS CLI v2 |
| SSH 守护进程 | 采用加固配置的 OpenSSH |
| 防火墙 | UFW (Uncomplicated Firewall) |
| 入侵检测 | Fail2ban |
| 监控 | Amazon CloudWatch Agent |
| 自动补丁更新 | unattended-upgrades |
## 👤 作者
**Abdelatef Mohamed**
DevOps 工程师
[](https://github.com/abdelatif2030)
标签:AI合规, Awesome, AWS, AWS CLI, CloudWatch监控, DevSecOps, DPI, EC2, Fail2ban, GitHub Advanced Security, IaC, IPv6, OISF, PowerShell, SSH安全, Ubuntu 22.04, UFW防火墙, 上游代理, 企业级安全, 动态API解析, 安全加固, 密钥认证, 日志管理, 服务器配置, 系统管理, 网络安全, 自动化运维, 虚拟防火墙, 警报配置, 运维脚本, 防暴力破解, 隐私保护