SQL injection

Classification of SQL injection attack vectors in 2010A classification of SQL injection attacking vector as of 2010

In computing, SQL injection is a code injection technique used to attack data-driven applications, in which malicious SQL statements are inserted into an entry field for execution (e.g. to dump the database contents to the attacker).[1][2] SQL injection must exploit a security vulnerability in an application's software, for example, when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and unexpectedly executed. SQL injection is mostly known as an attack vector for websites but can be used to attack any type of SQL database.

SQL injection attacks allow attackers to spoof identity, tamper with existing data, cause repudiation issues such as voiding transactions or changing balances, allow the complete disclosure of all data on the system, destroy the data or make it otherwise unavailable, and become administrators of the database server. Document-oriented NoSQL databases can also be affected by this security vulnerability.[3]

SQL injection remains a widely recognized security risk due to its potential to compromise sensitive data. The Open Web Application Security Project (OWASP) describes it as a vulnerability that occurs when applications construct database queries using unvalidated user input. Exploiting this flaw, attackers can execute unintended database commands, potentially accessing, modifying, or deleting data. OWASP outlines several mitigation strategies, including prepared statements, stored procedures, and input validation, to prevent user input from being misinterpreted as executable SQL code.[4]

History

[edit]

Discussions of SQL injection began in the late 1990s, including in a 1998 article in Phrack Magazine.[5] SQL injection was ranked among the top 10 web application vulnerabilities of 2007 and 2010 by the Open Web Application Security Project (OWASP).[6] In 2013, SQL injection was listed as the most critical web application vulnerability in the OWASP Top 10.[7]

In 2017, the OWASP Top 10 Application Security Risks grouped SQL injection under the broader category of "Injection," ranking it as the third most critical security threat. This category included various types of injection attacks, such as SQL, NoSQL, OS command, and LDAP injection. These vulnerabilities arise when an application processes untrusted data as part of a command or query, potentially allowing attackers to execute unintended actions or gain unauthorized access to data.[8]

By 2021, injection remained a widespread issue, detected in 94% of analyzed applications, with reported incidence rates reaching up to 19%. That year’s OWASP Top 10 further expanded the definition of injection vulnerabilities to include attacks targeting Object Relational Mapping (ORM) systems, Expression Language (EL), and Object Graph Navigation Library (OGNL). To address these risks, OWASP recommends strategies such as using secure APIs, parameterized queries, input validation, and escaping special characters to prevent malicious data from being executed as part of a query.[9][10]

Root cause

[edit]

SQL injection is a common security vulnerability that arises from letting attacker-supplied data become SQL code. This happens when programmers assemble SQL queries either by string interpolation or by concatenating SQL commands with user supplied data. Therefore, injection relies on the fact that SQL statements consist of both data used by the SQL statement and commands that control how the SQL statement is executed. For example, in the SQL statement select * from person where name = 'susan' and age = 2 the string 'susan' is data and the fragment and age = 2 is an example of a command (the value 2 is also data in this example).

SQL injection occurs when specially crafted user input is processed by the receiving program in a way that allows the input to exit a data context and enter a command context. This allows the attacker to alter the structure of the SQL statement which is executed.

As a simple example, imagine that the data 'susan' in the above statement was provided by user input. The user entered the string 'susan' (without the apostrophes) in a web form text entry field, and the program used string concatenation statements to form the above SQL statement from the three fragments select * from person where name=', the user input of 'susan', and ' and age = 2.

Now imagine that instead of entering 'susan' the attacker entered ' or 1=1; --.

The program will use the same string concatenation approach with the 3 fragments of select * from person where name=', the user input of ' or 1=1; --, and ' and age = 2 and construct the statement select * from person where name='' or 1=1; --' and age = 2. Many databases will ignore the text after the '--' string as this denotes a comment. The structure of the SQL command is now select * from person where name='' or 1=1; and this will select all person rows rather than just those named 'susan' whose age is 2. The attacker has managed to craft a data string which exits the data context and entered a command context.

Ways to exploit

[edit]

Although the root cause of all SQL injections is the same, there are different techniques to exploit it. Some of them are discussed below:

Getting direct output or action

[edit]

Imagine a program creates a SQL statement using the following string assignment command :

var statement = "SELECT * FROM users WHERE name = '" + userName + "'";

This SQL code is designed to pull up the records of the specified username from its table of users. However, if the "userName" variable is crafted in a specific way by a malicious user, the SQL statement may do more than the code author intended. For example, setting the "userName" variable as:

' OR '1'='1

or using comments to even block the rest of the query (there are three types of SQL comments[11]). All three lines have a space at the end:

