Nmap – Nmap的开关和扫描类型

Nmap可能是最著名的渗透测试工具和黑客工具之一。它本质上是一个端口扫描工具,可以帮助您扫描网络并识别网络中可用的各种端口和服务,此外还可提供有关目标的更多信息,包括反向DNS名称、操作系统猜测、设备类型和MAC地址。在网络审计过程中,它也非常方便。

Nmap的基本语法是:

$ nmap [FLAGS] [IP]

请注意,有时候您可能还需要以sudo特权运行它来执行某些特定类型的扫描。

Nmap开关

Nmap是一款强大而强大的网络扫描工具,它可以通过命令行传递的标志进行自定义扫描。一些重要的标志包括:

  • -h: Print a help summary page
  • -sS: Perform a TCP SYN scan
  • -sU: Perform a UDP scan
  • -sV: Probe open ports to determine service/version info
  • -O: Enable OS detection
  • -v: Enable verbosity. You can even set the verbosity level as such :-vv: Level 2 verbosity. The minimum level of verbosity advised for use.
    -v3: Level 3 verbosity. You can always specify the verbosity level by specifying a number like this.
  • -oA: Same Nmap output in “normal”, XML and grepable formats. However you can specify the format of your choice with :-oN: Redirect normal output to a given filename
    -oX: Produce output in a clean, XML format and store it in a given file
    -oG: Produce “grepable” output and store it to a file. Deprecated format as users are now moving towards XML outputs.
  • -A: Enables “aggressive” scanning. Presently this enables OS detection (-O), version scanning (-sV), script scanning (-sC) and traceroute (–traceroute)
  • -p: Specify the ports to scan. It can be a single port as well as a range of ports. For Example :nmap -p 80 127.0.0.0.1: This scans port 80 on localhost
    nmap -p 1-100 127.0.0.1: This scans ports from 1 to 100 on localhost
    nmap -p- 127.0.0.1: This scans all the ports on the localhost

Nmap中的扫描类型

Nmap支持许多不同的扫描类型。然而,最受欢迎的类型是:

1. TCP连接扫描 (-sT)

在这种类型的扫描中,Nmap向具有SYN标志设置的端口发送一个TCP数据包。在这种情况下,可能发生两件事情:

  • The target responds with an RST packet that signifies that the port is closed.
  • Target doesn’t respond at all, probably due to a firewall dropping all incoming packets in which case the port will be considered filtered
  • The target responds back with a TCP packet with the SYN/ACK flags set which would signify that the port is open and then Nmap would respond with a TCP packet with the ACK flag set and hence would complete the TCP 3-way handshake.

这种扫描技术并不是非常可靠,因为很容易通过配置防火墙规则来回应RST数据包或者丢弃所有进入的数据包。此外,这种方法非常缓慢,因为它要等待整个TCP 3次握手过程。

2. SYN “半开放式” 扫描 (-sS)

SYN扫描,也被称为“半开放式”或“隐蔽扫描”,是对以前方法的改进。在以前的方法中,我们在收到SYN/ACK数据包后发送带有ACK标志的TCP数据包,现在我们将发送一个RST数据包。这样可以阻止服务器反复尝试发出请求,大大减少了扫描时间。

这种方法是之前方法的改进之处,原因是:

  • They are faster
  • They might be able to bypass some primitive firewalls
  • Often, SYN Scans are not logged by applications running on the ports as most applications start logging a connection only after it has been fully established which is not the case with SYN Scans

然而,不建议在生产环境中运行SYN扫描,因为它可能会破坏某些不稳定的应用程序。还需要注意的是,SYN扫描也需要sudo特权,因为它需要制作原始数据包。

当以sudo权限运行时,事实上,nmap默认使用SYN扫描;否则,nmap默认使用TCP扫描。

3. UDP扫描(-sU)

UDP扫描比之前的两种方式要不可靠得多,因为UDP连接天生是无状态的。这意味着没有像TCP那样的“反馈机制”。UDP遵循“发送即忘记”的原则,将数据包发送到目标端口,并希望其能够到达。这更加注重速度而非质量。然而,缺乏反馈机制使得很难识别开放的端口。

