脚本扫描流程
  执行脚本扫描时,从nmap_main()中调用script_scan()函数。
  在进入script_scan()后,会标记扫描阶段类型,然后进入到初始化阶段返回的main()函数(来自nse_main.lua脚本中的main)中,在函数中解析具体的扫描类型。
  main()函数负责处理三种类型的脚本扫描:预扫描(SCRIPT_PRE_SCAN)、脚本扫描(SCRIPT_SCAN)、后扫描(SCRIPT_POST_SCAN)。预扫描即在Nmap调用的前面(没有进行主机发现、端口扫描等操作)执行的脚本扫描,通常该类扫描用于准备基本的信息,例如到第三服务器查询相关的DNS信息。而脚本扫描,是使用NSE脚本来扫描目标主机,这是核心的扫描方式。后扫描,是整个扫描结束后,做一些善后处理的脚本,比如优化整理某些扫描。
  在NSE脚本中都会定义触发规则(rule),确定在什么阶段什么条件执行该脚本。NSE共有4中规则,prerule(),如果脚本定义该规则,则在预扫描阶段该脚本会被执行;hostrule(host)该规则在检测到主机在线时候才执行;port(host,port),检测主机某个端口开放时才执行,通常用于侦查特定的服务类型;postrule(),在所有主机都被扫描完毕后执行。
  在main()函数中核心操作由run函数负责。而run()函数的本身设计用于执行所有同一级别的脚本(根据依赖关系划分的级别),直到所有线程执行完毕才退出。
  run()函数中实现三个队列:执行队列(Running Queue)、等待队列(Waiting Queue)、挂起队列(Pending Queue),并管理三个队列中线程的切换,直到全部队列为空或出错而退出。
  3、源码分析
