30
SQL Injection (Most common Injection Flaw) From Rich Helton’s October 2010 C# Web Security

Sql Injection and Entity Frameworks

Embed Size (px)

Citation preview

Page 1: Sql Injection and Entity Frameworks

SQL Injection(Most common Injection

Flaw)

From Rich Helton’s October 2010 C# Web Security

Page 2: Sql Injection and Entity Frameworks

Intro to SQL Injection…

Many web pages communicate directly to a backend database for processing.

For example, a username and password is asked for on the Web page and the web page will pass it to the database to validate the information.

Some applications will not validate the field adequately before passing it to the database, and the database will process whatever it will receive.

Hackers will pass SQL commands directly to the database, and in some cases tables like “passwords” are returned because the SQL commands are not being filtered adequately.

SQL may return errors in the web page that even lists the correct tables to query so that the hacker may make more accurate attempts to get data.

Page 3: Sql Injection and Entity Frameworks

SQL Injection

SQL Injection is the ability to inject malicious SQL commands into the backend code.

For example:

SELECT * FROM users WHERE username = ‘USRTEXT ' AND password = ‘PASSTEXT’

Passing ' OR 1=1-- in the USRTEXT field generates:

SELECT * FROM users WHERE username = ‘’ OR 1=1 -- '

AND password = ‘PASSTEXT’ The OR 1=1 returns true and the rest is commented

out

Page 4: Sql Injection and Entity Frameworks

ASP.NET Hacme Bank(Let’s try it)

Page 5: Sql Injection and Entity Frameworks

ASP.NET Hacme BankAuthentication without username/password

Page 6: Sql Injection and Entity Frameworks

Types of SQL Injection…

There are really two types of SQL injection, “Blind” SQL Injection and “Directed” SQL Injection.

Blind SQL Injection is performed when a hacker passes SQL commands into the web form and generic errors are returned to the user, for instance a “404” Error page or page not found. The hacker has to make more extensive guesses on the database behind the web server.

Directed SQL Injection is when the web server returns SQL errors to the user that give information about the table that has issue processing the SQL command. Some web pages may return “users.password table incorrect SQL query”, which gives the hacker the name of the database to launch the attack against.

Page 7: Sql Injection and Entity Frameworks

Common attack strings

‘ or 27(hex) – delineates SQL string values.“ or 22 (hex) – also delineates SQL string values.; or 3B (hex) - terminates statements.# or 23(hex) - also terminates a statement. (Access DB)/* or 2F2A (hex) - comment delimiter.-- or 2D2D (hex) – also comment delimiter.( or 28 (hex) or ) or 29 (hex) – logical sub clauses.{ or 7B (hex) or } or 7D (hex) – terminates a question.exec – used to call MS-SQL stored procedures.

union – a SQL command very common to SQL injection.

Page 8: Sql Injection and Entity Frameworks

HackmeBooks SQL Injection(shows org.hsqldb.jdbc connection)

Page 9: Sql Injection and Entity Frameworks

HackmeBooks SQL Injection(attacking)

HSQL DB, uses a SHUTDOWN to shut down the database, since the SEARCH field uses straight SQL commands, typing in ‘;+SHUTDOWN;-- will add ‘%’; SHUTDOWN; --%’ in the SQL statement, thus shutting down the database:

Session is now closed because we shutdown the database:

Page 10: Sql Injection and Entity Frameworks

Real life example

Start by identifying the SQL Server version, table name and fields in the error page:

We see that it is SQL Server, and an “id” field into the “business.dbo.urltracking” table. An Attacker can now try inserting into the table.

Page 11: Sql Injection and Entity Frameworks

Common fixes to SQL Injection…

SQL Injection is caused by “Dynamic SQL” with unconstrained validation.

Constrain the validation to not pass SQL commands to Dynamic SQL.

Use Stored Procedures.

Use Parameterized, or Prepared statements.

Use newer technology frameworks that are built using Parameterized statements like NHibernate and Spring.NET.

Use the ADO.NET Entity framework.

Page 12: Sql Injection and Entity Frameworks

Stored Procedures

A stored procedure is a precompiled subroutine that is stored in the data dictionary for use of applications accessing the SQL Server.

A sample stored procedure for exec sp_GetInventory ‘FL’ :

Page 13: Sql Injection and Entity Frameworks

