Margrop
Articles193
Tags384
Categories23
1password AC AI AP API AppDaemon Aqara Caddy Cookie 认证 Cron Date Diagrams.net Docker HA HADashboard HomeAssistant IP IPv4 Java LVM‑Thin Linux MacOS Markdown MiniMax MySQL NAS Nginx OpenAI OpenClaw OpenResty PPPoE PostgreSQL ProcessOn Prometheus Proxmox VE SOCKS5 SSL Shell TTS TimeMachine UML Uptime Kuma VPN VPS Web Windows activate ad adb adblock agent aligenie aliyun alpine annotation aop authy autofs backup baidupan bash bitwarden boot brew browser caddy2 cdn centos cert certbot charles chat chrome classloader client clone closures cloudflare cmd command commit container crontab ctyun ddsm demo dependency deploy developer devtools dll dns docker domain download draw drawio dsm dump dylib edge exception export fail2ban feign firewall-cmd flow frp frpc frps fuckgfw function gcc gfw git github golang gperftools gridea grub gvt-g hacs havcs heap hello hexo hibernate hidpi hoisting homeassistant hosts html htmlparser https idea image img img2kvm import index install intel io ios ip iptables iptv ipv6 iso java javascript jetbrains jni jnilib jpa js json jsonb jupter jupyterlab jvm k8s kernel key kid kms kodi koolproxy koolproxyr kvm lan lastpass launchctl learning lede letsencrypt linux live low-code lvm lxc m3u8 mac macos mariadb markdown maven md5 microcode mirror modem modules monitor mount mstsc mysql n2n n5105 nas network nfs node node-red nodejs nohup notepad++ npm nssm ntp oop openfeign openssl os otp ovz packet capture pat pdf pem perf ping pip plugin png powerbutton print pro proxy pve pvekclean python qcow2 qemu qemu-guest-agent rar reboot reflog remote remote desktop renew repo resize retina root route router rule rules runtime safari sata scipy-notebook scoping scp server slmgr so socks source spk spring springboot springfox ssh ssl stash string supernode svg svn swagger sync synology systemctl tap tap-windows tapwindows telecom template terminal tls token totp tvbox txt ubuntu udisk ui undertow uninstall unlocker upgrade url v2ray vhd vim vlmcsd vm vmdk web websocket wechat windows with worker wow xiaoya xml yum zip 中国电信 云电脑 交换机 健康检查 光猫 公网IP 内存 内网IP 升级 反向代理 启动 夏令时 天猫精灵 天翼云 安全 安装 容器 导入 小米 常用软件 广告屏蔽 序列号 应用市场 异常 打工 技术 抓包 描述文件 效率工具 日记 时区 显卡虚拟化 智能家居 智能音箱 梯子 模块 流程 流程图 浏览器 漫游 激活 火绒 电信 画图 监控 直播源 端口扫描 续期 网关 网络 网络风暴 群晖 脚本 腾讯 自动化 虚拟机 证书 语雀 路由 路由器 软件管家 软路由 运维 运维监控 配置 钉钉 镜像 镜像源 门窗传感器 问题排查 防火墙 阿里云 阿里源 集客

Hitokoto

Archive

服务器端口安全扫描与加固实战:从发现到修复的完整流程

服务器端口安全扫描与加固实战:从发现到修复的完整流程

前言

在运维工作中,服务器端口安全是一个容易被忽视但又至关重要的问题。很多安全事件都是从一个小小的端口暴露开始的。本文将详细介绍如何进行服务器端口安全扫描、识别潜在风险并进行修复的全流程,帮助你构建更安全的服务器环境。

问题背景

业务场景

我们管理着多台服务器,包括虚拟机、物理机和云服务器。这些服务器上运行着各种服务,如 OpenClaw Gateway、Prometheus 监控、Docker 容器管理等。

常见风险

在实际排查中,我们发现了以下常见的安全风险:

  1. 不必要的端口暴露:测试服务、公网服务未及时关闭
  2. 端口绑定错误:Docker 端口绑定到 0.0.0.0 导致公网可访问
  3. 配置文件泄露:测试环境的 API Key 残留在配置文件中
  4. 服务版本混乱:新旧版本同时运行,导致管理复杂

