SlideShare a Scribd company logo
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.

More Related Content

PPTX
SQL injection prevention techniques
PPTX
Cross-Site Scripting (XSS)
PDF
How to identify and prevent SQL injection
PPTX
Sql injection - security testing
PPTX
Sql injections - with example
PDF
Secure Coding principles by example: Build Security In from the start - Carlo...
PPTX
seminar report on Sql injection
PPTX
Cross Site Scripting
SQL injection prevention techniques
Cross-Site Scripting (XSS)
How to identify and prevent SQL injection
Sql injection - security testing
Sql injections - with example
Secure Coding principles by example: Build Security In from the start - Carlo...
seminar report on Sql injection
Cross Site Scripting

What's hot (20)

PDF
Sql injection bypassing hand book blackrose
PPTX
PDF
Cross site scripting attacks and defenses
PPTX
PPTX
SQL Injections - A Powerpoint Presentation
PPTX
Command injection
PPTX
Rest API Security
PPTX
Deep understanding on Cross-Site Scripting and SQL Injection
PPTX
Secure Your REST API (The Right Way)
PDF
Ch 1: Web Application (In)security & Ch 2: Core Defense Mechanisms
PPT
Cache poisoning
PPT
Sql injection
PPTX
SSRF For Bug Bounties
PDF
SSRF workshop
PPTX
Directory Traversal & File Inclusion Attacks
PPT
A Brief Introduction in SQL Injection
PPT
Sql injection
PPTX
SQL Injections (Part 1)
PDF
Not so blind SQL Injection
Sql injection bypassing hand book blackrose
Cross site scripting attacks and defenses
SQL Injections - A Powerpoint Presentation
Command injection
Rest API Security
Deep understanding on Cross-Site Scripting and SQL Injection
Secure Your REST API (The Right Way)
Ch 1: Web Application (In)security & Ch 2: Core Defense Mechanisms
Cache poisoning
Sql injection
SSRF For Bug Bounties
SSRF workshop
Directory Traversal & File Inclusion Attacks
A Brief Introduction in SQL Injection
Sql injection
SQL Injections (Part 1)
Not so blind SQL Injection
Ad

Viewers also liked (20)

PPTX
Unethical access to website’s databases hacking using sql injection
PPT
Sql Injection Attacks Siddhesh
PPT
D:\Technical\Ppt\Sql Injection
PPT
Web application attacks using Sql injection and countermasures
PPTX
SQL INJECTION
PPT
SQL Injection
PPT
SQL Injection in PHP
PPT
Sql Injection Tutorial!
PDF
Neutralizing SQL Injection in PostgreSQL
PPTX
03. sql and other injection module v17
PPTX
Threat modeling librarian freedom conference
PPTX
SQL injection
PDF
SQL Injection - The Unknown Story
PPT
Introduction to SQL Injection
PPTX
Sql injection
PDF
Defcon 17-joseph mccray-adv-sql_injection
PPT
Blind SQL Injection - Optimization Techniques
PDF
Sql Injection and XSS
PDF
Vulnerable Active Record: A tale of SQL Injection in PHP Framework
PPTX
SQL Injection Attacks cs586
Unethical access to website’s databases hacking using sql injection
Sql Injection Attacks Siddhesh
D:\Technical\Ppt\Sql Injection
Web application attacks using Sql injection and countermasures
SQL INJECTION
SQL Injection
SQL Injection in PHP
Sql Injection Tutorial!
Neutralizing SQL Injection in PostgreSQL
03. sql and other injection module v17
Threat modeling librarian freedom conference
SQL injection
SQL Injection - The Unknown Story
Introduction to SQL Injection
Sql injection
Defcon 17-joseph mccray-adv-sql_injection
Blind SQL Injection - Optimization Techniques
Sql Injection and XSS
Vulnerable Active Record: A tale of SQL Injection in PHP Framework
SQL Injection Attacks cs586
Ad

Similar to Sql Injection and Entity Frameworks (20)

PPTX
C#Web Sec Oct27 2010 Final
PPTX
ASP.NET Web Security
PPT
SQL injection and buffer overflows are hacking techniques used to exploit wea...
PPSX
Web application security
PPTX
Sql injection
PPTX
SQLi for Security Champions
PPT
Synapseindia dot net development chapter 8 asp dot net
PPTX
Owasp Top 10 2017
PPTX
Sql injection
PPTX
SQL Injection and Clickjacking Attack in Web security
PPTX
Sql injection
PDF
Web Security Threats and Solutions
PPTX
Hackers versus Developers and Secure Web Programming
PPTX
Hack through Injections
PPTX
PPTX
Secure Dot Net Programming
PPTX
Database security
PPTX
Understanding and preventing sql injection attacks
PDF
20111204 web security_livshits_lecture01
PDF
Think Like a Hacker - Database Attack Vectors
C#Web Sec Oct27 2010 Final
ASP.NET Web Security
SQL injection and buffer overflows are hacking techniques used to exploit wea...
Web application security
Sql injection
SQLi for Security Champions
Synapseindia dot net development chapter 8 asp dot net
Owasp Top 10 2017
Sql injection
SQL Injection and Clickjacking Attack in Web security
Sql injection
Web Security Threats and Solutions
Hackers versus Developers and Secure Web Programming
Hack through Injections
Secure Dot Net Programming
Database security
Understanding and preventing sql injection attacks
20111204 web security_livshits_lecture01
Think Like a Hacker - Database Attack Vectors

More from Rich Helton (20)

PPT
Java for Mainframers
PDF
I pad uicatalog_lesson02
ODP
Mongo db rev001.
PPT
NServicebus WCF Integration 101
PPT
AspMVC4 start101
PPT
Entity frameworks101
PPT
Tumbleweed intro
PPTX
Azure rev002
PPTX
Salesforce Intro
PPTX
LEARNING  iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
PPTX
Learning C# iPad Programming
PPT
First Steps in Android
PPTX
NServiceBus
PDF
Python For Droid
PDF
Spring Roo Rev005
PDF
Python Final
PPT
Overview of CSharp MVC3 and EF4
PPT
Adobe Flex4
PPT
Jira Rev002
PPTX
C# Security Testing and Debugging
Java for Mainframers
I pad uicatalog_lesson02
Mongo db rev001.
NServicebus WCF Integration 101
AspMVC4 start101
Entity frameworks101
Tumbleweed intro
Azure rev002
Salesforce Intro
LEARNING  iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
Learning C# iPad Programming
First Steps in Android
NServiceBus
Python For Droid
Spring Roo Rev005
Python Final
Overview of CSharp MVC3 and EF4
Adobe Flex4
Jira Rev002
C# Security Testing and Debugging

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.