PRELOADER

当前文章 : 《渗透测试神器——nmap》

12/2/2019 —— 

渗透测试神器——nmap

本篇作为渗透神器系列第一篇文章,将介绍一款经典的端口扫描工具–nmap。目前市面上成熟的端口扫描器有很多,

比如massscan(全网扫描器),zenmap(nmap的GUI版)等,但我个人还是钟爱nmap,原因很简单,因为它很强大,并且支持扩展。

Nmap最新几个版本中,加入了nmap script Engine(NSE)功能,支持扩展脚本,即可以在nmap中加载自定义的nse脚本,以达到扫描的目的。

目前官方的nse脚本已达500多个,nse脚本地址https://nmap.org/nsedoc/,或者查看[github库](https://github.com/nmap/nmap)。

本篇将会介绍如何编写以及使用nse脚本,以便能最大程度地发挥出nmap的强大功能(扩展功能),当然本文后本段也会简单介绍下nmap工具的基本使用方法以及参数设置。

NSE

nse全称是nmap脚本引擎,脚本后缀名为.nse,脚本用Lua语言编写,遵循特定的规则。nse脚本存放在nmap安装目录下的scripts目录下,

目前官方提供的大概有500多个,功能涵盖了常用的漏洞检测、端口检测、基线检测等。

nse script exploit

在scripts目录下新建一个文件,如:hello.nse,写入以下内容:

1. -- The Head Section --
1. -- The Rule Section --
1. portrule = function(host, port)
1. return port.protocol == "tcp" and port.number == 80 and port.state == "open"
1. end
1. -- The Action Section --
1. action = function(host, port)
1. return "Hello world"
1. end

以上代码运行后,会检测目标ip是否开放了80端口,若开放则返回helloworld。

nse脚本遵循nmap api规范,其包含三部分内容,其中–开头的行为注释内容。

The Head Section

该部分包含一些元数据,主要描述脚本的功能,作者,影响力,类别及其他。

The Rule Section

该部分定义脚本的一些规则,至少包含下面列表中的一个函数:

1. portrule
1. hostrule
1. prerule
1. postrule
1. The Action Section

该部分定义脚本逻辑,即满足条件后执行的内容,比如上面例子为输出helloworld。

调用内置库

NSE脚本可以调用内置库,比如http库、shortport库、nmap库等。

导入方式:

1. local http = require "http"
1. local nmap = require "nmap"
1. local shortport = require "shortport"

更多nse-api参考:https://nmap.org/book/nse-api.html

更多lua语法参考:http://www.runoob.com/lua/lua-tutorial.html

nse script usage

当在scripts下面编写完hello.nse脚本后,如何加载使用呢?

方法一:

  1. nmap –script-updatedb 更新脚本库
  2. nmap –script=hello使用该脚本

方法二:

  1. nmap –script=d:/…./hello.nse 绝对路径

其他参数:

1. -sC: 等价于–script=default,使用默认类别的脚本进行扫描 可更换其他类别
1. –script=<Lua scripts>: <Lua scripts>使用某个或某类脚本进行扫描,支持通配符描述
1. –script-args=<n1=v1,[n2=v2,...]>: 为脚本提供默认参数
1. –script-args-file=filename: 使用文件来为脚本提供参数
1. –script-trace: 显示脚本执行过程中发送与接收的数据
1. –script-updatedb: 更新脚本数据库
1. –script-help=<scripts>: 显示脚本的帮助信息,其中<scripts>部分可以逗号分隔的文件或脚本类别

nse example

对目标机器进行扫描,同时对smb的用户进行枚举。

1. nmap  --script=smb-enum-users  target_ip

对目标机器所开启的smb共享进行枚举。

1. nmap  --script=smb-enum-shares target_ip

对目标机器的用户名和密码进行暴力猜测。

1. nmap  --script=smb-brute target_ip

对目标机器测试心脏滴血漏洞。

1. nmap -sV --script=ssl-heartbleed target_ip

再举几个硬件设备的例子:

1. modbus-discover.nse (该脚本可以调用Modbus 43(2B功能码)功能码读取设备信息)
1. modbus-enum.nse (Modbus TCP设备枚举脚本)
1. s7-enumerate.nse (西门子S7 PLC设备发现脚本,可以枚举PLC的一些基本信息)
1. enip-enumerate.nse (可以读取EtherNet/IP设备的基本信息)
1. BACnet-discover-enumerate.nse (可以读取BACnet设备的基本信息)
1. iec-identify.nse (IEC104协议asdu address枚举脚本)
1. mms-identify.nse (IEC-61850-8-1协议信息枚举脚本)

nmap introduce

