Defeating SQL Injection

Defeating SQL Injection

CORE Metadata, citation and similar papers at core.ac.uk Provided by Institutional Knowledge at Singapore Management University Singapore Management University Institutional Knowledge at Singapore Management University Research Collection School Of Information Systems School of Information Systems 10-2012 Defeating SQL injection Lwin Khin SHAR Singapore Management University, [email protected] Hee Beng Kuan TAN Follow this and additional works at: https://ink.library.smu.edu.sg/sis_research Part of the Information Security Commons, OS and Networks Commons, and the Programming Languages and Compilers Commons Citation SHAR, Lwin Khin and TAN, Hee Beng Kuan. Defeating SQL injection. (2012). Computer. 46, (3), 69-77. Research Collection School Of Information Systems. Available at: https://ink.library.smu.edu.sg/sis_research/4898 This Journal Article is brought to you for free and open access by the School of Information Systems at Institutional Knowledge at Singapore Management University. It has been accepted for inclusion in Research Collection School Of Information Systems by an authorized administrator of Institutional Knowledge at Singapore Management University. For more information, please email [email protected]. PERSPECTIVES Defeating SQL Injection Lwin Khin Shar and Hee Beng Kuan Tan, Nanyang Technological University, Singapore The best strategy for combating SQL injection, which has emerged as the most widespread website security risk, calls for integrating defensive coding practices with both vulnerability detection and runtime attack prevention methods. tructured Query Language injection is a code injec- the past decade, we found that developers can effectively tion technique commonly used to attack websites combat SQL injection using the right combination of state- in which the attacker inserts SQL characters or key- of-the art methods. However, they must develop a better words into a SQL statement via unrestricted user understanding of SQL injection and how to practically inte- S 1 input parameters to change the intended query’s logic. This grate current defenses. threat exists in any Web application that accesses a data- base via SQL statements constructed with external input INSECURE CODING PRACTICES data. By manipulating this data to modify the statements, SQL is the standard language for accessing database an attacker can cause the application to issue arbitrary SQL servers, including MySQL, Oracle, and SQL Server.1 Web commands and thereby compromise the database. programming languages such as Java, ASP.NET, and PHP The Open Web Application Security Project (OWASP) provide various methods for constructing and executing ranks SQL injection as the most widespread website secu- SQL statements, but, due to a lack of training and develop- rity risk (www.owasp.org/index.php/Top_10). In 2011, the ment experience, application developers often misuse these National Institute of Standards and Technology’s National methods, resulting in SQL injection vulnerabilities (SQLIVs). Vulnerability Database (nvd.nist.gov) reported 289 SQL Developers commonly rely on dynamic query building injection vulnerabilities (7 percent of all vulnerabilities) in with string concatenation to construct SQL statements. websites, including those of IBM, Hewlett-Packard, Cisco, During runtime, the system forms queries with inputs WordPress, and Joomla. In December 2011, SANS Insti- directly received from external sources. This method tute security experts reported a major SQL injection attack makes it possible to build different queries based on vary- (SQLIA) that affected approximately 160,000 websites using ing conditions set by users. However, as this is the cause of Microsoft’s Internet Information Services (IIS), ASP.NET, and many SQLIVs, some developers opt to use parameterized SQL Server frameworks (isc.sans.org/diary/SQL+Injection+ queries or stored procedures. While these methods are Attack+happening+ATM/12127). more secure, their inappropriate use can still result in vul- Inadequate validation and sanitization of user inputs nerable code. In the PHP code examples below, name and make websites vulnerable to SQL injection, and research- pwd are the “varchar” type columns and id is the “integer” ers have proposed various ways to address this problem, type column of a user database table. ranging from simple static analysis to complex dynamic Absence of checks. The most common and serious mis- analysis. In 2006, William Halfond, Jeremy Viegas, and take developers make is using inputs in SQL statements Alessandro Orso2 evaluated then-available techniques and without any checks. The following PHP code is an example called for more precise solutions. In reviewing work during of such a dynamic SQL statement: 0018-9162/13/$31.00 © 2013 IEEE Published by the IEEE Computer Society MARCH 2013 69 PERSPECTIVES $query = “SELECT info FROM user WHERE name = For such queries, instead of escaping characters, devel- ‘$_GET[“name”]’ AND pwd = ‘$_GET[“pwd”]’”; opers should use a data type check—for example, i f(i s _ n u m e r i c($i d))—to prevent SQLIAs. Attackers can use tautologies to exploit this insecure Absence or misuse of delimiters in query strings. When practice. In this case, by supplying the value x’ OR ‘1’=‘1 constructing a query string with inputs, a programmer to the input parameter name, an attacker could access user must use proper delimiters to indicate the input’s data information without a valid account because the WHERE- type. The absence or misuse of delimiters could enable SQL clause condition becomes injection even in the presence of thorough input validation, escaping, and type checking. For example, the following WHERE name = ‘x’ OR ‘1’=‘1’ AND …”; PHP code does not include delimiters to indicate the input string used in the SQL statement: which the system will evaluate to be true. Insufficient escaping. If a developer escapes special $name = mysql_real_escape_string($_GET[“name”]); characters meaningful to a SQL parser, the parser will not $query = “SELECT info FROM user WHERE name = interpret them as SQL commands. For example, the above $ n a m e”; tautology-based attack could be prevented by escaping the ’ character (to avoid its being interpreted as a string In this case, when the database server has the automatic delimiter) from the inputs. However, many developers are type conversion function enabled, an attacker could use either not aware of the full list of characters that have spe- an alternate encoding method that circumvents input sani- cial meanings to the SQL parser or they are not familiar tization routines. For instance, if the attacker supplies the with the proper usage patterns. encoded HEX string 0x270x780x270x200x4f0x520x200x Consider the following PHP code, mysql_real_escape_ 310x3d0x31 to the parameter name, the database parser string, which is a function used to escape MySQL special may convert it to the “varchar” value, resulting in the tau- characters: tology string ‘x’ OR 1=1. Because the conversion occurs in the database, the server program’s escaping function $name = mysql_real_escape_string($_GET[“name”]); would not detect any special characters encoded in the $query = “SELECT info FROM user WHERE pwd LIKE HEX string. ‘%$p w d%’”; Improper parameterized queries or stored procedures. Most developers believe that SQL injection is impossible The function mysql_real_escape_string would when using parameterized queries or stored procedures to protect SQL statements that do not use pattern-matching run SQL statements. Although this is generally true, some database operators such as LIKE, GRANT, and REVOKE. In developers are not aware that SQL injection is still possible this case, however, an attacker could include the additional if parameterized query strings or stored procedures accept wildcard characters % and _ in the password field to match nonparameterized inputs. more password characters than the beginning and end Consider, for example, the following PHP code: characters because mysql_real_escape_string does not escape wildcard characters. $query = “SELECT info FROM user WHERE name = Absence of data type checks. Another error that develop- ?”.“ORDER BY ‘$_GET[“order”]’”; ers make is failing to check data types before constructing $stmt = $dbo->prepare($query); SQL statements. Instead, they often apply programming $stmt->bindParam(1, $_GET[“name”]); language or database-provided sanitization functions such $ s t m t-> e x e c u t e(); as addslashes and mysql_real_escape_string to the input parameters before using them in SQL statements. Although an attacker could not conduct a SQLIA through However, when the query is to access the database the parameter name, SQL injection is still possible through columns of numeric data and other non-text-based data order, which is not parameterized. An attacker could inject types, a SQLIA need not contain the escaped/sanitized piggy-backed query attacks—malicious queries attached to characters. For example, the following PHP code shows a the original query—such as ASC; DROP TABLE user; -- into SQL statement for which a tautology-based attack could be the parameter order. conducted by supplying the value 1 OR 1=1 to the param- eter id: SQL INJECTION DEFENSES SQL injection defense methods can be broadly classified $id = mysql_real_escape_string($_GET[“id”]); into three types: defensive coding, SQLIV detection, and $query = “SELECT info FROM user WHERE id = SQLIA runtime prevention. Table 1 compares the strengths $id”; and weaknesses of various approaches in each

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us