SSL 和安全基础
SSL 和安全基础
本节介绍如何为 IoT 基础设施配置基础安全措施,包括 SSL/TLS 加密、密码保护和基本防火墙规则。学习完成后,您将能够:
- 理解 SSL/TLS 在 IoT 通信中的作用
- 配置 MQTT 和 Web 服务的基本安全保护
- 向客户解释安全配置的必要性
- 使用 Let’s Encrypt 获取免费 SSL 证书
Why Security Matters in IoT
Section titled “Why Security Matters in IoT”安全风险分析
Section titled “安全风险分析”公网 IoT 服务器的常见风险:
攻击者 │ ┌───────┴───────┐ │ 端口扫描 │ └───────┬───────┘ │ ┌───────────────────┼───────────────────┐ │ │ │ ┌───┴───┐ ┌───┴───┐ ┌───┴───┐ │ MQTT │ │ Node- │ │ 其他 │ │(1883) │ │ RED │ │端口 │ │ 未加密 │ │(1880) │ │ │ │ 无密码 │ │ 无认证 │ │ │ └───────┘ └───────┘ └───────┘必须实施的安全措施
Section titled “必须实施的安全措施”| 安全措施 | 优先级 | 说明 |
|---|---|---|
| MQTT 密码认证 | ⭐⭐⭐ | 防止未授权设备连接 |
| MQTT TLS 加密 | ⭐⭐⭐ | 防止数据被窃听 |
| Node-RED 登录认证 | ⭐⭐⭐ | 防止未授权访问 |
| 防火墙配置 | ⭐⭐⭐ | 限制不必要的端口 |
| 强密码策略 | ⭐⭐ | 防止暴力破解 |
| 定期更新 | ⭐⭐ | 修复已知漏洞 |
Basic Password Authentication
Section titled “Basic Password Authentication”MQTT Mosquitto 密码配置
Section titled “MQTT Mosquitto 密码配置”# 1. 进入 Mosquitto 容器docker exec -it mosquitto sh
# 2. 创建密码文件mosquitto_passwd -c /mosquitto/config/password.txt iot_user# 输入密码: YourPassword2024!
# 3. 修改配置文件cat > /mosquitto/config/mosquitto.conf << 'EOF'persistence truepersistence_location /mosquitto/data/log_dest file /mosquitto/log/mosquitto.log
listener 1883allow_anonymous falsepassword_file /mosquitto/config/password.txt
listener 9001protocol websocketsallow_anonymous falsepassword_file /mosquitto/config/password.txtEOF
# 4. 重启 Mosquittodocker restart mosquittoNode-RED 登录认证
Section titled “Node-RED 登录认证”# 1. 生成密码哈希docker exec -it nodered shnode -e "console.log(require('bcryptjs').hashSync('YourPassword2024!', 8));"# 复制输出的哈希值
# 2. 编辑 settings.jsdocker exec -it nodered shnano /data/settings.js
# 3. 找到并修改 authentication 部分adminAuth: { type: "credentials", users: [{ username: "admin", password: "$2a$08$...", // 替换为生成的哈希 permissions: "*" }]}
# 4. 重启 Node-REDdocker restart noderedDocker Compose 集成
Section titled “Docker Compose 集成”version: '3.8'
services: mosquitto: image: eclipse-mosquitto:2 container_name: mosquitto restart: unless-stopped ports: - "1883:1883" # MQTT 加密端口 - "8883:8883" # MQTT TLS 端口 - "9001:9001" # WebSocket volumes: - ./mosquitto/config:/mosquitto/config - ./mosquitto/data:/mosquitto/data - ./mosquitto/log:/mosquitto/log - ./mosquitto/certs:/mosquitto/certs
nodered: image: nodered/node-red:latest container_name: nodered restart: unless-stopped ports: - "1880:1880" volumes: - ./nodered/data:/data - ./nodered/certs:/certs environment: - NODE_RED_CREDENTIAL_SECRET=your-secret-keySSL/TLS Certificate Setup
Section titled “SSL/TLS Certificate Setup”Using Let’s Encrypt (Auto)
Section titled “Using Let’s Encrypt (Auto)”# 1. 安装 Certbotsudo apt-get updatesudo apt-get install -y certbot
# 2. 获取证书(需要域名)sudo certbot certonly --standalone \ -d your-domain.com \ -d mqtt.your-domain.com \ --email your@email.com \ --agree-tos
# 证书存储在:# /etc/letsencrypt/live/your-domain.com/# ├── fullchain.pem # 完整证书链# └── privkey.pem # 私钥Manual Self-Signed Certificate(测试用)
Section titled “Manual Self-Signed Certificate(测试用)”# 创建证书目录mkdir -p mosquitto/certs
# 生成 CA 证书openssl genrsa -out mosquitto/certs/ca.key 2048openssl req -new -x509 -days 365 \ -key mosquitto/certs/ca.key \ -out mosquitto/certs/ca.crt \ -subj "/C=CN/ST=Shanghai/L=Shanghai/O=IoT Demo/CN=IoT CA"
# 生成服务器证书openssl genrsa -out mosquitto/certs/server.key 2048openssl req -new \ -key mosquitto/certs/server.key \ -out mosquitto/certs/server.csr \ -subj "/C=CN/ST=Shanghai/L=Shanghai/O=IoT Demo/CN=iot-server"
# 签名证书openssl x509 -req -days 365 \ -in mosquitto/certs/server.csr \ -CA mosquitto/certs/ca.crt \ -CAkey mosquitto/certs/ca.key \ -CAcreateserial \ -out mosquitto/certs/server.crtMosquitto TLS Configuration
Section titled “Mosquitto TLS Configuration”Configuring TLS
Section titled “Configuring TLS”# 完整安全配置
# MQTT 默认端口(无加密,仅内网)listener 1883allow_anonymous falsepassword_file /mosquitto/config/password.txt
# MQTT TLS 端口listener 8883allow_anonymous falsepassword_file /mosquitto/config/password.txt
# TLS 证书配置cafile /mosquitto/certs/ca.crtcertfile /mosquitto/certs/server.crtkeyfile /mosquitto/certs/server.keytls_version tlsv1.2
# WebSocket TLS 端口listener 9883protocol websocketsallow_anonymous falsepassword_file /mosquitto/config/password.txtcafile /mosquitto/certs/ca.crtcertfile /mosquitto/certs/server.crtkeyfile /mosquitto/certs/server.keyTesting TLS Connection
Section titled “Testing TLS Connection”# 使用 mosquitto_sub 测试 TLS 连接mosquitto_sub \ -h your-domain.com \ -p 8883 \ --cafile ca.crt \ -t "test/topic" \ -u "iot_user" \ -P "YourPassword2024!" \ -d
# 使用 mosquitto_pub 发送加密消息mosquitto_pub \ -h your-domain.com \ -p 8883 \ --cafile ca.crt \ -t "test/topic" \ -m "{\"temperature\": 25.5}" \ -u "iot_user" \ -P "YourPassword2024!" \ -dFirewall Configuration
Section titled “Firewall Configuration”UFW (Uncomplicated Firewall)
Section titled “UFW (Uncomplicated Firewall)”# 安装 UFWsudo apt-get install -y ufw
# 默认策略sudo ufw default deny incomingsudo ufw default allow outgoing
# SSH 访问(必须保留)sudo ufw allow 22/tcp comment 'SSH'
# IoT 服务端口sudo ufw allow 1883/tcp comment 'MQTT'sudo ufw allow 8883/tcp comment 'MQTT TLS'sudo ufw allow 1880/tcp comment 'Node-RED'sudo ufw allow 8086/tcp comment 'InfluxDB'sudo ufw allow 3000/tcp comment 'Grafana'sudo ufw allow 9000/tcp comment 'Portainer'
# 限制访问源(推荐)sudo ufw allow from 192.168.0.0/16 to any port 1880 comment 'Node-RED internal'sudo ufw allow from 192.168.0.0/16 to any port 3000 comment 'Grafana internal'
# 启用防火墙sudo ufw enablesudo ufw status verboseIP Tables Rules
Section titled “IP Tables Rules”# 更精细的 IPTABLES 规则sudo iptables -A INPUT -p tcp --dport 22 -s 192.168.0.0/16 -j ACCEPTsudo iptables -A INPUT -p tcp --dport 1883 -s 0.0.0.0/0 -j ACCEPTsudo iptables -A INPUT -p tcp --dport 8883 -s 0.0.0.0/0 -j ACCEPTsudo iptables -A INPUT -p tcp --dport 1880 -s 192.168.0.0/16 -j ACCEPTsudo iptables -A INPUT -p tcp --dport 3000 -s 192.168.0.0/16 -j ACCEPTsudo iptables -A INPUT -j DROP
# 保存规则sudo apt-get install -y iptables-persistentsudo netfilter-persistent saveSecurity Checklist
Section titled “Security Checklist”基础安全检查
Section titled “基础安全检查”- MQTT 密码认证已启用
- Node-RED 管理密码已设置
- SSH 使用密钥认证(不是密码)
- 防火墙已启用并正确配置
- Docker 引擎保持更新
- 服务使用指定版本(非 latest)
高级安全配置
Section titled “高级安全配置”- TLS 证书已配置
- MQTT 端口 8883 启用 TLS
- Web 界面仅限内网访问
- fail2ban 已安装并配置
- 日志监控已设置
- 定期安全更新计划
Common Customer Questions
Section titled “Common Customer Questions”Q1: 自签名证书可以用于生产吗?
Section titled “Q1: 自签名证书可以用于生产吗?”A: 自签名证书适合测试和内部网络使用。生产环境建议使用 Let’s Encrypt 或其他受信任 CA 签发的证书。
Q2: MQTT 加密会影响性能吗?
Section titled “Q2: MQTT 加密会影响性能吗?”A: TLS 加密会有约 5-10% 的性能开销,但对于 IoT 场景(小数据包、低频通信)影响可以忽略不计。
Q3: 如何管理多个设备的证书?
Section titled “Q3: 如何管理多个设备的证书?”A: 可以使用私有 CA,所有设备信任同一个 CA 证书,然后为每个设备签发单独的证书。
✅ 推荐做法:
- 始终为公网服务启用密码认证
- 使用 TLS 加密 MQTT 通信
- 遵循最小权限原则开放端口
- 定期更新所有容器镜像
- 使用强密码(12+ 位,包含特殊字符)
❌ 避免做法:
- 在启用密码前暴露服务到公网
- 使用默认或弱密码
- 开放不必要的端口
- 忽略安全更新
- 在非加密通道传输敏感数据
Summary
Section titled “Summary”- 安全是 IoT 部署的首要前提
- MQTT 密码认证是必须的最低安全措施
- TLS 加密保护数据在传输中的安全
- 防火墙限制不必要的网络访问
- 定期安全检查和更新维护安全基线
References
Section titled “References”
正在开发商业 IoT 产品?
我们提供 ESP32 ODM 定制设计与制造服务。从原型到量产——编写这套教程的团队,可以和你一起实现。
联系我们 →