| 漏洞扫描 --编写Nmap脚本 |
| 2006年12月份,Nmap4.21 ALPHA1版加入脚本引擎,并将其作为主线代码的一部分。NSE脚本库如今已经有400多个脚本,覆盖了各种不同的网络机制(从SMB漏洞检测到Stuxnet探测,及中间的一些内容)。NSE的强大,依赖它强大的功能库,这些库可以非常容易的与主流的网络服务和协议,进行交互。 |
挑战 |
| 我们经常会扫描网络环境中的主机是否存在某种新漏洞,而扫描器引擎中没有新漏洞的检测方法,这时候我们可能需要自己开发扫描工具。 |
| 你可能已经熟悉了某种脚本(例如:Python,Perl,etc.),并可以快速写出检测漏洞的程序。但是,如果面临许多主机时, 针对两三个主机的检测方法,可能并不奏效。 |
| Nmap 解救你 !使用内嵌的Lua语言和强大的集合库,你可以结合nmap高效的主机和端口扫描引擎,开发出针对多数主机的检测方法。 |
实现 |
| Nmap 引擎脚本,由Lua编程语言、NmapAPI、系列强大的NSE库实现。为了达到本文的目的,现假设某个应用中存在一个叫ArcticFission漏洞。与许多其他的web应用程序类似,可以通过探测特定的文件,假设这个文件就是/arcticfission.html,用正则表达式提取文件内容中的版本号,与有漏洞的值进行对比.听起来好像很简单,让我们开始吧 ! |
框架代码 |
| 基于传统的语言标准,我们写一个脚本,作用:遇到开放的HTTP端口,就返回”Hello World”。 |
| -- The Head Section -- -- The Rule Section -- portrule = function(host, port) return port.protocol == "tcp" and port.number == 80 and port.state == "open" end -- The Action Section -- action = function(host, port) return "Hello world !" end |
| 注意:以--起始的行表示注释。 |
| NSE脚本主要由三部分组成: |
| The Head Section 该部分包含一些元数据,主要描述脚本的功能,作者,影响力,类别及其他。 |
| The Rule Section 该部分定义脚本执行的必要条件。至少包含下面列表中的一个函数: 此案例中,重点介绍portrule。portrule能够在执行操作前,检查host和port属性。portrule会利用nmap的API检查TCP80端口。 |
| The Action Section 该部分定义脚本逻辑。此处案例中,检测到开放80端口,则打印“HelloWorld”。脚本的输出内容,会在nmap执行期间显示出来。 |
| root@security:/home/offensive/nmap_nse# nmap -sS -p 22,80,443 --script /home/offensive/nmap_nse/http-vuln-check.nse www.exploit-db.com Starting Nmap 6.47 ( http://nmap.org ) at 2014-09-29 10:39 EDT Nmap scan report for www.exploit-db.com (192.99.12.218) Host is up (0.47s latency). Other addresses for www.exploit-db.com (not scanned): 198.58.102.135 rDNS record for 192.99.12.218: cloudproxy71.sucuri.net PORT STATE SERVICE 22/tcp filtered ssh 80/tcp open http |_http-vuln-check: Hello world ! 443/tcp open https |
调用脚本库 |
| 优秀的库集合,促使其变的强大。例如,可调用现有库中的函数,针对http端口创建portrule。此处用到了shortport. |
| local shortport = require "shortport" -- The Rule Section -- portrule = shortport.http -- The Action Section -- action = function(host, port) return "Hello world!" end |
| 同样的扫描,产生了不同的结果 |
| root@security:/home/offensive/nmap_nse# nmap -sS -p 22,80,443 --script /home/offensive/nmap_nse/http-vuln-check_shortport.nse www.exploit-db.com Starting Nmap 6.47 ( http://nmap.org ) at 2014-09-29 10:36 EDT Nmap scan report for www.exploit-db.com (192.99.12.218) Host is up (0.46s latency). Other addresses for www.exploit-db.com (not scanned): 198.58.102.135 rDNS record for 192.99.12.218: cloudproxy71.sucuri.net PORT STATE SERVICE 22/tcp filtered ssh 80/tcp open http |_http-vuln-check_shortport: Hello world! 443/tcp open https |_http-vuln-check_shortport: Hello world! Nmap done: 1 IP address (1 host up) scanned in 6.32 seconds |

本文将引导读者如何创建自定义的nmap扫描脚本,包括理解nmap脚本引擎的工作原理,设置脚本参数,以及调试和优化脚本的过程。
最低0.47元/天 解锁文章
7万+

被折叠的 条评论
为什么被折叠?



