SQL Injection. Blossom Hands-on exercises for computer forensics and security



Similar documents
Introduction to Websites & Dynamic Content

Network Forensics Network Traffic Analysis

Forensic Imaging and Artifacts analysis of Linux & Mac (EXT & HFS+)

Network Attacks. Blossom Hands-on exercises for computer forensics and security

Multimedia im Netz Online Multimedia Winter semester 2015/16

SQL Injection Attack Lab Using Collabtive

Network Packet Analysis and Scapy Introduction

SQL Injection Attack Lab

LAMP Quickstart for Red Hat Enterprise Linux 4

Application note: Connecting the to a Database

INSTALLING, CONFIGURING, AND DEVELOPING WITH XAMPP

Python Scripting with Scapy

UQC103S1 UFCE Systems Development. uqc103s/ufce PHP-mySQL 1

LAMP : THE PROMINENT OPEN SOURCE WEB PLATFORM FOR QUERY EXECUTION AND RESOURCE OPTIMIZATION. R. Mohanty Mumbai, India

CSCI110 Exercise 4: Database - MySQL

A SQL Injection : Internal Investigation of Injection, Detection and Prevention of SQL Injection Attacks

Mul$media im Netz (Online Mul$media) Wintersemester 2014/15. Übung 03 (Nebenfach)

USING MYWEBSQL FIGURE 1: FIRST AUTHENTICATION LAYER (ENTER YOUR REGULAR SIMMONS USERNAME AND PASSWORD)

Ulteo Open Virtual Desktop Installation

Backup and Restore MySQL Databases

CPE111 COMPUTER EXPLORATION

A table is a collection of related data entries and it consists of columns and rows.

PHP Authentication Schemes

User's Guide and Software Design of. Job Tracking System for the NTC s Machine Shop Version 2.0

AJ Matrix V5. Installation Manual

Designing for Dynamic Content

The Whole OS X Web Development System

Installation Instructions

Sugar Open Source Installation Guide. Version 4.5.1

Installation documentation for Ulteo Open Virtual Desktop

Webapps Vulnerability Report

About This Document 3. About the Migration Process 4. Requirements and Prerequisites 5. Requirements... 5 Prerequisites... 5

Apache and Virtual Hosts Exercises

Usage Tracking for IBM InfoSphere Business Glossary

PHP Tutorial From beginner to master

IBM DB2 for Linux, UNIX, and Windows. Deploying IBM DB2 Express-C with PHP on Ubuntu Linux

E-Commerce: Designing And Creating An Online Store

Oracle Database 10g Express

A Brief Introduction to MySQL

Advanced Web Security, Lab

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Hadoop Basics with InfoSphere BigInsights

Contents. 1. Infrastructure

Online shopping store

Build it with Drupal 8

Using SQL Server Management Studio

Database Security. Principle of Least Privilege. DBMS Security. IT420: Database Management and Organization. Database Security.

All the materials and/or graphics included in the IceThemetheme folders MUST be used ONLY with It TheCityTheme from IceTheme.com.

Raspberry Pi Webserver

Installing buzztouch Self Hosted

A basic create statement for a simple student table would look like the following.

An Introduction to Developing ez Publish Extensions

SECURING APACHE : THE BASICS - III

How to Install Multicraft on a VPS or Dedicated Server (Ubuntu bit)

Testing Web Applications for SQL Injection Sam Shober

Server-side scripting with PHP4

SQL Server Instance-Level Benchmarks with DVDStore

Using Internet or Windows Explorer to Upload Your Site

Server side scripting and databases

HOW TO BUILD A VMWARE APPLIANCE: A CASE STUDY

Other Language Types CMSC 330: Organization of Programming Languages

Forms Printer User Guide

Install MS SQL Server 2012 Express Edition

Upgrading MySQL from 32-bit to 64-bit

MySQL Quick Start Guide

Short notes on webpage programming languages

webapp.secure Installation and Setup Guide

SQL Injection. The ability to inject SQL commands into the database engine through an existing application

Preparing a SQL Server for EmpowerID installation

PHP ON A FAST TRACK INTRODUCTION: ROADMAP BY JAROSLAW FRANCIK. Companion web site: php.francik.name

Installation of PHP, MariaDB, and Apache

SQL Injection Attack Lab

3 Setting up Databases on a Microsoft SQL 7.0 Server

