Nmap
2026/5/29工具工具Web安全Nmap大约 6 分钟
Nmap
是什么
Nmap(Network Mapper)是一款开源的网络扫描和安全审计工具,用于发现网络上的主机、开放端口、运行的服务及其版本信息。它是网络侦察和信息收集阶段的核心工具,广泛应用于渗透测试、漏洞评估和 CTF 竞赛中。
核心功能:
- 主机发现:识别网络中的活跃主机
- 端口扫描:检测目标开放的 TCP/UDP 端口
- 服务识别:确定端口上运行的服务类型和版本
- 操作系统检测:识别目标的操作系统类型和版本
- 脚本引擎(NSE):使用 Lua 脚本进行高级扫描和漏洞检测
- 防火墙/IDS 规避:使用各种技术绕过网络安全设备
在 CTF 竞赛中,Nmap 是 Pwn 和 Misc 类题目的基础工具,也常用于 Web 类题目的信息收集阶段。
安装与配置
安装方法
# Linux
# Ubuntu/Debian
sudo apt update && sudo apt install nmap
# CentOS/RHEL
sudo yum install nmap
# Arch Linux
sudo pacman -S nmap
# Kali Linux(预装)
nmap --version
# macOS
brew install nmap
# Windows
# 从官网下载安装程序:https://nmap.org/download.html
# 或使用 Chocolatey
choco install nmap
# 验证安装
nmap --version配置文件
Nmap 配置文件位于:
- Linux/macOS:
/etc/nmap/nmap.conf或~/.nmap/nmap.conf - Windows:
C:\Program Files (x86)\Nmap\nmap.conf
NSE 脚本数据库更新
# 更新脚本数据库
nmap --script-updatedb
# 查看脚本位置
ls /usr/share/nmap/scripts/基本用法
主机发现
# Ping 扫描(只发现主机,不扫描端口)
nmap -sn 192.168.1.0/24
# 跳过 Ping 直接扫描(适用于禁 Ping 的主机)
nmap -Pn 192.168.1.100
# ARP 扫描(局域网内最有效)
nmap -PR -sn 192.168.1.0/24
# TCP SYN Ping
nmap -PS22,80,443 -sn 192.168.1.0/24
# ICMP Echo Ping
nmap -PE -sn 192.168.1.0/24
# 列表扫描(只列出目标,不发送任何数据包)
nmap -sL 192.168.1.0/24
# DNS 解析
nmap -R 192.168.1.0/24端口扫描技术
# TCP SYN 扫描(默认,半开扫描,速度快,隐蔽性好)
nmap -sS 192.168.1.100
# TCP Connect 扫描(完成三次握手,不需要 root 权限)
nmap -sT 192.168.1.100
# UDP 扫描(速度慢,需要 root 权限)
nmap -sU 192.168.1.100
# TCP ACK 扫描(用于探测防火墙规则)
nmap -sA 192.168.1.100
# TCP Window 扫描
nmap -sW 192.168.1.100
# TCP Null/FIN/Xmas 扫描(绕过简单防火墙)
nmap -sN 192.168.1.100 # Null 扫描
nmap -sF 192.168.1.100 # FIN 扫描
nmap -sX 192.168.1.100 # Xmas 扫描
# Idle 扫描(极度隐蔽,利用僵尸主机)
nmap -sI zombie_host:port 192.168.1.100端口指定
# 扫描特定端口
nmap -p 80 192.168.1.100
nmap -p 80,443,8080 192.168.1.100
# 扫描端口范围
nmap -p 1-1000 192.168.1.100
nmap -p 1-65535 192.168.1.100
# 扫描所有端口
nmap -p- 192.168.1.100
# 扫描前 N 个常用端口
nmap --top-ports 100 192.168.1.100
# 快速扫描(前 100 个常用端口)
nmap -F 192.168.1.100
# 只扫描特定协议端口
nmap -p T:80,443 U:53,161 192.168.1.100服务版本检测
# 检测服务版本
nmap -sV 192.168.1.100
# 设置版本检测强度(0-9,默认 7)
nmap -sV --version-intensity 5 192.168.1.100
# 只进行版本检测(不扫描端口)
nmap -sV --version-all 192.168.1.100
# 轻量级版本检测
nmap -sV --version-light 192.168.1.100操作系统检测
# 检测操作系统
nmap -O 192.168.1.100
# 设置操作系统检测强度
nmap -O --osscan-guess 192.168.1.100
# 同时检测服务版本和操作系统
nmap -A 192.168.1.100综合扫描
# 全面扫描(包含版本检测、操作系统检测、脚本扫描和 Traceroute)
nmap -A 192.168.1.100
# 常用组合扫描
nmap -sS -sV -O -p- 192.168.1.100
# CTF 中常用的快速全面扫描
nmap -sC -sV -O -p- --open 192.168.1.100扫描速度控制
# 设置时间模板(T0-T5,T5 最快但最不准确)
nmap -T0 192.168.1.100 # 极慢(IDS 规避)
nmap -T1 192.168.1.100 # 慢
nmap -T2 192.168.1.100 # 正常
nmap -T3 192.168.1.100 # 默认
nmap -T4 192.168.1.100 # 快速(CTF 推荐)
nmap -T5 192.168.1.100 # 极快
# 自定义速率
nmap --min-rate 1000 192.168.1.100
nmap --max-rate 500 192.168.1.100
# 并行度
nmap --min-parallelism 10 192.168.1.100输出格式
# 正常输出
nmap -oN result.txt 192.168.1.100
# XML 格式
nmap -oX result.xml 192.168.1.100
# Grepable 格式
nmap -oG result.grep 192.168.1.100
# 脚本 kiddie 格式
nmap -oS result.sk 192.168.1.100
# 所有格式
nmap -oA result 192.168.1.100CTF常用技巧
常用扫描命令
# CTF 中最常见的快速扫描命令
nmap -sC -sV -T4 -p- --open -oN scan.txt <target>
# 参数说明:
# -sC: 使用默认脚本扫描
# -sV: 检测服务版本
# -T4: 快速扫描
# -p-: 扫描所有 65535 个端口
# --open: 只显示开放端口
# -oN: 保存结果到文件UDP 端口扫描
# 扫描常见 UDP 服务
nmap -sU -T4 --top-ports 100 192.168.1.100
# CTF 中常出现的 UDP 服务
# 53 (DNS), 69 (TFTP), 123 (NTP), 161 (SNMP), 500 (IKE), 1900 (SSDP)NSE 脚本使用
# 使用默认脚本
nmap -sC 192.168.1.100
# 使用特定脚本
nmap --script=http-enum 192.168.1.100
nmap --script=http-title 192.168.1.100
# 使用脚本类别
nmap --script=vuln 192.168.1.100
nmap --script=safe 192.168.1.100
nmap --script=auth 192.168.1.100
# 使用多个脚本
nmap --script="http-*" 192.168.1.100
# 传递脚本参数
nmap --script=http-brute --script-args=http-brute.path=/admin 192.168.1.100常用 NSE 脚本
# HTTP 相关
nmap --script=http-enum -p80,443,8080 <target> # 枚举 Web 目录
nmap --script=http-title -p80,443 <target> # 获取网页标题
nmap --script=http-headers -p80 <target> # 获取 HTTP 头
nmap --script=http-robots.txt -p80 <target> # 获取 robots.txt
nmap --script=http-shellshock -p80 <target> # 检测 Shellshock 漏洞
nmap --script=http-sql-injection -p80 <target> # SQL 注入检测
# SMB 相关
nmap --script=smb-enum-shares -p445 <target> # 枚举 SMB 共享
nmap --script=smb-enum-users -p445 <target> # 枚举 SMB 用户
nmap --script=smb-vuln* -p445 <target> # SMB 漏洞检测
# SSH 相关
nmap --script=ssh-auth-methods -p22 <target> # SSH 认证方式
nmap --script=ssh-hostkey -p22 <target> # SSH 主机密钥
# FTP 相关
nmap --script=ftp-anon -p21 <target> # 匿名 FTP 检测
nmap --script=ftp-bounce -p21 <target> # FTP bounce 检测
# DNS 相关
nmap --script=dns-brute -p53 <target> # DNS 子域名爆破
nmap --script=dns-zone-transfer -p53 <target> # DNS 区域传送
# 漏洞检测
nmap --script=vuln <target> # 通用漏洞扫描
nmap --script=http-heartbleed -p443 <target> # Heartbleed 检测防火墙规避
# 分段扫描
nmap -f 192.168.1.100
# 指定 MTU
nmap --mtu 24 192.168.1.100
# 使用诱饵 IP
nmap -D RND:10 192.168.1.100
# 源端口欺骗
nmap --source-port 53 192.168.1.100
# 自定义数据长度
nmap --data-length 25 192.168.1.100
# 使用 MAC 地址欺骗
nmap --spoof-mac 0 192.168.1.100
# 慢速扫描(规避 IDS)
nmap -T0 192.168.1.100批量扫描
# 扫描多个目标
nmap 192.168.1.1 192.168.1.2 192.168.1.3
# 从文件读取目标
nmap -iL targets.txt
# 扫描整个网段
nmap 192.168.1.0/24
# 排除特定主机
nmap 192.168.1.0/24 --exclude 192.168.1.1,192.168.1.2
nmap 192.168.1.0/24 --excludefile exclude.txt解析 XML 结果
# 使用 xsltproc 转换为 HTML
xsltproc result.xml -o report.html
# 使用 Python 解析
python3 -c "
import xml.etree.ElementTree as ET
tree = ET.parse('result.xml')
root = tree.getroot()
for host in root.findall('host'):
addr = host.find('address').get('addr')
ports = host.find('ports')
if ports:
for port in ports.findall('port'):
pid = port.get('portid')
state = port.find('state').get('state')
service = port.find('service').get('name') if port.find('service') is not None else 'unknown'
print(f'{addr}:{pid} ({state}) - {service}')
"常见问题
扫描结果不准确
原因:防火墙或 IDS 干扰。
解决:
# 使用 -Pn 跳过主机发现
nmap -Pn -sS -p- <target>
# 使用不同扫描技术
nmap -sS -T2 <target> # 降低速度权限不足
原因:SYN 扫描和 OS 检测需要 root 权限。
解决:
sudo nmap -sS <target>
# 或使用 Connect 扫描(不需要 root)
nmap -sT <target>扫描速度太慢
解决:
# 使用 -T4 加速
nmap -T4 -F <target> # 只扫描常用端口
# 只扫描需要的端口
nmap -p 80,443,8080 <target>UDP 扫描不准确
原因:UDP 扫描依赖 ICMP 端口不可达消息,很多系统限制 ICMP 速率。
解决:
nmap -sU -T4 --top-ports 20 <target>
# 配合版本检测提高准确性
nmap -sU -sV <target>NSE 脚本执行错误
解决:
# 更新脚本数据库
nmap --script-updatedb
# 使用调试模式
nmap --script=http-enum --script-trace <target>