Post

Vulnerability Scanning Bash Script

An automation Bash script run basic vulnerability assessment on target’s network and generate a summary included open port discovered, weak password used.

Vulnerability Scanning Bash Script

Introduction

Vulnerability assessment is a systematic review of a system. It evaluates if the system has any kind of vulnerability or if those vulnerabilities are caused by misconfigured services, and recommends remediation or mitigation, if and whenever needed.

Vulnerability assessment is a time-consuming process. This project aims to automate some of the processes in vulnerability assessment. Processes like using Nmap to discover hosts in the network, discover if any users have weak passwords implemented, run vulnerability scans, etc.

Methodology

Predefined variable

Before the script executes any command, we define some variables on top of the script. This will help us easily manage the script and modify the value as needed.

  • OUTPUT_DIR: The output directory path that going to store all the results including the output generated by Nmap, summary, etc.
  • TARGET: The target network going to scan.
  • USER_WORDLIST: The default user wordlist path used to check weak password
  • PASS_WORDLIST: The default password wordlist path used to check weak password
  • FULL_MODE: Specific to run it at full mode or basic mode.
  • UDP_SCAN: Specific to run UDP scan or not.
  • ZIP_RESULT: Specific to zip the result at the end.
  • DB_NAME: The name of SQLite DB

Available options

In the meantime, we also create a help menu to let users input their preferences when executing this script.

We need to modify the arguments that pass into the script to support long string options in the bash script. We utilize getopt to get all of the long and short options arguments, in this case, the options arguments are “-f, -d, -h, -u, -p, -z” and “–full, –directory, –help, –user, –password, –skip-udp”. At last, because we will read these arguments in an always true loop, we need a stop sign to exit the loop, adding “–” to the end of the arguments acts as a stop sign.

Now, we can specify the options.

Input validation

The script required users to provide the network’s IP address to operate or change the wordlist used to brute-force. The user might enter an invalid IP address or wordlist path and cause the script to not be able to operate properly. Hence we implement some validation before actually executing the script.

The script defines a regex used to validate the provided network’s IP address, it will be valid for “255.255.255.255”, “0.0.0.0”, “192.168.236.0/24”, etc. Furthermore, it also verifies the wordlist path to check if the file exists.

  • [ ! -f ]: Check if the file exists.
  • =~: Regex check

If the user doesn’t specify the name of the output directory, the script will use the network IP address as the name of the output directory. For example, if the network IP address is “192.168.234.16”, the output directory will be “192.168.234.16”. If the network IP address is “192.168.234.0/24”, the output directory will be “192.168.234.0_24”.

Install required package

Before we could run a command, we needed to install those packages related to the command. We need to check if the required package is installed and install it if not.

First, we create a function to run the install package command, because most installation commands are the same.

Most of the packages that can be installed with a single command.

Before we install the package, we need to check if the package is installed by running dpkg-query \-W \-f='${Status}' $1 2\>/dev/null.

  • $1: This is the package name we trying to install.
  • -W: List the package with a specific pattern, the default pattern shows the name and its version.
  • -f: Specific the output format. In our case, we just want to show the status of the package.
  • >/dev/null 2>&1: Hide the progress output.

Dpkg-query will output “install ok installed” if the package is installed, we will use grep "install ok installed" to determine if the package is installed.

The full command will be dpkg-query \-W \-f='${Status}' $1 2\>/dev/null | grep "install ok installed"

It means the package is not yet installed If the above command generates empty output. In this case, we will perform installation processes.

The command to install a package is sudo apt-get install \-y $1 \>/dev/null 2\>&1.

  • -y: Agree on everything when installing the package.
  • $1: This is the package name we trying to install.
  • >/dev/null 2>&1: Hide the progress output.

There will be a lot of output when we install a package. The above command will hide all the output during the installation, making the script output clean.

Now we have created the function, we can feed the package name into the function to run the installation processes.

We define an APP_TO_INSTALL variable that includes all the necessary packages and loop it to installRequiredPackage function. But before we loop into the function, we need to update the package repo to get the latest version of the package information.

Create DB for easier query

In order to perform other vulnerability scans, we need to extract information from Nmap’s scan results. One of the ways to extract information is to generate the scan results in XML format. However the process of extracting information is not easy, it involves many considerations in order to extract that information into the format we expected.

To solve this, I decide to create a SQLite DB file and parse the information from Nmap’s scan result into the DB.

First, we need to define the DB structure. We will have a table named “hosts” that stores the active hosts discovered. Another table named “ports” stores the open ports of the hosts. Finally, a table named “found_cred” stores all the weak passwords discovered.

We created a few insert data functions for better management.

We also created a function for query data, the function gets the first arguments as an SQL statement and passes the remaining argument to sqlite3. The remaining arguments that pass to this function will be explained later.

Network scanning

