0% found this document useful (0 votes)
48 views

SANS Penetration Testing

The document discusses the automation of web application penetration testing using tools like nmap and Nikto. It provides insights into scripting for repetitive tasks, including command examples and the structure of an automation script. The article encourages readers to enhance their scripts by incorporating additional tools and methodologies for improved efficiency in penetration testing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

SANS Penetration Testing

The document discusses the automation of web application penetration testing using tools like nmap and Nikto. It provides insights into scripting for repetitive tasks, including command examples and the structure of an automation script. The article encourages readers to enhance their scripts by incorporating additional tools and methodologies for improved efficiency in penetration testing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SANS Penetration Testing

23 Jan 2019

Web Application Scanning Automation (/blog/2019/01/23/web-

application-scanning-and-automation#)

0 comments (/blog/2019/01/23/web-application-scanning-and-automation#respond) Posted by sanspentest (/blog/author/sanspentest)


Filed under web pen testing (/blog/category/web-pen-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.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:

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

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:

-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.

nikto -host nmap_output.gnmap -ask no —nointeractive -Format htm -output nikto_nmap-scans.html


Table 3: Example Nikto Command

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/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.

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.

The following grep command could return the results shown below:

grep '80/open' $curdate\_nmap_tcp.gnmap


Host: 10.10.10.10 () Ports: 80/open/tcp//http///
Table 5: Grep Command to Find Lines with Port 80/tcp

The cut command can extract a section of text using a delimiter.

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

Another way to extract the IP address uses the tool awk.

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.

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;
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.

for testport in $ports


do for targetip in $(awk '/'$testport'\/open/{print $2}' $curdate\_nmap_tcp.gnmap)
do nikto -host $targetip -ask no —nointeractive
done
done
Table 10: FOR Loop to Iterate through Ports and IP Addresses

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.

The Initial Automation Script

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)

Screenshot 1: Files Created by the Initial Automation Script

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

"Web Application Scanning Automation (https://www.sans.org/webcasts/web-application-scanning-automation-109070?msc=ptblog)"

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

SEC542: Web App Penetration Testing and Ethical Hacking (https://www.sans.org/course/web-app-penetration-testing-ethical-hacking?


msc=ptblog)

Upcoming classroom opportunities with instructor Tim McKenzie include

Tysons Corner, VA - Feb 11


Singapore - Mar 18

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)

[2] https://nmap.org/ (https://nmap.org/)

[3] https://github.com/sullo/nikto (https://github.com/sullo/nikto)

Permalink (/blog/2019/01/23/web-application-scanning-and-automation) | Comments RSS Feed (/blog/2019/01/23/web-application-scanning-


and-automation/feed) - Post a comment | Trackback URL (/blog/2019/01/23/web-application-scanning-and-automation)

Post a Comment

*Name

*Email

Website

*Comment
Captcha

*Response

Post Comment
* Indicates a required field.

Subscribe to SANS Newsletters

Join the SANS Community to receive the latest curated cyber security news, vulnerabilities and mitigations, training opportunities,
and our webcast schedule.

Enter email address...

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

Advanced Web App Pentesting (/blog/category/advanced-web-app-pentesting) (2)


Anomaly Analysis (/blog/category/anomaly-analysis) (1)
Anti-Virus Evasion (/blog/category/anti-virus-evasion) (7)
Backdoor (/blog/category/backdoor) (2)
Bash (/blog/category/bash) (11)
Challenge Coins (/blog/category/challenge-coins) (1)
Challenges (/blog/category/challenges) (27)
Cheatsheet (/blog/category/cheatsheet) (9)
cloud (/blog/category/cloud) (2)
Command Line Kung Fu (/blog/category/command-line-kung-fu) (17)
Conferences (/blog/category/conferences) (4)
Cryptography (/blog/category/cryptography) (4)
CyberCity (/blog/category/cybercity) (1)
Databases (/blog/category/databases) (1)
Enumeration (/blog/category/enumeration-2) (2)
Exploit Development (/blog/category/exploit-development) (4)
File Analysis (/blog/category/file-analysis-2) (2)
fuzzing (/blog/category/fuzzing) (1)
Infrastructure (/blog/category/infrastructure) (4)
Introduction (/blog/category/introduction) (3)
Legal Issues (/blog/category/legal-issues) (1)
Linux (/blog/category/linux) (2)
Metasploit (/blog/category/metasploit) (9)
Methodology (/blog/category/methodology) (48)
Mobile (/blog/category/mobile) (21)
Network Devices (/blog/category/network-devices) (3)
Nmap (/blog/category/nmap-2) (2)
Passwords (/blog/category/passwords) (7)
Post Exploitation (/blog/category/post-exploitation-2) (13)
Posters (/blog/category/posters) (23)
PowerShell (/blog/category/powershell) (8)
Presentations (/blog/category/presentations) (10)
Protocol Analysis (/blog/category/protocol-analysis) (1)
Python (/blog/category/python) (20)
Quiz (/blog/category/quiz) (2)
Recon (/blog/category/recon) (1)
Reporting (/blog/category/reporting) (4)
Scanning (/blog/category/scanning) (7)
scapy (/blog/category/scapy) (3)
Shell Fu (/blog/category/shell-fu) (5)
Summit (/blog/category/summit) (1)
web pen testing (/blog/category/web-pen-testing) (18)
Welcome (/blog/category/welcome) (2)
wireless (/blog/category/wireless) (5)
Writing (/blog/category/writing) (2)
Recent Posts

Tips for Creating a Strong Cybersecurity Assessment Report (/blog/2019/01/23/tips-for-creating-a-strong-cybersecurity-assessment-


report)
Web Application Scanning Automation (/blog/2019/01/23/web-application-scanning-and-automation)
Secrets to Successful Cybersecurity Writing: Hack the Reader (/blog/2019/01/11/secrets-to-successful-cybersecurity-writing)
Using gdb to Call Random Functions! (/blog/2018/12/11/using-gdb-to-call-random-functions)
SANS Pen Test Poster: Pivots Payloads Boardgame (/blog/2018/10/02/sans-pen-test-poster-pivots-payloads-boardgame)
Archives

Select Month

Links

Log in (https://blogs.sans.org/pen-testing/login.php)
Entries RSS (/blog/feed/)
Comments RSS (/blog/feed/comments/)

Latest Blog Posts

Tips for Creating a Strong Cybersecurity Assessment Report (/blog/2019/01/23/tips-for-creating-a-strong-cybersecurity-assessment-report)


January 23, 2019 - 11:57 AM

Web Application Scanning Automation (/blog/2019/01/23/web-application-scanning-and-automation)


January 23, 2019 - 4:03 AM

Secrets to Successful Cybersecurity Writing: Hack the Reader (/blog/2019/01/11/secrets-to-successful-cybersecurity-writing)


January 11, 2019 - 2:00 PM

Latest Tweets @SANSPenTest

Challenge yourself in 2019! If you are a #PenTest expert an [...] (https://twitter.com/SANSPenTest/statuses/1090341380348616706)


January 29, 2019 - 8:10 PM
Have what it takes to get on stage and show that killer expl [...] (https://twitter.com/SANSPenTest/statuses/1088854057345781760)
January 25, 2019 - 5:40 PM

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

Don't Knock Bro (/resources/papers/gcih/knock-bro-116990)


By Brian Nafziger

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

"This is the best hands-on course available anywhere."


- Whitney Janes, FedEx

"Ed Skoudis is the best teacher I've ever had. He is 100% competent and professional."
- Petra Klein, FRA

"This was by far the best course I have ever taken."


- Peter Lombars, Intrucom Inc.

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

© 2008 - 2019 SANS™ Institute

You might also like