///L_NSE用于保存Lua程序的状态
staticlua_State*L_NSE=NULL;
///open_nse用于创建Lua状态,准备Lua解释器环境
///调用init_main()完成初始化操作。
voidopen_nse(void)
{
if(L_NSE==NULL)///全局维护一份Lua状态
{
/*
Settherandomseedvalueonbehalfofscripts.SinceLuausesthe
Crandandsrandfunctions,whichhaveastaticseedfortheentire
program,wedon'twantscriptsdoingthisthemselves.
*/
srand(get_random_uint());
///创建Lua状态机,用于管理整个Lua程序的执行
if((L_NSE=luaL_newstate())==NULL)
fatal("%s:failedtoopenaLuastate!",SCRIPT_ENGINE);
lua_atpanic(L_NSE,panic);///注册发生严重故障的回调函数为panic函数
#if0
/*Lua5.2*/
lua_pushcfunction(L_NSE,init_main);
lua_pushlightuserdata(L_NSE,&o.chosenScripts);
if(lua_pcall(L_NSE,1,0,0))
#else
///此处lua_cpcall()以保护模式执行C语言函数init_main()
if(lua_cpcall(L_NSE,init_main,&o.chosenScripts))
#endif
fatal("%s:failedtoinitializethescriptengine:n%sn",SCRIPT_ENGINE,
lua_tostring(L_NSE,-1));
}
}
///scipt_scan函数具体执行脚本扫描的过程
///设置扫描状态;调用run_main()函数执行具体脚本扫描过程。
voidscript_scan(std::vector<Target*>&targets,stypescantype)
{
///设置全局的扫描状态为此处状态(可能是SCRIPT_PRE_SCAN/SCRIPT_SCAN/SCRIPT_POST_SCAN)
o.current_scantype=scantype;
///断言L_NSE非空,并清空栈区(C与Lua调用交互过程均会在栈内完成)
assert(L_NSE!=NULL);
lua_settop(L_NSE,0);/*clearthestack*/
#if0
/*Lua5.2*/
lua_pushcfunction(L_NSE,run_main);
lua_pushlightuserdata(L_NSE,&targets);
if(lua_pcall(L_NSE,1,0,0))
#else
///此处lua_cpcall()以保护模式执行C语言函数run_main()
if(lua_cpcall(L_NSE,run_main,&targets))
#endif
error("%s:ScriptEngineScanAborted.nAnerrorwasthrownbythe"
"engine:%s",SCRIPT_ENGINE,lua_tostring(L_NSE,-1));
}
voidclose_nse(void)
{
///关闭Lua状态
if(L_NSE!=NULL)
{
lua_close(L_NSE);
L_NSE=NULL;
}
}
staticintinit_main(lua_State*L)
{
charpath[MAXPATHLEN];
std::vector<std::string>*rules=(std::vector<std::string>*)
lua_touserdata(L,1);
/*Loadsomebasiclibraries*/
luaL_openlibs(L);///加载Lua自身的库
set_nmap_libraries(L);///加载Nmap扩展的Lua库
lua_newtable(L);
lua_setfield(L,LUA_REGISTRYINDEX,NSE_CURRENT_HOSTS);
/*Loaddebug.tracebackforcollectinganyerrortracebacks*/
lua_settop(L,0);/*clearthestack*/
lua_getglobal(L,"debug");
lua_getfield(L,-1,"traceback");
lua_replace(L,1);//debug.tracebackstackposition1
lua_pushvalue(L,1);
lua_setfield(L,LUA_REGISTRYINDEX,NSE_TRACEBACK);/*savecopy*/
/*LoadmainLuacode,stackposition2*/
///将nse_main.lua文件加载进来,文件被转换为匿名函数(栈索引为2),后续调用lua_pcall()执行它。
if(nmap_fetchfile(path,sizeof(path),"nse_main.lua")!=1)
luaL_error(L,"couldnotlocatense_main.lua");
if(luaL_loadfile(L,path)!=0)
luaL_error(L,"couldnotloadnse_main.lua:%s",lua_tostring(L,-1));
/*ThefirstargumenttotheNSEMainLuacodeistheprivatense
*librarytablewhichexposescertainnecessaryCfunctionsto
*theLuaengine.
*/
///加载提供给nse_main.lua调用的C语言函数表(栈索引为3)
open_cnse(L);//stackindex3
/*Thesecondargumentisthescriptrules,includingthe
*files/directories/categoriespassedastheuserdatatothisfunction.
*/
///将脚本规则作为参数压入栈区(栈索引为4)
lua_createtable(L,rules->size(),0);//stackindex4
for(std::vector<std::string>::iteratorsi=rules->begin();
si!=rules->end();si++)
{
lua_pushstring(L,si->c_str());
lua_rawseti(L,4,lua_objlen(L,4)+1);
}
/*GetLuamainfunction*/
///调用由nse_main.lua转换后的匿名函数(栈索引2):
///传入2个参数(栈索引3/4),输出1个结果(执行完毕后放在栈顶),
///错误处理函数对应的栈区索引为1(即debug.traceback)。
///功能:在nse_main.lua会加载用户选择的所有的脚本,并初始化Script/Thread类
if(lua_pcall(L,2,1,1)!=0)lua_error(L);/*wewantedatraceback*/
///将执行nse_main.lua返回的结果(nse_main.lua中的main函数对象)放入注册表中,
///以便后续的脚本扫描过程直接调用此main函数。
lua_setfield(L,LUA_REGISTRYINDEX,NSE_MAIN);
return0;
}
staticintrun_main(lua_State*L)
{
std::vector<Target*>*targets=(std::vector<Target*>*)
lua_touserdata(L,1);
lua_settop(L,0);///清空栈区
/*Newhostgroup*/
lua_newtable(L);///清空当前主机组
lua_setfield(L,LUA_REGISTRYINDEX,NSE_CURRENT_HOSTS);
///读出errortraceback函数
lua_getfield(L,LUA_REGISTRYINDEX,NSE_TRACEBACK);/*index1*/
///获取nse_main.lua中的main()函数
lua_getfield(L,LUA_REGISTRYINDEX,NSE_MAIN);/*index2*/
assert(lua_isfunction(L,-1));///若不是函数,那此处必然有错
/*Thefirstandonlyargumenttomainisthelistoftargets.
*Thishasallthetargetnames,1-N,inalist.
*/
///main(hosts,scantype)
///main函数需要两个参数,被扫描的主机组与扫描类型(PRE/SCRIPT/POST)
///以下代码将逐次加入等待扫描主机到NSE_CURRENT_HOSTS表中
lua_createtable(L,targets->size(),0);//stackindex3
lua_getfield(L,LUA_REGISTRYINDEX,NSE_CURRENT_HOSTS);/*index4*/
for(std::vector<Target*>::iteratorti=targets->begin();
ti!=targets->end();ti++)
{
Target*target=(Target*)*ti;
constchar*TargetName=target->TargetName();
constchar*targetipstr=target->targetipstr();
lua_newtable(L);
set_hostinfo(L,target);
lua_rawseti(L,3,lua_objlen(L,3)+1);
if(TargetName!=NULL&&strcmp(TargetName,"")!=0)
lua_pushstring(L,TargetName);
else
lua_pushstring(L,targetipstr);
lua_pushlightuserdata(L,target);
lua_rawset(L,4);/*addtoNSE_CURRENT_HOSTS*/
}
lua_pop(L,1);/*popNSE_CURRENT_HOSTS*/
///设置main()第二个参数,扫描类型
/*pushscriptscantypephase*/
switch(o.current_scantype)
{
caseSCRIPT_PRE_SCAN:
lua_pushstring(L,NSE_PRE_SCAN);
break;
caseSCRIPT_SCAN:
lua_pushstring(L,NSE_SCAN);
break;
caseSCRIPT_POST_SCAN:
lua_pushstring(L,NSE_POST_SCAN);
break;
default:
fatal("%s:failedtosetthescriptscanphase.n",SCRIPT_ENGINE);
}
///以保护模式运行main()函数,两个参数,0个返回值,错误处理函数在栈区的index1位置
if(lua_pcall(L,2,0,1)!=0)lua_error(L);/*wewantedatraceback*/
return0;
}