SANS Penetration Testing

Some functions within penetration testing can be mundane and repetitive. To feed some life into these parts of the test, it can be fun and challenging to develop an automation script for these elements of an assessment. Furthermore, automating parts of a penetration test can help the output to be more consistent, reproducible, rigorous, and introduce some quality control as suggested by the OWASP Testing Methodology[1]. This article will introduce a few features of nmap and Nikto that support script automation, and walk through building a simple starter automation script.

Ready, Aim, FIRE!

To feed your automation, you will need a way to document your target information. Frequently a flat-text file with one target per line is sufficient. Other formats, such as XML, may be helpful depending on the tools you intend to use within your automated scripts.

Most command-line tools have features, such as reading target information from files, that facilitate automation nicely.

NMAP

The popular port scanning tool, nmap[2], can read a list of targets from a file through the "-iL" switch. For nmap, the file can specify targets in the same format that they can be listed on the command line, and multiple target designations can be placed on separate lines.

The following sample shows the flexibility nmap offers in the formatting of files it can read through the "-iL" switch:

192.168.17.3410.23-25.0.0/24172.16.1.1-10

Table 1: Sample nmap "targets.txt" File

The nmap tool also permits the results to be saved into several different formats:

Normal: The same output as nmap sends to STDOUTXML:

The results in XML data structure

Greppable: Output with scan results for each host on a single line

Script Kiddie: Results are in "L33T" speak

Using the "-oA" switch, nmap will output the results into three (3) separate files associated with the first three (3) output options listed above.

These file outputs will become the inputs for other tools to select targets.

Putting the input and output options together, the following nmap command will read in targets from the "targets.txt" file and output results to the three (3) useful file formats.

nmap -iL targets.txt -oA $(date +%Y%m%d)_nmap_tcp

Table 2: Example nmap Command

The "$(date +%Y%m%d)" portion of the filename in the previous command causes the current date to be prepended to the file names nmap creates.

Nikto's ability to read an nmap greppable format file, a flat-text file with targets listed within, and the specification of a target on the command line makes target selection versatile. Each of these ways to target hosts can be specified by using the "-h" switch.

There are several conditions in which Nikto may prompt for further input, pausing the scan process until a response is received. To prevent a situation where Nikto is waiting for feedback, rather than scanning targets, the following switches can be used to disable prompts.

-ask no: disables the prompt that asks if you want to submit new banners for software identified while scanning.

-nointeractive: disables all other prompts.

Like nmap, Nikto has the ability to save results to multiple file formats by using the "-Format" switch. Some of the formats available include those in the following list:

-Format HTM: Saves an HTML formatted report.

-Format CSV: Saves Nikto's results in comma separated value format.

-Format XML: Results are stored in XML format.

Multiple file types can be specified after the "-Format" switch by separating them with a comma.

Additionally, Nikto can save a replay file associated with each finding it reports, making it easy to jump in and verify findings or exploit the vulnerability.

The following example Nikto command reads the "nmap_output.gnmap" file for hosts for which the HTTP/HTTPS protocol was discovered, disables all interactive prompts, and saves a report in the "nikto_nmap-scans.html" file using HTML format.

Automation: Bash Script Magic

Armed with script specific features of nmap and Nikto, it is time to build a basic automation script. This script can be used to begin the automation process, and expanded to include more tools.

Script Setup

To start the script out, it is best to define the path to the command interpreter you want the script to use, in this case we will point to bash. Afterwards, the definition of a couple variables, "ports" and "date", will be useful for this script.

ports: Stores the list of ports Nikto will scan. For this starter script the focus will be on 80/tcp and 443/tcp.

date: Stores the date to use as a timestamp in filenames.

The beginning of the script will look like this:

#!/bin/bashports="80 443"curdate=$(date +%Y%m%d)

Table 4: Start of Automation Script

The $(date +%Y%m%d)is used to execute the date command with parameters to specify the format YYYYMMDD and its output will be stored in the $curdate variable.

Grab Those IPs

The tools run by the script will need the IP addresses of the targets to run. There are many tactics to provide the targeting details, some of which are dependent upon the tool's capabilities. Though Nikto can read a greppable nmap output file, for demonstration purposes let's look at how the addresses can be pulled from nmap's output.

Grep is a tool that searches through a file and returns the line that matches a specified query.

In the following example, a will be used as the delimiter, and the second "field" will be extracted. Based on the previous output from the grep command, this should isolate the IP address of the host with port 80/tcp available.

This command searches for instances where port 80/tcp are reported open, and then prints the second field within each of the search results. By default awk will use whitespace (tabs and spaces) as the field delimiter.

$awk '/80\/open/{print $2}' $curdate\_nmap_tcp.gnmap10.10.10.10

Table 7: Another Way to Extract the IP Address using Awk

Note that in the previous commands the "$curdate" variable was used as part of the file name. The "\_" in the filename uses the backslash to tell bash that the "_" is a literal underscore character and not part of the "$curdate" variable. Otherwise bash would look for the variable "$curdate_nmap_tcp.gnmap", which does not exist.

Throw Us for a Loop

Bash includes the ability to iterate through a set of items using the for loop. A simple example of the for loop is:

$for a in hello there; do echo $a; done;hellothere

Table 8: Simple for Loop Example

The above loop iterated through the two items hello and there, then wrote each item to stdout using the echo command.

There are a few features of for loops that are useful in scripts.

The loops can be nested inside of each other to iterate through multiple sets of data. As can be seen in the code snippet below, the script will loop through each of the ports specified in the $ports variable, as well as all the IP addresses inside the nmap output.

The semicolon (";") can be replaced with a carriage return to make the loop easier to read in a script.

The set of items the for loop will iterate through can be returned by a command.

As an example, the following command will loop through each of the results in the ls command, and print them to the screen.

for a in $(ls -1); do echo $a; done;

Table 9: FOR Loop Using a Command's Results as the Iterable Data

At this point, the details about pulling the IP addresses and the for loops can be combined.

The alternative version of the script differs from the first script in the following ways:

All of Nikto's results for every target will be stored in the same file.Nikto identifies other ports that nmap reports as using http/https protocols, and scans those systems as well.A small amount of flexibility is lost, as the tools are making decisions about what to scan; but, the script is easier to read.

Homework

This article focuses on starting an initial script. Be like the crew members of the Enterprise and go where no hacker has gone before? Change the script, add in your favorite tools, and enjoy the productivity gains from introducing automation to improve your pen test methodology.

More Information

Related Webcast

Abstract: Many functions within a penetration test are routine, and using a script to automate portions of the process can provide a consistent, repeatable and auditable process. This presentation will begin with a walk through the command-line switches of a couple well-know tools that aid automation. By the end of the hour, attendees will have an automation script that they can use to run their own tests, and grow to incorporate more of their own tools and methodology.