Hacking Stored Procedures

Stored procedures can be just as dangerous as SQL Injection, if not properly configured.

One the most dangerous Stored Procs in SQL Server is the default xp_cmd_shell.

If you have admin permissions with SQL server, you can try this simple example: exec master..xp_cmdshell ‘dir c:\’

Extending this feature, dynamic SQL may allow, in the username form : MyUsername; exec xp_cmdshell '"echo open 192.168.10.12" >> c:\hack.txt’;

See http://www.informit.com/articles/article.aspx?p=30124&seqNum=3 for an example attack.

Page 14: Sql Injection and Entity Frameworks

Stored Procedures Hacks(Who’s hacking them? From SANs )

Page 15: Sql Injection and Entity Frameworks

Entity Framework

With the ADO.NET Entity Framework, Visual Studio can be used to create Entity Relationship Models (ERM) in order to create a database.

Entity Framework is part of .NET 4 and is often referred to as EF4.

Page 16: Sql Injection and Entity Frameworks

Entity Framework(Generate from DB)

Page 17: Sql Injection and Entity Frameworks

Entity Framework(Selecting ADO.NET in VS 2010)

Page 18: Sql Injection and Entity Frameworks

A Sample Entity Framework(Model1.edmx with the VS Model Browser)

Changes made to the model can propagate to the Database.

Page 19: Sql Injection and Entity Frameworks

Another Example(Has all the details of the data)

Page 20: Sql Injection and Entity Frameworks

A Database can be generated

Page 21: Sql Injection and Entity Frameworks

Customize the code generated by the Entity Designer with T4 (.tt) templates

T4 is the Text Template Transformation Toolkit. T4 is a means for creating code generated artifacts. T4 will generate a .tt file which looks like ASP classic syntax with the brackets. The .tt file is the Text Template file that will generate the background C# code from the Entity Model. Click on the model .edmx file and select “Add Code Generation File…”

Page 22: Sql Injection and Entity Frameworks

Use a T4 Editor to highlight code

VS 2010 does not come with a T4 Visual Editor, so a plugin needs to be installed to offer IntelliSense. For VS 2010, I use the plugin at http://t4-editor.tangible-engineering.com

To

Page 23: Sql Injection and Entity Frameworks

T4 Editor

The .tt is just the template to generate the underlying .cs (C#) file:

Page 24: Sql Injection and Entity Frameworks

PEM

Microsoft’s Portable Extension Metadata, a subset of shema metadata, can be installed to add validation to the Entity Module and its entities, http://visualstudiogallery.msdn.microsoft.com/en-us/e6467914-d48d-4075-8885-ce5a0dcb744d

Page 25: Sql Injection and Entity Frameworks

PEM

After installing PEM, validation not only shows up in properties, but generation code can be generated through T4.

Page 26: Sql Injection and Entity Frameworks

PEM

PemValidation.cs with the Validate method for Employee:

Page 27: Sql Injection and Entity Frameworks

Object-Relational Mapping (ORM)

NHibernate, the .NET version of Hibernate, can be used as a object-relational mapping (ORM) and persistence framework that allows you to map .NET objects to relational database tables using (XML) configuration files.Its purpose is to relieve the developer from a significant amount of relational data persistence-related programming tasks. The main advantages of Hibernate is that maps database entities to objects and hides the details of the data access from the business logic.Hibernate uses prepared statements, so it is protected from direct SQL injection, but it could still be vulnerable to injecting HQL statements which are more complex to execute.

Page 28: Sql Injection and Entity Frameworks

Sample Customer Mapping

Page 29: Sql Injection and Entity Frameworks

NHibernate Validator

NHibernate has it’s own Validator plugin http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation.aspx .This validator (or constraint) will not only validate the values but can also validate the size of the data before being persisted. Sample constraint annotations:

public class Address {

[NotNull]

private string name; // Cannot be null

[NotNull]

[Length(Max = 5, Message = "{long}")]

[Pattern(Regex = "[0-9]+")] // Regex for Digits

private string zip; // 5 digits

Page 30: Sql Injection and Entity Frameworks

Recommendations

It is recommended to validate the data at the entity level, just in case the Front End is compromised. ORM’s not only make the coding of data easier to the Database, by not using SQL in multiple places, but also alleviates many of the Dynamic SQL issues.