After creating the DB, we create a function to parse the Nmap XML result and store the information in the DB. UDP network scans usually take longer times to run, so we provided an option to skip the UDP scanning. Below are the Nmap commands used to scan the target’s network.

  • -sV: Indicate Nmap to get the version of the service.
  • -sT: Indicate running the scan with TCP
  • -sU: Indicate running the scan with UDP
  • -oN: Output the scan results in text form
  • -oX: Output the scan results in XML form
  • &>/dev/null: Hide all the output and errors from the terminal

After we run the scan, we use xmlstarlet to extract the information from the Nmap XML result. This tool can generate an XSLT stylesheet which is used to query XML data. We first extract all discovered hosts, and then the open ports from each host. After extracting the information from XML, we save the data into the DB we created earlier.

  • sel: Command for query the XML
  • -t: Create a XSLT template
  • -m: Retrieve every node that matches the XPATH
  • -v: Output the value that matches the XPATH
  • -o: Print the provided value
  • -n: Print the next line
  • –if: Check condition
  • –else: Check if the previous condition failed
  • -b: Exit the if-else statement

Weak password detection

Even if we discover all the hosts and ports from the network, it does not mean that networks are vulnerable. One of the reasons that make the network vulnerable is the users of the network using weak passwords that are easy to brute-force.

Next, we will use Hydra to brute-force the network to discover if any users using weak passwords. We query the DB to extract all the host information that was stored in the DB earlier. Each host might have several services that allow users to log in. Since the objective is to discover weak passwords of users, we can check if the host running any SSH, RDP, Telnet, and FTP services that will authenticate users.

If any service stated above is found, we can start to brute-force that service with Hydra. We will be using the word lists from SecLists to perform brute-force attacks.

We need to output the found credential into a file so that we can extract it and store it in the DB. We form the service URL to indicate Hydra which service and host to attack. Moreover, we need to specify the port to attack in case the host is not using the default port for the service.

  • [ ! -z ]: Return true if the variable is not empty
  • -s: Specific the port of the service
  • -L: Specific which user wordlist to use
  • -P: Specific which password wordlist to use
  • -I: Ignore previous hydra attack
  • -o: Output the found credential to file
  • &>/dev/null: Hide all output and error

After Hydra brute-force and discovering any user credentials from all the hosts, we extracted the user credentials from the output file and stored them in DB.

  • [ ! -z ]: Return true if the variable is not empty
  • sed ‘/#/d’: Remove the Hydra command information
  • tr -s ‘[]’ ‘ ‘: Replace the brackets with space

Vulnerability scan

In full mode, we will scan the vulnerability with the Msfconsole scanner script, we need to list all the available scripts for users to select. First, we need to know the host has what kind of services.

Second, we need to determine if there are any scanner scripts for that service and only perform a vulnerability scan if found any script for that service. The Msfconsole stores all the scanner scripts on “/usr/share/metasploit-framework/modules/auxiliary/scanner/” and has different directories for different services.

If any script is found, we list out all the scripts and let the users select which script to run for each service.

Users get to select which script to run the vulnerability scan. The script will feed into the host’s IP address and the port of the service into Msfconsole.

Below is the wrapped version of the command that is used to launch Msfconsole for a better explanation. We first select the module with “use” command, then set the host IP address and port. Finally we run the module and exit Msfconsole.

Msfconsole options:

  • -n: No need to run the database
  • -q: Don’t display banner
  • -x: Execute the command after start

Summary report

After discovering all the active hosts on the network, the script will output a summary of all the information. Including active hosts, open ports of each host, and found credentials. Query the data from the DB, format the data, print it out, and generate a summary text file named “summary.txt”.

Interactive stage

At the end of the script, it will allow users to query the data. Similar to script options, the interactive stage also provided some kind of predefined command. Users who are familiar with SQL can use SQL statements to query the data. The “tables” command will output the structure of DB, users can use that information to build SQL statements.


The interactive stage is run within a while loop, it will keep running until the user decides to quit the interactive mode by inputting a “q” or “quit” command.

  • ${action,,}: Convert user input to lowercase.
  • -column: Output the result in a table-like form, using whitespace characters to separate the columns and align the output.
  • -header: Output the result with header.

As you can notice from the picture above, the “hosts” command is just a predefined SQL statement that will query all the discovered hosts’ data. The same is done to the “ports” and “creds” commands.


Moreover, we can retrieve the table structure with the “tables” command. It will output the table structure for users to refer to in case they wish to input an SQL select statement.

Zip the output

The script will create various outputs, and the users might want to review it or even send it to others. The script provided the option to zip all the output.

  • -r: Zip all the files inside the directory

Conclusion

Although this script is made to automate vulnerability assessment, but there is no universal method to run a proper vulnerability assessment. It usually involves multiple thinking processes and needs to be flexible for different environments.

This script just executes a very basic vulnerability assessment. Users should use this information as an aid in the vulnerability assessment.

You can access the Bash script from my GitHub

Reference

This post is licensed under CC BY 4.0 by the author.