HackingIn-Depth ConceptsNETWORK FEDILITIES

SQL Injection Attacks: Know How to Prevent Them


With advancement in technology, modern society has accomplished many unthinkable goals. However, as technology develops, so does the risk involved in using it. Same is the case with web applications. Today’s applications are fraught with vulnerabilities. Since 2003, SQL injection has remained in the OWASP top ten list of application security risks that companies are wrestling with. In this article, we will explore SQL injection attacks and ways to prevent it. Let’s take a look at topics covered in this article:

  1. What is an SQL Injection Attack?
  2. What can an SQL Injection do?
  3. How do SQL Injection attacks work?
  4. What are different types of SQL Injection attacks?
  5. How can SQL Injection Attacks be prevented?

You can go through this cybersecurity video lecture where our training expert is discussing each and every nitty-gritty of the technology.

Application Security | SQL Injection Attack | Edureka

What Is an SQL Injection Attack?

SQL injection (SQLi) is an injection attack where an attacker executes malicious SQL statements to control a web application’s database server, thereby accessing, modifying, and deleting unauthorized data.

In the early days of the Internet, building websites was a simple process: no JavaScript, no CSS, and few images. But as the websites gained popularity, the need for more advanced technology and dynamic websites grew. This led to the development of server-side scripting languages like JSP and PHP. Websites started storing user input and content in databases. MySQL became the most popular and standardized language for accessing and manipulating databases. However, hackers found new ways to leverage the loopholes present in SQL technology. SQL injection attacks are one of the most popular ways of targeting databases. SQL injections target the databases using specifically-crafted SQL statements to trick the systems into doing unexpected and undesired things.

What Can SQL Injection Attacks Do?

There are a lot of things an attacker can do when exploiting an SQL injection on a vulnerable website. By leveraging an SQL injection vulnerability, given the right circumstances, an attacker can do the following things:

  • Bypass a web application’s authorization mechanisms and extract sensitive information
  • Easily control application behavior that’s based on data in the database
  • Inject further malicious code to be executed when users access the application
  • Add, modify, and delete data, corrupting the database, and making the application or unusable
  • Enumerate the authentication details of a user registered on a website and use the data in attacks on other sites

It all depends on the capability of the attacker, but sometimes, an SQL injection attack can lead to a complete takeover of the database and web application. Now, how does an attacker achieve that?

How Do SQL Injection Attacks Work?

A developer usually defines an SQL query to perform some database action necessary for his application to function. This query has one or two arguments so that only desired records are returned when the value for that argument is provided by a user.

An SQL injection attack plays out in two stages:

  1. Research: Attacker gives some random unexpected values for the argument, observes how the application responds, and decides an attack to attempt.
  2. Attack: Here, the attacker provides carefully-crafted value for the argument. The application will interpret the value part of an SQL command rather than merely data. The database then executes the SQL command as modified by the attacker.

Consider the following example in which a website user is able to change the values of $user and $password, such as in a login form:

$statement = "SELECT * FROM users WHERE username ='$user'  AND  password '$password'";

This particular SQL statement is passed to a function, which, in turn, sends the string to the connected database where it is parsed, executed, and returns a result.

#Define POST variables
uname = request.POST['username']
passwd = request.POST['password']
#SQL query vulnerable to SQLi
sql = "SELECT id FROM users WHERE username='" + uname + "' AND password='" + passwd + "'"
#Execute the SQL statement 

Now, if the input is not properly sanitized but the application, the attacker can easily insert carefully crafted value as input. For example, this will look something like:

$statement = "SELECT * FROM users WHERE username ='Dean' OR '1'='1'-- ' AND password = 'WinchesterS'";

So, what’s happening here? The highlighted part is the attacker’s input, it contains two special parts:

  • OR ‘1’ = ‘1’ is a condition that will always be true, thereby it is accepted as a valid input by the application
  • -(double hyphen) instructs the SQL parser that the rest of the line is a comment and should not be executed

Once the query executes, the SQL injection effectively removes the password verification, resulting in an authentication bypass. The application will most likely log the attacker in with the first account from the query result – the first account in a database is usually of an administrative user.

Note that this is just one way of exploiting the SQL Queries to get the necessary information in an unofficial way. SQL injection attacks are divided into multiple types.

What Are the Different Types of SQL Injection Attacks?

Attackers can extract data from servers by leveraging an SQL injection vulnerability in various ways. SQL injection can be classified into three major categories:

