14.1 Understand SQL Injection and SQL Injection Black Box Penetration Testing
Total Page:16
File Type:pdf, Size:1020Kb
SQL injection is considered as the most common website vulnerability available on the Internet. It is a flaw in Web applications. It is not a database or webserver problem.
In this chapter, we will learn about SQL injection, SQL injection attacks, server side technologies, and SQL injection detection. This chapter focuses on blind SQL injection, SQL injection methodology, SQL injection tools, and defensive strategies against SQL injection attacks.
14.1 Understand SQL injection and SQL injection black box penetration testing Exam Focus: Understand SQL injection and SQL injection black box penetration testing. Objective includes: Understand SQL injection. Examine SQL injection attacks. Identify server side technologies. Understand SQL injection detection. Discuss SQL injection black box pen testing.
SQL injection attack An SQL injection attack is a process in which an attacker tries to execute unauthorized SQL statements. These statements can be used to delete data from a database, delete database objects such as tables, views, stored procedures, etc. An attacker can either directly enter the code into input variables or insert malicious code in strings that can be stored in a database.
For example, the following line of code illustrates one form of an SQL injection attack: query = "SELECT * FROM users WHERE name = '" + userName + "';"
This SQL code is designed to fetch the records of any specified username from its table of users. However, if the "userName" variable is crafted in a specific way by a malicious hacker, the SQL statement may do more than the code author intended.
For example, if the attacker puts the "userName" value as ' or ''=', the SQL statement will be as follows:
SELECT * FROM users WHERE name = '' OR ''='';
The following is an example of a normal SQL query:
Select * from mytable where user name = 'Mark' and password =`12345`;
Example: Code analysis
A user enters a user name and password that matches a record in the table. The number of matched rows is retrieved using a dynamically generated SQL query. The user is then authenticated and redirected to the requested page. The SQL query will look like as follows when the attacker enters blah' or 1=1--:
SELECT Count (*) FROM Users WHERE UserName= 'blah' Or 1=1 -- ' AND Password=' '
The query simply becomes as follows because a pair of hyphens designate the beginning of a comment in SQL:
SELECT Count(*) FROM Users WHERE UserName= 'blah' Or 1=1
The following is an example of SQL injection query:
Select * from mytable where user name = ''OR 1=1; --' and password=`dummy`;
The following attacks are performed by SQL injection based on the application and how it processes user-supplied data: Authentication bypass: In this attack, an attacker can log onto an application and gain administrative privileges. The attacker does not even need to provide a valid username and password. Information disclosure: In this attack, an attacker obtains sensitive information stored in the database. Compromised data integrity: In this attack, an attacker defaces a webpage, inserts malicious content into web pages, or changes the content of a database. Compromised availability of data: In this attack, an attacker deletes the information, deletes log, or audits information that is included in a database. Remote code execution: In this attack, an attacker can compromise the host operating system. The following image shows SQL injection threats:
SQL injections There are many SQL injection attack codes for the blind SQL injection attack, which are as follows: admin' -- admin' # admin'/* ' or 1=1-- ' or 1=1# ' or 1=1/* ') or '1'='1-- ') or ('1'='1-- Login as different user (SM*): ' UNION SELECT 1, 'anotheruser', 'doesn't matter', 1--
Server side technologies Developers use powerful server-side technologies like ASP.NET and database servers to create dynamic and data-driven Web sites with incredible ease. Hackers can use SQL injection attacks to exploit the power of ASP.NET and SQL. SQL injection attacks target websites that do not follow secure coding practices to access and manipulate data stored in a relational database. They do not exploit specific software vulnerability. Relational databases such as SQL Server, Oracle, IBM DB2, and MySQL are susceptible to SQL-injection attacks.
HTTPS POST request When a user provides information and clicks the submit button in the Account Login page, the browser submits a string to the web server that includes the user's credentials. This string is visible in the body of the HTTP or HTTPS POST request as follows:
SQL query at the database select * from Users where (username = 'mark' and password = 'johnson');