6 ,nmap NSE中的API port table(表),NSE库文件编写
书名诸神之眼NMAP 学习笔记
20年6月11日7:23
port table(表)
-
port.number字段
这个字段标识了目标端口
咧
代码
我保存到了名为wode.nse
local shortport = require "shortport" description = [[]] author = "root" license = "Same as Nmap--See http://nmap.org/book/man-legal.html" categories = {"default"} portrule = function(host, port) return true end action = function(host, port) return port.number end
叫
wode.nse
移动到/usr/share/nmap/scripts/
文件下面
复制进去要更新一下nmap脚本的数据库
命令nmap --script-updatedb
结果
命令sudo nmap --script wode 192.168.31.21
-
port.protocol
这个字段是识别TCP和UDP的端口的类型
咧
代码
我保存到了名为wode.nse
local shortport = require "shortport" description = [[]] author = "root" license = "Same as Nmap--See http://nmap.org/book/man-legal.html" categories = {"default"} portrule = function(host, port) return true end action = function(host, port) return port.protocol end
叫
wode.nse
移动到/usr/share/nmap/scripts/
文件下面
复制进去要更新一下nmap脚本的数据库
命令nmap --script-updatedb
结果
命令sudo nmap --script wode 192.168.31.21
-
port.service字段
字段是目标的端口的运行的服务
咧
代码
我保存到了名为wode.nse
local shortport = require "shortport" description = [[]] author = "root" license = "Same as Nmap--See http://nmap.org/book/man-legal.html" categories = {"default"} portrule = function(host, port) return true end action = function(host, port) return port.service end
叫
wode.nse
移动到/usr/share/nmap/scripts/
文件下面
复制进去要更新一下nmap脚本的数据库
命令nmap --script-updatedb
结果
命令sudo nmap --script wode 192.168.31.21
-
port.version字段
字段中保存了通过服务扫描发现的版本信息,包括name、name_confidence、product、version、extrainfo、hostname、ostype、devicetype、service_tunnel、service_ftp以及cpe_code等字段。注意这个字段需要使用参数-sV
咧
代码local shortport = require "shortport" description = [[]] author = "root" license = "Same as Nmap--See http://nmap.org/book/man-legal.html" categories = {"default"} portrule = function(host, port) return true end action = function(host, port) return port.version end
叫
wode.nse
移动到/usr/share/nmap/scripts/
文件下面
复制进去要更新一下nmap脚本的数据库
命令nmap --script-updatedb
结果
命令sudo nmap --script wode 192.168.31.21 -sV
-
port.state字段
存放端口的状态
咧
代码local shortport = require "shortport" description = [[]] author = "root" license = "Same as Nmap--See http://nmap.org/book/man-legal.html" categories = {"default"} portrule = function(host, port) return true end action = function(host, port) return port.state end
叫
wode.nse
移动到/usr/share/nmap/scripts/
文件下面
复制进去要更新一下nmap脚本的数据库
命令nmap --script-updatedb
结果
命令sudo nmap --script wode 192.168.31.21
NSE中的异常处理
代码
local nmap = require "nmap"
local comm = require "comm"
local shortport = require "shortport"
description = [[]]
author = "root"
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
categories = {"default,discovery,safe"}
portrule = shortport.port_or_service(79, "finger")
action = function(host, port)
try = nmap.new_try()
return try(comm.exchange(host, port, "\r\n",
{lines=100, timeout=5000}))
end
上面代码说明
nmap提供了nmap库,叫nmap
监控异常的代码放置在Nmap.new_try()函数的括号中即可,这个函数的第一个返回值就表明了状态。如果返回值为false或者nil,第二个返回值就是一个错误相关的字符串
如果comm.exchange正常执行的话,就可以返回原本的值,如果出现异常,就可以返回这个异常
NSE中的注册表
NSE注册表也是一个Lua
tablc 类型的数据文件,他主要用来保持住一次扫描中各个脚本之间共享的变量,这个注册表保持住一个名为nmap.refistry的变量中。举个例子,在使用脚本对目标的口令进行**的时候,就可以使用这个注册表把已经**的用户密码保持起来,已提供其他脚本的使用。例如,**得到目标的用户admin,密码123456,就会执行一个插入操作
table.iNSErt(Nmap.registry.credentials.http,
{ username = admin, password =123456 } )
NSE中的库文件
-
库文件的位置
库文件在
/usr/share/nmap/nselib
halcyon编辑器会自动按照nmap的路径进行找到库
这些库文件涵盖了几乎当前所有的流行协议、常见的字符串处理操作,甚至包含了用来实现对用户名和密码进行**的brute库文件。当在编写NSE脚本的时候,你可能会考虑到代码重构的问题。最好的解决方法还是将核心的代码创建为NSE的库文件。事实上,NSE库文件的创建是非常简单的。NSE中的库文件大都是使用Lua语言编写的,但是如果你使用C或者C++语言也是可行的
NSE库文件编写和调用
-
NSE库文件编写
咧
代码保存的文件名
a.lua
保持到/usr/share/nmap/nselib
目录里面function b(port) return string.format("The port '%s' is open",port) end
代码说明
function 定义函数的,定义了一个a函数传参是port
string.format函数是一个类似printf的格式化字符串我看到了一个写的很详细的在这个地址https://blog.csdn.net/hello_crayon/article/details/50667927 -
调用
NSE脚本调用
编写一个NSE脚本
代码
保持的文件名为wode.nse
local shortport = require "shortport" local a = require "a" description = [[]] author = "root" license = "Same as Nmap--See http://nmap.org/book/man-legal.html" categories = {"default"} portrule = function(host, port) return true end action = function(host, port) return b(port.number) end
代码调用说明
上面的代码local a = require "a"
就是调用上面编写的a.lua的脚本文件
上面的代码return b(port.number)
代码就是b就是a.lua的脚本文件里面的函数,port.number传参给a.lua的脚本文件文件里面的port叫
wode.nse
移动到/usr/share/nmap/scripts/
文件下面
复制进去要更新一下nmap脚本的数据库
命令nmap --script-updatedb
结果
命令sudo nmap --script wode 192.168.31.21