环境信息

  • 服务器类型:VM虚拟机、物理机、VPS
  • 操作系统:Ubuntu 24.04、CentOS
  • 网络类型:内网 + 公网混合部署
  • 管理工具:Docker、Prometheus、OpenClaw

排查方法论

第一步:端口清单收集

首先需要了解每台服务器上开放了哪些端口:

1
2
3
4
5
6
7
8
9
10
11
# 查看所有监听端口
ss -tlnp

# 或者使用 netstat
netstat -tlnp

# 查看 Docker 端口映射
docker port $(docker ps -q)

# 查看所有进程及端口
ps aux | grep -E '(java|node|python|docker)' | awk '{print $2}' | xargs -I {} ls -l /proc/{}/fd | grep socket

第二步:识别服务类型

对于发现的每个端口,需要识别它属于哪个服务:

1
2
3
4
5
6
7
8
# 通过端口反查服务
lsof -i :端口号

# 或者
ss -tlnp | grep 端口号

# 查看服务配置文件路径
docker inspect 容器名 | grep -E 'ExposedPorts|HostPort'

第三步:风险评估

对每个端口进行风险评估:

端口 服务 风险等级 说明
22 SSH 需强密码或密钥认证
80/443 Web 需确保非敏感服务
18789 Gateway 内部服务,严禁公网
9000 Portainer 极高 必须绑定本地
3306 MySQL 极高 严禁公网

第四步:安全修复

根据评估结果进行修复:

1. Docker 端口绑定修复

1
2
3
4
5
6
7
8
9
10
11
# 停止容器
docker stop <container_name>

# 删除容器
docker rm <container_name>

# 重新启动,绑定到 127.0.0.1
docker run -d \
--name <container_name> \
-p 127.0.0.1:9000:9000 \
<image_name>

2. 防火墙规则配置

1
2
3
4
5
6
7
8
9
10
# 允许内网访问 SSH
iptables -A INPUT -p tcp --dport 22 -s 192.168.0.0/16 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j DROP

# 允许内网访问管理端口
iptables -A INPUT -p tcp --dport 9000 -s 192.168.0.0/16 -j ACCEPT
iptables -A INPUT -p tcp --dport 9000 -j DROP

# 保存规则
iptables-save > /etc/iptables/rules.v4

3. 服务进程清理

1
2
3
4
5
6
7
8
# 查找并停止旧版本服务
ps aux | grep '服务名' | grep -v grep

# 停止旧进程
kill -9 <PID>

# 确认服务已停止
ss -tlnp | grep <端口号>

自动化扫描方案

方案一:定时端口扫描脚本

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
#!/bin/bash
# 端口安全检查脚本

LOG_FILE="/var/log/port_check.log"
ALERT_EMAIL="[email protected]"

echo "===== 端口安全检查 $(date) =====" >> $LOG_FILE

# 检查 Docker 端口绑定
echo "--- Docker 端口检查 ---" >> $LOG_FILE
docker ps --format "{{.Names}}\t{{.Ports}}" | grep -v "127.0.0.1" >> $LOG_FILE

# 检查高危端口
echo "--- 高危端口检查 ---" >> $LOG_FILE
HIGH_RISK_PORTS="3306 5432 6379 27017 9000"
for port in $HIGH_RISK_PORTS; do
result=$(ss -tlnp | grep ":$port ")
if [ -n "$result" ]; then
echo "警告: 端口 $port 正在监听" >> $LOG_FILE
echo "$result" >> $LOG_FILE
fi
done

# 发送告警(如果有异常)
if grep -q "警告" $LOG_FILE; then
echo "检测到端口安全问题,请检查日志"
# 这里可以添加邮件或钉钉通知
fi

方案二:集成到 Prometheus 监控

1
2
3
4
5
6
7
8
9
# node_exporter 配置
- job_name: 'port_check'
static_configs:
- targets: ['localhost:9100']
metrics_path: '/metrics'
relabel_configs:
- source_labels: [__address__]
regex: '.*'
action: keep

方案三:使用 Nmap 进行外网扫描

1
2
3
4
5
6
7
8
9
10
11
# 安装 nmap
apt-get install nmap

# 扫描本机开放端口
nmap -sT -O localhost

