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.

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

The profile_name indicates the profile to use to send the e-mail; if it’s left blank the default<br />

public profile for the msdb database will be used. If a profile does not exist, you can create<br />

one using the following procedure:<br />

1. Create a Database Mail account using msdb..sysmail_add_account_sp. You will need<br />

to know a valid SMTP server that the remote database can contact <strong>and</strong> through<br />

which the e-mail can be sent. This SMTP server can be some server on the<br />

Internet, or one that is under the control of the attacker. However, if the database<br />

server can contact an arbitrary IP address on port 25, there are much faster ways<br />

to extract the data (e.g., using OPENROWSET on port 25, as I will show you in<br />

a following section) than using e-mail. Therefore, if you need to use this technique<br />

it’s very likely that the database server cannot access external hosts, <strong>and</strong> so you will<br />

need to know the IP address of a valid SMTP server that resides on the target<br />

network. This may not be as hard as it sounds: If the Web application has some<br />

functionality that sends e-mail messages (e.g., with the results of some action of<br />

the user, or an e-mail to reset a user’s password), it’s very likely that an SMTP server<br />

will appear in the e-mail headers. Alternatively, sending an e-mail to a non-existent<br />

recipient might trigger a response that contains the same information. However,<br />

this might not be enough if the SMTP server is authenticated: If this is the case,<br />

you will need a valid username <strong>and</strong> password to successfully create the Database<br />

Mail account.<br />

2. Create a Database Mail profile, using msdb..sysmail_add_profile_sp.<br />

3. Add the account that you created in step 1 to the profile that you created in<br />

step 2, using msdb..sysmail_add_profileaccount_sp.<br />

4. Grant access to the profile that you created to the users in the msdb database, using<br />

msdb..sysmail_add_principalprofile_sp.<br />

The process, complete with examples, is described in detail at http://msdn.microsoft.<br />

com/en-us/library/ms187605(<strong>SQL</strong>.90).aspx. If everything works <strong>and</strong> you have a valid<br />

Database Mail account, you can finally run queries <strong>and</strong> have their results sent in an e-mail.<br />

Here is an example of the whole process:<br />

--Enable Database Mail<br />

EXEC sp_configure 'show advanced', 1;<br />

RECONFIGURE;<br />

EXEC sp_configure 'Database Mail XPs', 1;<br />

RECONFIGURE<br />

--Create a new account, MYACC. The SMTP server is provided in this call.

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

Saved successfully!

Ooh no, something went wrong!