Welcome to In Depth Defense. In Depth Defense LLC is a privately owned Information Security Consulting company owned and operated by Mark Baggett. In Depth Defense specializes in Penetration Testing and Incident Response. At this time In Depth Defense is not accepting any new client work, but we are happy to speak to you and point you to other resources in the community.

Mark Baggett has been active in Information Security for 18+ years. I've served in a variety of roles from software developer to CISO. You can find archives of older blog entries below and read my newer posts on http://www.pauldotcom.com, http://isc.sans.edu and http://pen-testing.sans.org

Wednesday, June 25, 2008

First Stab at NSE Scripting

Over the weekend I decided to take my first look at the NMAP scripting engine.  I’ve read about it, but had not really tried it until now.  First, here is how to use the built in scripts.  First make sure you have the latest scripts.   Similar to NIKTO and other vulnerability scanning systems NMAP has the ability to update its detection scripts.   To update your scripts type this;

nmap --script-updatedb

This will download the latest .NSE scripts from the nmap site.   The scripts (by default) are located in /usr/local/share/nmap/scripts.  

Here is the list of scripts as of today:

You can run ALL of these scripts against a host like this…


Macintosh:scripts mark.baggett$ nmap localhost -n --script all           

Starting Nmap 4.65 ( http://nmap.org ) at 2008-06-20 10:44 EDT

Interesting ports on


80/tcp open  http

|_ HTML title: Test Page for Apache Installation

Nmap done: 1 IP address (1 host up) scanned in 42.216 seconds


-n   : says do not do an DNS query

--script all : tells it to run all the scripts against the host


The scripts themselves tell nmap which ports they are applicable to.  So any scripts related to PORT 25 (smtp) will not run on a host unless port 25 is open for that host.

You can also pass a script CATEGORY and nmap will run all of the scripts in that category.   Categories include SAFE, DEMO, INTRUSIVE, DISCOVERY, VULERABILTY, VERSION and BACKDOOR


Nmap –sS –P0 –n –script INTRUSIVE, VULNERABILITY localhost

Will run all the scripts that have been classified by the script author as “INTRUSIVE” and run all of the scripts that have been categorized as VULNERABILTY against hosts which have the applicable ports open

You can get more information on a script by looking at its sourcce.  For example:

Cat /usr/local/share/nmap/scripts/robots.nse will show you the script that displays disallowed entries from the robots.txt file on web servers.

That’s all good, but I want to try my own scripts.    NMAP uses a LUA interpreter to process its scripts.   I’d never heard of LUA, but it looks pretty intuitive so I thought I’d give it a go.    I figured I’d write a script that would look for Apache servers with the USERDIR directory

 enabled, then brute force the usernames off of the system.  In writing it I found it was easier to use the LUA interpreter built into my Powerbook (as a result of the NMAP install??) rather than repeatedly running it through NMAP.  But since my scripts would use functions defined in nmap I needed to be in the /usr/local/share/nmap/nselib directory when I ran the script.  For example …

Macintosh:nselib mark.baggett$ pwd


Macintosh:nselib mark.baggett$ lua ~/HTTPApacheUsers2.txt

BUT there is one important thing to do when debugging your scripts using the LUA interpreter rather than running the code through nmap.   Nmap calls the following section of code when it determines the script needs to be run.  This is the MAIN function of your nmap script.  But LUA doesn’t have a concept of MAIN.   So for testing in LUA you will want to comment out the function header and footer with a double dash (--) and statically set the parameters to your function.  For example 


action = function(host, port)

-- do stuff here

return output


Becomes this:

--action = function(host, port)

host = BLA.Com

port = 90

-- do stuff here

-- return output


So after 3 hours of learning the nuances of LUA, I came up with the script below.  Recursive code isn’t the fastest way to do this, but the network I/O will be the real bottle neck.  To make it faster I’d need to parallelize the GET request and that would probably take a little more LUA experience.   To me it means the NSE is very powerful tool and could prove to be a viable alternative to Nessus in the future. 

Here is a sample run.   Now I can just give it an NSE extension and add it to my /usr/local/share/nmap/scripts directory!

Macintosh:scripts mark.baggett$ nmap localhost -p 80 -n --script ~/HTTPApacheUsers2.txt

Here is the script.   Adjust your CHARSET and MAXLENGTH as desired.





id = "HTTPApacheUsers.nse"

author = "Mark Baggett "

description = "Brute force usernames on Apache"

license = "Same as Nmap--See http://nmap.org/book/man-legal.html"

categories = {"intrusive", "discovery"}

runlevel = 1.0

portrule = shortport.port_or_service({80,443}, {"http","https"}

--charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,-_"

charset = "abcdefghijklmnopqrstuvwxyz"

username = ''

maxlength = 3

local function replacechar( instring, pos, newchar)

        if pos == 1 then

                -- replace first character

                instring = newchar .. string.sub(instring,2)


                -- replace mid or end character

                instring = string.sub(instring,1, pos-1) .. newchar .. string.sub(instring,pos+1)


        return instring


local function IncUser(position) 

        local userchar = string.sub(username, position, position)

        if userchar == '' then 

                --the current character is Null add the first char from charset to the end

                username = string.sub(username, 1 ,position) .. string.sub(charset,1,1)

        elseif userchar == string.sub(charset,-1) then

                --Reset current char and Increment the next char

                username = replacechar(username, position, string.sub(charset,1,1)) 



                -- Just increment the current character

                curchar = string.find(charset, userchar)

                username = replacechar(username, position, string.sub(charset,curchar + 1,curchar+1))


        return username


action = function(host, port)

        local output = "No Root User Found"

        local answer = http.get( host, port, "/~root")

        --      print(answer.body)

        if answer.status == 403 then

          --  print("Root User Found.")

            output = "Root user found.  "

            while string.len(username) <= maxlength do


                tryme= "/~" .. username

                local answer = http.get( host, port, tryme)

--              print(answer.status)

                if answer.status == 403 then

                        output = output .. " User found " .. username .. "." 

     --   print(output)




        return output