nmap之nse脚本简单学习
nmap之nse脚本简单学习
环境:centos8
nmap安装
yum -y install nmap
-- 版本
[root@qingchen /]# nmap -version
Nmap version 7.70 ( https://nmap.org )
脚本入门学习
cd /usr/share/nmap
[root@qingchen nmap]# ls
nmap.dtd nmap-mac-prefixes nmap-os-db nmap-payloads nmap-protocols nmap-rpc nmap-service-probes nmap-services nmap.xsl nselib nse_main.lua scripts-- nselib scripts是脚本库和脚本都是lua语言编写的
cd /usr/share/nmap/scripts
nmap的脚本存放路径,自己写的脚本放进去就可以使用了,脚本语言是 lua
lua学习参考菜鸟或者https://blog.csdn.net/qq_40893942/article/details/127986572
NSE脚本基本格式
一个完整的NSE脚本通常都有这么几个部分的代码字段:
description 字段:本脚本的说明介绍。
categories 字段:本脚本的分类。Nmap执行脚本除了指定单个脚本外,还可以指定某一类脚本,比如default类,我们没有使用–script参数时,默认会加载这一类的脚本。
rule 字段:本脚本的执行规则,也即触发脚本执行的条件会在rule字段定义。一般执行规则是一个lua函数,返回值只有true和false两种。
action字段:脚本执行的具体内容。rule字段返回true时会执行action字段定义的函数。
local shortport = require "shortport"
description = [[demo]]
author = "qingchen"
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
categories = {"default"}portrule = function( host, port )return true
endaction = function(host, port)end
Demo中rule字段是portrule,NSE脚本的执行规则是和nmap的扫描相结合的,两者执行的先后顺序目前有如下4种。
- prerule():规则早于nmap的扫描,执行的顺序是先执行脚本,后nmap扫描。
- hostrule():nmap完成了主机发现之后运行脚本。
- portrule():nmap执行了端口扫描后运行脚本。
- postrule():nmap完成所有的扫描后才执行脚本。
编写脚本
在前面脚本demo代码的基础上,只需修改portrule函数的代码和让action函数来输出。
栗子:
编写文件名为qingchen-simple-test.nse
的脚本放在/usr/share/nmap/scripts
,判断80端口是不是tcp协议。内容如下:
-- 引用shortport脚本库
local shortport = require "shortport"
-- 描述
description = [[qingchen port simple test]]
-- 作者
author = "qingchen"
-- license
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
-- 类别
categories = {"default"}portrule = function(host,port)return port.protocol == "tcp" and port.number == 80
endaction = function(host, port)
print(host.ip)
return "qingchen-script-test"
end
nmap --script-updatedb
命令用来更新脚本库(貌似不更新也是可以直接在scripts文件夹下识别到自定义脚本的)
nmap -p 80 127.0.0.1 --script qingchen-simple-test.nse
运行脚本
[root@qingchen scripts]# vim qingchen-simple-test.nse
[root@qingchen scripts]# nmap --script-updatedb
[root@qingchen scripts]# nmap -p 80 127.0.0.1 --script qingchen-simple-test.nse
Starting Nmap 7.70 ( https://nmap.org ) at 2022-11-24 13:41 CST
127.0.0.1
Nmap scan report for VM-4-3-centos (127.0.0.1)
Host is up (0.000052s latency).PORT STATE SERVICE
80/tcp open http
|_qingchen-simple-test: qingchen-script-testNmap done: 1 IP address (1 host up) scanned in 0.45-- 换8080试试[root@qingchen scripts]# nmap -p 8080 127.0.0.1 --script qingchen-simple-test.nse
Starting Nmap 7.70 ( https://nmap.org ) at 2022-11-24 13:44 CST
Nmap scan report for VM-4-3-centos (127.0.0.1)
Host is up (0.000046s latency).PORT STATE SERVICE
8080/tcp closed http-proxyNmap done: 1 IP address (1 host up) scanned in 0.44 seconds
编写脚本库文件
在/nselib/
文件夹下新建一个名为qingchenlib.lua
的文件,填入如下内容:
function Porttest(port)return string.format("The port '%s' is open",port)
end
作用是查看哪些端口是开放的
使用脚本库
编写脚本 qingchen-lib-test.nse
local shortport = require "shortport"
local qingchenlib = require "qingchenlib"description = [[引用库文件测试]]author = "qingchen"
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
categories = {"default"}portrule = function( host, port )return true
endaction = function(host,port)return Porttest(port.number)
end
引用库文件使用local,格式一般为: local 库文件名 = require “库文件名”,引用后可直接使用库里面的方法和属性值
Nmap 命令:nmap -Pn 127.0.0.1 --script qingchen-lib-test.nse(可以不带后缀)
[root@qingchen nselib]# nmap -Pn 127.0.0.1 --script qingchen-lib-test
Starting Nmap 7.70 ( https://nmap.org ) at 2022-11-24 13:58 CST
Nmap scan report for VM-4-3-centos (127.0.0.1)
Host is up (0.0000050s latency).
Not shown: 996 closed ports
PORT STATE SERVICE
22/tcp open ssh
|_qingchen-lib-test: The port '22' is open
80/tcp open http
|_qingchen-lib-test: The port '80' is open
3306/tcp open mysql
|_qingchen-lib-test: The port '3306' is open
9000/tcp open cslistener
|_qingchen-lib-test: The port '9000' is openNmap done: 1 IP address (1 host up) scanned in 1.80 seconds
自定义脚本对mysql数据库操作
涉及库文件
- mysql:用来进行数据库操作。
- nmap:通过nmap建立socket连接mysql。
- shortport:基本的port规则库。
创建数据库和存放结果的表
在你的MySql中建一个名为nmap的数据库,然后建立表和字段:
CREATE TABLE IF NOT EXISTS nmap.scanData (date varchar(40),hostos varchar(256),hostname varchar(100), ip varchar(16), port integer(5), protocol varchar(3), state varchar(20), service varchar(256), version varchar(256)
);
脚本
mysql-test.nse
local mysql = require "mysql"
local nmap = require "nmap"
local shortport = require "shortport"-- 登陆mysql
local function mysqlLogin(socket, username, password)local status, response = mysql.receiveGreeting( socket )if ( not(status) ) thenreturn responseendreturn mysql.loginRequest( socket, { authversion = "post41", charset = response.charset }, username, password, response.salt )
enddescription = [[mysql test]]
author = "qingchen"
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
categories = {"default"}
portrule = function () return true endfunction portaction (host,port)local host_local="127.0.0.1"local port_local="3306"local username="root"local password="1234"local hostos_str= host.oslocal version = port.versionif (port.version.product~=nil) thenversion = port.version.productendif (port.version.version~=nil) thenversion = version .. port.version.versionendlocal date=os.date("%Y-%m-%d %H:%M:%S")local sql = string.format("INSERT INTO nmap.scanData (date,hostos,hostname,ip, port,protocol,state,service,version) VALUES ('%s','%s','%s', '%s', %d, '%s', '%s', '%s', '%s');select 1",date,hostos_str,host.name,host.ip, port.number,port.protocol,port.state,port.service,version)local socket = nmap.new_socket()if ( not(socket:connect(host_local, port_local)) ) thenreturn fail("Failed to connect to server")endlocal status, response = mysqlLogin(socket, username, password)if ( status ) thenlocal status, rs = mysql.sqlQuery( socket, sql )socket:close()elsesocket:close()end
end
local ActionsTable = {portrule = portaction
}
-- execute the action function corresponding to the current rule
action = function(...) return ActionsTable[SCRIPT_TYPE](...) end
执行 nmap -O 127.0.0.1 --script mysql-test
[root@qingchen nselib]# nmap -O 127.0.0.1 --script mysql-test
Starting Nmap 7.70 ( https://nmap.org ) at 2022-11-24 14:08 CST
Nmap scan report for VM-4-3-centos (127.0.0.1)
Host is up (0.0000050s latency).
Not shown: 996 closed ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
3306/tcp open mysql
9000/tcp open cslistener
Device type: general purpose
Running: Linux 3.X
OS CPE: cpe:/o:linux:linux_kernel:3
OS details: Linux 3.7 - 3.10
Network Distance: 0 hopsOS detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 4.11 seconds
查看数据库已经写入数据了
但是其中hostos和version是lua中table格式的数据这里没有转成string所以看不到具体数据
可以写一个lua库文件作用是:把table转成string
table2string.lua
放入nselib库中
function ToStringEx(value)if type(value)=='table' thenreturn TableToStr(value)elseif type(value)=='string' thenreturn "\\'"..value.."\\'"elsereturn tostring(value)end
endfunction TableToStr(t)if t == nil then return "" endlocal retstr= "{"local i = 1for key,value in pairs(t) dolocal signal = ","if i==1 thensignal = ""endif key == i thenretstr = retstr..signal..ToStringEx(value)elseif type(key)=='number' or type(key) == 'string' thenretstr = retstr..signal..'['..ToStringEx(key).."]="..ToStringEx(value)elseif type(key)=='userdata' thenretstr = retstr..signal.."*s"..TableToStr(getmetatable(key)).."*e".."="..ToStringEx(value)elseretstr = retstr..signal..key.."="..ToStringEx(value)endendendi = i+1endretstr = retstr.."}"return retstr
end
注意return "\\'"..value.."\\'"
这里要加两个转义字符不然就会报错 like this
connect sucess
INSERT INTO nmap.scanData (date,hostos,hostname,ip, port,protocol,state,service,version) VALUES ('2022-11-24 14:51:45','nil','VM-4-3-centos', '127.0.0.1', 9000, 'tcp', 'open', 'cslistener', '{['cpe']={},['service_tunnel']='none',['service_dtype']='table',['name']='cslistener',['name_confidence']=3.0}');
false
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cpe']={},['service_tunnel']='none',['service_dtype']='table',['name']='http',['n' at line 1
false
-- 插入数据库时也需要对单引号转义否则插入失败
写完库文件后,我们只需要引用一下我们写的库调用一下table2string
完整代码如下:
local mysql = require "mysql"
local nmap = require "nmap"
local shortport = require "shortport"
local table2string = require "table2string"local function mysqlLogin(socket, username, password)local status, response = mysql.receiveGreeting( socket )if ( not(status) ) thenreturn responseendreturn mysql.loginRequest( socket, { authversion = "post41", charset = response.charset }, username, password, response.salt )
end
description = [[mysql save test]]
author = "qingchen"
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
categories = {"default"}
portrule = function () return true end
function portaction (host,port)local host_local="127.0.0.1"local port_local="3306"local username="root"local password="1234"local hostos_str = ToStringEx(host.os)local version = ToStringEx(port.version)if (port.version.product~=nil) thenversion = port.version.productendif (port.version.version~=nil) thenversion = version .. port.version.versionendlocal date=os.date("%Y-%m-%d %H:%M:%S")local sql = string.format("INSERT INTO nmap.scanData (date,hostos,hostname,ip, port,protocol,state,service,version) VALUES ('%s','%s','%s', '%s', %d, '%s', '%s', '%s', '%s');",date,hostos_str,host.name,host.ip, port.number,port.protocol,port.state,port.service,version)local socket = nmap.new_socket()if ( not(socket:connect(host_local, port_local)) ) thenreturn fail("Failed to connect to server")endlocal status, response = mysqlLogin(socket, username, password)-- 这里我打印了连接状态和sql语句,方便查看定位错误if ( status ) thenprint("connect sucess")print(sql)local status, rs = mysql.sqlQuery( socket, sql )print(status)print(rs)socket:close()elsesocket:close()end
end
local ActionsTable = {portrule = portaction
}
-- execute the action function corresponding to the current rule
action = function(...) return ActionsTable[SCRIPT_TYPE](...) end
执行命令:nmap -O 127.0.0.1 --script mysql-save-test
可以看到打印出数据了
end
到此就是完成对nmap脚本nse的简单学习了
nmap之nse脚本简单学习相关推荐
- NMAP 使用 NSE 脚本
Nmap 有一个少为人知的部分是NSE脚本引擎,NSE是 Nmap 的最强大和灵活的功能之一.它允许用户编写(并共享)简单的脚本来自动执行各种网络任务.Nmap 内置了全面的 NSE 脚本集合,用户可 ...
- Nmap-06:Nmap的NSE脚本使用
目录 1.NSE介绍 2.NSE的使用 3.NSE分类使用 4.NSE调试功能使用 5.NSE参数的使用 6.NSE更新 7.NSE脚本分类 1.NSE介绍 NSE(Nmap Script Engin ...
- Unity游戏脚本简单学习
Unity游戏脚本 1.脚本操作游戏对象 1.1.创建游戏对象 创建一个空的游戏对象 GameObject obj = new GameObject("obj1"); 用该方法创建 ...
- 通过nmap的nse脚本(dns-brute.nse)扫描和收集子域名
- Nmap NSE脚本使用
NSE脚本使用 NSE介绍 NSE使用 探测web服务的title信息(http-title) 探测http服务的http头(http-headers) NSE分类使用 基本使用 使用发现和版本信息分 ...
- Nmap学习10 - 对目标主机使用 NSE 脚本
Nmap学习10 - 对目标主机使用 NSE 脚本 脚本类别 --script 使用脚本 使用类别 brute 枚举.暴力破解 使用多个类别 使用文件名 dns-brute 枚举子域名 broadca ...
- 一些Nmap NSE脚本推荐
前言 Nmap是一款强大的开源扫描工具.同时Nmap提供了强大的脚本引擎(Nmap Scripting Engine),支持通过Lua脚本语言来扩展Nmap的功能,在Nmap的发行版中已经包含了数百个 ...
- Linux中nmap脚本的目录,在Linux中,如何使用Nmap脚本引擎(NSE )脚本
Nmap是一个流行的,功能强大,跨平台的命令行网络安全扫描和探测工具,您可以使用它来查找所有活动主机的IP地址,扫描在这些主机上运行的开放端口和服务等等. Nmap的一个有趣的特性是Nmap脚本引擎( ...
- nmap加载nse脚本在内网渗透中的使用-上
转载自:https://mp.weixin.qq.com/s/zEgHxJEOfaiYVZYmg7NnXA? 大多数情况下,大家都认为nmap只是一个扫描工具,而不把当成是一个渗透工具.nmap集成了 ...
最新文章
- 批量修改漫游配置文件路径
- install ros indigo tf2
- C++ const char* 学习
- 基于SpringBoot+Vue开发的前后端分离博客项目-Java后端接口开发
- 13-union 、distinc、 join
- Python的PyDBG调试器的用法
- RedHat6使用centos6的yum源
- matlab进行分子动力学模拟,一种基于分子动力学模拟测试碳纳米管力学性能的方法与流程...
- java 身份证地址提取籍贯_excel从身份证地址中提取籍贯
- IT项目管理之第6章 项目成本管理习题之案例分析汇总
- ubuntu里解决Firefox登陆12306问题
- 【图像检测-缺陷检测】基于灰度共生矩阵实现痕迹检测matlab代码
- 怎样制作Lrc歌词文件
- SpringBoot基础知识
- 在商城项目开发中怎么保证促销商品不会超卖
- HowTo如何制作一个文字冒险游戏-里篇(1)
- charles系列破解激活办法(最高charles4.2都可以激活)
- JavaScript 指南 - 使用对象
- 贺州教师评职称计算机考试,2017年广西贺州市中职教师系列中级职称评选结果公示...
- 每日学习参考答案(1000-1021)
热门文章
- HistCite的Format: Unknown问题如何解决
- 翻译君Mobx,Ten minute introduction to MobX and React
- c++ SIMD 样例
- android反射替换字体,快速使用反射更换Android全局字体
- 解决:Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse
- CAD怎么保存为JPG图片?这个方法了解一下
- 易康_最佳分割尺度的选择——ESP2工具的使用
- 双机热备、双机互备和双机双工,这三者之间的区别
- An import path cannot end with a ‘.ts’ extension. Consider importing ‘@/*.js’ instead.
- Android创建桌面快捷方式所遇到的问题与解决方案