Let’s explore the variants.

In-Band SQL Injection

It is the most common SQL injection attack. Usually, it occurs when an attacker is able to use the same communication channel to both launch the attack and gather results. The two most common types of in-band SQL injection are:

  • Error-based SQL Injection — It is a technique that relies on error messages thrown by the database server to obtain information about the structure of the database. Sometimes, this simple attack is more than enough for an attacker to enumerate an entire database.
  • Union-based SQL Injection — This technique leverages the UNION SQL operator to combine the results of two or more SELECT statements into a single result, which is then returned as part of the HTTP response.

In this type of injection, no data is actually transferred via the web application. So, the attacker will not be able to see the result of an attack. Here, the attacker reconstructs the database structure by sending payloads and observing the web application’s response and the resulting behavior of the database server. The two types of inferential SQL injection are:

  • Boolean-based SQL Injection — In this technique application is forced to return a different result depending on whether the query returns a TRUE or FALSE result. Based on the result, the content within the HTTP response will change, or remain the same.
  • Time-based SQL Injection — It is a technique that relies on sending an SQL query to the database, which forces the database to wait for a specified amount of time (in seconds) before responding. The time website takes to respond will indicate to the attacker whether the result of the query is TRUE or FALSE.

Out-of-Band SQL Injection

These types of SQL injection attacks are the least common and generally the most difficult to execute. They usually involve sending the data directly from the database server to a machine that is controlled by the attacker. Out-of-band techniques offer the attacker an alternative to In-band or Blind SQL injection attacks, especially if the server responses are not very stable.

So, server-scripting languages are not able to determine if or not the SQL query string is malformed. All that they can do is send a string to the database server and wait for the interpreted response. But surely, there must be ways to sanitize user input and ensure that an SQL injection is infeasible, right?

How Can SQL Injection Attacks Be Prevented?

There are a lot of easy ways to avoid falling prey for SQL injection attacks and to limit the damage they can cause. Few of them include:

  • Discover SQL injection vulnerabilities by routinely testing applications both using static testing and dynamic testing
  • Avoid and repair injection vulnerabilities by using parameterized queries and Object Relational Mappers (ORMs). These types of queries specify placeholders for parameters so that the database will always treat them as data rather than part of a SQL command.
  • Remediate SQL injection vulnerabilities by using escape characters so that special characters are ignored.
  • Mitigate the impact of SQL injection vulnerabilities by enforcing least privilege on the database, this way each software component of an application can access and affect only the resources it needs.
  • Use a Web Application Firewall (WAF) for web applications that access databases. This can help identify SQL injection attempts and sometimes help prevent SQL injection attempts from reaching the application as well.

SQL injection attacks are popular attack methods for cybercriminals, but by taking the proper precautions, such as ensuring that data is encrypted, performing security tests, and being up to date with patches, you can take meaningful steps toward keeping your data secure.

There are a variety of ways a hacker may infiltrate an application due to web application vulnerabilities. So stay informed!

Lovepreet Singh

Scripting for the Ambitious Hacker, Part 2 (Conditional Statements)

Previous article

Hacking the Heartbleed Vulnerability

Next article