以上内容为nmap nse扩展脚本的基础知识,其中涉及到nse脚本编写的语法规则等,本篇暂不做详细介绍,可参考官方文档。以下内容为nmap基础使用,包含命令行参数等内容。

nmap parameter

nmap参数:

  1. nmap [Scan Type(s)] [Options] {target specification}
  2. scan type(s) 用于指定扫描类型
  3. options 用于指定选项
  4. target specification 用于指定扫描目标
  5. -s 指定扫描类型
  6. 如下:
  7. -sP (ping扫描) *存活主机探测
  8. -sS (TCP SYN扫描 隐身扫描) *默认扫描方式
  9. -sT (tcp 扫描) * syn 不能用时就tcp扫描
  10. -sU (UDP 扫描)
  11. -sA (ACK扫描) *三次握手 用于探测出防火墙过滤端口 实际渗透中没多大用
  12. -sV (版本探测)
  13. -A操作系统探测
  14. -O (启用操作系统检测)
  15. -v详细
  16. 选项说明
  17. -P0 [指定端口] (无ping扫描)
  18. -PU [指定端口] (udp ping扫描)
  19. -PS [指定端口] (TCP SYN ping 扫描)
  20. -PA [指定端口] (tcp ack ping扫描)
  21. -PI 使用真正的pingICMP echo请求来扫描目标主机是否正在运行
  22. -iL 指定扫描主机列表
  23. -iR 随机选择目标
  24. –exclude 排除扫描目标
  25. –excludefile 排除文件中目标列表
  26. -n (不用域名解析)
  27. -R (为所有目标解析域名)
  28. -T 时间优化(每隔多久发一次包 ) -T5 最快 -T0 最慢
  29. -F 快速扫描
  30. -e 指定网络接口
  31. -M 设置tcp扫描线程

nmap output

输出结果:

  1. -oS 保存扫描结果输出
  2. -oN 把扫描结果重定向到一个可读的文件logfilename中
  3. -oM 每个结果一行输出
  4. -oA 同上
  5. –append-output 附在原来的结果前面

nmap status

nmap端口状态:

1. open(开放的)
1. closed(关闭的)
1. filtered(被过滤的)不确定开放还是关闭
1. unfiltered (未被过滤的)
1. openfiltered (开放或者被过滤的)
1. closedfiltered (关闭或者未被过滤的)

nmap常用命令

以下命令部分收集于网络,部分来自个人总结。

轻量级扫描:

  1. nmap -sP 192.168.0.0/24 判断哪些主机存活
  2. nmap -sT 192.168.0.3 开放了哪些端口
  3. nmap -sS 192.168.0.127 开放了哪些端口(隐蔽扫描)
  4. nmap -sU 192.168.0.127 开放了哪些端口(UDP)
  5. nmap -sS -O 192.168.0.127 操作系统识别
  6. nmap -sT -p 80 -oG – 192.168.1.* | grep open列出开放了指定端口的主机列表
  7. nmap -sV -p 80 thief.one 列出服务器类型(列出操作系统,开发端口,服务器类型,网站脚本类型等)

批量扫描:

1. nmap -sT -sV -O -P0 --open -n -oN result.txt -p80-89,8080-8099,8000-8009,7001-7009,9000-9099,21,443,873,2601,2604,3128,4440,6082,6379,8888,3389,9200,11211,27017,28017,389,8443,4848,8649,995,9440,9871,2222,2082,3311,18100,9956,1433,3306,1900,49705,50030,7778,5432,7080,5900,50070,5000,5560,10000 -iL ip.txt

批量扫描:

1. nmap -sT -sV -p80-89,8080-8099,8000-8009,7001-7009,9000-9099,21,443,873,2601,2604,3128,4440,6082,6379,8888,3389,9200,11211,27017,28017,389,8443,4848,8649,995,9440,9871,2222,2082,3311,18100,9956,1433,3306,1900,49705,50030,7778,5432,7080,5900,50070,5000,5560,10000 --open --max-hostgroup 10 --max-parallelism 10 --max-rtt-timeout 1000ms --host-timeout 800s --max-scan-delay 2000ms -iL ~/Desktop/ip.txt -oN ~/Desktop/result/result.txt

nmap api

nmap支持很多语言的扩展,本文简单介绍下python中如何使用nmap。

python-nmap

安装:pip install python-nmap

作用:利用python调用nmap接口,实现端口扫描。

使用:

1. >>import nmap
1. >>nm = nmap.PortScanner()
1. >>nm.scan('127.0.0.1', '22-443')
1. >>nm.command_line()

更多使用方法,参考:http://xael.org/pages/python-nmap-en.html