Nmap端口扫描

认知端口状态

divides ports into six states: open, closed, filtered, unfiltered, open|filtered, or closed|filtered.

1
2
3
4
5
6
7
open: (端口开放)端口扫描的主要目标
端口可访问,存在应用程序。

应用程序正在该端口上积极接受 TCP 连接、UDP数据报文或 SCTP协议的关联。
开放端口是攻击者利用的入口点,因此通常会成为攻击的目标。

在不影响合法用户使用的同时,安全管理员的会通过防火墙或其他保护措施关闭这些端口,以防止未授权访问。
1
2
3
4
5
6
7
closed:(端口关闭)
端口可访问,无应用程序监听链接。

通过扫描关闭端口,Nmap 可以帮助识别主机存活、操作系统的类型和版本。
之后可能会部署应用程序,薛定谔的扫描价值

管理员尝试用防火墙 阻止用户对这些端口的访问。
1
2
3
4
5
6
7
8
9
filtered
无法确定该端口是否可访问。

因为防火墙或过滤设备(如路由器、防火墙软件)阻止了探测包的到达。
信息少
Sometimes they respond with ICMP error messages such as type 3 code 13 (destination unreachable: communication administratively prohibited), but filters that simply drop probes without responding are far more common.

大大增加扫描时间:This forces Nmap to retry several times just in case the probe was dropped due to network congestion rather than filtering. This slows down the scan dramatically.
Nmap会多次尝试发送数据包,以避免是因为网络阻塞而导致的数据包丢失。
1
2
3
4
5
6
unfiltered
端口可访问,无法确定是否存在应用程序监听链接(开放or关闭)

Only the ACK scan(-PA),用于分析防火墙的规则集。

通过其他扫描类型(如 SYN 扫描、FIN 扫描 等),可以进一步确定端口是开放的还是关闭的
1
2
3
4
5
6
7
8
open|filtered(开放或过滤端口)
无法确定端口是开放还是被过滤

通常发生在没有响应的情况下,如 UDP 扫描 或 FIN、NULL、Xmas 扫描。
没有响应可能是因为防火墙丢弃了数据包,或者目标端口本身没有应用程序监听。

常见于 UDP 扫描,因为 UDP 协议本身是无连接的,没有确认机制,所以在探测时很难区分端口是开放的还是被过滤的。
适用于某些扫描方法(如 Xmas 扫描)的结果,在没有明确响应的情况下,Nmap 会将端口归类为 open|filtered。
1
2
3
4
5
6
7
closed|filtered
无法确定端口是关闭的还是被过滤的

Only IP ID 空闲扫描(Idle Scan)
空闲扫描通过第三方主机发起扫描,而非直接从源主机发起,因此难以明确判断端口是关闭的还是被过滤。

空闲扫描:这种扫描技术较为隐蔽,利用第三方主机的 IP 地址进行扫描,以避免让目标主机知道是哪个主机发起了扫描。

端口扫描

all of its insights are based on packets returned by the target machines (or firewalls in front of them)

所有的信息只来自目标机器(或者是它前面的防火墙)

Such hosts may be untrustworthy and send responses intended to confuse or mislead Nmap.

部分数据包具有误导性,不是真实的

Nmap 支持的十几种端口扫描技术。每次扫描只能使用一种方法,除了 UDP 扫描(-sU)和 SCTP 扫描类型(-sY-sZ)可以与任何一种 TCP 扫描类型结合使用

Nmap 默认执行的是 SYN 扫描

普通用户只能执行 连接扫描FTP 跳跃扫描

1
2
3
4
5
6
7
8
9
-sS (TCP SYN scan) 半开放扫描
SYN 包
SYN-ACK 包/SYN包(开放)/RST包(关闭)
RST包

(filtered)
If no response is received after several retransmissions, the port is marked as filtered
The port is also marked filtered if an ICMP unreachable error (type 3, code 0, 1, 2, 3, 9, 10, or 13) is received.
如果几次发包都没有响应/接收到 ICMP unreachable erro端口被标记为 filtered
1
2
3
4
5
6
7
8
9
10
11
-sT (TCP connect scan) 全连接扫描
when SYN scan(-sS) is not an option。(非root用户)