' OR '1'='1' --
' OR '1'='1' }:  CS1 maint: numeric names: authors list (link)

  • ^ Andrey Rassokhin; Dmitry Oleksyuk. "TDSS botnet: full disclosure". Archived from the original on December 9, 2012. Retrieved December 6, 2012.
  • ^ "Questions for TalkTalk - BBC News". BBC News. October 26, 2015. Archived from the original on October 26, 2015. Retrieved October 26, 2015.
  • ^ "Input Validation Cheat Sheet". Open Web Application Security Project (OWASP). Retrieved March 10, 2025.
  • ^ "Testing for ORM Injection". OWASP. Retrieved March 17, 2025.
  • ^ "SQL Injection Attacks & Prevention: Complete Guide". appsecmonkey.com. February 13, 2021. Retrieved February 24, 2021.
  • ^ "MySQL String Literals".
  • ^ "MySQL SELECT Statement".
  • ^ "MySQL Schema Object Names".
  • ^ "Guesswork Plagues Web Hole Reporting". SecurityFocus. March 6, 2002. Archived from the original on July 9, 2012.
  • ^ "WHID 2005-46: Teen uses SQL injection to break to a security magazine web site". Web Application Security Consortium. November 1, 2005. Archived from the original on January 17, 2010. Retrieved December 1, 2009.
  • ^ "WHID 2006-3: Russian hackers broke into a RI GOV website". Web Application Security Consortium. January 13, 2006. Archived from the original on February 13, 2011. Retrieved May 16, 2008.
  • ^ "Anti-U.S. Hackers Infiltrate Army Servers". Information Week. May 29, 2009. Archived from the original on December 20, 2016. Retrieved December 17, 2016.
  • ^ Alex Papadimoulis (April 15, 2008). "Oklahoma Leaks Tens of Thousands of Social Security Numbers, Other Sensitive Data". The Daily WTF. Archived from the original on May 10, 2008. Retrieved May 16, 2008.
  • ^ "US man 'stole 130m card numbers'". BBC. August 17, 2009. Archived from the original on August 18, 2009. Retrieved August 17, 2009.
  • ^ "The pirate bay attack". July 7, 2010. Archived from the original on August 24, 2010.
  • ^ "Did Little Bobby Tables migrate to Sweden?". Alicebobandmallory.com. Retrieved June 3, 2011.}: CS1 maint: deprecated archival service (link)
  • ^ "Royal Navy website attacked by Romanian hacker". BBC News. November 8, 2010. Archived from the original on November 9, 2010. Retrieved November 15, 2023.
  • ^ Sam Kiley (November 25, 2010). "Super Virus A Target For Cyber Terrorists". Archived from the original on November 28, 2010. Retrieved November 25, 2010.
  • ^ "Hacker breaks into Barracuda Networks database". Archived from the original on July 27, 2011.
  • ^ "site user password intrusion info". Dslreports.com. Archived from the original on October 18, 2012. Retrieved June 3, 2011.
  • ^ "DSLReports says member information stolen". Cnet News. April 28, 2011. Archived from the original on March 21, 2012. Retrieved April 29, 2011.
  • ^ "DSLReports.com breach exposed more than 100,000 accounts". The Tech Herald. April 29, 2011. Archived from the original on April 30, 2011. Retrieved April 29, 2011.
  • ^ "LulzSec hacks Sony Pictures, reveals 1m passwords unguarded", electronista.com, June 2, 2011, archived from the original on June 6, 2011, retrieved June 3, 2011
  • ^ "Imperva.com: PBS Hacked - How Hackers Probably Did It". Archived from the original on June 29, 2011. Retrieved July 1, 2011.
  • ^ Ngak, Chenda. "Yahoo reportedly hacked: Is your account safe?". CBS News. Archived from the original on July 14, 2012. Retrieved July 16, 2012.
  • ^ Yap, Jamie (July 12, 2012). "450,000 user passwords leaked in Yahoo breach". ZDNet. Archived from the original on July 2, 2014. Retrieved February 18, 2017.
  • ^ Perlroth, Nicole (October 3, 2012). "Hackers Breach 53 Universities and Dump Thousands of Personal Records Online". New York Times. Archived from the original on October 5, 2012.
  • ^ Kovacs, Eduard (November 4, 2013). "Hackers Leak Data Allegedly Stolen from Chinese Chamber of Commerce Website". Softpedia News. Archived from the original on March 2, 2014. Retrieved February 27, 2014.
  • ^ Damon Poeter. 'Close-Knit' Russian Hacker Gang Hoards 1.2 Billion ID Creds Archived July 14, 2017, at the Wayback Machine, PC Magazine, August 5, 2014
  • ^ Nicole Perlroth. Russian Gang Amasses Over a Billion Internet Passwords Archived February 27, 2017, at the Wayback Machine, The New York Times, August 5, 2014.
  • ^ "TalkTalk gets record £400,000 fine for failing to prevent October 2015 attack". October 5, 2016. Archived from the original on October 24, 2016. Retrieved October 23, 2016.
  • ^ Goodin, Dan (March 2, 2021). "Rookie coding mistake prior to Gab hack came from site's CTO". Ars Technica.
  • ^ Goodin, Dan (March 8, 2021). "Gab, a haven for pro-Trump conspiracy theories, has been hacked again". Ars Technica.
  • ^ "Mass exploitation of critical MOVEit flaw is ransacking orgs big and small". Ars Technica. June 6, 2023. Retrieved March 9, 2025.
  • ^ "Researchers say a bug let them add fake pilots to rosters used for TSA checks". The Verge. September 8, 2024. Retrieved March 9, 2025.
  • ^ Munroe, Randall. "XKCD: Exploits of a Mom". Archived from the original on February 25, 2013. Retrieved February 26, 2013.
  • ^ "The Bobby Tables Guide to SQL Injection". September 15, 2009. Archived from the original on November 7, 2017. Retrieved October 30, 2017.
  • ^ "Jego firma ma w nazwie SQL injection. Nie zazdrościmy tym, którzy będą go fakturowali ;)". Niebezpiecznik (in Polish). September 11, 2014. Archived from the original on September 24, 2014. Retrieved September 26, 2014.
  • [edit]

    This article is sourced from Wikipedia. Content is available under the Creative Commons Attribution-ShareAlike License.