当一个UDP数据包被发送到目标端口时,可能会出现三种情况:

  • Usually there is no response received in which case nmap marks the port as open|filtered. If no response is received yet, it sends another UDP packet to double check and if yet again no response is received, it marks the port as open|filtered and moves on
  • It might get a UDP response back which is very rare. In such a scenario, the port is marked open
  • If the port is closed and it receives an ICMP echo request back which signifies that the port is unreachable.

特殊扫描在Nmap中

除此以外,还有一些比TCP SYN扫描更加“隐秘”的不太常见的扫描类型。具体如下:

1. TCP空(Null)扫描 (-sN)

在TCP Null扫描中,发送的TCP数据包没有设置任何标志位。根据RFC,在这种情况下,如果端口关闭,目标主机应该用RST进行回复。

2. TCP终止扫描 (-sF)

这与TCP空扫描非常相似,唯一的区别在于它不发送完全空的TCP数据包,而是发送一个带有FIN标志的数据包,该标志用于优雅地关闭连接。根据RFC,如果端口关闭,目标主机必须以RST回应。

3. TCP圣诞扫描(-sX)

TCP Xmas扫描与最后两种扫描技术非常相似,只是它使用设置了PSH、URG和FIN标志的TCP数据包。与最后两种扫描类型一样,这种扫描也期望在RFC中对关闭的端口收到RST数据包。

限制

由于这些扫描的性质相似,因此它们产生的输出也非常类似于UDP扫描。在这种情况下,我们只能有以下三种回应:

  • open|filtered : When no response is received then the port is categorized as this this because no response can mean only two things :The port is open
    The port is protected behind a firewall hence filtered
  • filtered : When the port is protected behind a firewall which sends an ICMP ping back
  • closed : When it receives and RST packet

需要注意的是,虽然RFC 793规定网络主机对于关闭的端口应该用RST TCP数据包回应格式错误的数据包,并且对于开放的端口不应该做出任何应答,但一些系统拒绝遵守这一规定。这种行为主要在Microsoft Windows服务器和某些CISCO设备上观察到,这些系统默认情况下会丢弃所有的格式错误的数据包。

使用Nmap扫描网络主机。

连接到网络时,最重要的一件事情就是在进一步探测之前获取网络上所有活动主机的列表。这可以通过“Ping扫描”来实现,顾名思义,就是向网络中的所有IP地址发送ICMP数据包并等待响应。回复ICMP数据包的主机被视为活动主机。

您可以通过使用连字符或CIDR来指定您的目标IP范围,如下所示:

$ nmap -sn 192.168.0.1-254

或者

$ nmap -sn 192.168.0.0/24

-sn参数可以抑制任何端口扫描,并且强迫nmap仅依靠ICMP回显包(或者ARP请求,如果以超级用户权限运行)来识别网络中的活动主机。它还会向目标的443端口发送TCP SYN数据包,并向目标的80端口发送TCP ACK请求(如果以超级用户权限运行,则发送TCP SYN)

Nmap 脚本引擎

Nmap的脚本引擎(NSE)是对其功能的有力补充,可以进一步扩展其功能。由Lua编写,我们可以使用它来编写脚本并自动化很多工作,如漏洞测试和利用。

有许多可用的类别。一些有用的类别包括:

  • safe:- Won’t affect the target
  • intrusive:- Not safe: likely to affect the target
  • vuln:- Scan for vulnerabilities
  • exploit:- Try to exploit a vulnerability
  • auth:- Attempt to bypass authentication for running services
  • brute:- Try to brute force credentials for running services
  • discovery:- Attempt to query running services for further information about the network (

要运行一个脚本,我们需要将其指定为–script=<脚本名称>。

你还可以通过分隔脚本名称(如–script=,)来同时指定多个要运行的脚本。

有些脚本还需要一个参数,可以用 –script-args <参数> 来指定。

有些脚本内置了帮助菜单,可通过以下方式进行查阅:

$ nmap --script-help <script-name>

你可以在这里找到一个全面的脚本列表。

结论

Nmap拥有大量免费和精心撰写的文档。您可以在他们的官方网站上找到关于标志、脚本和更多信息的许多内容。您可以尝试各种不同的标志和脚本,并观察它们在不同环境下的输出差异。

发表回复 0

Your email address will not be published. Required fields are marked *