Topic(s):   DB2 Application Development : Security

January 15, 2008

SQL Injection and Stored Procedures
Posted by Martin Hubel and Rob Williams @ 05:58 PM ET | Jan 15, 2008

In doing numerous reviews of stored procedures, we see many customers with SQL injection issues. SQL injection is one of the most basic and common security issues in database applications.

Injection refers to a situation where people can substitute malicious arguments into an SQL statement. Over the last several years, all of the major web programming languages have tightened down on injections. They have added features that automatically escape characters or steer developers away from this problem. Developers take this for granted when writing web applications, and tend to forget this when writing DB2 stored procedures.

To add to the problem, there are a large number of dynamic SQL examples in DB2 books and articles that also suffer from SQL injection issues. New developers starting from these examples may unknowingly be introducing security issues.

Most developers concentrate on front end UI development and accuracy of input, and they worry less about the stripping of variables at the stored procedure level. With the adoption of SOA services that use a thin layer to externalize stored procedures, this issue is becoming more prevalent. The following example is quite common and similar to an example in common DB2 sources:


CREATE PROCEDURE SET_BONUS ( IN in_empno VARCHAR(1000),
IN in_bonus INTEGER )

------------------------------------------------------------------------
-- SQL Stored Procedure
-- in_empno - The employee number to update
-- in_bonus - Set the bonus to this employee
------------------------------------------------------------------------

P1: BEGIN
DECLARE v_stmt VARCHAR(1000);
SET v_stmt = 'UPDATE employee SET BONUS= ' CHAR(in_bonus) ' WHERE empno =''' in_empno '''';
EXECUTE IMMEDIATE v_stmt;
END P1


What happens if someone sent a call to this stored procedure with the following argument (“000000' or EMPNO = '000010”,9000). Inside the stored procedure the query that is executed is:

UPDATE EMPLOYEE SET BONUS = 2000 WHERE EMPNO = '000000' or EMPNO = '000010'

This is probably not a desired behavior. It is common for the front end to automatically rewrite strings such as “000000' or EMPNO = '000010” to “000000'' or EMPNO = ''000010” so this type of injection doesn't work.

Now, say you don't have thin layers around your stored procedure and you know your web team escapes all incoming variables. So your protected, right? Not so fast - there are ways to circumvent escaped strings.

Escaped strings may be subject to character encoding exploits. How does your application stripping algorithm handle a multi-byte character such '0xbf27'? Most people don't know. When interpreted as two separate characters it can be considered a ¿ followed by a single quote or a single character that won't be escaped. If you don't have consistent character encoding this can be exploited. While the risk is small, you can't be sure of which holes exist in other sections of code. Also, what happens if there is a language exploit that allows hackers to override the escaping?

To avoid SQL injection issues, the best strategy is to always use parameter markers for dynamic sql in stored procedures or static sql. This rule also applies to all development languages. You can quickly filter for common mistakes in your SQL stored procedures by doing a db2look -d Yourdb -e and checking for concatenations in the stored procedure section.

Other types of SQL injection exists. What about adding an “' or 't'='t'” to a select statement? You can have access to all the rows. Or using the union all operator to gain access to other tables. Suddenly, all table data is publicly available.

Martin Hubel and Rob Williams
mhubel.com

« DB2 9 DBA Certification Study Questions: DB2 configuration and stored procedure calling privileges | Main | Y2K38 and DB2, Is Your Application Safe? »





This is a public forum. CMP Media and its affiliates are not responsible for and do not control what is posted herein. CMP Media makes no warranties or guarantees concerning any advice dispensed by its staff members or readers.

Community standards in this comment area do not permit hate language, excessive profanity, or other patently offensive language. Please be aware that all information posted to this comment area becomes the property of CMP Media LLC and may be edited and republished in print or electronic format as outlined in CMP Media's Terms of Service.

Important Note: This comment area is NOT intended for commercial messages or solicitations of business.



CAREER CENTER
Ready to take that job and shove it?
SEARCH JOBS
RECENT JOB POSTINGS
CAREER NEWS
10 Search Engines You Don't Know About
Go beyond Google and get vertical. These specialized search sites will help you find the business information you need -- fast.

Subscribe to the new digital version of IBM Database Magazine
New Digital Version

Sponsored links:



Subscribe to the IBM Database Magazine Newsletter

Email Address *
First Name
Last Name
HTML Preference
HTML Text
 

Fields with * are required.

 




Visit these other IBM and TechWeb Partner Sites: :
Maximizing ROI Through Business Process Management (BPM) and Service-Oriented Architecture (SOA)
Internet Evolution – The Macrosite for News, Analysis, & Opinion About the Future of the Internet
Business Innovation – Technology Strategies and Solutions for Driving Business Success