跳转到内容

SSL 和安全基础

SSL 和安全基础

本节介绍如何为 IoT 基础设施配置基础安全措施,包括 SSL/TLS 加密、密码保护和基本防火墙规则。学习完成后,您将能够:

  • 理解 SSL/TLS 在 IoT 通信中的作用
  • 配置 MQTT 和 Web 服务的基本安全保护
  • 向客户解释安全配置的必要性
  • 使用 Let’s Encrypt 获取免费 SSL 证书
公网 IoT 服务器的常见风险:
攻击者
┌───────┴───────┐
│ 端口扫描 │
└───────┬───────┘
┌───────────────────┼───────────────────┐
│ │ │
┌───┴───┐ ┌───┴───┐ ┌───┴───┐
│ MQTT │ │ Node- │ │ 其他 │
│(1883) │ │ RED │ │端口 │
│ 未加密 │ │(1880) │ │ │
│ 无密码 │ │ 无认证 │ │ │
└───────┘ └───────┘ └───────┘
安全措施优先级说明
MQTT 密码认证⭐⭐⭐防止未授权设备连接
MQTT TLS 加密⭐⭐⭐防止数据被窃听
Node-RED 登录认证⭐⭐⭐防止未授权访问
防火墙配置⭐⭐⭐限制不必要的端口
强密码策略⭐⭐防止暴力破解
定期更新⭐⭐修复已知漏洞
Terminal window
# 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 true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
listener 1883
allow_anonymous false
password_file /mosquitto/config/password.txt
listener 9001
protocol websockets
allow_anonymous false
password_file /mosquitto/config/password.txt
EOF
# 4. 重启 Mosquitto
docker restart mosquitto
Terminal window
# 1. 生成密码哈希
docker exec -it nodered sh
node -e "console.log(require('bcryptjs').hashSync('YourPassword2024!', 8));"
# 复制输出的哈希值
# 2. 编辑 settings.js
docker exec -it nodered sh
nano /data/settings.js
# 3. 找到并修改 authentication 部分
adminAuth: {
type: "credentials",
users: [{
username: "admin",
password: "$2a$08$...", // 替换为生成的哈希
permissions: "*"
}]
}
# 4. 重启 Node-RED
docker restart nodered
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-key
Terminal window
# 1. 安装 Certbot
sudo apt-get update
sudo 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(测试用)”
Terminal window
# 创建证书目录
mkdir -p mosquitto/certs
# 生成 CA 证书
openssl genrsa -out mosquitto/certs/ca.key 2048
openssl 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 2048
openssl 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.crt
mosquitto/config/mosquitto.conf
# 完整安全配置
# MQTT 默认端口(无加密,仅内网)
listener 1883
allow_anonymous false
password_file /mosquitto/config/password.txt
# MQTT TLS 端口
listener 8883
allow_anonymous false
password_file /mosquitto/config/password.txt
# TLS 证书配置
cafile /mosquitto/certs/ca.crt
certfile /mosquitto/certs/server.crt
keyfile /mosquitto/certs/server.key
tls_version tlsv1.2
# WebSocket TLS 端口
listener 9883
protocol websockets
allow_anonymous false
password_file /mosquitto/config/password.txt
cafile /mosquitto/certs/ca.crt
certfile /mosquitto/certs/server.crt
keyfile /mosquitto/certs/server.key
Terminal window
# 使用 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!" \
-d
Terminal window
# 安装 UFW
sudo apt-get install -y ufw
# 默认策略
sudo ufw default deny incoming
sudo 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 enable
sudo ufw status verbose
Terminal window
# 更精细的 IPTABLES 规则
sudo iptables -A INPUT -p tcp --dport 22 -s 192.168.0.0/16 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 1883 -s 0.0.0.0/0 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 8883 -s 0.0.0.0/0 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 1880 -s 192.168.0.0/16 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 3000 -s 192.168.0.0/16 -j ACCEPT
sudo iptables -A INPUT -j DROP
# 保存规则
sudo apt-get install -y iptables-persistent
sudo netfilter-persistent save
  • MQTT 密码认证已启用
  • Node-RED 管理密码已设置
  • SSH 使用密钥认证(不是密码)
  • 防火墙已启用并正确配置
  • Docker 引擎保持更新
  • 服务使用指定版本(非 latest)
  • TLS 证书已配置
  • MQTT 端口 8883 启用 TLS
  • Web 界面仅限内网访问
  • fail2ban 已安装并配置
  • 日志监控已设置
  • 定期安全更新计划

Q1: 自签名证书可以用于生产吗?

Section titled “Q1: 自签名证书可以用于生产吗?”

A: 自签名证书适合测试和内部网络使用。生产环境建议使用 Let’s Encrypt 或其他受信任 CA 签发的证书。

A: TLS 加密会有约 5-10% 的性能开销,但对于 IoT 场景(小数据包、低频通信)影响可以忽略不计。

A: 可以使用私有 CA,所有设备信任同一个 CA 证书,然后为每个设备签发单独的证书。

推荐做法:

  • 始终为公网服务启用密码认证
  • 使用 TLS 加密 MQTT 通信
  • 遵循最小权限原则开放端口
  • 定期更新所有容器镜像
  • 使用强密码(12+ 位,包含特殊字符)

避免做法:

  • 在启用密码前暴露服务到公网
  • 使用默认或弱密码
  • 开放不必要的端口
  • 忽略安全更新
  • 在非加密通道传输敏感数据
  1. 安全是 IoT 部署的首要前提
  2. MQTT 密码认证是必须的最低安全措施
  3. TLS 加密保护数据在传输中的安全
  4. 防火墙限制不必要的网络访问
  5. 定期安全检查和更新维护安全基线

正在开发商业 IoT 产品?

我们提供 ESP32 ODM 定制设计与制造服务。从原型到量产——编写这套教程的团队,可以和你一起实现。

联系我们 →