SQL EXPRESS INSTALLATION...

Advanced PostgreSQL SQL Injection and Filter Bypass Techniques

G563 Quantitative Paleontology. SQL databases. An introduction. Department of Geological Sciences Indiana University. (c) 2012, P.

SIMIAN systems. Setting up a Sitellite development environment on Windows. Sitellite Content Management System

Lesson 07: MS ACCESS - Handout. Introduction to database (30 mins)

CC ICT-SUD. Setting up and integrate Apache, MySQL and PHP on a Linux system

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

How to Install and use Windows XP Mode and Windows Virtual PC in Windows 7 for older 32 bit only Applications

MOODLE Installation on Windows Platform

Jet Data Manager 2012 User Guide

Response Time Analysis of Web Templates

MySQL Quick Start Guide

Create a New Database in Access 2010

Installing Moodle for Windows with Easy PHP Illustrated Install Guide By Floyd Collins

Bubble Code Review for Magento

Version of this tutorial: 1.06a (this tutorial will going to evolve with versions of NWNX4)

SQL injection: Not only AND 1=1. The OWASP Foundation. Bernardo Damele A. G. Penetration Tester Portcullis Computer Security Ltd

Database Administration with MySQL

Transferring Your Hosting Account

Installing an open source version of MateCat

Beginning with SubclipseSVN

Product: DQ Order Manager Release Notes

CCM 4350 Week 11. Security Architecture and Engineering. Guest Lecturer: Mr Louis Slabbert School of Science and Technology.

SVNManager Installation. Documentation. Department of Public Health Erasmus MC University Medical Center

Benchmarking and monitoring tools

Configuring an Alternative Database for SAS Web Infrastructure Platform Services

Transcription:

Copyright: The development of this document is funded by Higher Education of Academy. Permission is granted to copy, distribute and /or modify this document under a license compliant with the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/. SQL Injection BLOSSOM Manchester Metropolitan University (Funded by Higher Education Academy) l.han@mmu.ac.uk

1. Learning Objectives This lab aims to understand SQL injection. 2. Preparation 1) Under Linux environment 2) Files that you will need from /home/user/blossomfiles/sqlinjection: 'sqlinjection.php' 3) Some documents that you may need to refer to: 3. Tasks 'Virtual-MachineGuide.pdf' Linux-Guide.pdf BLOSSOM-UserGuide.pdf Setup & Installation: Start a single virtual machine as you have done with previous exercises (see Virtual Machine Guide) # kvm -cdrom /var/tmp/blossomfiles/blossom-0.98.iso -m 512 -net nic,macaddr=52:54:00:12:34:57 -net vde -name node-one Set the extension for mysql in PHP to 'mysql.so' using the following commands, and then restart the apache2 server: # gedit /etc/php5/apache2/php.ini > extension=mysql.so # /etc/init.d/apache2 restart

Task 1 MYSQL 1.1 Nearly all databases that you see in use on websites are provided by either MYSQL(*nix systems) or SQL Server(Microsoft). In these labs we will be using MYSQL. Unlike other database software you may already be familiar with, such as Microsoft Access, it's very easy to access the databases through a variety of programming languages. Like most websites, we will be accessing the MYSQL databases through PHP, a server side language. Typically, the routine for accessing a MYSQL database is as follows: 1) Connect to the MYSQL backend, using the address, password and user 2) Select the correct database 3) Assemble a MYSQL query (or command) 4) Query the database, checking the query is valid. 5) Read the results if required. 1.2 The SQL query syntax is very easy to learn, below are some examples that you can adapt for use in the lab. SELECT * from ANIMALS; The above displays the entire table `ANIMALS' SELECT * from ANIMALS WHERE animal=`chicken'; Displays all data about chickens from table, `ANIMALS' SELECT animal from ANIMALS WHERE name=`dave'; Returns a list of animals called Dave You can also write data into a table using the following SQL commands as examples: INSERT INTO ANIMALS (id, name, animal, favcrisps) VALUES (1, "Jim", "cow", "Salt and Vinegar"); Inserts data into table ANIMALS about Jim the cow

