HrPapers|Nmap渗透测试指南
字数 1500 2025-08-29 08:32:09
Nmap脚本引擎(NSE)渗透测试指南
一、NSE简介
Nmap脚本引擎(NSE)是Nmap功能的重要扩展,于2007年谷歌夏令营期间推出。它通过Lua脚本语言扩展了Nmap的功能,目前包含14个类别的脚本,涵盖从网络发现到安全漏洞检测和利用的广泛任务。
NSE基本使用
nmap -sV -sC scanme.nmap.org
-sV: 服务检测-sC: 启用默认NSE脚本(等同于--script default)- 默认脚本被认为是安全的,不会干扰目标服务
二、NSE脚本分类
| 类别 | 描述 |
|---|---|
| auth | 用户认证相关脚本 |
| broadcast | 使用广播收集网络信息 |
| brute | 暴力破解脚本 |
| default | 默认脚本(-sC) |
| discovery | 主机和服务发现 |
| dos | 拒绝服务攻击相关 |
| exploit | 安全漏洞利用 |
| external | 第三方服务脚本 |
| fuzzer | 模糊测试脚本 |
| intrusive | 入侵性脚本 |
| malware | 恶意软件检测 |
| safe | 安全脚本 |
| vuln | 安全漏洞检测和利用 |
| version | 高级系统脚本 |
三、NSE脚本选择
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. 高级选择表达式
# 排除特定类别
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>
四、NSE脚本参数
基本参数设置
nmap -sV --script http-title --script-args http.useragent="Mozilla 1337" <target>
共享参数处理
nmap --script http-majordomo2-dir-traversal,http-axis2-dir-traversal \
--script-args http-axis2-dir-traversal.uri=/axis2/,uri=/majordomo/ <target>
五、NSE脚本开发基础
Lua语言基础
NSE脚本使用Lua语言编写,需要掌握以下基本概念:
-
注释:
-- 单行注释 --[[ 多行注释 --]] -
标识符:
- 以字母或下划线开头
- 区分大小写
- 避免使用保留关键字
-
数据类型:
- nil
- boolean
- number
- string
- function
- userdata
- thread
- table
-
变量:
- 全局变量:默认
- 局部变量:使用
local声明
NSE脚本结构
-
导入库:
local stdnse = require "stdnse" local http = require "http" -
脚本描述:
description = [[脚本描述]] -
Rule类型:
- prerule: Nmap扫描前触发
- hostrule: 主机发现或探测时触发
- portrule: 端口扫描时触发
- postrule: Nmap结束时触发
-
Action函数:执行主要逻辑
Rule类型详解
-
Prerule示例:
prerule = function(host, port) return true end -
Hostrule示例:
hostrule = function(host) return host.os ~= nil end -
Portrule示例:
portrule = function(host, port) return port.protocol == "tcp" and port.state == "open" end -
Postrule示例:
postrule = function() return true end
六、NSE脚本开发实战
1. 获取网站客服电话示例
local http = require "http"
local string = require "string"
description = [[获取网站客服电话]]
portrule = function(host, port)
return port.service == "http" and port.state == "open"
end
action = function(host, port)
local response = http.get(host, port, "/")
local phone = string.match(response.body, "%d+-%d+")
if phone then
return "客服电话: " .. phone
end
end
2. 检测反射型XSS漏洞
local http = require "http"
local string = require "string"
local stdnse = require "stdnse"
description = [[检测zzcms8.2反射型XSS]]
portrule = function(host, port)
return port.service == "http" and port.state == "open"
end
action = function(host, port)
local options = {
headers = {
["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, "/install/index.php", options, nil, postdata)
if response.status == 200 and string.find(response.body, "alert") then
return "存在反射型XSS漏洞"
end
end
七、调试技巧
使用-d选项进行调试:
nmap -d 3 --script your_script.nse <target>
- 调试等级越高,输出信息越详细
- 等级3通常足够用于一般调试
八、常用库函数
http库
http.get(host, port, path, options): 发起GET请求http.post(host, port, path, options, ignored, postdata): 发起POST请求
stdnse库
stdnse.format_output(true, table): 格式化输出stdnse.get_script_args(): 获取脚本参数
string库
string.match(str, pattern): 模式匹配string.find(str, pattern): 查找字符串
九、最佳实践
- 为脚本添加详细的描述和用法示例
- 使用
local声明局部变量 - 合理处理错误和异常情况
- 避免使用过于侵入性的脚本
- 为脚本添加适当的分类和标签
- 使用调试模式测试脚本
通过掌握这些NSE脚本开发技术,您可以扩展Nmap的功能,实现自定义的网络探测和安全检测任务。