TCP Connect Scan:由于执行了完整的三次握手,连接尝试会被目标主机记录在日志中。

如果使用 -sT 扫描并连接到某个端口,目标主机的 syslog 或其他日志文件中可能会出现类似于以下内容的记录:
Connection attempt from [IP] to [target port] closed without data.
这些记录会表明有一个扫描者尝试建立连接但没有发送任何数据。

无 root 权限时:如果你没有 root 权限(特别是在受限的环境中),你只能使用 -sT 扫描来获取开放端口信息。
防火墙/IDS 测试:在某些环境中,目标的防火墙可能会阻止 SYN 扫描或其他低级扫描类型,这时 TCP Connect Scan 可能仍然有效。
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
-sU (UDP scans)
慢(因为open|filtered 重发包与等待、closed ICMP port unreachable包的默认速率限制(尤其是Linux and Solaris))

可与-sS同时进行,检测目标是否支持两个协议。
For some common ports such as 53 and 161, a protocol-specific payload is sent to increase response rate, but for most ports the packet is empty unless the --data, --data-string, or --data-length options are specified.如无定义,一般是空包。

(closed)ICMP port unreachable error (type 3, code 3)
(filtered)Other ICMP unreachable errors (type 3, codes 0, 1, 2, 9, 10, or 13)
(open)Occasionally, a service will respond with a UDP packet, proving that it is open
(open|filtered) no response is received after retransmissions
Version detection (-sV) can be used to help differentiate the truly open ports from the filtered ones.-sV 版本探查可以进一步帮助区分开放的端口/过滤的端口

加快扫描速度的方法
1. 增加并行扫描的主机数量 nmap -sU -T4 -iL hosts.txt
2. 先扫描最常用的端口 nmap -sU -p 53,123,161,162 <target_ip>
3. 从防火墙后面扫描
4. --host-timeout 设置超时时间,来跳过缓慢的主机 nmap -sU --host-timeout 30s <target_ip>
5. --max-rate 控制发送速率 nmap -sU --max-rate 100 <target_ip>每秒最多发送 100 个数据包
6. --min-rate 每秒至少发送数据包的数量 nmap -sU --min-rate 1000 <target_ip>


UDP services are widely deployed. DNS, SNMP, and DHCP (registered ports 53, 161/162, and 67/68) are three of the most common.
some security auditors ignore these ports.

Unfortunately, a Linux-style limit of one packet per second makes a 65,536-port scan take more than 18 hours.
1
2
3
4
5
6
7
-sY (SCTP INIT scan) 办连接扫描
It can be performed quickly, scanning thousands of ports per second on a fast network not hampered by restrictive firewalls. 每秒数千

