SlideShare a Scribd company logo
Advanced Topics on SQL Injection  Protection Sam NG CISA, CISSP SQLBlock.com [email_address] Feb 27 th , 2006
Introduction SQL injection  [1,  2 ]  is now one of the most common attacks in the Internet. Simply go to Yahoo! or Google and search for "SQL injection" and we can find tones of related documents.  Although the awareness of SQL injection is rising, still many people do not have very concrete ideas on how to prevent SQL injection attack.  This article is not going to tell you what is SQL injection, nor going to tell you the latest techniques in SQL injection attacks, but more important,  how to prevent SQL injection   correctly and in a more integrated approach.
Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC methods Development Phase QA Phase Production Phase
Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC methods Development Phase QA Phase Production Phase
Method 1: Input Validation Some programmers may think escaping apostrophe with two apostrophes (and back slash with two back slashes for MySQL) is all input validation has to do This is completely  WRONG!  A few important steps are missed and probably the program is still vulnerable to SQL injection.
Method 1: Input Validation (cont’d) There are at least four steps we have to do for input validation Escape apostrophe with two apostrophes (and back slash with two back slashes for MySQL) Make sure numeric fields really look like numbers Do step “1&quot; and “2&quot; not only on users' direct input, but on all non-constant variables Check if the inputs are within your expectation (e.g. 0 < age < 120, login id without space, etc.)
1.1:  Escape inputs properly Escaping apostrophe with two apostrophes (or back slash with two back slashes for MySQL) usually can be done with one line of code.  However, we have to ensure that the decoding is done in the correct order.  To avoid SQL injection properly, the apostrophe-escaped input should NOT be further en/decoded by any other coding scheme.
Consider the following PHP code Malicious user can bypass the magic quote by using “%27” to represent an apostrophe.  “ %27” will be decoded to a single apostrophe by the urldecode() function, and hence destroying the protection provided by the magic quote. [PHP] $magic_quotes_runtime  =  “on” ; $url  =  urldecode ($_REQUEST [ ‘url’ ]); $query  =  “INSERT INTO tbl_links (type, url) VALUES(1, ‘ $url ’)” ;
1.2: Validate numeric fields Basically, the above ASP code will issue query like select * from sales where prod='foo' and price > 100 It is true that the &quot;prod&quot; field will not be injected. [ASP] Dim  conn, rec, query, prod, price prod =  Replace (Request. Form ( “prod” ),  “’ ”,  “’’” ) price =  Replace (Request. Form (“price”),  “’” ,  “’’” ) Set  conn =  CreateObject (&quot;ADODB.Connection&quot;) conn.Open =  &quot;DSN=AccountDB;UID=sa;PWD=password;&quot; query =  “select * from sales where prod=’”  & prod  &  “‘ and price > ”  & price Set  rec = conn.Execute(query)
However… The &quot;price&quot; field can still be injected Numeric fields are not prefixed nor suffixed with apostrophe The hacker doesn't need to put an apostrophe inside the field to balance the other apostrophe So there is actually nothing to escape!  Hacker can inject with “ 100 union ... -- ” Note again,  no apostrophe is needed inside the injecting code, and escaping can’t help you
Table 1. SQL injection vulnerabilities found in BugTraq SecurityFocus 194 1 7 94 92 2005 Jan-Jun 57     29 28 2004 Jan-Jun Total Second Order StoredProc Numeric Field Others Period
Table 1 (cont’d) Table 1 shows statistics of SQL injection vulnerabilities found during 2005 Jan-Jun, and 2004 Jan-Jun.  The data are collected from BugTraq SecurityFocus, filtered by selecting the publish date as stated above and with titles containing the word “SQL”. Non-SQL injection related vulnerabilities are manually removed.  From the table, we see about  50% of the SQL injection vulnerabilities are related to numeric field injection.
Prevent Numeric Field Injection 1 Check if the numeric field is really a number Please be reminded that some languages, such as Perl and PHP, can convert a string into to a number as long as the string begins with digits.  if  ( $category  > 0) { $categ  =  &quot;AND catid=$category &quot;; }  elseif  ( $category  == 0) { .... } “ AND catid=2 union…”; Return true even if  $category = “ 2 union … ”
Prevent Numeric Field Injection 2 Put a pair of apostrophes before and after the numeric field variable, and escape the variable as usual.  i.e. treat the numeric field variable just as string e.g.  some_var >  ‘ 20 ’  instead of  some_var > 20 Works as most database servers will convert the string back to a number if necessary, and the overhead is very minimal.
1.3 Column Names Some web application may dynamically include different column names in a query depending on user’s input.  For example, a web application may allow a user to select which column to be sorted by.  Like numeric fields, column names are usually not apostrophe quoted.
1.3 Column Names (cont’d) The above code fragment shows a typical vulnerable ASP code with dynamic column names specified after the “order by” keyword. Dim  cat, orderBy, query cat =  Replace ( Request .Form( “cat” ),  “’” ,  “’’” ) orderBy =  Replace ( Request .Form( “orderBy” ),  “’” ,  “’’” ) query =  “select * from tbl_prod ”    &  “where cat = ‘”  & cat &  “’ “   &  “order by “  & orderBy
Prevent SQL injection in column names The two techniques in numeric fields handling can be applied to column names as well.  verify if the column name is within our expectations  (e.g. alphabets without spaces), OR quote the column name  with -- not apostrophe this time – double-quote (“) for MS-SQL, PostgreSQL and Oracle, back-tick (`) for MySQL.  MS-SQL and PostgreSQL allow double-quote to occur inside the column name by using two double-quotes (““), while Oracle seems not supporting this. Although both techniques work, we suggest not allowing meta-characters in column names and verifying the column names accordingly.
1.4 Prevent second order attacks Dim  conn, rec, query1, query2, login_id, old_pass, new_pass login_id =  Replace (Request. Form ( “login_id” ),  “’” ,  “’’” ) old_pass =  Replace (Request. Form ( “old_pass” ),  “’” ,  “’’” ) new_pass =  Replace (Request. Form ( “new_pass” ),  “’” ,  “’’” ) Set conn =  CreateObject ( &quot;ADODB.Connection&quot; ) conn.Open =  &quot;DSN=AccountDB;UID=sa;PWD=password;&quot; query1 =  “select * from tbl_user where login_id=’”  & login_id    &  “’ and password=‘”  & old_pass &  “’”  Set  rec = conn.Execute(query1) If  (rec.EOF) Then   Response.Write  &quot;Invalid Password&quot; Else   query2 =  “update from tbl_user set password=’”  & new_pass    &  “’ where login_id=’”  &  rec.( “login_id” )  &  “’”   conn.Execute(query2)   ..   .. End If Unescaped data, read from database. But, what about if  login_id =  “foo’ union…. – ” All properly escaped
What is 2 nd  Order SQL Injection? A second order code injection attack can be classified as the process in which malicious code is injected into a web base application and not immediately executed, but instead is stored by the application (e.g. temporary cached, logged, stored in database, etc.) and then later retrieved, rendered and executed by the victim  [ 3 ] .
Prevent 2 nd  Order SQL Injection Escaping  ALL  inputs that will be embedded into the query statement  All means not only GET/ POST/Cookie, but includes data read from files, database, etc. Check if the input data is really what we expected to be (why allow user Id = “ foo’ union… -- ”)  And of cause, we can simply not using dynamic queries.
   PHP magic_quotes_gpc, magic_quotes_runtime Magic Quotes is a process that automatically escapes incoming data to the PHP script. When on, all ' (single-quote), &quot; (double quote), \ (backslash) and NULL characters are escaped with a backslash automatically. magic_quotes_gpc (default “on” in PHP) escapes only HTTP GET/POST/Cookie fields and is definitely still vulnerable to second-order SQL injection and numeric field injection. magic_quotes_runtime (default “off” in PHP) is somehow more secure as it escapes data also from an external source, including databases and text files. However, it still lacks numeric fields validation. However,   a s quoted in PHP manual  [ 4 ]  “It's preferred to code with magic quotes off (author note: it means both) and to instead escape the data at runtime, as needed.”
Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC Methods Development Phase QA Phase Production Phase
Method 2: Use Static Query Statement Covered by most of documents related to SQL injection prevention The basic idea is to use parameterised statement (or prepared statement) and have the server to encode the parameters as needed An effective solution and is suggested whenever possible.  Still a few points we have to be aware of
2.1 parameterized stmt  !=  static stmt [Java] String  sql =  “select * from product where cat=’”  +  request.get( “cat” ) +  “’ and price > ?” ; PreparedStatement  pstmt = con.prepare(sql); pstmt.setString(1, request.getParameter( “price” )); ResultSet  rs = pstmt.executeQuery(); Obviously vulnerable to SQL injection Even this is called in a parameterized form Prepare statement
2.2 Stored Procedure  !=  SAFE CREATE PROCEDURE  sp_dynamic (   @name  varchar(50) = '' ) AS DECLARE   @Query  varchar(500) SET   @Query  =  'SELECT * FROM userlist where name = '''   +  @name  +  ''' EXEC( @Query ) GO Dangerous Function SQL style string concatenation [Solution] SET   @name  =  REPLACE ( @name ,  '''' ,  '''''' ) Insert at HERE
2.3 Static query doesn’t always work The  N  in “ select top N ...   ”, table name, column name can’t be parameterised in most SQL database servers.  May force us to use dynamic query and we may still have to validate our inputs.
Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC Methods Development Phase QA Phase Production Phase
Method 3: Least Privilege Don’t connect to the database with root access Setting restricted read/write on tables or views Denying access to special system utilities and system stored procedures.  Calls stored procedures would be more secure if we have fine-grained control on what will be returned in the stored procedure For example, a stored procedure always return only one row of data will be  better than granting the user/role read access on the whole table.
Invoker’s right for stored procedure A stored procedure is usually executed under the permission of the stored procedure’s owner (similar to SUID files in UNIX file system).  However, when executing dynamic query (i.e. “ exec() ”) in a stored procedure, some database servers, such as Oracle [ 5 ]  and MS-SQL [ 6 ] , provide an extra layer of defence by executing the dynamic query under the caller’s permission  (i.e. invoker’s right) .  That is, except for granting the user/role permission to access the stored procedure, we also have to explicitly grant privileges to all other object that is to be accessed by the dynamic query.
However... Setting least privilege on database layer provides only limited help Access control of most web applications is not performed by database, but by the application itself.  Users connect to the database through a shared (may be even pooled) connection with the same web application specific database username and password (the database role), and the application username and password (the application role) are stored in the database as an ordinary table in the database.  The application checks if the user is allowed to perform certain task base on the application role, not the database role.
If the code DOES contain SQL Injection bug… The database role would  already  contain  ENOUGH   (enough for hacker)  privileges no matter how you configure (provided that the  hacker  is aim at manipulating data and not to execute special OS commands or add/drop tables)  For example, the hacker will  ALWAYS  be able to insert/delete/update the table storing the  user names and passwords  because the application needs to manipulate that table even before the user login!
Conclusion Similar to preventing buffer overflow, setting least privilege, chroot environment for network daemons won’t solve buffer overflow problems But it can help to reduce the loss in case of a successful intrusion Almost a must before deploying your web application to production environment
Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC Methods Development Phase QA Phase Production Phase
Method 4: Verifies Your code How do you ensure your development staff do not make any mistakes? Audit: review the source code of the program (a programmer’s point of view) Assess: conduct penetration test on the program (a hacker’s point of view)
4.1 Source Code Auditing The simplest way to do a source code auditing is probably by using the editor’s “search” function.  For example, to check if a Java program is vulnerable to SQL injection attack, we could search for  execute() ,  prepareStatement()  and  prepareCall() , and then back trace the formation of their corresponding input query string to see if they contains unchecked/unescaped  user input. Can also performed in an automatic fashion
Automatic  Source Code Scanner  [7] Mainly  two  technologies : static and runtime A static scanner :  analyzes a program without “running” the application. This is usually done through a  pseudo compiling process to analyze the flow of the program , and then to locate and analyze the dangerous function as mentioned  previously A  runtime analyzer :  analyzes the code by  “running” all or part of the program/function, send input to it , and then analyze the flow like a unit tester or debugger
4.2 Web Application Vulnerability Scanner H ack  (Assess)  your own web application C an be done manually or automatically M annually assess  the  web application by input  “’ or  1=1 -–“  or input  “1 union …..” , and check if the web application behaviour will be affected by these unexpected input.  Clearly, although the above test input is a valid test, this is not a thoughtful one. Many other  test  vectors have to be t ried  to verify the application.  And this is how an automatic tool can help. It works similar to a manual testing, just in a faster and  an  automatic fashion
Semi-automatic tools: Web Proxy Even if you hire an expert to test your application, a semi-automatic tool may help to speed-up the process.  There are tools that works like a proxy to intercept the HTTP traffic, and let you change the  POST  form data before  it is  sen t  to the web server, and it can screen out hidden fields, list JSP/ASP comments that may reveal some of the program flow. Edit Post Data Before Send
Automatic  Source Code Scanner   vs  Automatic  Vulnerability Scanner Although not directly related to SQL injection,  automatic  web application vulnerability scanner  may  do better in finding  logic bugs .  C onsider a web mail application ,  a logic bug may exists so that it will show emails even not belongs to the authenticated user, as long as you have login successfully and supplied  a  valid message id http://www.your-domain.com/show_msg?msg_id=1234 <it won’t check if you own the message 1234> While this would be quite difficult to be detected by  automatic  source code scanner, there is a  higher chance  that an automatic vulnerability scanner will be able to report this bug, provided that the vulnerability scanner will try to mutate the URL it crawled.
Conclusion T he most important factor for both source code auditing and vulnerability assessment is  the  false negative ratio.  T his differs from vendor to vendor, from implementation to implementation or even from  the people who perform the task Although having passed source code auditing and assessment phase does not guarantee 100% security, these two  method s are almost a mandatory if you really want to maintain quality on a sizable application.
Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC Methods Development Phase QA Phase Production Phase
Method 5: Web Application Gateway (WAG) WAG  works like  a reverse  web proxy, except that  it is  much more secure.  It c an interpret more information in the HTTP protocol  and HTML content, it checks Form  inputs are within our expectation H idden fields and cookies are unmodified Multiple choice (select/radio) input is one of the allowed option URL flow is according to the original design A nd many more WA G  protect s  more than just SQL injection.
The downside of WAG Although WA G  is very promising, it is difficult to configure it precisely, especially for protecting SQL injection attacks on  free-format text input Without proper configuration, the  WAG  ( or  even for human) cannot judge if the input should be allowed or blocked and return an error page to the browser. A new user register to a  web portal application N oted the apostrophe But should we block this?
The downside of WAG (cont’d) I t is difficult to tell if the  WAG  should block the input just base on the input itself.  The  WAG  should, however, decide if it should be blocked  base on  the allowable input pattern of the  backend application .  That is , if the backend application will escape the apostrophe properly before inserting into the database, or if the data is completely not SQL related, then the  WAG  may accept this input; otherwise, this input may cause exception in the server and hence should be blocked – even if it doesn’t look like a SQL injection!
The downside of WAP (cont’d) Likewise, it is also difficult to tell if the backend application is vulnerable  without testing it .  C an’t ask the programmer because the program may not behave as the programmer expected (and that’s why we have a bug),  C an’t determine by looking at the JavaScript of the form field to detect the format requirement because that may also be wrong. The only reliable way to determine the allowable input format is to test it.
Solution Basically two methods to make configuration much  easier  if we can accept certain amount of false alarms
To make configuration easier:  1 st  method Default d en y  any text input with an apostrophe , but allow exceptional cases We will block our example input “ 115 Admin’s Street ”,  and then  generate an alert to notify the sys-admin or developer.  The sys-admin or developer then verifies if the program is vulnerable;  if not ,  he/she then  change the WA G  configuration to allow the apostrophe in “this” form field next time.  But what about numeric field or column name injection? Deny all input with space?
To make configuration easier:  2 nd  method Den y  input only if it really looks like a SQL injection attack We will allow our example input “ 115 Admin’s Street ”, because it really doesn’t look like a SQL injection attack.  In case the backend application d oesn ’t escape the input properly,  (hopefully)  this will generate an error but may not result in data lose.
To make configuration easier Clearly, both are not perfect solutions T he first method result s  in higher false positive  error T he second one result s  in higher false negative error and is susceptible to attack evasion At first glance, the first method seems to be a better choice, however,  for  a well tested web application, the second method may be a more practical approach.
Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC Methods Development Phase QA Phase Production Phase
Method 6: SQL Driver Proxy A SQL driver proxy  [8]  works like web application gateway, except that it intercepts API calls instead of network connections, and monitors database function calls instead of HTTP requests.  And same as web proxy, it will pass the request to the backend “original driver” if it is a legitimate request. The proxy nature enables these tools to monitor and block malicious SQL execution, as well as sanitizing error message send from database server back to the client application.  Sanitizing error message is import as the error message usually reveals information about the database schema.
Architecture of a SQL Driver Proxy HTTP Client HTTP Server HTTP Server HTTP Client ODBC JDBC App Original Driver ODBC/JDBC Driver ODBC/JDBC App Analysis Analysis HTTP Proxy ODBC/JDBC Proxy HTTP Protocol HTTP Protocol API Calls API Calls
How SQL Driver Proxy works? For example, an online banking web application may only issue three SQL queries: query user table during authentication, query user owned bank accounts information, and transfer money between bank accounts. The application may ONLY issue queries in the following forms SELECT  *  FROM  tbl_user  WHERE  user_id =  ‘<some thing>’   AND  password =  ‘<some thing>’ SELECT  *  FROM  tbl_accounts WHERE  user_id =  ‘<some thing>’ UPDATE  tbl_accounts  SET  balance = balance +  <some value> WHERE  account_id =  ‘<some thing>’
How SQL Driver Proxy works? (cont’d) When the application is under SQL injection attack, it may issues queries as follows: SELECT  *  FROM  tbl_user  WHERE  user_id =  ‘<some thing>’   AND  password =  ‘<some thing>’  OR   1=1 --’ SELECT  *  FROM  tbl_accounts WHERE  user_id =  ‘<some thing>’   UNION  … UPDATE  tbl_accounts  SET  balance = balance +  <some value>  WHERE  account_id =  ‘<some thing>’ ;  DROP  …
How SQL Driver Proxy works? (cont’d) Because the “ basic structure ” of these query statements will not be the same as the three original queries, and hence can be detected. Moreover, because all SQL queries are monitored, these tools can also prevent second order SQL injection attacks. For some implementations, the list of allowable SQL statements can be “auto-learned” which makes configuration easier.
SQL Driver Proxy limitation Like many other technologies, SQL driver proxy has it own limitation. Since the proxy has to determine if a SQL query is legitimate, the query can not have its structures varying depending on user’s input. Consider a web page allowing user to select the data fields to be reported by using a multi-line selection box as shown below
SQL Driver Proxy limitation (cont’d) The client application may issue a SQL query as follows: SELECT   p_cat, p_name, p_price   FROM   tbl_prod   WHERE   p_name   LIKE   ‘%hello%’   However, if the user selects 2 data fields only, then the SQL query will be different (only two column names after SELECT) SELECT   p_name, p_price   FROM   tbl_prod   WHERE   p_name   LIKE   ‘%hello%’   Depending on the implementation and number of columns, the maximum number of queries it can “mutate” is n!, which may not be a trivial configuration task if n is large.
Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC Methods Development Phase QA Phase Production Phase
Intrusion Detection System (IDS) Network Intelligence provides some Snort signatures for detecting SQL injection.  But of course, IDS technology is susceptible to detection evasion and can’t handle SSL traffic. http://www.niiconsulting.com/resources/snort.html
Context-Sensitive String Evaluation   [9] The concept not only works for protecting SQL injection attack, but is also applicable for all general command injection attacks The general ideal is to change the language design to distinguish user supplied strings with static strings, and depending on the usage of a string, impose some runtime meta-character restriction on the user supplied string.  For example, user supplied input can’t contain apostrophe if it is to be used in a SQL query statement, and can’t contain “&&” or “|” if it is to be used in a system() command.  A  prototype implementation for PHP  is currently available
Database Layer Protection [1 0 , 1 1 , 1 2 , 1 3, 14 ]  describe methods  to prevent SQL injection at the database layer Techniques involves static (source code) analysis, run-time (query statement) analysis and/or provide another set of “Secured API”
Conclusion SQL Injection is one of the most important problem in web application security As shown in  Table 1 , the number of vulnerabilities reported increased more than triples from 2004 Jan-Jun to the same period in 2005, and it is expected that this figure will continue to increase in the near future. The solutions for SQL injection are not very complicate but it requires good management to deploy properly Don’t under estimate SQL injection and tackle the problem in a more holistic and systematic approach
Reference SecuriTeam, SQL Injection Walkthrough, May 2002 http://www.securiteam.com/securityreviews/5DP0N1P76E.html Steve Friedl,  SQL Injection Attacks by Example, Dec 2004 http://www.unixwiz.net/techtips/sql-injection.html Gunter Ollmann, “Second-order Code Injection Attacks” http://www.nextgenss.com/papers/SecondOrderCodeInjection.pdf PHP Magic Quotes Manual http://www.php.net/manual/en/security.magicquotes.php Oracle Invoker's Rights Procedures http://www.stanford.edu/dept/itss/docs/oracle/10g/network.101/b10773/glossary.htm Security Context of Dynamic SQL Statements Inside a Stored Procedure, http://support.microsoft.com/default.aspx?scid=kb;en-us;301299 Jeff Forristal, Source-Code Assessment Tools Kill Bugs Dead, Secure Enterprise, Dec 2005 http://www.secureenterprisemag.com/showArticle.jhtml?articleId=174402221 Sam M.S NG, SQLBlock: SQL Injection Protection by Variable Normalization of SQL Statement, May 2005 http://www.sqlblock.com/sqlblock.pdf
Reference - 2 T. Pietraszek and C. V. Berghe. Defending against Injection Attacks through Context-Sensitive String Evaluation. In Proceedings of the 8th International Symposium on Recent Advances in Intrusion Detection (RAID), Sept. 2005. S. W. Boyd and A. D. Keromytis. SQLRand: Preventing SQL injection attacks, In Proceedings of the 2nd Applied Cryptography and Network Security (ACNS) Conference, pages 292{302. Springer-Verlag, June 2004. Gregory T. Buehrer, Bruce W. Weide, and Paolo A. G. Sivilotti: Using Parse Tree Validation to Prevent SQL Injection Attacks, In Proceedings of the Fifth FSE International Workshop on Software Engineering and Middleware (SEM), September 2005 Zhendong Su and Gary Wassermann: The Essence of Command Injection Attacks in Web Applications, 33rd Annual Symposium on Principles of Programming Languages, Charleston, SC, Jan 11-13. p. 372-382.  W. G. Halfond and A. Orso. Combining static analysis and runtime monitoring to counter SQL-injection attacks. In Online Proceeding of the Third International ICSE Workshop on Dynamic Analysis (WODA 2005), pages 22-28, May 2005. http://www.csd.uwo.ca/woda2005/proceedings.html. William G.J. Halfond and Alessandro Orso: AMNESIA: Analysis and Monitoring for NEutralizing SQLInjection Attacks, Proceedings of the IEEE and ACM International Conference on Automated Software Engineering (ASE 2005).

More Related Content

PPT
SQL Injection
PPT
Advanced SQL Injection
PDF
SQL Injection: complete walkthrough (not only) for PHP developers
PPT
Sql injection
PPTX
Sql injection
PPT
A Brief Introduction in SQL Injection
PPTX
Sql injections - with example
PDF
Sql Injection Myths and Fallacies
SQL Injection
Advanced SQL Injection
SQL Injection: complete walkthrough (not only) for PHP developers
Sql injection
Sql injection
A Brief Introduction in SQL Injection
Sql injections - with example
Sql Injection Myths and Fallacies

What's hot (20)

PPT
Sql injection
PDF
Sql Injection - Vulnerability and Security
PPTX
SQL Injections - A Powerpoint Presentation
PDF
ORM2Pwn: Exploiting injections in Hibernate ORM
PPTX
SQL injection prevention techniques
PPT
04 Handling Exceptions
PDF
Sql injection with sqlmap
PPT
MySQL and its basic commands
PPT
Sql server T-sql basics ppt-3
PDF
4.2 PHP Function
PPTX
SQL Injections (Part 1)
PDF
Advanced PLSQL Optimizing for Better Performance
PPT
SQL subquery
PPT
Methods to Bypass a Web Application Firewall Eng
PDF
SQL Complete Tutorial. All Topics Covered
PDF
SQL Overview
PPT
Introduction to-sql
PPTX
Asp.net and .Net Framework ppt presentation
Sql injection
Sql Injection - Vulnerability and Security
SQL Injections - A Powerpoint Presentation
ORM2Pwn: Exploiting injections in Hibernate ORM
SQL injection prevention techniques
04 Handling Exceptions
Sql injection with sqlmap
MySQL and its basic commands
Sql server T-sql basics ppt-3
4.2 PHP Function
SQL Injections (Part 1)
Advanced PLSQL Optimizing for Better Performance
SQL subquery
Methods to Bypass a Web Application Firewall Eng
SQL Complete Tutorial. All Topics Covered
SQL Overview
Introduction to-sql
Asp.net and .Net Framework ppt presentation
Ad

Viewers also liked (14)

PDF
SQL injection: Not Only AND 1=1 (updated)
ODP
MongoDB : The Definitive Guide
PPTX
Introduction to APIs & how to automate APIs testing with selenium web driver?
PPTX
Top 5 Javascript Frameworks for Web and Mobile App Development
PPTX
Software Automation Testing Introduction
PPTX
Selenium topic 3 -Web Driver Basics
PPT
Sql injection attack
PDF
SQL injection: Not only AND 1=1
DOCX
Selenium WebDriver FAQ's
PPTX
Introduction to Selenium Web Driver
PPT
Java Basics for selenium
PPTX
Automation Testing by Selenium Web Driver
PDF
The What, Why and How of (Web) Analytics Testing (Web, IoT, Big Data)
PDF
Denial of Service Attacks
SQL injection: Not Only AND 1=1 (updated)
MongoDB : The Definitive Guide
Introduction to APIs & how to automate APIs testing with selenium web driver?
Top 5 Javascript Frameworks for Web and Mobile App Development
Software Automation Testing Introduction
Selenium topic 3 -Web Driver Basics
Sql injection attack
SQL injection: Not only AND 1=1
Selenium WebDriver FAQ's
Introduction to Selenium Web Driver
Java Basics for selenium
Automation Testing by Selenium Web Driver
The What, Why and How of (Web) Analytics Testing (Web, IoT, Big Data)
Denial of Service Attacks
Ad

Similar to Advanced Topics On Sql Injection Protection (20)

PDF
A Brief Introduction About Sql Injection in PHP and MYSQL
PDF
Appsec SQL injection case study
PPT
SQLSecurity.ppt
PPT
SQLSecurity.ppt
PPT
Sql Injection Adv Owasp
PPTX
03. sql and other injection module v17
PPSX
Web application security
PPT
SQL Injection Attacks
PPTX
Hack through Injections
PPT
Advanced Sql Injection ENG
PPT
D:\Technical\Ppt\Sql Injection
PPTX
Sql injection
PPT
Sql security
PPT
Sql Injection Attacks Siddhesh
PPT
Sql injection attacks
PDF
Sql Injection Attacks(Part1 4)
PDF
PPTX
Database security
PDF
2nd-Order-SQLi-Josh
PDF
Chapter 14 sql injection
A Brief Introduction About Sql Injection in PHP and MYSQL
Appsec SQL injection case study
SQLSecurity.ppt
SQLSecurity.ppt
Sql Injection Adv Owasp
03. sql and other injection module v17
Web application security
SQL Injection Attacks
Hack through Injections
Advanced Sql Injection ENG
D:\Technical\Ppt\Sql Injection
Sql injection
Sql security
Sql Injection Attacks Siddhesh
Sql injection attacks
Sql Injection Attacks(Part1 4)
Database security
2nd-Order-SQLi-Josh
Chapter 14 sql injection

More from amiable_indian (20)

PDF
Phishing As Tragedy of the Commons
PDF
Cisco IOS Attack & Defense - The State of the Art
PDF
Secrets of Top Pentesters
PPS
Workshop on Wireless Security
PDF
Insecure Implementation of Security Best Practices: of hashing, CAPTCHA's and...
PPS
Workshop on BackTrack live CD
PPS
Reverse Engineering for exploit writers
PPS
State of Cyber Law in India
PPS
AntiSpam - Understanding the good, the bad and the ugly
PPS
Reverse Engineering v/s Secure Coding
PPS
Network Vulnerability Assessments: Lessons Learned
PPS
Economic offenses through Credit Card Frauds Dissected
PPS
Immune IT: Moving from Security to Immunity
PPS
Reverse Engineering for exploit writers
PPS
Hacking Client Side Insecurities
PDF
Web Exploit Finder Presentation
PPT
Network Security Data Visualization
PPT
Enhancing Computer Security via End-to-End Communication Visualization
PDF
Top Network Vulnerabilities Over Time
PDF
What are the Business Security Metrics?
Phishing As Tragedy of the Commons
Cisco IOS Attack & Defense - The State of the Art
Secrets of Top Pentesters
Workshop on Wireless Security
Insecure Implementation of Security Best Practices: of hashing, CAPTCHA's and...
Workshop on BackTrack live CD
Reverse Engineering for exploit writers
State of Cyber Law in India
AntiSpam - Understanding the good, the bad and the ugly
Reverse Engineering v/s Secure Coding
Network Vulnerability Assessments: Lessons Learned
Economic offenses through Credit Card Frauds Dissected
Immune IT: Moving from Security to Immunity
Reverse Engineering for exploit writers
Hacking Client Side Insecurities
Web Exploit Finder Presentation
Network Security Data Visualization
Enhancing Computer Security via End-to-End Communication Visualization
Top Network Vulnerabilities Over Time
What are the Business Security Metrics?

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Modernizing your data center with Dell and AMD
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation theory and applications.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Machine learning based COVID-19 study performance prediction
PDF
Empathic Computing: Creating Shared Understanding
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
NewMind AI Weekly Chronicles - August'25 Week I
NewMind AI Monthly Chronicles - July 2025
Modernizing your data center with Dell and AMD
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation theory and applications.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectral efficient network and resource selection model in 5G networks
Mobile App Security Testing_ A Comprehensive Guide.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Big Data Technologies - Introduction.pptx
Review of recent advances in non-invasive hemoglobin estimation
Machine learning based COVID-19 study performance prediction
Empathic Computing: Creating Shared Understanding
Reach Out and Touch Someone: Haptics and Empathic Computing
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Advanced methodologies resolving dimensionality complications for autism neur...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication

Advanced Topics On Sql Injection Protection

  • 1. Advanced Topics on SQL Injection Protection Sam NG CISA, CISSP SQLBlock.com [email_address] Feb 27 th , 2006
  • 2. Introduction SQL injection [1, 2 ] is now one of the most common attacks in the Internet. Simply go to Yahoo! or Google and search for &quot;SQL injection&quot; and we can find tones of related documents. Although the awareness of SQL injection is rising, still many people do not have very concrete ideas on how to prevent SQL injection attack. This article is not going to tell you what is SQL injection, nor going to tell you the latest techniques in SQL injection attacks, but more important, how to prevent SQL injection correctly and in a more integrated approach.
  • 3. Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC methods Development Phase QA Phase Production Phase
  • 4. Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC methods Development Phase QA Phase Production Phase
  • 5. Method 1: Input Validation Some programmers may think escaping apostrophe with two apostrophes (and back slash with two back slashes for MySQL) is all input validation has to do This is completely WRONG! A few important steps are missed and probably the program is still vulnerable to SQL injection.
  • 6. Method 1: Input Validation (cont’d) There are at least four steps we have to do for input validation Escape apostrophe with two apostrophes (and back slash with two back slashes for MySQL) Make sure numeric fields really look like numbers Do step “1&quot; and “2&quot; not only on users' direct input, but on all non-constant variables Check if the inputs are within your expectation (e.g. 0 < age < 120, login id without space, etc.)
  • 7. 1.1: Escape inputs properly Escaping apostrophe with two apostrophes (or back slash with two back slashes for MySQL) usually can be done with one line of code. However, we have to ensure that the decoding is done in the correct order. To avoid SQL injection properly, the apostrophe-escaped input should NOT be further en/decoded by any other coding scheme.
  • 8. Consider the following PHP code Malicious user can bypass the magic quote by using “%27” to represent an apostrophe. “ %27” will be decoded to a single apostrophe by the urldecode() function, and hence destroying the protection provided by the magic quote. [PHP] $magic_quotes_runtime = “on” ; $url = urldecode ($_REQUEST [ ‘url’ ]); $query = “INSERT INTO tbl_links (type, url) VALUES(1, ‘ $url ’)” ;
  • 9. 1.2: Validate numeric fields Basically, the above ASP code will issue query like select * from sales where prod='foo' and price > 100 It is true that the &quot;prod&quot; field will not be injected. [ASP] Dim conn, rec, query, prod, price prod = Replace (Request. Form ( “prod” ), “’ ”, “’’” ) price = Replace (Request. Form (“price”), “’” , “’’” ) Set conn = CreateObject (&quot;ADODB.Connection&quot;) conn.Open = &quot;DSN=AccountDB;UID=sa;PWD=password;&quot; query = “select * from sales where prod=’” & prod & “‘ and price > ” & price Set rec = conn.Execute(query)
  • 10. However… The &quot;price&quot; field can still be injected Numeric fields are not prefixed nor suffixed with apostrophe The hacker doesn't need to put an apostrophe inside the field to balance the other apostrophe So there is actually nothing to escape! Hacker can inject with “ 100 union ... -- ” Note again, no apostrophe is needed inside the injecting code, and escaping can’t help you
  • 11. Table 1. SQL injection vulnerabilities found in BugTraq SecurityFocus 194 1 7 94 92 2005 Jan-Jun 57     29 28 2004 Jan-Jun Total Second Order StoredProc Numeric Field Others Period
  • 12. Table 1 (cont’d) Table 1 shows statistics of SQL injection vulnerabilities found during 2005 Jan-Jun, and 2004 Jan-Jun. The data are collected from BugTraq SecurityFocus, filtered by selecting the publish date as stated above and with titles containing the word “SQL”. Non-SQL injection related vulnerabilities are manually removed. From the table, we see about 50% of the SQL injection vulnerabilities are related to numeric field injection.
  • 13. Prevent Numeric Field Injection 1 Check if the numeric field is really a number Please be reminded that some languages, such as Perl and PHP, can convert a string into to a number as long as the string begins with digits. if ( $category > 0) { $categ = &quot;AND catid=$category &quot;; } elseif ( $category == 0) { .... } “ AND catid=2 union…”; Return true even if $category = “ 2 union … ”
  • 14. Prevent Numeric Field Injection 2 Put a pair of apostrophes before and after the numeric field variable, and escape the variable as usual. i.e. treat the numeric field variable just as string e.g. some_var > ‘ 20 ’ instead of some_var > 20 Works as most database servers will convert the string back to a number if necessary, and the overhead is very minimal.
  • 15. 1.3 Column Names Some web application may dynamically include different column names in a query depending on user’s input. For example, a web application may allow a user to select which column to be sorted by. Like numeric fields, column names are usually not apostrophe quoted.
  • 16. 1.3 Column Names (cont’d) The above code fragment shows a typical vulnerable ASP code with dynamic column names specified after the “order by” keyword. Dim cat, orderBy, query cat = Replace ( Request .Form( “cat” ), “’” , “’’” ) orderBy = Replace ( Request .Form( “orderBy” ), “’” , “’’” ) query = “select * from tbl_prod ” & “where cat = ‘” & cat & “’ “ & “order by “ & orderBy
  • 17. Prevent SQL injection in column names The two techniques in numeric fields handling can be applied to column names as well. verify if the column name is within our expectations (e.g. alphabets without spaces), OR quote the column name with -- not apostrophe this time – double-quote (“) for MS-SQL, PostgreSQL and Oracle, back-tick (`) for MySQL. MS-SQL and PostgreSQL allow double-quote to occur inside the column name by using two double-quotes (““), while Oracle seems not supporting this. Although both techniques work, we suggest not allowing meta-characters in column names and verifying the column names accordingly.
  • 18. 1.4 Prevent second order attacks Dim conn, rec, query1, query2, login_id, old_pass, new_pass login_id = Replace (Request. Form ( “login_id” ), “’” , “’’” ) old_pass = Replace (Request. Form ( “old_pass” ), “’” , “’’” ) new_pass = Replace (Request. Form ( “new_pass” ), “’” , “’’” ) Set conn = CreateObject ( &quot;ADODB.Connection&quot; ) conn.Open = &quot;DSN=AccountDB;UID=sa;PWD=password;&quot; query1 = “select * from tbl_user where login_id=’” & login_id & “’ and password=‘” & old_pass & “’” Set rec = conn.Execute(query1) If (rec.EOF) Then Response.Write &quot;Invalid Password&quot; Else query2 = “update from tbl_user set password=’” & new_pass & “’ where login_id=’” & rec.( “login_id” ) & “’” conn.Execute(query2) .. .. End If Unescaped data, read from database. But, what about if login_id = “foo’ union…. – ” All properly escaped
  • 19. What is 2 nd Order SQL Injection? A second order code injection attack can be classified as the process in which malicious code is injected into a web base application and not immediately executed, but instead is stored by the application (e.g. temporary cached, logged, stored in database, etc.) and then later retrieved, rendered and executed by the victim [ 3 ] .
  • 20. Prevent 2 nd Order SQL Injection Escaping ALL inputs that will be embedded into the query statement All means not only GET/ POST/Cookie, but includes data read from files, database, etc. Check if the input data is really what we expected to be (why allow user Id = “ foo’ union… -- ”) And of cause, we can simply not using dynamic queries.
  • 21. PHP magic_quotes_gpc, magic_quotes_runtime Magic Quotes is a process that automatically escapes incoming data to the PHP script. When on, all ' (single-quote), &quot; (double quote), \ (backslash) and NULL characters are escaped with a backslash automatically. magic_quotes_gpc (default “on” in PHP) escapes only HTTP GET/POST/Cookie fields and is definitely still vulnerable to second-order SQL injection and numeric field injection. magic_quotes_runtime (default “off” in PHP) is somehow more secure as it escapes data also from an external source, including databases and text files. However, it still lacks numeric fields validation. However, a s quoted in PHP manual [ 4 ] “It's preferred to code with magic quotes off (author note: it means both) and to instead escape the data at runtime, as needed.”
  • 22. Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC Methods Development Phase QA Phase Production Phase
  • 23. Method 2: Use Static Query Statement Covered by most of documents related to SQL injection prevention The basic idea is to use parameterised statement (or prepared statement) and have the server to encode the parameters as needed An effective solution and is suggested whenever possible. Still a few points we have to be aware of
  • 24. 2.1 parameterized stmt != static stmt [Java] String sql = “select * from product where cat=’” + request.get( “cat” ) + “’ and price > ?” ; PreparedStatement pstmt = con.prepare(sql); pstmt.setString(1, request.getParameter( “price” )); ResultSet rs = pstmt.executeQuery(); Obviously vulnerable to SQL injection Even this is called in a parameterized form Prepare statement
  • 25. 2.2 Stored Procedure != SAFE CREATE PROCEDURE sp_dynamic ( @name varchar(50) = '' ) AS DECLARE @Query varchar(500) SET @Query = 'SELECT * FROM userlist where name = ''' + @name + ''' EXEC( @Query ) GO Dangerous Function SQL style string concatenation [Solution] SET @name = REPLACE ( @name , '''' , '''''' ) Insert at HERE
  • 26. 2.3 Static query doesn’t always work The N in “ select top N ... ”, table name, column name can’t be parameterised in most SQL database servers. May force us to use dynamic query and we may still have to validate our inputs.
  • 27. Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC Methods Development Phase QA Phase Production Phase
  • 28. Method 3: Least Privilege Don’t connect to the database with root access Setting restricted read/write on tables or views Denying access to special system utilities and system stored procedures. Calls stored procedures would be more secure if we have fine-grained control on what will be returned in the stored procedure For example, a stored procedure always return only one row of data will be better than granting the user/role read access on the whole table.
  • 29. Invoker’s right for stored procedure A stored procedure is usually executed under the permission of the stored procedure’s owner (similar to SUID files in UNIX file system). However, when executing dynamic query (i.e. “ exec() ”) in a stored procedure, some database servers, such as Oracle [ 5 ] and MS-SQL [ 6 ] , provide an extra layer of defence by executing the dynamic query under the caller’s permission (i.e. invoker’s right) . That is, except for granting the user/role permission to access the stored procedure, we also have to explicitly grant privileges to all other object that is to be accessed by the dynamic query.
  • 30. However... Setting least privilege on database layer provides only limited help Access control of most web applications is not performed by database, but by the application itself. Users connect to the database through a shared (may be even pooled) connection with the same web application specific database username and password (the database role), and the application username and password (the application role) are stored in the database as an ordinary table in the database. The application checks if the user is allowed to perform certain task base on the application role, not the database role.
  • 31. If the code DOES contain SQL Injection bug… The database role would already contain ENOUGH (enough for hacker) privileges no matter how you configure (provided that the hacker is aim at manipulating data and not to execute special OS commands or add/drop tables) For example, the hacker will ALWAYS be able to insert/delete/update the table storing the user names and passwords because the application needs to manipulate that table even before the user login!
  • 32. Conclusion Similar to preventing buffer overflow, setting least privilege, chroot environment for network daemons won’t solve buffer overflow problems But it can help to reduce the loss in case of a successful intrusion Almost a must before deploying your web application to production environment
  • 33. Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC Methods Development Phase QA Phase Production Phase
  • 34. Method 4: Verifies Your code How do you ensure your development staff do not make any mistakes? Audit: review the source code of the program (a programmer’s point of view) Assess: conduct penetration test on the program (a hacker’s point of view)
  • 35. 4.1 Source Code Auditing The simplest way to do a source code auditing is probably by using the editor’s “search” function. For example, to check if a Java program is vulnerable to SQL injection attack, we could search for execute() , prepareStatement() and prepareCall() , and then back trace the formation of their corresponding input query string to see if they contains unchecked/unescaped user input. Can also performed in an automatic fashion
  • 36. Automatic Source Code Scanner [7] Mainly two technologies : static and runtime A static scanner : analyzes a program without “running” the application. This is usually done through a pseudo compiling process to analyze the flow of the program , and then to locate and analyze the dangerous function as mentioned previously A runtime analyzer : analyzes the code by “running” all or part of the program/function, send input to it , and then analyze the flow like a unit tester or debugger
  • 37. 4.2 Web Application Vulnerability Scanner H ack (Assess) your own web application C an be done manually or automatically M annually assess the web application by input “’ or 1=1 -–“ or input “1 union …..” , and check if the web application behaviour will be affected by these unexpected input. Clearly, although the above test input is a valid test, this is not a thoughtful one. Many other test vectors have to be t ried to verify the application. And this is how an automatic tool can help. It works similar to a manual testing, just in a faster and an automatic fashion
  • 38. Semi-automatic tools: Web Proxy Even if you hire an expert to test your application, a semi-automatic tool may help to speed-up the process. There are tools that works like a proxy to intercept the HTTP traffic, and let you change the POST form data before it is sen t to the web server, and it can screen out hidden fields, list JSP/ASP comments that may reveal some of the program flow. Edit Post Data Before Send
  • 39. Automatic Source Code Scanner vs Automatic Vulnerability Scanner Although not directly related to SQL injection, automatic web application vulnerability scanner may do better in finding logic bugs . C onsider a web mail application , a logic bug may exists so that it will show emails even not belongs to the authenticated user, as long as you have login successfully and supplied a valid message id http://www.your-domain.com/show_msg?msg_id=1234 <it won’t check if you own the message 1234> While this would be quite difficult to be detected by automatic source code scanner, there is a higher chance that an automatic vulnerability scanner will be able to report this bug, provided that the vulnerability scanner will try to mutate the URL it crawled.
  • 40. Conclusion T he most important factor for both source code auditing and vulnerability assessment is the false negative ratio. T his differs from vendor to vendor, from implementation to implementation or even from the people who perform the task Although having passed source code auditing and assessment phase does not guarantee 100% security, these two method s are almost a mandatory if you really want to maintain quality on a sizable application.
  • 41. Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC Methods Development Phase QA Phase Production Phase
  • 42. Method 5: Web Application Gateway (WAG) WAG works like a reverse web proxy, except that it is much more secure. It c an interpret more information in the HTTP protocol and HTML content, it checks Form inputs are within our expectation H idden fields and cookies are unmodified Multiple choice (select/radio) input is one of the allowed option URL flow is according to the original design A nd many more WA G protect s more than just SQL injection.
  • 43. The downside of WAG Although WA G is very promising, it is difficult to configure it precisely, especially for protecting SQL injection attacks on free-format text input Without proper configuration, the WAG ( or even for human) cannot judge if the input should be allowed or blocked and return an error page to the browser. A new user register to a web portal application N oted the apostrophe But should we block this?
  • 44. The downside of WAG (cont’d) I t is difficult to tell if the WAG should block the input just base on the input itself. The WAG should, however, decide if it should be blocked base on the allowable input pattern of the backend application . That is , if the backend application will escape the apostrophe properly before inserting into the database, or if the data is completely not SQL related, then the WAG may accept this input; otherwise, this input may cause exception in the server and hence should be blocked – even if it doesn’t look like a SQL injection!
  • 45. The downside of WAP (cont’d) Likewise, it is also difficult to tell if the backend application is vulnerable without testing it . C an’t ask the programmer because the program may not behave as the programmer expected (and that’s why we have a bug), C an’t determine by looking at the JavaScript of the form field to detect the format requirement because that may also be wrong. The only reliable way to determine the allowable input format is to test it.
  • 46. Solution Basically two methods to make configuration much easier if we can accept certain amount of false alarms
  • 47. To make configuration easier: 1 st method Default d en y any text input with an apostrophe , but allow exceptional cases We will block our example input “ 115 Admin’s Street ”, and then generate an alert to notify the sys-admin or developer. The sys-admin or developer then verifies if the program is vulnerable; if not , he/she then change the WA G configuration to allow the apostrophe in “this” form field next time. But what about numeric field or column name injection? Deny all input with space?
  • 48. To make configuration easier: 2 nd method Den y input only if it really looks like a SQL injection attack We will allow our example input “ 115 Admin’s Street ”, because it really doesn’t look like a SQL injection attack. In case the backend application d oesn ’t escape the input properly, (hopefully) this will generate an error but may not result in data lose.
  • 49. To make configuration easier Clearly, both are not perfect solutions T he first method result s in higher false positive error T he second one result s in higher false negative error and is susceptible to attack evasion At first glance, the first method seems to be a better choice, however, for a well tested web application, the second method may be a more practical approach.
  • 50. Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC Methods Development Phase QA Phase Production Phase
  • 51. Method 6: SQL Driver Proxy A SQL driver proxy [8] works like web application gateway, except that it intercepts API calls instead of network connections, and monitors database function calls instead of HTTP requests. And same as web proxy, it will pass the request to the backend “original driver” if it is a legitimate request. The proxy nature enables these tools to monitor and block malicious SQL execution, as well as sanitizing error message send from database server back to the client application. Sanitizing error message is import as the error message usually reveals information about the database schema.
  • 52. Architecture of a SQL Driver Proxy HTTP Client HTTP Server HTTP Server HTTP Client ODBC JDBC App Original Driver ODBC/JDBC Driver ODBC/JDBC App Analysis Analysis HTTP Proxy ODBC/JDBC Proxy HTTP Protocol HTTP Protocol API Calls API Calls
  • 53. How SQL Driver Proxy works? For example, an online banking web application may only issue three SQL queries: query user table during authentication, query user owned bank accounts information, and transfer money between bank accounts. The application may ONLY issue queries in the following forms SELECT * FROM tbl_user WHERE user_id = ‘<some thing>’ AND password = ‘<some thing>’ SELECT * FROM tbl_accounts WHERE user_id = ‘<some thing>’ UPDATE tbl_accounts SET balance = balance + <some value> WHERE account_id = ‘<some thing>’
  • 54. How SQL Driver Proxy works? (cont’d) When the application is under SQL injection attack, it may issues queries as follows: SELECT * FROM tbl_user WHERE user_id = ‘<some thing>’ AND password = ‘<some thing>’ OR 1=1 --’ SELECT * FROM tbl_accounts WHERE user_id = ‘<some thing>’ UNION … UPDATE tbl_accounts SET balance = balance + <some value> WHERE account_id = ‘<some thing>’ ; DROP …
  • 55. How SQL Driver Proxy works? (cont’d) Because the “ basic structure ” of these query statements will not be the same as the three original queries, and hence can be detected. Moreover, because all SQL queries are monitored, these tools can also prevent second order SQL injection attacks. For some implementations, the list of allowable SQL statements can be “auto-learned” which makes configuration easier.
  • 56. SQL Driver Proxy limitation Like many other technologies, SQL driver proxy has it own limitation. Since the proxy has to determine if a SQL query is legitimate, the query can not have its structures varying depending on user’s input. Consider a web page allowing user to select the data fields to be reported by using a multi-line selection box as shown below
  • 57. SQL Driver Proxy limitation (cont’d) The client application may issue a SQL query as follows: SELECT p_cat, p_name, p_price FROM tbl_prod WHERE p_name LIKE ‘%hello%’ However, if the user selects 2 data fields only, then the SQL query will be different (only two column names after SELECT) SELECT p_name, p_price FROM tbl_prod WHERE p_name LIKE ‘%hello%’ Depending on the implementation and number of columns, the maximum number of queries it can “mutate” is n!, which may not be a trivial configuration task if n is large.
  • 58. Methods to prevent SQL Injection Input Validation Static query statement Least Privilege Code Verification Web Application Gateway SQL Driver Proxy MISC Methods Development Phase QA Phase Production Phase
  • 59. Intrusion Detection System (IDS) Network Intelligence provides some Snort signatures for detecting SQL injection. But of course, IDS technology is susceptible to detection evasion and can’t handle SSL traffic. http://www.niiconsulting.com/resources/snort.html
  • 60. Context-Sensitive String Evaluation [9] The concept not only works for protecting SQL injection attack, but is also applicable for all general command injection attacks The general ideal is to change the language design to distinguish user supplied strings with static strings, and depending on the usage of a string, impose some runtime meta-character restriction on the user supplied string. For example, user supplied input can’t contain apostrophe if it is to be used in a SQL query statement, and can’t contain “&&” or “|” if it is to be used in a system() command. A prototype implementation for PHP is currently available
  • 61. Database Layer Protection [1 0 , 1 1 , 1 2 , 1 3, 14 ] describe methods to prevent SQL injection at the database layer Techniques involves static (source code) analysis, run-time (query statement) analysis and/or provide another set of “Secured API”
  • 62. Conclusion SQL Injection is one of the most important problem in web application security As shown in Table 1 , the number of vulnerabilities reported increased more than triples from 2004 Jan-Jun to the same period in 2005, and it is expected that this figure will continue to increase in the near future. The solutions for SQL injection are not very complicate but it requires good management to deploy properly Don’t under estimate SQL injection and tackle the problem in a more holistic and systematic approach
  • 63. Reference SecuriTeam, SQL Injection Walkthrough, May 2002 http://www.securiteam.com/securityreviews/5DP0N1P76E.html Steve Friedl, SQL Injection Attacks by Example, Dec 2004 http://www.unixwiz.net/techtips/sql-injection.html Gunter Ollmann, “Second-order Code Injection Attacks” http://www.nextgenss.com/papers/SecondOrderCodeInjection.pdf PHP Magic Quotes Manual http://www.php.net/manual/en/security.magicquotes.php Oracle Invoker's Rights Procedures http://www.stanford.edu/dept/itss/docs/oracle/10g/network.101/b10773/glossary.htm Security Context of Dynamic SQL Statements Inside a Stored Procedure, http://support.microsoft.com/default.aspx?scid=kb;en-us;301299 Jeff Forristal, Source-Code Assessment Tools Kill Bugs Dead, Secure Enterprise, Dec 2005 http://www.secureenterprisemag.com/showArticle.jhtml?articleId=174402221 Sam M.S NG, SQLBlock: SQL Injection Protection by Variable Normalization of SQL Statement, May 2005 http://www.sqlblock.com/sqlblock.pdf
  • 64. Reference - 2 T. Pietraszek and C. V. Berghe. Defending against Injection Attacks through Context-Sensitive String Evaluation. In Proceedings of the 8th International Symposium on Recent Advances in Intrusion Detection (RAID), Sept. 2005. S. W. Boyd and A. D. Keromytis. SQLRand: Preventing SQL injection attacks, In Proceedings of the 2nd Applied Cryptography and Network Security (ACNS) Conference, pages 292{302. Springer-Verlag, June 2004. Gregory T. Buehrer, Bruce W. Weide, and Paolo A. G. Sivilotti: Using Parse Tree Validation to Prevent SQL Injection Attacks, In Proceedings of the Fifth FSE International Workshop on Software Engineering and Middleware (SEM), September 2005 Zhendong Su and Gary Wassermann: The Essence of Command Injection Attacks in Web Applications, 33rd Annual Symposium on Principles of Programming Languages, Charleston, SC, Jan 11-13. p. 372-382. W. G. Halfond and A. Orso. Combining static analysis and runtime monitoring to counter SQL-injection attacks. In Online Proceeding of the Third International ICSE Workshop on Dynamic Analysis (WODA 2005), pages 22-28, May 2005. http://www.csd.uwo.ca/woda2005/proceedings.html. William G.J. Halfond and Alessandro Orso: AMNESIA: Analysis and Monitoring for NEutralizing SQLInjection Attacks, Proceedings of the IEEE and ACM International Conference on Automated Software Engineering (ASE 2005).