# 扫描远程服务器
nmap -sT -O 服务器IP

# 只显示开放端口
nmap -sT -O 服务器IP --open

一键修复脚本

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
#!/bin/bash
# 服务器端口安全加固脚本

set -e

echo "开始端口安全加固..."

# 1. 修复 Docker 端口绑定
echo "步骤1: 修复 Docker 端口绑定..."
for container in $(docker ps --format "{{.Names}}" | grep -E 'portainer|openclaw'); do
echo "处理容器: $container"
# 获取当前端口映射
old_port=$(docker port $container | cut -d':' -f2)

# 停止容器
docker stop $container

# 删除容器
docker rm $container

# 重新启动,绑定到 127.0.0.1
docker run -d \
--name $container \
-p 127.0.0.1:$old_port:$old_port \
$(docker images --format "{{.Repository}}:{{.Tag}}" | head -1)

echo "容器 $container 端口已绑定到 127.0.0.1"
done

# 2. 配置防火墙规则
echo "步骤2: 配置防火墙规则..."
# 允许内网 SSH
iptables -A INPUT -p tcp --dport 22 -s 192.168.0.0/16 -j ACCEPT

# 拒绝其他 SSH 访问
iptables -A INPUT -p tcp --dport 22 -j DROP

# 允许内网访问管理端口
for port in 9000 18789 9090; do
iptables -A INPUT -p tcp --dport $port -s 192.168.0.0/16 -j ACCEPT
iptables -A INPUT -p tcp --dport $port -j DROP
done

# 保存规则
iptables-save > /etc/iptables/rules.v4

echo "端口安全加固完成!"

常见问题解答

Q:如何判断一个端口是否应该暴露到公网?

A:一般情况下,只有以下服务可以暴露公网:

  • Web 服务(80/443)
  • 需要公网访问的 API

以下服务严禁公网暴露:

  • SSH(22)
  • 数据库(3306/5432/27017)
  • 容器管理(9000)
  • 监控系统(9090/9077)
  • 内部服务(如 Gateway 18789)

Q:Docker 端口绑定到 127.0.0.1 后,局域网其他机器还能访问吗?

A:不能。只有本机(127.0.0.1)可以访问。如果需要局域网访问,需要通过其他方式(如 Nginx 反向代理)。

Q:如何批量检查多台服务器的端口情况?

A:可以使用 Ansible 进行批量检查:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 安装 Ansible
apt-get install ansible

# 创建 hosts 文件
cat > hosts <<EOF
[webservers]
192.168.1.10
192.168.1.11

[databases]
192.168.1.20
EOF

# 批量执行端口检查
ansible all -i hosts -m shell -a "ss -tlnp | grep LISTEN"

Q:已经暴露的端口如何快速修复?

A:

  1. 立即停止相关服务或修改端口绑定
  2. 检查日志确认是否有异常访问
  3. 如果涉及凭证泄露,立即更换
  4. 配置防火墙规则阻止后续访问
  5. 记录事件并通知相关人员

安全最佳实践

  1. 最小化暴露原则:只开放必要的端口,其他一律关闭

  2. 本地绑定优先:Docker 端口尽量绑定到 127.0.0.1

  3. 定期扫描:建议每周进行一次端口安全扫描

  4. 日志审计:记录端口访问日志,便于事后排查

  5. 及时更新:及时更新系统和软件版本,修复已知漏洞

  6. 配置管理:使用配置管理工具(如 Ansible)统一管理配置

  7. 权限最小化:服务使用最小权限账户运行

总结

服务器端口安全是运维工作中不可忽视的重要环节。通过本文介绍的方法,你可以:

  1. 系统性地排查服务器端口开放情况
  2. 识别潜在风险,如不必要的端口暴露、错误的绑定地址
  3. 自动化修复常见安全问题
  4. 建立长期机制,定期进行安全检查

记住:安全无小事,预防胜于补救。宁可现在多花时间检查,也不要等到出了问题再后悔。

希望这篇文章能帮到你。如果有问题,欢迎在评论区讨论。


作者:小六,一个在上海努力搬砖的程序员

Author:Margrop
Link:http://blog.margrop.com/post/2026-03-11-port-security-scan-hardening/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可