SlideShare una empresa de Scribd logo
1 de 30
SQL Injection
(Most common Injection
Flaw)
From Rich Helton’s October 2010
C# Web Security
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.
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
ASP.NET Hacme Bank
(Let’s try it)
ASP.NET Hacme Bank
Authentication without username/password
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.
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.
HackmeBooks SQL Injection
(shows org.hsqldb.jdbc connection)
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:
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.
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.
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’ :
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.
Stored Procedures Hacks
(Who’s hacking them? From SANs )
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.
Entity Framework
(Generate from DB)
Entity Framework
(Selecting ADO.NET in VS 2010)
A Sample Entity Framework
(Model1.edmx with the VS Model Browser)
Changes made to the model can propagate to the Database.
Another Example
(Has all the details of the data)
A Database can be generated
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…”
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
T4 Editor
 The .tt is just the template to generate the underlying .cs (C#) file:
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
PEM
 After installing PEM, validation not only shows up in properties,
but generation code can be generated through T4.
PEM
 PemValidation.cs with the Validate method for Employee:
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.
Sample Customer Mapping
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
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.

Más contenido relacionado

La actualidad más candente

Ppt on sql injection
Ppt on sql injectionPpt on sql injection
Ppt on sql injectionashish20012
 
Sql Injection - Vulnerability and Security
Sql Injection - Vulnerability and SecuritySql Injection - Vulnerability and Security
Sql Injection - Vulnerability and SecuritySandip Chaudhari
 
Sql injections - with example
Sql injections - with exampleSql injections - with example
Sql injections - with examplePrateek Chauhan
 
Web Security: SQL Injection
Web Security: SQL InjectionWeb Security: SQL Injection
Web Security: SQL InjectionVortana Say
 
A Brief Introduction in SQL Injection
A Brief Introduction in SQL InjectionA Brief Introduction in SQL Injection
A Brief Introduction in SQL InjectionSina Manavi
 
Practical Approach towards SQLi ppt
Practical Approach towards SQLi pptPractical Approach towards SQLi ppt
Practical Approach towards SQLi pptAhamed Saleem
 
Sql injection attacks
Sql injection attacksSql injection attacks
Sql injection attacksKumar
 
Types of sql injection attacks
Types of sql injection attacksTypes of sql injection attacks
Types of sql injection attacksRespa Peter
 
SQL Injection Tutorial
SQL Injection TutorialSQL Injection Tutorial
SQL Injection TutorialMagno Logan
 
Unethical access to website’s databases hacking using sql injection
Unethical access to website’s databases hacking using sql injectionUnethical access to website’s databases hacking using sql injection
Unethical access to website’s databases hacking using sql injectionSatyajit Mukherjee
 
Time-Based Blind SQL Injection
Time-Based Blind SQL InjectionTime-Based Blind SQL Injection
Time-Based Blind SQL Injectionmatt_presson
 
SQL INJECTION
SQL INJECTIONSQL INJECTION
SQL INJECTIONMentorcs
 
Time-Based Blind SQL Injection using Heavy Queries
Time-Based Blind SQL Injection using Heavy QueriesTime-Based Blind SQL Injection using Heavy Queries
Time-Based Blind SQL Injection using Heavy QueriesChema Alonso
 
Advanced SQL Injection: Attacks
Advanced SQL Injection: Attacks Advanced SQL Injection: Attacks
Advanced SQL Injection: Attacks Nuno Loureiro
 

La actualidad más candente (20)

Ppt on sql injection
Ppt on sql injectionPpt on sql injection
Ppt on sql injection
 
Sql Injection - Vulnerability and Security
Sql Injection - Vulnerability and SecuritySql Injection - Vulnerability and Security
Sql Injection - Vulnerability and Security
 
Sql injection
Sql injectionSql injection
Sql injection
 
SQL Injections (Part 1)
SQL Injections (Part 1)SQL Injections (Part 1)
SQL Injections (Part 1)
 
Sql injections - with example
Sql injections - with exampleSql injections - with example
Sql injections - with example
 
Web Security: SQL Injection
Web Security: SQL InjectionWeb Security: SQL Injection
Web Security: SQL Injection
 
A Brief Introduction in SQL Injection
A Brief Introduction in SQL InjectionA Brief Introduction in SQL Injection
A Brief Introduction in SQL Injection
 
Practical Approach towards SQLi ppt
Practical Approach towards SQLi pptPractical Approach towards SQLi ppt
Practical Approach towards SQLi ppt
 
Sql injection attacks
Sql injection attacksSql injection attacks
Sql injection attacks
 
SQL Injection
SQL InjectionSQL Injection
SQL Injection
 
Types of sql injection attacks
Types of sql injection attacksTypes of sql injection attacks
Types of sql injection attacks
 
Sql injection & command injection
Sql injection & command injectionSql injection & command injection
Sql injection & command injection
 
How to identify and prevent SQL injection
How to identify and prevent SQL injection  How to identify and prevent SQL injection
How to identify and prevent SQL injection
 
SQL Injection Tutorial
SQL Injection TutorialSQL Injection Tutorial
SQL Injection Tutorial
 
Sql injection
Sql injectionSql injection
Sql injection
 
Unethical access to website’s databases hacking using sql injection
Unethical access to website’s databases hacking using sql injectionUnethical access to website’s databases hacking using sql injection
Unethical access to website’s databases hacking using sql injection
 
Time-Based Blind SQL Injection
Time-Based Blind SQL InjectionTime-Based Blind SQL Injection
Time-Based Blind SQL Injection
 
SQL INJECTION
SQL INJECTIONSQL INJECTION
SQL INJECTION
 
Time-Based Blind SQL Injection using Heavy Queries
Time-Based Blind SQL Injection using Heavy QueriesTime-Based Blind SQL Injection using Heavy Queries
Time-Based Blind SQL Injection using Heavy Queries
 
Advanced SQL Injection: Attacks
Advanced SQL Injection: Attacks Advanced SQL Injection: Attacks
Advanced SQL Injection: Attacks
 

Destacado

Sql Injection Attacks Siddhesh
Sql Injection Attacks SiddheshSql Injection Attacks Siddhesh
Sql Injection Attacks SiddheshSiddhesh Bhobe
 
D:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql InjectionD:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql Injectionavishkarm
 
Web application attacks using Sql injection and countermasures
Web application attacks using Sql injection and countermasuresWeb application attacks using Sql injection and countermasures
Web application attacks using Sql injection and countermasuresCade Zvavanjanja
 
SQL Injection in PHP
SQL Injection in PHPSQL Injection in PHP
SQL Injection in PHPDave Ross
 
Sql Injection Tutorial!
Sql Injection Tutorial!Sql Injection Tutorial!
Sql Injection Tutorial!ralphmigcute
 
Neutralizing SQL Injection in PostgreSQL
Neutralizing SQL Injection in PostgreSQLNeutralizing SQL Injection in PostgreSQL
Neutralizing SQL Injection in PostgreSQLJuliano Atanazio
 
03. sql and other injection module v17
03. sql and other injection module v1703. sql and other injection module v17
03. sql and other injection module v17Eoin Keary
 
Threat modeling librarian freedom conference
Threat modeling   librarian freedom conferenceThreat modeling   librarian freedom conference
Threat modeling librarian freedom conferenceevacide
 
SQL Injection - The Unknown Story
SQL Injection - The Unknown StorySQL Injection - The Unknown Story
SQL Injection - The Unknown StoryImperva
 
Introduction to SQL Injection
Introduction to SQL InjectionIntroduction to SQL Injection
Introduction to SQL Injectionjpubal
 
Defcon 17-joseph mccray-adv-sql_injection
Defcon 17-joseph mccray-adv-sql_injectionDefcon 17-joseph mccray-adv-sql_injection
Defcon 17-joseph mccray-adv-sql_injectionAhmed AbdelSatar
 
Blind SQL Injection - Optimization Techniques
Blind SQL Injection - Optimization TechniquesBlind SQL Injection - Optimization Techniques
Blind SQL Injection - Optimization Techniquesguest54de52
 
Sql Injection and XSS
Sql Injection and XSSSql Injection and XSS
Sql Injection and XSSMike Crabb
 
Vulnerable Active Record: A tale of SQL Injection in PHP Framework
Vulnerable Active Record: A tale of SQL Injection in PHP FrameworkVulnerable Active Record: A tale of SQL Injection in PHP Framework
Vulnerable Active Record: A tale of SQL Injection in PHP FrameworkPichaya Morimoto
 
SQL Injection Attacks cs586
SQL Injection Attacks cs586SQL Injection Attacks cs586
SQL Injection Attacks cs586Stacy Watts
 

Destacado (20)

Sql Injection Attacks Siddhesh
Sql Injection Attacks SiddheshSql Injection Attacks Siddhesh
Sql Injection Attacks Siddhesh
 
D:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql InjectionD:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql Injection
 
Web application attacks using Sql injection and countermasures
Web application attacks using Sql injection and countermasuresWeb application attacks using Sql injection and countermasures
Web application attacks using Sql injection and countermasures
 
SQL Injection
SQL Injection SQL Injection
SQL Injection
 
SQL Injection in PHP
SQL Injection in PHPSQL Injection in PHP
SQL Injection in PHP
 
Sql Injection Tutorial!
Sql Injection Tutorial!Sql Injection Tutorial!
Sql Injection Tutorial!
 
Neutralizing SQL Injection in PostgreSQL
Neutralizing SQL Injection in PostgreSQLNeutralizing SQL Injection in PostgreSQL
Neutralizing SQL Injection in PostgreSQL
 
03. sql and other injection module v17
03. sql and other injection module v1703. sql and other injection module v17
03. sql and other injection module v17
 
Threat modeling librarian freedom conference
Threat modeling   librarian freedom conferenceThreat modeling   librarian freedom conference
Threat modeling librarian freedom conference
 
SQL injection
SQL injectionSQL injection
SQL injection
 
SQL Injection - The Unknown Story
SQL Injection - The Unknown StorySQL Injection - The Unknown Story
SQL Injection - The Unknown Story
 
Introduction to SQL Injection
Introduction to SQL InjectionIntroduction to SQL Injection
Introduction to SQL Injection
 
Sql injection
Sql injectionSql injection
Sql injection
 
Defcon 17-joseph mccray-adv-sql_injection
Defcon 17-joseph mccray-adv-sql_injectionDefcon 17-joseph mccray-adv-sql_injection
Defcon 17-joseph mccray-adv-sql_injection
 
Blind SQL Injection - Optimization Techniques
Blind SQL Injection - Optimization TechniquesBlind SQL Injection - Optimization Techniques
Blind SQL Injection - Optimization Techniques
 
Sql Injection and XSS
Sql Injection and XSSSql Injection and XSS
Sql Injection and XSS
 
Vulnerable Active Record: A tale of SQL Injection in PHP Framework
Vulnerable Active Record: A tale of SQL Injection in PHP FrameworkVulnerable Active Record: A tale of SQL Injection in PHP Framework
Vulnerable Active Record: A tale of SQL Injection in PHP Framework
 
SQL Injection Attacks cs586
SQL Injection Attacks cs586SQL Injection Attacks cs586
SQL Injection Attacks cs586
 
Sql injection attack
Sql injection attackSql injection attack
Sql injection attack
 
NASL
NASLNASL
NASL
 

Similar a Sql Injection and Entity Frameworks

Overview of CSharp MVC3 and EF4
Overview of CSharp MVC3 and EF4Overview of CSharp MVC3 and EF4
Overview of CSharp MVC3 and EF4Rich Helton
 
What's New for Data?
What's New for Data?What's New for Data?
What's New for Data?ukdpe
 
Windows Azure and a little SQL Data Services
Windows Azure and a little SQL Data ServicesWindows Azure and a little SQL Data Services
Windows Azure and a little SQL Data Servicesukdpe
 
C# and ASP.NET Code and Data-Access Security
C# and ASP.NET Code and Data-Access SecurityC# and ASP.NET Code and Data-Access Security
C# and ASP.NET Code and Data-Access SecurityDarren Sim
 
SQL Server Security - Attack
SQL Server Security - Attack SQL Server Security - Attack
SQL Server Security - Attack webhostingguy
 
Using Rails to Create an Enterprise App: A Real-Life Case Study
Using Rails to Create an Enterprise App: A Real-Life Case StudyUsing Rails to Create an Enterprise App: A Real-Life Case Study
Using Rails to Create an Enterprise App: A Real-Life Case StudyDavid Keener
 
Entity Framework Code First Migrations
Entity Framework Code First MigrationsEntity Framework Code First Migrations
Entity Framework Code First MigrationsDiluka99999
 
Hackers Paradise SQL Injection Attacks
Hackers Paradise SQL Injection AttacksHackers Paradise SQL Injection Attacks
Hackers Paradise SQL Injection Attacksamiable_indian
 
C#Web Sec Oct27 2010 Final
C#Web Sec Oct27 2010 FinalC#Web Sec Oct27 2010 Final
C#Web Sec Oct27 2010 FinalRich Helton
 
Intro to .NET for Government Developers
Intro to .NET for Government DevelopersIntro to .NET for Government Developers
Intro to .NET for Government DevelopersFrank La Vigne
 
Chapter 14 sql injection
Chapter 14 sql injectionChapter 14 sql injection
Chapter 14 sql injectionnewbie2019
 
android sqlite
android sqliteandroid sqlite
android sqliteDeepa Rani
 
Microsoft Entity Framework
Microsoft Entity FrameworkMicrosoft Entity Framework
Microsoft Entity FrameworkMahmoud Tolba
 
TechEd Africa 2011 - OFC308: SharePoint Security in an Insecure World: Unders...
TechEd Africa 2011 - OFC308: SharePoint Security in an Insecure World: Unders...TechEd Africa 2011 - OFC308: SharePoint Security in an Insecure World: Unders...
TechEd Africa 2011 - OFC308: SharePoint Security in an Insecure World: Unders...Michael Noel
 

Similar a Sql Injection and Entity Frameworks (20)

Overview of CSharp MVC3 and EF4
Overview of CSharp MVC3 and EF4Overview of CSharp MVC3 and EF4
Overview of CSharp MVC3 and EF4
 
Web Security
Web SecurityWeb Security
Web Security
 
What's New for Data?
What's New for Data?What's New for Data?
What's New for Data?
 
Windows Azure and a little SQL Data Services
Windows Azure and a little SQL Data ServicesWindows Azure and a little SQL Data Services
Windows Azure and a little SQL Data Services
 
C# and ASP.NET Code and Data-Access Security
C# and ASP.NET Code and Data-Access SecurityC# and ASP.NET Code and Data-Access Security
C# and ASP.NET Code and Data-Access Security
 
SQL Server Security - Attack
SQL Server Security - Attack SQL Server Security - Attack
SQL Server Security - Attack
 
Using Rails to Create an Enterprise App: A Real-Life Case Study
Using Rails to Create an Enterprise App: A Real-Life Case StudyUsing Rails to Create an Enterprise App: A Real-Life Case Study
Using Rails to Create an Enterprise App: A Real-Life Case Study
 
Entity Framework Code First Migrations
Entity Framework Code First MigrationsEntity Framework Code First Migrations
Entity Framework Code First Migrations
 
Hackers Paradise SQL Injection Attacks
Hackers Paradise SQL Injection AttacksHackers Paradise SQL Injection Attacks
Hackers Paradise SQL Injection Attacks
 
Practical OData
Practical ODataPractical OData
Practical OData
 
C#Web Sec Oct27 2010 Final
C#Web Sec Oct27 2010 FinalC#Web Sec Oct27 2010 Final
C#Web Sec Oct27 2010 Final
 
ADO.NET by ASP.NET Development Company in india
ADO.NET by ASP.NET  Development Company in indiaADO.NET by ASP.NET  Development Company in india
ADO.NET by ASP.NET Development Company in india
 
Ef code first
Ef code firstEf code first
Ef code first
 
PPT
PPTPPT
PPT
 
Intro to .NET for Government Developers
Intro to .NET for Government DevelopersIntro to .NET for Government Developers
Intro to .NET for Government Developers
 
Chapter 14 sql injection
Chapter 14 sql injectionChapter 14 sql injection
Chapter 14 sql injection
 
android sqlite
android sqliteandroid sqlite
android sqlite
 
unit 3.docx
unit 3.docxunit 3.docx
unit 3.docx
 
Microsoft Entity Framework
Microsoft Entity FrameworkMicrosoft Entity Framework
Microsoft Entity Framework
 
TechEd Africa 2011 - OFC308: SharePoint Security in an Insecure World: Unders...
TechEd Africa 2011 - OFC308: SharePoint Security in an Insecure World: Unders...TechEd Africa 2011 - OFC308: SharePoint Security in an Insecure World: Unders...
TechEd Africa 2011 - OFC308: SharePoint Security in an Insecure World: Unders...
 

Más de Rich Helton

Java for Mainframers
Java for MainframersJava for Mainframers
Java for MainframersRich Helton
 
I pad uicatalog_lesson02
I pad uicatalog_lesson02I pad uicatalog_lesson02
I pad uicatalog_lesson02Rich Helton
 
Mongo db rev001.
Mongo db rev001.Mongo db rev001.
Mongo db rev001.Rich Helton
 
NServicebus WCF Integration 101
NServicebus WCF Integration 101NServicebus WCF Integration 101
NServicebus WCF Integration 101Rich Helton
 
AspMVC4 start101
AspMVC4 start101AspMVC4 start101
AspMVC4 start101Rich Helton
 
Entity frameworks101
Entity frameworks101Entity frameworks101
Entity frameworks101Rich Helton
 
Tumbleweed intro
Tumbleweed introTumbleweed intro
Tumbleweed introRich Helton
 
Salesforce Intro
Salesforce IntroSalesforce Intro
Salesforce IntroRich Helton
 
LEARNING  iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
LEARNING	 iPAD STORYBOARDS IN OBJ-­‐C LESSON 1LEARNING	 iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
LEARNING  iPAD STORYBOARDS IN OBJ-­‐C LESSON 1Rich Helton
 
Learning C# iPad Programming
Learning C# iPad ProgrammingLearning C# iPad Programming
Learning C# iPad ProgrammingRich Helton
 
First Steps in Android
First Steps in AndroidFirst Steps in Android
First Steps in AndroidRich Helton
 
Python For Droid
Python For DroidPython For Droid
Python For DroidRich Helton
 
Spring Roo Rev005
Spring Roo Rev005Spring Roo Rev005
Spring Roo Rev005Rich Helton
 
C# Security Testing and Debugging
C# Security Testing and DebuggingC# Security Testing and Debugging
C# Security Testing and DebuggingRich Helton
 
Web Application Firewall intro
Web Application Firewall introWeb Application Firewall intro
Web Application Firewall introRich Helton
 

Más de Rich Helton (20)

Java for Mainframers
Java for MainframersJava for Mainframers
Java for Mainframers
 
I pad uicatalog_lesson02
I pad uicatalog_lesson02I pad uicatalog_lesson02
I pad uicatalog_lesson02
 
Mongo db rev001.
Mongo db rev001.Mongo db rev001.
Mongo db rev001.
 
NServicebus WCF Integration 101
NServicebus WCF Integration 101NServicebus WCF Integration 101
NServicebus WCF Integration 101
 
AspMVC4 start101
AspMVC4 start101AspMVC4 start101
AspMVC4 start101
 
Entity frameworks101
Entity frameworks101Entity frameworks101
Entity frameworks101
 
Tumbleweed intro
Tumbleweed introTumbleweed intro
Tumbleweed intro
 
Azure rev002
Azure rev002Azure rev002
Azure rev002
 
Salesforce Intro
Salesforce IntroSalesforce Intro
Salesforce Intro
 
LEARNING  iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
LEARNING	 iPAD STORYBOARDS IN OBJ-­‐C LESSON 1LEARNING	 iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
LEARNING  iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
 
Learning C# iPad Programming
Learning C# iPad ProgrammingLearning C# iPad Programming
Learning C# iPad Programming
 
First Steps in Android
First Steps in AndroidFirst Steps in Android
First Steps in Android
 
NServiceBus
NServiceBusNServiceBus
NServiceBus
 
Python For Droid
Python For DroidPython For Droid
Python For Droid
 
Spring Roo Rev005
Spring Roo Rev005Spring Roo Rev005
Spring Roo Rev005
 
Python Final
Python FinalPython Final
Python Final
 
Adobe Flex4
Adobe Flex4 Adobe Flex4
Adobe Flex4
 
Jira Rev002
Jira Rev002Jira Rev002
Jira Rev002
 
C# Security Testing and Debugging
C# Security Testing and DebuggingC# Security Testing and Debugging
C# Security Testing and Debugging
 
Web Application Firewall intro
Web Application Firewall introWeb Application Firewall intro
Web Application Firewall intro
 

Sql Injection and Entity Frameworks

  • 1. SQL Injection (Most common Injection Flaw) From Rich Helton’s October 2010 C# Web Security
  • 2. 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.
  • 3. 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
  • 5. ASP.NET Hacme Bank Authentication without username/password
  • 6. 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.
  • 7. 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.
  • 8. HackmeBooks SQL Injection (shows org.hsqldb.jdbc connection)
  • 9. 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:
  • 10. 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.
  • 11. 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.
  • 12. 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’ :
  • 13. 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.
  • 14. Stored Procedures Hacks (Who’s hacking them? From SANs )
  • 15. 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.
  • 18. A Sample Entity Framework (Model1.edmx with the VS Model Browser) Changes made to the model can propagate to the Database.
  • 19. Another Example (Has all the details of the data)
  • 20. A Database can be generated
  • 21. 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…”
  • 22. 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
  • 23. T4 Editor  The .tt is just the template to generate the underlying .cs (C#) file:
  • 24. 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
  • 25. PEM  After installing PEM, validation not only shows up in properties, but generation code can be generated through T4.
  • 26. PEM  PemValidation.cs with the Validate method for Employee:
  • 27. 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.
  • 29. 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
  • 30. 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.