SANS Penetration Testing
SANS Penetration Testing
23 Jan 2019
application-scanning-and-automation#)
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.34
10.23-25.0.0/24
172.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:
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.
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
A tool commonly used to perform initial web application scans is Nikto[3]. Nikto looks for common web application vulnerabilities associated
with outdated software, misconfigurations and dangerous files.
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:
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.
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.
#!/bin/bash
ports="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.
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.
The following grep command could return the results shown below:
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.
$grep '80/open' $curdate\_nmap_tcp.gnmap | cut —d ' ' -f2
10.10.10.10
Table 6: Grep + Cut to Return Only the IP Address for Systems Listening on Port 80/tcp
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.gnmap
10.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.
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;
hello
there
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.
NOTE: To expand the $testport variable in the search string, the string delimited by the single quotes must be terminated before the variable is
referenced, and then resumed afterwards.
All the pieces necessary to build the initial automation script have been presented. The following code block puts everything together.
#!/bin/bash
ports="80 443"
curdate=$(date +%Y%m%d)
nmap —n -Pn -iL targets.txt -oA $curdate\_nmap_tcp —-reason
for testport in $ports
do for targetip in $(awk '/'$testport'\/open/{print $2}' $curdate\_nmap_tcp.gnmap)
do nikto -host $targetip:$testport -ask no —nointeractive -useragent "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0)
Gecko/20100101 Firefox/40.1" -Format htm -output
$curdate\_nikto_$targetip\_$testport.html
done
done
Table 11: The Initial Automation Script
If a targets.txt file contains the target hack.me in the same folder as the automation script (test.sh in this case), the following files will be
present in the directory from which the script is run.
(https://blogs.sans.org/pen-testing/files/2019/01/Files-created-by-the-Initial-Automation-Script.png)
An Alternative Path
Leveraging Nikto's ability to read target details from nmap's greppable formatted output, another way the script could be written is provided
below.
#!/bin/bash
curdate=$(date +%Y%m%d)
nmap —n -Pn -iL targets.txt -oA $curdate\_nmap_tcp —reason
nikto -host $curdate\_nmap_tcp.gnmap -ask no —nointeractive -useragent "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0)
Gecko/20100101 Firefox/40.1" -Format htm —output .
Table 12: An Alternative Initial Automation Script
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
available: ondemand
presenter: Timothy McKenzie (https://www.sans.org/instructors/timothy-mckenzie)
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.
Related Course
For the course syllabus, additional classroom opportunities, and online training options, visit the SEC542: Web App Pen Testing and Ethical
Hacking course description. (https://www.sans.org/course/web-app-penetration-testing-ethical-hacking?msc=ptblog)
[1] https://www.owasp.org/index.php/Testing:_Introduction_and_objectives
(https://www.owasp.org/index.php/Testing:_Introduction_and_objectives)
Post a Comment
*Name
Website
*Comment
Captcha
*Response
Post Comment
* Indicates a required field.
Join the SANS Community to receive the latest curated cyber security news, vulnerabilities and mitigations, training opportunities,
and our webcast schedule.
Enter country...
Subscribe
Share
(https://www.addtoany.com/share#url=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fpen-testing.sans.org%2Fblog%2F2019%2F01%2F23%2Fweb-application-scanning-
and-
automation&title=SANS%20Penetration%20Testing%20%7C%20Web%20Application%20Scanning%20Automation%20%7C%20SANS%20Institute)
(/#facebook) (/#twitter) (/#google_plus) (/#linkedin) (/#email)
Categories
Select Month
Links
Log in (https://blogs.sans.org/pen-testing/login.php)
Entries RSS (/blog/feed/)
Comments RSS (/blog/feed/comments/)
Add new pen testing/ethical hacking skills to your arsenal a [...] (https://twitter.com/SANSPenTest/statuses/1083416710609281024)
January 10, 2019 - 5:34 PM
Latest Papers
Template Injection Attacks - Bypassing Security Controls by Living off the Land (/resources/papers/gcih/template-injection-attacks-bypassing-
security-controls-living-land-146745)
By Brian Wiltse
A Swipe and a Tap: Does Marketing Easier 2FA Increase Adoption? (/resources/papers/gcih/swipe-tap-marketing-easier-2fa-increase-adoption-
139464)
By Preston Ackerman
"Ed Skoudis is the best teacher I've ever had. He is 100% competent and professional."
- Petra Klein, FRA
(http://twitter.com/SANSPenTest) (https://www.facebook.com/pages/SANS-Institute/173623382673767)
(/blog/feed/)
Resources (/resources/) Courses (/courses/) Events (/events/) Certification (/certification/) Instructors (/instructors/) About (/about/)