You may also like


  1. Cialis Gр с–рўв˜nstig Kaufen Order Zithromax Azithromycin [url=http://cialtadalaf.com]cialis[/url] Prescription Free 2 5 Next Day Viagra

  2. If you wish for to obtain a great deal from this piece of writing then you have to
    apply such strategies to your won web site.

  3. Cialis Generique 20mg Viagra In Pomata Cytotec5mg [url=http://sildenaf50.com]viagra[/url] Il Viagra Donne Cialis En Inde Viagra Ninos

  4. Outstanding post however I was wondering if you could write a litte more on this topic?
    I’d be very thankful if you could elaborate a little bit more.
    Many thanks!

  5. Helpful info. Fortunate me I found your
    web site unintentionally, and I am stunned why this twist of fate did not took place in advance!
    I bookmarked it.

  6. Hi there! Do you use Twitter? I’d like to follow you if that would
    be okay. I’m absolutely enjoying your blog and look forward to new

  7. Cialis E Viagra Online [url=http://ciali10mg.com]cialis overnight shipping from usa[/url] Zithromax Dosage Pneumonia Achat Cialis En Ligne

  8. Hi, I log on to your blog daily. Your humoristic style is awesome,
    keep doing what you’re doing!

  9. An outstanding share! I have just forwarded this onto a colleague who was doing a little homework on this.

    And he actually bought me dinner because I found it for him…
    lol. So allow me to reword this…. Thanks for the meal!!
    But yeah, thanks for spending the time to talk about this matter here on your blog.

  10. Excellent goods from you, man. I have understand your stuff
    previous to and you are just too magnificent.
    I actually like what you’ve acquired here, really like what
    you’re saying and the way in which you say
    it. You make it entertaining and you still take care of to keep it smart.

    I can’t wait to read much more from you. This is actually a
    wonderful site.

  11. I think this is among the most significant info for me. And i’m glad reading your article.
    But want to remark on few general things, The website style is ideal, the articles is really great
    : D. Good job, cheers

  12. Nice post. I was checking continuously this blog and I am impressed!
    Extremely useful info specially the last part 🙂 I care for such information much.
    I was seeking this certain info for a very long time.
    Thank you and good luck.

  13. Usually I do not read article on blogs, however
    I would like to say that this write-up very pressured me
    to take a look at and do it! Your writing style has been surprised me.

    Thanks, very nice post.

  14. Hi! Someone in my Facebook group shared this website with us so I came to give
    it a look. I’m definitely enjoying the information.
    I’m book-marking and will be tweeting this to my followers!
    Superb blog and great style and design.

  15. I just could not leave your web site prior to suggesting that I really loved the standard information an individual provide to your guests?
    Is gonna be back frequently to check up on new posts

  16. wonderful publish, very informative. I’m wondering
    why the other specialists of this sector don’t realize this.
    You must continue your writing. I’m sure, you’ve a great readers’
    base already!

  17. Attractive part of content. I just stumbled upon your web site and in accession capital to say that I acquire actually loved account your weblog posts.

    Anyway I will be subscribing on your augment or even I success you access consistently

  18. Hello, I think your blog might be having browser compatibility issues.
    When I look at your blog in Opera, it looks fine but when opening in Internet Explorer,
    it has some overlapping. I just wanted to give you
    a quick heads up! Other then that, excellent blog!

  19. Simply wish to say your article is as surprising. The clearness in your
    post is just cool and i can assume you’re an expert on this
    subject. Fine with your permission allow me to grab your RSS feed to keep up to date with forthcoming post.
    Thanks a million and please continue the gratifying work.

  20. Thanks a lot for sharing this with all of us you really recognize what you’re speaking about!
    Bookmarked. Please also discuss with my web site =).
    We may have a link exchange contract among us

  21. Howdy! Do you know if they make any plugins to protect against hackers?
    I’m kinda paranoid about losing everything
    I’ve worked hard on. Any suggestions?

  22. I all the time emailed this weblog post page to all
    my contacts, since if like to read it afterward my contacts will too.

  23. First of all I want to say wonderful blog! I had a quick question that
    I’d like to ask if you don’t mind. I was curious to
    know how you center yourself and clear your head before writing.

    I have had a difficult time clearing my thoughts in getting my ideas out there.
    I do take pleasure in writing but it just seems like the first 10 to
    15 minutes are usually wasted simply just trying to figure out how to begin. Any recommendations or hints?
    Thank you!

  24. I used to be suggested this blog by my cousin. I am not sure whether or not this post is written through him as no one else understand
    such unique approximately my difficulty. You are amazing!

  25. I think the admin of this website is really working hard in favor of his web site, as here every stuff
    is quality based information.

  26. Please let me know if you’re looking for a article author for your weblog.

    You have some really great posts and I feel I would be a good asset.
    If you ever want to take some of the load off, I’d love to write some articles for your blog in exchange for a link back to mine.
    Please blast me an e-mail if interested. Thank you!

  27. Having read this I thought it was rather informative.

    I appreciate you taking the time and energy to
    put this short article together. I once again find myself spending
    way too much time both reading and posting comments.

    But so what, it was still worth it!

  28. Very rapidly this web site will be famous among all
    blog viewers, due to it’s fastidious articles

  29. Its such as you learn my mind! You seem to grasp so much about this, like you wrote the e book in it or something.
    I feel that you simply can do with a few percent to pressure the message house a bit, but
    instead of that, that is magnificent blog. A fantastic read.
    I will definitely be back.

Leave a reply

Your email address will not be published. Required fields are marked *

More in Hacking