{"id":20980,"date":"2021-04-11T16:32:47","date_gmt":"2021-04-11T11:02:47","guid":{"rendered":"https:\/\/valeurbit.com\/blog\/?p=20980"},"modified":"2021-04-11T16:32:50","modified_gmt":"2021-04-11T11:02:50","slug":"what-is-sql-injection-and-how-it-works","status":"publish","type":"post","link":"https:\/\/valeurbit.com\/blog\/what-is-sql-injection-and-how-it-works\/","title":{"rendered":"What is SQL Injection and How it works?"},"content":{"rendered":"\n<p>In modern web applications, injections are less and less common, everyone uses prepared queries and ORMs, but we still see injections during penetration tests.<\/p>\n\n\n\n<p>Of particular interest are the SQL dialects built into ORM libraries.&nbsp;This is an additional abstraction that is also prone to injection, and vulnerabilities can arise when translating expressions from a dialect into a specific SQL implementation.<\/p>\n\n\n\n<p><strong>Introduction<\/strong><\/p>\n\n\n\n<p>ORM is a library that associates objects and their attributes in code with tables and fields in a database.<\/p>\n\n\n\n<p>The ORM abstraction allows you to represent relational database tables as ordinary objects and treat them like objects.<\/p>\n\n\n\n<p>ORM allows you to separate the tasks of the database and the application, so that the programmer does not even have to write SQL queries, but simply perform actions with objects, and the corresponding SQL queries will be generated by the ORM library.<\/p>\n\n\n\n<p><strong>What is ORM used for?<\/strong><\/p>\n\n\n\n<p>It is clear that the absence of the need to manually write hundreds of SQL queries simplifies the development process, especially in large projects.<\/p>\n\n\n\n<p>At the same time, queries generated by the library are more difficult to optimize, and the library itself adds an overhead.<\/p>\n\n\n\n<p>Using an ORM in itself is not a means of protecting against injection, but when used correctly, the libraries provide a means for parameterized and prepared queries.<\/p>\n\n\n\n<p><strong>Doctrine and SQL<\/strong><\/p>\n\n\n\n<p>There are many ORM libraries for various programming languages \u200b\u200band frameworks.&nbsp;Let&#8217;s take a closer look at the Doctrine project written in PHP and the injection exploitation in the Doctrine Query Language.&nbsp;Doctrine is used by default in the popular Symfony PHP framework.<\/p>\n\n\n\n<p>You can use Doctrine both by performing actions on objects in PHP code (using QueryBuilder) and manually executing SQL queries.\u00a0It is also possible to execute raw queries directly in SQL.<\/p>\n\n\n\n<p>The SQL language is based on HQL (Hibernate Query Language in the Hibernate Java library) and is a subset of SQL, but it still has quite a few features that can help with injection exploitation.<\/p>\n\n\n\n<p>SQL supports the familiar SELECT, UPDATE, DELETE statements, but there is no implementation of the INSERT and UNION statements, the LIMIT expression (you must use the setMaxResults method).\u00a0The authors of the library did not implement the UNION operator due to the strict SQL typing (and UNION implies the ability to select data of different types).<\/p>\n\n\n\n<p>SQL also provides support for subqueries, JOIN, WHERE, ORDER BY, HAVING, IN, etc.<\/p>\n\n\n\n<p>Below is a list of the built-in functions in SQL that can be used after the SELECT, WHERE and HAVING clauses.\u00a0Also, after the SELECT and GROUP BY expressions, you can use the AVG, COUNT, MIN, MAX, SUM functions.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.deteact.com\/ru\/wp-content\/uploads\/sites\/2\/2019\/08\/image.png\" alt=\"\" class=\"wp-image-27\"\/><\/figure>\n\n\n\n<p>As with many DBMSs, in Doctrine you can create your own User Defined Function in PHP and make it available from SQL.<\/p>\n\n\n\n<p><strong>SQL injection<\/strong><\/p>\n\n\n\n<p>This is how Doctrine&#8217;s creating an SQL query for fetching data looks like when working with objects in code:<\/p>\n\n\n\n<p>And below is the difference between a SQL query and an SQL query:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td>$sqlQuery = &#8220;SELECT p FROM App\\Entity\\Post p WHERE id = &#8216;$query&#8217; ORDER BY p.publishedAt DESC&#8221;;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td>$sqlQuery = &#8220;SELECT * FROM post WHERE id = &#8216;$query&#8217; ORDER BY publishedAt DESC&#8221;;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Obviously, in both cases there is a concatenation of some variable with a query.\u00a0If it is user data, a SQL injection is possible.<\/p>\n\n\n\n<p>The principles of SQL injection exploitation, of course, do not differ from the SQL injection exploitation, but it is necessary to understand that an attacker cannot completely control the query that will be sent to the DBMS.\u00a0In fact, the work does not go with the database, but with the models, therefore, for example, it will not be possible to retrieve data from tables for which no models are defined in the code.<\/p>\n\n\n\n<p>Let&#8217;s see what happens when creating such a query (QueryBuilder is called from the Post class method):<\/p>\n\n\n\n<p>The SQL query is converted into a syntax tree, after which an SQL query is generated in the grammar of the connected DBMS.<\/p>\n\n\n\n<p><strong>Injection techniques<\/strong><\/p>\n\n\n\n<p>Depending on the DBMS used, the type of request, the context of the injection and the settings (the presence of a debug mode), various algorithms for exploiting the injection are possible, such as Boolean Based and Error Based.<\/p>\n\n\n\n<ul><li><strong>Boolean Based<\/strong><\/li><\/ul>\n\n\n\n<p>The substring function and subqueries allow you to iterate over the values \u200b\u200bof model attributes character by character:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td>1 or 1=(select 1 from App\\Entity\\User a where a.id=1 and substring(a.password,1,1)=&#8217;$&#8217;)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>From the screenshots you can see that we got the value of the first character of the password hash (&#8220;$&#8221;).&nbsp;However, in the SELECT statement, we used the fully qualified name of the User model.&nbsp;There is no easy way to get a list of all models.<\/p>\n\n\n\n<ul><li><strong>Error Based (SQLite)<\/strong><\/li><\/ul>\n\n\n\n<p>When using the SQLite DBMS, there is another feature &#8211; the SQLite dialect is rather poor, and SQL provides the same interface regardless of the DBMS used.\u00a0Therefore, in the absence of any native functions in SQLite, you have to write their implementation in PHP.<\/p>\n\n\n\n<p>This concerns the udfSqrt, udfMod, udfLocate functions (corresponding SQL functions: SQRT, MOD, LOCATE).\u00a0When incorrect data is passed to these functions, an exception occurs at the PHP level, and not at the DBMS level, therefore, when displaying errors, the entire SQL subquery result may leak.<\/p>\n\n\n\n<p>Error:<\/p>\n\n\n\n<p>The result of an SQL subquery with a password hash:<\/p>\n\n\n\n<p>It is clear that in the absence of a debug mode, the application is unlikely to display this data, but, nevertheless, it is possible to exploit Error Based injection by brute force (extract a bit of information on the presence or absence of an internal error).<\/p>\n\n\n\n<ul><li><strong>Injection in ORDER BY<\/strong><\/li><\/ul>\n\n\n\n<p>The SQL grammar does not provide for the use of complex expressions and subqueries after ORDER BY and GROUP BY, so exploitation of the injection in such a context is not possible, the parser will only skip literals.<\/p>\n\n\n\n<ul><li><strong>Injection into IN<\/strong><\/li><\/ul>\n\n\n\n<p>A subquery can be passed as arguments to the IN expression, which provides various possibilities for exploiting injection, for example, using the Error Based technique:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td>$sqlQuery = &#8220;SELECT p FROM App\\Entity\\Post p WHERE p.id IN (select sqrt(a.password) from App\\Entity\\User a where a.id=2)&#8221;;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul><li><strong>UPDATE injection.<\/strong><\/li><\/ul>\n\n\n\n<p>The UPDATE statement allows you to write the result of a subquery into the value of a model attribute, so that you can retrieve the entire data via an external channel (by writing secret data to a table with public data):<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td>UPDATE App\\Entity\\Post p SET p.title = (SELECT u.password FROM App\\Entity\\User u WHERE u.id = 2), slug = testslug, summary = testsum, content = testcon WHERE id = 25<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>conclusions<\/strong><\/p>\n\n\n\n<p>Using ORM is not a panacea for SQL injection.&nbsp;It is necessary to carefully validate and sanitize the data transmitted by users, use prepared queries.<\/p>\n\n\n\n<p>Many developers are used to the fact that frameworks do all the work for them, and there is no need to worry about the security of their code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In modern web applications, injections are less and less common, everyone uses prepared queries and ORMs, but we still see injections during penetration tests. Of particular interest are the SQL dialects built into ORM libraries.&nbsp;This is an additional abstraction that is also prone to injection, and vulnerabilities can arise when translating expressions from a dialect&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is SQL Injection and How it works? | ValeurBit Infosec<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/valeurbit.com\/blog\/what-is-sql-injection-and-how-it-works\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is SQL Injection and How it works? | ValeurBit Infosec\" \/>\n<meta property=\"og:description\" content=\"In modern web applications, injections are less and less common, everyone uses prepared queries and ORMs, but we still see injections during penetration tests. Of particular interest are the SQL dialects built into ORM libraries.&nbsp;This is an additional abstraction that is also prone to injection, and vulnerabilities can arise when translating expressions from a dialect...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/valeurbit.com\/blog\/what-is-sql-injection-and-how-it-works\/\" \/>\n<meta property=\"og:site_name\" content=\"ValeurBit Infosec\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/valeurbitinfo\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-11T11:02:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-11T11:02:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.deteact.com\/ru\/wp-content\/uploads\/sites\/2\/2019\/08\/image.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@valeurbit\" \/>\n<meta name=\"twitter:site\" content=\"@valeurbit\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/valeurbit.com\/blog\/#organization\",\"name\":\"Valeurbit Infosec\",\"url\":\"https:\/\/valeurbit.com\/blog\/\",\"sameAs\":[\"https:\/\/www.facebook.com\/valeurbitinfo\/\",\"https:\/\/www.instagram.com\/valeurbit\",\"https:\/\/www.linkedin.com\/company\/valeurbit-infosec\/\",\"https:\/\/twitter.com\/valeurbit\"],\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/valeurbit.com\/blog\/#logo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/valeurbit.com\/blog\/wp-content\/uploads\/2021\/02\/Valeurbit-new-logo-center.png\",\"contentUrl\":\"https:\/\/valeurbit.com\/blog\/wp-content\/uploads\/2021\/02\/Valeurbit-new-logo-center.png\",\"width\":1080,\"height\":512,\"caption\":\"Valeurbit Infosec\"},\"image\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/#logo\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/valeurbit.com\/blog\/#website\",\"url\":\"https:\/\/valeurbit.com\/blog\/\",\"name\":\"ValeurBit Infosec\",\"description\":\"Cyber Security Company\",\"publisher\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/valeurbit.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/valeurbit.com\/blog\/what-is-sql-injection-and-how-it-works\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/blog.deteact.com\/ru\/wp-content\/uploads\/sites\/2\/2019\/08\/image.png\",\"contentUrl\":\"https:\/\/blog.deteact.com\/ru\/wp-content\/uploads\/sites\/2\/2019\/08\/image.png\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/valeurbit.com\/blog\/what-is-sql-injection-and-how-it-works\/#webpage\",\"url\":\"https:\/\/valeurbit.com\/blog\/what-is-sql-injection-and-how-it-works\/\",\"name\":\"What is SQL Injection and How it works? | ValeurBit Infosec\",\"isPartOf\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/what-is-sql-injection-and-how-it-works\/#primaryimage\"},\"datePublished\":\"2021-04-11T11:02:47+00:00\",\"dateModified\":\"2021-04-11T11:02:50+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/what-is-sql-injection-and-how-it-works\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/valeurbit.com\/blog\/what-is-sql-injection-and-how-it-works\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/valeurbit.com\/blog\/what-is-sql-injection-and-how-it-works\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/valeurbit.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What is SQL Injection and How it works?\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/valeurbit.com\/blog\/what-is-sql-injection-and-how-it-works\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/what-is-sql-injection-and-how-it-works\/#webpage\"},\"author\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/#\/schema\/person\/df20c1cd317765fa8677a3056caeccfa\"},\"headline\":\"What is SQL Injection and How it works?\",\"datePublished\":\"2021-04-11T11:02:47+00:00\",\"dateModified\":\"2021-04-11T11:02:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/what-is-sql-injection-and-how-it-works\/#webpage\"},\"wordCount\":1165,\"publisher\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/valeurbit.com\/blog\/what-is-sql-injection-and-how-it-works\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/blog.deteact.com\/ru\/wp-content\/uploads\/sites\/2\/2019\/08\/image.png\",\"articleSection\":[\"Valeurbit\"],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/valeurbit.com\/blog\/#\/schema\/person\/df20c1cd317765fa8677a3056caeccfa\",\"name\":\"ValeurBit\",\"sameAs\":[\"https:\/\/valeurbit.com\/blog\"],\"url\":\"https:\/\/valeurbit.com\/blog\/author\/valeurbit\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/posts\/20980"}],"collection":[{"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/comments?post=20980"}],"version-history":[{"count":0,"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/posts\/20980\/revisions"}],"wp:attachment":[{"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/media?parent=20980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/categories?post=20980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/valeurbit.com\/blog\/wp-json\/wp\/v2\/tags?post=20980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}