发送SCTP INIT chunk。这个 INIT chunk 表示你正在尝试建立一个 SCTP 连接。
(open)INIT-ACK chunk
(closed)ABORT chunk
(filtered)若干次重试后仍然没有响应,ICMP unreachable 错误(类型 3,代码 0、1、2、3、9、10 或 13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-sN; -sF; -sX (TCP NULL, FIN, and Xmas scans)
As long as none of those three bits(SYN, RST, or ACK bits) are included, any combination of the other three (FIN, PSH, and URG) are OK

(open|filtered) no response at all
(closed) any packet not containing SYN, RST, or ACK bits will result in a returned RST
(filtered)an ICMP unreachable error (type 3, code 0, 1, 2, 3, 9, 10, or 13)

Null scan(-sN):
Does not set any bits (TCP flag header is 0)
FIN scan (-sF)
Sets just the TCP FIN bit.
Xmas scan(-sX)
Sets the FIN, PSH, and URG flags, lighting the packet up like a Christmas tree.

sneak through certain non-stateful firewalls and packet filtering routers
a little more stealthy than even a SYN scan(Don't count on this though—most modern IDS products can be configured to detect them.)
A number of systems send RST responses to the probes regardless of whether the port is open or not. This causes all of the ports to be labeled closed. Major operating systems that do this are Microsoft Windows, many Cisco devices, BSDI, and IBM OS/400. This scan does work against most Unix-based systems though。主要对Unix有用
1
2
3
4
5
6
7
8
-sA (TCP ACK scan) 
1. map out firewall rulesets
2. determining whether they are stateful or not and
3. which ports are filtered.

only the ACK flag set
(unfiltered)When scanning unfiltered systems, open and closed ports will both return a RST packet。
(filtered) Ports that don't respond, or send certain ICMP error messages back (type 3, code 0, 1, 2, 3, 9, 10, or 13)
1
2
3
4
5
6
7
8
9
10
11
12
-sW (TCP Window scan)
an ACK scan + the TCP Window value(TCP Window field)

(open)positive
(close)zero
(filtered) Ports that don't respond, or send certain ICMP error messages back (type 3, code 0, 1, 2, 3, 9, 10, or 13)

This scan relies on an implementation detail of a minority of systems out on the Internet, so you can't always trust it.极少数的系统会使用。
Systems that don't support it will usually return all ports closed. 不支持该实现的系统的端口会被判断为closed

If most scanned ports are closed but a few common port numbers (such as 22, 25, 53) are filtered, the system is most likely susceptible.防火墙具有脆弱性。表明防火墙或某种过滤设备存在,但系统在某些方面仍然可能存在脆弱性。防火墙配置不当,或者某些服务未正确隐藏,攻击者可能会尝试通过其他方式绕过防火墙。
ccasionally, systems will even show the exact opposite behavior. If your scan shows 1,000 open ports and three closed or filtered ports, then those three may very well be the truly open ones.表明那3个关闭或过滤的端口实际上是真正开放的端口,而其他开放端口可能是故意隐藏或误报的。
1
2
3
4
5
6
7
8
9
10
-sM (TCP Maimon scan)
This technique is exactly the same as NULL, FIN, and Xmas scans, except that the probe is FIN/ACK. 只不过探针是FIN/ACK. (FIN/ACK 的标志组合)

BSD 衍生系统
(open)many BSD-derived systems simply drop the packet if the port is open。
(closed)响应一个 RST 包。
现代系统
RST packet,无论端口开放or关闭(可以用来检测主机状态)

隐蔽
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
--scanflags (Custom TCP scan)自定义规则
arbitrary TCP flags.自己设置特定的tcp标志
Just mash together any combination of URG, ACK, PSH, RST, SYN, and FIN. For example, --scanflags URGACKPSHRSTSYNFIN sets everything,
--scanflags URGACKPSHRSTSYNFIN 顺序不重要

tcp标志:
URG(紧急)
ACK(确认)
PSH(推送)
RST(重置)
SYN(同步)
FIN(结束)

自定义标志仅修改发送包时使用的标志,整体响应的解释仍然遵循基础扫描类型的规则。

指定基础扫描类型:
Nmap现有的组合方式,告诉Nmap如何解释响应(下一步做什么/打标签open、closed……)
默认是 SYN scan

Eg.
nmap -sA --scanflags URGACK <target_ip>
这个命令执行 ACK 扫描 (-sA),但发送 URG 和 ACK 标志的 TCP 包。在这种情况下:
(unfilter)如果端口是 开放 或 关闭,目标会响应一个 RST 包。
(filter)如果没有响应,则该端口可能是过滤的。

绕过 IDS/IPS、测试防火墙行为、绕过安全机制
1
2
3
4
5
6
7
8
9
-sZ (SCTP COOKIE ECHO scan) 探测 SCTP 端口。a more advanced SCTP scan. 更高级
发送 COOKIE ECHO 数据包

( open|filtered)无响应
(closed) send an ABORT 中止

与其他 SCTP 扫描方式相比,SCTP COOKIE ECHO 扫描不那么明显,且能绕过一些 无状态防火墙,这些防火墙可能会阻止 INIT 数据包,但允许 COOKIE ECHO 数据包。

可能被 IDS 检测到:虽然 SCTP COOKIE ECHO 扫描比其他扫描更不显眼,但一个良好的入侵检测系统(IDS)依然能够检测到这种扫描。发送数据包后不收到任何响应(或者收到 ABORT 数据包)的模式可能会被 IDS 系统识别。
1
2
3
4
5
6
7
8
9
-sI <zombie host>[:<probeport>] (idle scan)
数据包不从我的IP地址发出

‘扫描者向僵尸主机发送数据包(例如 SYN 数据包),要求它与目标进行交互。僵尸主机随后会生成包含分片 ID 的响应数据包。
通过分析僵尸主机数据包中的分片 ID 变化,攻击者可以推测目标端口是 开放、关闭 还是 过滤。

可以利用僵尸主机来 映射 IP 基于信任的关系。如果某个僵尸主机被目标信任(例如,通过路由器或防火墙规则),你就可以利用它进行隐蔽的扫描。

通过僵尸主机的 IP 分片 ID 序列变化来推测目标端口的状态。
1
2
3
4
5
6
7
-sO (IP protocol scan)
协议扫描发送 IP 包头,并在八位 IP 协议字段 上迭代,试图识别哪些协议在目标主机上处于活动状态。扫描过程中发送的包头通常是空的,不包含数据,甚至没有正确的头部信息,除了 TCP、UDP、ICMP、SCTP 和 IGMP 协议外,对于这些协议,Nmap 会附加相应的协议头,因为某些系统不会自动发送这些协议的头部,且 Nmap 本身已经有功能来创建它们。

(open)port unreachable (type 3, code 3)
(closed) An ICMP protocol unreachable error (type 3, code 2)
(filtered)Other ICMP unreachable errors (type 3, code 0, 1, 9, 10, or 13) cause the protocol——prove that ICMP is open
(open|filtered)no response is received after retransmissions,
1
2
3
4
5
-b <FTP relay host> (FTP bounce scan)

It takes an argument of the form
<username>:<password>@<server>:<port>.
<Server> is the name or IP address of a vulnerable FTP server.

If you are just trying to cover your tracks, you don’t need to (and, in fact, shouldn’t) limit yourself to hosts on the target network. Before you go scanning random Internet addresses for vulnerable FTP servers, consider that sysadmins may not appreciate you abusing their servers in this way.

指向

空闲扫描:尝试一次空闲扫描

打造一个在nmap看来安全的主机

-sI 细节·https://nmap.org/book/idlescan.html

如何查看自己的主机是否被扫描

端口指定和扫描顺序

1
2
3
4
5
6
7
8
9
10
11
12
13
-p <port ranges> (Only scan specified ports)

(-p-)The beginning and/or end values of a range may be omitted, causing Nmap to use 1 and 65535, respectively. So you can specify -p- to scan ports from 1 through 65535. 可省略开始和结束的端口号,nmap和自动用1或65535去补充

(-sO协议范围) For IP protocol scanning (-sO), this option specifies the protocol numbers you wish to scan for (0–255).

-p U:53,111,137,T:21-25,80,139,8080

(protocol)If no protocol qualifier is given, the port numbers are added to all protocol lists.

(通配符) You can even use the wildcards * and ? with the names. For example, to scan FTP and all ports whose names begin with “http”, use -p ftp,http*.

([-1024]) the following will scan all ports in nmap-services equal to or below 1024: -p [-1024].
1
2
3
--exclude-ports <port ranges> (Exclude the specified ports from scanning) 需要排除哪些端口

-sO --exclude-protocols 需要排除哪些协议
1
-F (Fast (limited port) scan)扫描比默认端口(1000)更少数量的端口(100)会扫描nmap-services 中的端口
1
-r (Don't randomize ports) 顺序扫描(默认无序)
1
--port-ratio <ratio><decimal number between 0 and 1> 扫描nmap-services文件中 开放概率>给定概率 的端口
1
--top-ports <n> 扫描nmap-services文件中最高比率的n个端口(在用--exclude-ports排除一些端口之后)

指向

https://nmap.org/book/nmap-services.html nmap-service 端口名称与其对应编号和协议的注册表。每个条目都有一个数字,表示该端口打开的可能性。大多数行也都有注释。