INSERT INTO ANIMALS (id, animal, favcrisps, name) VALUES (1, "Chicken", "Doesn't Like Crisps", "Dave"), (2, "Pig", "Bacon", "Sam"), (3, "Dog", "Ready Salted", "Lauren"); Inserts multiple rows of data, in the given order Rows can be altered using commands similar to the following command too: UPDATE ANIMALS SET name=`percy' WHERE animal='pig'; Renames all pigs Percy The above commands should be sufficient for this exercise, more commands can be found in the MYSQL reference manual at http://dev.mysql.com/doc/refman/5.0/en/ 1.3 We now need to create the basic tables that will be used for this task. Start mysql and create a table using the following commands: #mysql -u root -p # mypass > CREATE DATABASE users; > USE users; > CREATE TABLE people (id int NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name varchar(15), email varchar(20)); > INSERT INTO people(id,name,email) VALUES (1,'Betty','bo@illumati.com'), (2,'Jamie','jh@something.com'); This will provide us with enough data to perform some basic SQL Injections. Task 2 SQL Injection 2.1 Due to the nature of the MYSQL syntax, it is possible to extend the intended command to perform other commands. Below is a simple example of SQL injection implemented through a PHP page: <?php //sweetstock.php //This PHP page returns the number of items in stock for given sweet //Connect to the mysql database mysql_connect(`localhost', `root', `mypass');

//Select database sweets mysql_select_db(`sweets'); //Get sweet name from url $name = $_GET[`sweet']; //Construct a mysql query $cmd = sprintf("select stock FROM stock WHERE sweet='%s'", $name); //mysql returns an array of results $result = mysql_query("$cmd"); //Open the result array with this while loop //Keep echoing the first column of the result until none left while($row = mysql_fetch_array($result)){ echo "$row[0]"; }?> When the above PHP page is called with 'http://insecuresweetshop.com/sweetstock.php?sweet='milkbottles', the website will return the number of milkbottles in stock. The MYSQL command is assembled to be: SELECT stock FROM stock WHERE sweet=`milkbottles' However, if we access the following URL, 'http://insecuresweetshop.com/php?sweet=milkbottles'or'1'='1' The query will become: SELECT stock FROM stock WHERE sweet='milkbottles' OR '1' = '1' Which is always TRUE. The program will output all stock for all sweets; we have performed an SQL Injection. By injecting the URL, we have obtained information from a MYSQL database that we shouldn't have. This may not seem like much, but in some cases it's possible to completely escape the command, changing tables and accessing other information. There are hundreds of examples of SQL injection being used in just this way to obtain people's personal information. 2.2 Now we can look at an SQL Injection for ourselves. Move the file that you downloaded earlier called 'sqlinjection.php' into a directory under /var/www/: # mkdir /var/www/sql # cp sqlinjection.php /var/www/sql

Open up a browser and navigate to 'http://localhost/sql/sqlinjection.php'. We should be confronted with a few messages stating the success of the connection, the basis of the query being used in this example, and a message telling us that the query is not valid due to the fact that not data has been read in to the PHP page. Using the browser, type the following in to the URL address bar: > http://localhost/sql/sqlinjection.php?name=betty This should then display the ID number attached to the name Betty, and it should because this is how the page is meant to function; however, due to the fact that the code is vulnerable to an SQL Injection, we can input the following URL in to the browser in order to obtain more information: > http://localhost/sql/sqlinjection.php?name=' OR email='bo@illumati.com This will display the ID number attached to the email 'bo@illumati.com'. We have managed to enter the rest of the query in to the address bar due to the code's vulnerabilities. Even though this is a very simple example, it's quite easy to understand the potential of an SQL Injection. Try to input a URL into the browser that will output the ID number for every single row from the table. HINT: Refer back to the milkbottles example in task 2.1. 2.3 We should have developed quite an understanding of how an SQL Injection is performed, so we will now take a brief look in to how we can prevent them. Open up the source code for 'sqlinjection.php' and look at it, you should be able to make sense of what's happening at each bit of code. Take a look at the commented out line which when uncommented will apply the method 'mysql_real_escape_string()' to the variable '$user' and then store it in '$validuser'. After uncommenting this line, change the variable '$user' on the next line to '$validuser'. This will remove certain special characters such as apostrophes, quotation marks or new line characters by prepending them with backslashes, which should render the query as invalid. This is known as Input Validation and it is something that should be done whenever trying to prevent SQL code from potential injections. Try using one of the URLs we used earlier and take note of the difference.