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.

366 Chapter 8 • Code-Level <strong>Defense</strong>s<br />

<strong>SQL</strong> injection. Being at the design level, however, these techniques are more beneficial<br />

to new development, as significantly rearchitecting an existing application to incorporate<br />

different design techniques could require a great deal of effort.<br />

Each design technique we’ll discuss in the subsections that follow can be implemented<br />

in isolation; however, for best results it is recommended that you implement all of these<br />

techniques together with the techniques outlined earlier in the chapter, where appropriate,<br />

to provide true defense in depth against <strong>SQL</strong> injection vulnerabilities.<br />

Using Stored Procedures<br />

One design technique that can prevent or mitigate the impact of <strong>SQL</strong> injection is to design<br />

the application to exclusively use stored procedures for accessing the database. Stored<br />

procedures are programs stored within the database, <strong>and</strong> you can write them in a number of<br />

different languages <strong>and</strong> variants depending on the database, such as <strong>SQL</strong> (PL/<strong>SQL</strong> for Oracle,<br />

Transact-<strong>SQL</strong> for <strong>SQL</strong> Server, <strong>SQL</strong>:2003 st<strong>and</strong>ard for My<strong>SQL</strong>), Java (Oracle), or others.<br />

Stored procedures can be very useful for mitigating the seriousness of a potential <strong>SQL</strong><br />

injection vulnerability, as it is possible to configure access controls at the database level when<br />

using stored procedures on most databases. This is important, because it means that if an<br />

exploitable <strong>SQL</strong> injection issue is found, the attacker should not be able to access sensitive<br />

information within the database if the permissions are correctly configured.<br />

This happens because dynamic <strong>SQL</strong>, due to its dynamic nature, requires more<br />

permissions on the database than the application strictly needs. As dynamic <strong>SQL</strong> is assembled<br />

at the application, or elsewhere in the database, <strong>and</strong> is then sent to the database for<br />

execution, all data within the database that needs to be readable, writable, or updateable by<br />

the application needs to be accessible to the database user account that is used to access the<br />

database. Therefore, when an <strong>SQL</strong> injection issue occurs, the attacker can potentially access<br />

all of the information within the database that is accessible to the application, as the attacker<br />

will have the database permissions of the application.<br />

With the use of stored procedures, you can change this situation. In this case, you would<br />

create stored procedures to perform all of the database access the application needs.<br />

The database user that the application uses to access the database is given permissions to<br />

execute the stored procedures that the application needs, but does not have any other data<br />

permissions within the database (i.e., the user account does not have SELECT, INSERT,<br />

or UPDATE rights to any of the application’s data, but does have EXECUTE rights on the<br />

stored procedures). The stored procedures then access the data with differing permissions—for<br />

example, the permissions of the user who created the procedure rather than the user invoking<br />

the procedure—<strong>and</strong> can interact with the application data as necessary. This can help you to<br />

mitigate the impact of an <strong>SQL</strong> injection issue, as the attacker will be limited to calling the<br />

stored procedures, therefore limiting the data the attacker can access or modify, <strong>and</strong> in many<br />

cases preventing the attacker from accessing sensitive information in the database.

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

Saved successfully!

Ooh no, something went wrong!