Hardening Your Web Application Against SQL Injections

 WARNING:


The information provided is for educational purposes only and may not be used for malicious purposes


Before delving into what SQL Injection actually is, let me explain to you what SQL itself is.


What is SQL?


Structured Query Language (SQL) is a specialized programming language for sending queries to databases. Most small and industrial database applications can be accessed using SQL statements. SQL is both an ANSI and ISO standard. However, many database products that support SQL do so with proprietary extensions to the standard language. Web applications can use user-supplied input to create custom SQL statements for dynamic web page requests.


What is SQL Injection?


SQL injection is a technique that exploits a security flaw in the database layer of a web application. This vulnerability occurs when user input is either improperly filtered for string literal escape characters embedded in SQL statements, or user input is not strongly typed and is therefore executed unexpectedly. In fact, this is an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is embedded within another.


"SQL Injection" is a subset of the unvalidated/unmodified user input vulnerability ("buffer overflow" is another subset) and the idea is to convince an application to execute SQL code that was not intended. If an application naively creates SQL strings at runtime and then executes them, it's easy to create real surprises.


Many organizations' web servers have been compromised precisely because of SQL Injections, including big names that I don't want to mention here, you can easily look them up on the Internet.


What is Blind SQL Injection?


This particular type of attack is called a blind SQL injection attack because the attacker cannot take advantage of detailed error messages from the server or other sources of information about the application. Getting the SQL syntax right is usually the trickiest part of the blind SQL insertion process and can take a lot of trial and error. But by adding additional conditions to the SQL statement and evaluating the output of the web application, the attacker ultimately determines whether the application is vulnerable to SQL injection.


Blind SQL injection is a special case that plays on the security of the website developers or website owners. While they may think that everything on the server is heavily guarded, a Blind SQL injection attack will silently play truth or dare with the web server. This type of attack, while very time consuming, is the one that provides the most potentially damaging security hole. This is because the attacker not only gains access, but also a vast amount of knowledge about the database and can potentially gain access to the server's file system. This type of attack is automatic and requires good setup to succeed. But once it's done, it doesn't require much effort to repeat.


What is a SQL Injection error message?


Web applications commonly use SQL queries with client-supplied input in the WHERE clause to retrieve data from a database. When a web application executes such queries without validating or scanning user-supplied data to ensure it is not malicious, an SQL injection attack can occur. By sending unexpected data, an attacker can generate and send SQL queries to the web application database. The SQL injection vulnerability is tested by sending application data that generates an invalid SQL query. If the server returns an error message, this information can be used to attempt to gain uncontrolled access to the database. This is the basis of one of the most popular SQL injection attacks.


Hiding error messages does not stop a SQL injection attack. What usually happens is that the attacker uses the knowledge gained from the failure of this attack to change tactics. What they are turning to is blind SQL injection.


Why SQL Injection?


When a web application fails to properly sanitize user-supplied input, it is possible for an attacker to alter the construction of backend SQL statements. When an attacker is able to modify an SQL statement, the process starts with the same privileges as the component that executed the statement. (E.g. database server, web application server, web server, etc.). The impact of this attack can allow attackers to gain complete control over the database or even execute commands on the system.


When your computer only has port 80 open, your most trusted vulnerability scanner can't return anything useful, and you know that the admin is always patching his server, that's the point where a malicious hacker would turn to hacking the site. SQL injection is one type of web hack that doesn't require anything other than port 80 and it can work even if the admin is happy with the fixes. It attacks the web application itself (like ASP, JSP, PHP, CGI, etc.) rather than the web server or services running in the OS.


SQL injection types:


There are four main categories of SQL Injection attacks against the database layer of a web application


1. SQL manipulation: manipulation is the process of modifying SQL statements using various operations such as UNION. Another way to implement SQL injection using the SQL manipulation method is to change the where clause of the SQL statement to get different results.


2. Code injection: Code injection is the process of inserting new SQL statements or database statements into a vulnerable SQL statement. One code injection attack is to attach a SQL Server EXECUTE statement to a vulnerable SQL statement. This type of attack is only possible if multiple SQL statements per database request are supported.


3. Function Call Injection: Function call injection is the process of injecting various database function calls into a vulnerable SQL statement. These function calls can make operating system calls or manipulate data in the database.


4. Buffer Overflow: Buffer overflow is caused by the use of function call embedding. Patches are available for most commercial and open source databases. This type of attack is possible when the server is unpatched


SQL Injection Prevention Techniques:


Mitigating the SQL injection vulnerability would be to use one of two routes, i.e. either using stored procedures along with callable statements or using prepared statements with dynamic SQL statements. Regardless of which method is adopted, data validation is essential.


A. Login Verification


Data sanitization is key. The best way to sanitize data is to use the default days, regular expression. Write specific filters. Use numbers, numbers and letters whenever possible. If punctuation of any kind needs to be included, convert it using HTML encoding. SO that " becomes """ or > becomes ">" For example, if the user enters an email address, allow only @, -, . And _ except for numbers and letters to be used, only after they have been converted to their HTML

SQL

SQL , also pronounced as See-Quel, stands for Structured Query Language, letting you access, manipulate, create, delete, update, and retriev...