28.10.2014 Views

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

References • Chapter 10 417<br />

For the purposes of this tutorial, each example query is aimed at the tblUsers table unless<br />

otherwise specified. Table 10.1 outlines the structure of the tblUsers table.<br />

Table 10.1 Example <strong>SQL</strong> Table, tblUsers<br />

ID Username Password Privilege<br />

1 gary leedsutd1992 0<br />

2 sarah Jasper 1<br />

3 michael w00dhead111 1<br />

4 admin letmein 0<br />

SELECT Statement<br />

The primary role of the SELECT statement is to retrieve data from a database <strong>and</strong> return<br />

it to the application or user. As a basic example, the following <strong>SQL</strong> statement will return<br />

data from every row <strong>and</strong> column within the tblUsers table:<br />

SELECT * FROM tblUsers<br />

The asterisk (*) character is a wildcard that instructs the database server to return<br />

all data. If only specific columns are required, the name of each column is listed in place<br />

of the wildcard. The following example will return the username column for all rows<br />

within tblUsers:<br />

SELECT username FROM tblUsers<br />

To return specific rows from a table based on conditional criteria, you can add the<br />

WHERE clause followed by the desired condition. For example, the following <strong>SQL</strong> query<br />

will return all rows that have a username value of admin <strong>and</strong> a password value of letmein:<br />

SELECT * FROM tblUsers WHERE username ='admin' AND password = 'letmein'<br />

Microsoft <strong>SQL</strong> server also allows you to use SELECT statements to read table data<br />

from one table <strong>and</strong> insert it into another. In the following example, all data from the tblUsers<br />

table is copied into the hackerTable table:<br />

SELECT * INTO hackerTable FROM tblusers<br />

UNION Operator<br />

You use the UNION operator to combine the result sets of two or more SELECT statements.<br />

All SELECT statements within the union must return the same number of columns <strong>and</strong><br />

their data type must be compatible. In the following example, the <strong>SQL</strong> query will combine<br />

the username <strong>and</strong> password columns from the tblUsers <strong>and</strong> tblAdmins tables:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!