0
点赞
收藏
分享

微信扫一扫

Nmap 扩展(二)

数数扁桃 2022-02-26 阅读 87

一、NSE 引擎执行流程

Nmap的扩展脚本语言都基于lua来开发的,执行也是调用了内部封装的lua解释器。

正常情况下,调用任何一个扩展脚本会首先执行 nse_main.lua,该脚本主要做了以下几件事:

二、验证 nse_main.lua 最先执行

使用vim编辑器修改 nse_main.lua

在第一行添加:

保存后,运行一个脚本观察效果:

可以发现,在nmap启动后就会执行nse_main.lua中的代码。

三、扩展脚本执行规则

在nse_main.lua的64行左右,定义了一些规则: 

-- Table of different supported rules.
local NSE_SCRIPT_RULES = {
  prerule = "prerule",
  hostrule = "hostrule",
  portrule = "portrule",
  postrule = "postrule",
};

 每一个规则代表了函数,由函数的返回值决定执行流程:

也就是说,prerule和postrule是在开始和结束运行,并且只运行一次,hostrule是扫描一个主机就运行一次,有N个主机就会运行N次,portrule是扫描到一个端口就运行一次,有N个端口就运行N次。 

为了验证得出的结论,写了一个测试脚本:

内容如下: 

prerule=function()
	print("prerule()")
end
hostrule=function(host)
	print("hostrule()")
end
portrule=function(host,port)
	print("portrule()")
end
action=function()
	print("action()")
end
postrule=function()
	print("postrule()")
end

 使用nmap扫描一个主机调用这个脚本看看执行效果:

至此我们就清楚了扩展脚本的执行流程,后续我们将会慢慢细化其中的知识点。

四、扩展脚本— action 函数

在前面的代码中,我们发现了action这个函数,它的主要作用是用于在portrule或hostrule返回true之后自动执行的函数。

local stdnse = require "stdnse"
prerule=function()
end
hostrule=function(host)
	return true
end
portrule=function(host,port)
	return true
end
action = function()
	print("[*]action ...")
end
postrule=function()
end

本机开启了一个80端口,但是action执行了两次:

第一次是hostrule返回true而调用的,第二次是由portrule函数扫描到80端口返回true而调用的。 

举报

相关推荐

0 条评论