Margrop
Articles293
Tags757
Categories6

Categories

1password 24GB VRAM 2K 3.6 Flash 30B dense 4-bit 量化 6-DoF SLAM AC ACP AI & Economy ATLAS AI Agent AI Coding Assistant AI Tech AI Workflow AI tutor AI 基础设施 AI 安全 AI 工程 AI 应用 AI 日记 AI 生产力 AI编程助手 ALTK-Evolve AMIE AP API API 定价 API 降价 ARC-AGI-3 ASR ATEM chat 模板 AUTOMATIC1111 Agent Agent Harness Agent Memory Agent 入侵 Agent 可靠性 Agent 工程 Agent 推理 Agent 架构 Agent 检索 Agent 沙箱 Agent 系统 Agent 训练 Agent 记忆 Agent 评测 Agent 路由 Agentic AI Agentic SOC Agentic tools Ai2 Alertmanager AllenAI Android 17 Antigravity Apertus 1.5 AppDaemon AppWorld Apple Silicon Aqara Astra Async GRPO Attention AutoSynthData Baseten Benchmark CC-Switch CI/CD CLI Tools CLI工具 COBOL CPU 推理 Cache Hit Rate Caddy ChatGPT Claude Code Claude Sonnet ClawLoader Code Interpreter CodeMender Codex Coding Agent ComfyUI Computer Use Cookie 认证 Cosmos DB Cosmos-H-Dreams Cost Optimization Cron Cybersecurity DFIR DSpark Date DeepSeek DeepSeek V4 Flash Diagrams.net Diary Diffusers Diffusion Docker Efficiency Tools Embedding English Evaluation Cards FSDP2 Fable 5 Falcon ASR Fireworks AI FlashAttention FlashDreams Frontier AI Function Calling Funes GGUF GLM 5.2 GLM-5.2 GPT-4.1 GPT-5.6 GPT-5.6 Sol GPT-6 GPT-6 Astra GPT-Live GPT-Live-1 GPT-Red GPU 云 GPU 加速 GPU 性能分析 GPU 集群 GRPO Gateway Gemini Gemini 3.5 Flash Gemini 3.7 Flash Gemini 3.8 Gemini 4 Argon Gemini API Gemini CLI Gemini Omni Flash Gemma 4 12B Gemma Translator Gemma4 GitHub Actions Google Google AI Google Beam Google Data Commons Google DeepMind Google Research Google Sheets Grabette Gradio Gradio Workflow Gripette HA HADashboard HF Security Incident Hailuo Hermes Hexo Holo4 HomeAssistant Hugging Face IBM Granite IBM Research In-context Learning Inference Providers Isaac Lab JSON Output Java KV cache KV 缓存 Kimi K3 Kubernetes LFM2.5 LFM2.5-VL LLM Router LLM 安全 LLM 工程 LLM 评测 LVM‑Thin Late Interaction LeRobot Linux Liquid AI LiquidAI Live Translate LoRA Luna MCP MLX MTP MacOS Magpie TTS Managed Agents Meta Microsoft 365 Copilot MiniMax Mistral Shieldstral MoE Model Routing MuJoCo Warp Multi-Agent Multi-Vector MultiLangBench Muse Glimmer Muse Spark Open MySQL NAS NIM NVIDIA NeMo Automodel NeMo Data Designer Nemotron Nemotron 3 Embed NeoMME Newton Nginx Node.js Nunchaku OCR OOM OlmoEarth On-device AI Open Source OpenAI OpenAI Codex OpenAI 兼容 OpenClaw OpenCode OpenResty OpenWrt PII 检测 Physical AI Pollen Robotics Portainer PostgreSQL ProcessOn Project Astra Prometheus Prompt Caching Prompt Injection Prompt 缓存 Proxmox VE PyTorch Qwen3 Qwen3-VL Qwen3.6 RAG RPC RTEB Real-Time Inference Red Teaming Reflection Beam Responses API SNAP SOCKS5 SPED SVDQuant SWE-Bench Schema Scientific Computing Self-Forcing Distillation Sentence Transformers Session Sheets canvas Shell Sol Speech-to-Text Storage Buckets Strands Agents Subagent Surgical Robotics TRL TTS Tabular Terra Think button TimeMachine Tokenizer Transformers TutorMoments UML Uptime Kuma V4-Pro VPS Voice Agent VoiceEQ WARP WebRTC WebSocket Windows World Foundation Model agent agentic aligenie aliyun annotation aop autofs backup bash bitwarden boot brew browser budget control centos cert certbot charles chat chrome classloader client clone closures cloudflare command commit commoditization container crontab cyber capability demo dependency deploy developer devtools dll dns docker domain download drafter draw drawio dsm dump dylib environment hooks exception fail2ban feign firewall-cmd flow free tier frontier hosted model frp frpc frps fuckgfw full-duplex function gfw git github gperftools gridea grub guardrail lockout gvt-g hacs havcs heap hello hexo hibernate hidpi hoisting homeassistant hosts html htmlparser https huggingface_hub iKuai iMessage image img img2kvm immortalwrt import index inference cost install intel io ios ip iptables iso java javascript jni jnilib jpa js json jsonb jupter jupyterlab jvm k8s kernel key kvm lastpass launchctl learning letsencrypt linux llama.cpp low-code lvm mac mariadb markdown maven md5 microcode mirror modules monitor mount mstsc multimodal mysql n5105 network nfs node node-red nodejs nohup notepad++ npm nssm ntp oop open d1 open weights openfeign openssl os ovz packet capture pdf pem perf pip plugin png powerbutton print pro productive struggle pve pvekclean python qcow2 qemu qemu-guest-agent rar reasoning control reasoning slider reboot reflog remote remote desktop renew repo resize retina router runtime safari sata scaffolding scheduled triggers scipy-notebook scoping scp self-play server serverless inference silent test simulated student so speculative decoding spk spring springboot springfox ssh ssl stash string support svg svn swagger sync synology systemctl systemd template terminal txt ubuntu ui undertow unlocker upgrade vLLM vhd vim vm vmdk web windows with worker xml yum zai-org/GLM-5.2 zip 一致性评测 上下文压缩 上下文工程 事件响应 交换机 人才争夺 人机协作 代理 代码生成 代码迁移 企业 AI 企业自动化 优化 低延迟 低资源编程语言 供应链 健康检查 光猫 免费层 内存 内存优化 内网渗透 决策模型 分布式存储 分布式推理 分布式训练 医疗 AI 升级 卫星影像 反向代理 反诉 可复现性 合成数据 向量检索 启动 告警 告警优化 图像生成 地球观测 地理空间推理 基础设施 复盘评测 夏令时 多 token 预测 多向量检索 多智能体 多模型编排 多模态 多模态 AI 多模态 API 多模态 Agent 多模态交互 多模态检索 多模态模型 多语言 多语言 AI 大厂人才战 大模型评测 天猫精灵 安全 安全事件 安全对齐 安全评测 安全运营 安装 定时任务 实时协作 实时语音 客户端 SDK 容器 导入 小米 屏幕理解 工作流编排 工具审计 工具调用 工具调用拦截 工程团队 工程实践 工程笔记 常用软件 应用市场 延迟优化 开权重 开源权重 开源模型 异常 异步任务 异步委派 强化学习 微信 心跳 性能优化 性能捕捉 成本优化 成本控制 扩散模型 技术 抓包 拒答校准 持续预训练 按 provider 优先级 排查 推测解码 推理加速 推理成本 推理模型 推理速度 推理预算 描述文件 提示词敏感性 搜索 API 故障排查 效率工具 教育数据开源 教育评测 数学推理 数据工作流 数据治理 数据流 数据管线 数据集偏差 文本编码器 旁路由 日志分析 日记 时区 显卡虚拟化 智能家居 智能音箱 服务管理 本地 Agent 本地 agent 本地推理 机器人仿真 机器人学习 机器人数据采集 架构 模块 模型微调 模型快照 模型推理 模型评测 模型路由 残存访问 流式推理 流程 流程图 浏览器 混合专家 漫游 火绒 生成式影像 电信 画图 监控 监控系统 监管 知识蒸馏 磁盘 科研自动化 稀疏注意力 立体声 端侧 AI 端侧推理 端口 端口冲突 端口扫描 结构化输出 续期 网关 网络 网络安全 网络风暴 群晖 脚本 脚本优化 腾讯 自动化 自动恢复 自动攻击 自部署 苹果 虚拟机 表格基础模型 视觉语言模型 视频生成 视频问诊 认证 训练基础设施 证书 评测 评测基准 评测方法学 诉讼 语音 AI 语音 Agent 语音识别 资源调度 超时 路由 路由器 软件工程 软件管家 软路由 边缘 AI 运维 运维监控 连接保活 连接问题 通信机制 通知 邮件漏发 部署 配置 量化 量子计算 钉钉 镜像 镜像源 长上下文 长任务编排 长连接 门窗传感器 问题排查 防火墙 阿里云 阿里源 集客 非人类身份 飞书

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:https://blog.margrop.com/post/2026-03-11-port-security-scan-hardening/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可