Skip to main content

Welcome to Mark Baggett - In Depth Defense

I am the course Author of SANS SEC573 Automating Information Security with Python. Check back frequently for updated tools and articles related to course material.

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 ( ) 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"

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


Popular posts from this blog

Awesome Keyboard Tricks - Clevo/Sager Backlight control from Powershell

I'm back on Windows.   After 8 years on a Macintosh I just couldn't go another day with ONLY 16GB of RAM.   I priced it out and for the cost of a top of the line MacBook I could get a tricked out PC with 32GB of ram and 2.5 TB or hard drive space (1.5 of it being SSD).   So I made the switch.  To get a top performing laptop I ended up buying a gaming machine from   The model is Sager NP9752 ( Clevo P750ZM ).    I have to say I like it quite a bit.    One of the features I was curious about was the "Programmable backlit keyboard".   With it you can set your keyboard backlight to various colors and light movement patterns.    Now, when I hear "programmable" I think APIs.   I was a little disappointed to find out there weren't any documented APIs that I could use to control the keyboard.    Your only choice is to use their built in tool to configure the lights on the keyboard.   That stinks.  I want to be able to change key colors automatically

SRUM-DUMP and SRUM_DUMP_CSV Ported to Python 3

SRUM_DUMP and SRUM_DUMP_CSV have been ported to Python3 and are available for download from the PYTHON3 branch of my github page. In moving to Python3 I also updated the modules that I depend upon to parse and create XLSX files and access the ESE database that contains the SRUM data.  I hope that this will fix the issue that some users have experienced with SRUDB.dat files that create very large spreadsheets.  If it does not please let me know and continue to use SRUM_DUMP_CSV.EXE to avoid the XLSX problem. In moving to Python3 you will find the process to be faster. If you would like to run the tools from source instructions for doing so are in the README on the github page.

New tool

I read an article on Fireeye's website the other day where they used Machine Learning to eliminate a lot of the noise that comes out of tools like strings.  It's pretty interesting and looks like it would save me some time when looking through malware. I wondered how effective scores would be in helping to eliminate the noise.  45 minutes and 29 lines of Python code later I have something that looks like it works.  Check out Before here is the output of strings on a piece of malware: student@573:~/freq$ strings -n 6 malware.exe | head -n 20 !This program cannot be run in DOS mode. e!Rich `.rdata .pdata @.gfids @.rsrc @.reloc \$0u"H L$ SVWH K SVWH |$ H;_ <bt%<xt!<Zt |$ AVH l$ VWAV L$ SUVWH UVWATAUAVAWH 0A_A^A]A\_^] UVWATAUAVAWH @A_A^A]A\_^] After the useful stings quickly bubble to t

Security Onion getting the most from and Domain States

My talk at Security Onion conference has been posted and is available for viewing here.

Three Free Python apps to improve your defenses and incident response

I did a SANS Webcast on a couple of tools I developed to use automation to enhance your network defenses and incident response capability.    If you missed it you can check it out here: