Docsity

Sql Injection - Description, Appunti di Elementi di Informatica

Anteprima parziale del testo

Scarica Sql Injection - Description e più Appunti in PDF di Elementi di Informatica solo su Docsity!

Contents

This page intentionally left blank

1 ˛ Summary ˛ Solutions Fast Track ˛ Frequently Asked Questions

Chapter 1

What Is SQL

Injection?

Solutions in this chapter:

■ Understanding How Web Applications Work ■ Understanding SQL Injection ■ Understanding How It Happens

What Is SQL Injection? • Chapter 1 3 They normally consist of a back-end database with Web pages that contain server-side script written in a programming language that is capable of extracting specific information from a database depending on various dynamic interactions with the user. One of the most common applications for a database-driven Web application is an e-commerce application, where a variety of information is stored in a database, such as product information, stock levels, prices, postage and packing costs, and so on. You are probably most familiar with this type of application when purchasing goods and products online from your e-retailer of choice. A database-driven Web application commonly has three tiers: a presentation tier (a Web browser or rendering engine), a logic tier (a programming language, such as C#, ASP, .NET, PHP, JSP, etc.), and a storage tier (a database such as Microsoft SQL Server, MySQL, Oracle, etc.). The Web browser (the presentation tier, such as Internet Explorer, Safari, Firefox, etc.) sends requests to the middle tier (the logic tier), which services the requests by making queries and updates against the database (the storage tier). Take, for example, an online retail store that presents a search form that allows you to sift and sort through products that are of particular interest, and provides an option to further refine the products that are displayed to suit financial budget constraints. To view all products within the store that cost less than $100, you could use the following URL: ■ http://www.victim.com/products.php?val=10 0 The following PHP script illustrates how the user input ( val ) is passed to a dynamically created SQL statement. The following section of the PHP code is executed when the URL is requested. // connect to the database $conn = mysql_connect("localhost","username","password"); // dynamically build the sql statement with the input $query = "SELECT * FROM Products WHERE Price < '$_GET["val"]' ". "ORDER BY ProductDescription"; // execute the query against the database $result = mysql_query($query); // iterate through the record set while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { // display the results to the browser echo "Description : {$row['ProductDescription']}
". "Product ID : {$row['ProductID']}
". "Price : {$row['Price']}

"; } The following code sample more clearly illustrates the SQL statement that the PHP script builds and executes. The statement will return all of the products in the database that cost less than $100. These products will then be displayed and presented to your Web browser so that you can continue shopping within your budget constraints.

4 Chapter 1 • What Is SQL Injection? In principle, all interactive database-driven Web applications operate in the same way, or at least in a similar fashion. SELECT * FROM Products WHERE Price < '100.00' ORDER BY ProductDescription; A Simple Application Architecture As noted earlier, a database-driven Web application commonly has three tiers: presentation, logic, and storage. To help you better understand how Web application technologies interact to present you with a feature-rich Web experience, Figure 1. 1 illustrates the simple three-tier example that I outlined previously. Figure 1.1 Simple Three-Tier Architecture The presentation tier is the topmost level of the application. It displays information related to such services as browsing merchandise, purchasing, and shopping cart contents, and it communicates with other tiers by outputting results to the browser/client tier and all other tiers in the network. The logic tier is pulled out from the presentation tier, and as its own layer, it controls an application’s functionality by performing detailed processing. The data tier consists of database servers. Here, information is stored and retrieved. This tier keeps data independent from application servers or business logic. Giving data its own tier also improves scalability and performance. In Figure 1. 1 , the Web browser (presentation) sends requests to the middle tier (logic), which services them by making queries and updates against the database (storage). A fundamental rule in a three-tier architecture is that the

6 Chapter 1 • What Is SQL Injection? In Figure 1. 2 , the Web browser (presentation) sends requests to the middle tier (logic), which in turn calls the exposed APIs of the application server residing within the application tier, which services them by making queries and updates against the database (storage). In Figure 1. 2 , the user fires up his Web browser and connects to http://www.victim. com. The Web server that resides in the logic tier loads the script from the file system and passes it through its scripting engine where it is parsed and executed. The script calls an exposed API from the application server that resides in the application tier. The application server opens a connection to the storage tier using a database connector and executes an SQL statement against the database. The database returns the data to the database connector and the application server then implements any application or business logic rules before returning the data to the Web server. The Web server then implements any final logic before presenting the data in HTML format to the user’s Web browser within the presentation tier. The user’s Web browser renders the HTML and presents the user with a graphical representation of the code. All of this happens in a matter of seconds and is transparent to the user. The basic concept of a tiered architecture involves breaking an application into logical chunks, or tiers, each of which is assigned general or specific roles. Tiers can be located on different machines or on the same machine where they are virtually or conceptually separate from one another. The more tiers you use, the more specific each tier’s role is. Separating the responsibilities of an application into multiple tiers makes it easier to scale the application, allows for better separation of development tasks among developers, and makes an application more readable and its components more reusable. The approach can also make applications more robust by eliminating a single point of failure. For example, a decision to change database vendors should require nothing more than some changes to the applicable portions of the application tier; the presentation and logic tiers remain unchanged. Three-tier and four-tier architectures are the most commonly deployed architectures on the Internet today; however, the n -tier model is extremely flexible and, as previously discussed, the concept allows for many tiers and layers to be logically separated and deployed in a myriad of ways. Understanding SQL Injection Web applications are becoming more sophisticated and increasingly technically complex. They range from dynamic Internet and intranet portals, such as e-commerce sites and partner extranets, to HTTP-delivered enterprise applications such as document management systems and ERP applications. The availability of these systems and the sensitivity of the data that they store and process are becoming critical to almost all major businesses, not just those that have online e-commerce stores. Web applications and their supporting infrastructure and environments use diverse technologies and can contain a significant amount of modified and customized code. The very nature of their feature-rich design and their capability to collate, process, and disseminate information over the Internet or from within an intranet makes them a popular target for attack. Also, since the network security technology market has

What Is SQL Injection? • Chapter 1 7 matured and there are fewer opportunities to breach information systems through network- based vulnerabilities, hackers are increasingly switching their focus to attempting to compromise applications. SQL injection is an attack in which SQL code is inserted or appended into application/ user input parameters that are later passed to a back-end SQL server for parsing and execution. Any procedure that constructs SQL statements could potentially be vulnerable, as the diverse nature of SQL and the methods available for constructing it provide a wealth of coding options. The primary form of SQL injection consists of direct insertion of code into parameters that are concatenated with SQL commands and executed. A less direct attack injects malicious code into strings that are destined for storage in a table or as metadata. When the stored strings are subsequently concatenated into a dynamic SQL command, the malicious code is executed. When a Web application fails to properly sanitize the parameters which are passed to dynamically created SQL statements (even when using parameterization techniques) it is possible for an attacker to alter the construction of back-end SQL statements. When an attacker is able to modify an SQL statement, the statement will execute with the same rights as the application user; when using the SQL server to execute commands that interact with the operating system, the process will run with the same permissions as the component that executed the command (e.g., database server, application server, or Web server), which is often highly privileged. To illustrate this, let’s return to the previous example of a simple online retail store. If you remember, we attempted to view all products within the store that cost less than $100, by using the following URL: ■ http://www.victim.com/products.php?val=10 0

Note

The URL examples in this chapter use GET parameters instead of POST parameters for ease of illustration. POST parameters are just as easy to manipulate; however, this usually involves the use of something else, such as a traffic manipulation tool, Web browser plug-in, or inline proxy application. This time, however, you are going to attempt to inject your own SQL commands by appending them to the input parameter val. You can do this by appending the string ‘ OR ‘1’= ‘1 to the URL: ■ http://www.victim.com/products.php?val=100’ OR ‘1’=’ This time, the SQL statement that the PHP script builds and executes will return all of the products in the database regardless of their price. This is because you have altered the