Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

A1

...

Injection

A1.1

...

SQL

...

Injection

...

Injection

...

flaws,

...

such

...

as

...

SQL,

...

OS,

...

and

...

LDAP

...

injection

...

occur

...

when

...

untrusted

...

data

...

is

...

sent

...

to

...

an

...

interpreter

...

as

...

part

...

of

...

a

...

command

...

or

...

query.

...

The

...

attacker's

...

hostile

...

data

...

can

...

trick

...

the

...

interpreter

...

into

...

executing

...

unintended

...

commands

...

or

...

accessing

...

data

...

without

...

proper

...

authorization.

...

Defenses:

...

1.1

...

https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet#Primary_Defenses

...

Defense

...

Option

...

1:

...

Prepared

...

Statements

...

(with

...

Parameterized

...

Queries)

...

1.1.1 Safe Java Prepared Statement Example

The following code example uses a PreparedStatement, Java's implementation of a parameterized query, to execute the same database query.

 String custname = request.getParameter("customerName");

...

//

...

This

...

should

...

REALLY

...

be

...

validated

...

too

 //

...

perform

...

input

...

validation

...

to

...

detect

...

attacks

 String query = "SELECT

...

account_balance

...

FROM

...

user_data

...

WHERE

...

user_name

...

=

...

 ?

...

";

...


 PreparedStatement pstmt = connection.prepareStatement(

...

query

...

);

...

 pstmt.setString(

...

1,

...

custname);

...

 ResultSet results = pstmt.executeQuery(

...

);

...

1.1.2.

...

Hibernate

...

Query

...

Language

...

(HQL)

...

Prepared

...

Statement

...

(Named

...

Parameters)

...

Examples

 First is an unsafe HQL Statement

Code Block

 Query unsafeHQLQuery = session.createQuery("from Inventory where productID='"+userSuppliedParameter+"'");\\

 Here is a safe version of the same query using named parameters:\\

 

 Here is a safe version of the same query using named parameters:


Code Block

Query safeHQLQuery = session.createQuery("from Inventory where productID=:productid");

 safeHQLQuery safeHQLQuery.setParameter("productid", userSuppliedParameter);

h4. [

1.2 Defense

...

Option

...

2:

...

Stored Procedures

Code Block

String custname = request.getParameter("customerName"); // This should REALLY be validated

 try {

                    *CallableStatement cs = connection.prepareCall try {

                    *CallableStatement cs = connection.prepareCall("{call sp_getAccountBalance\(?)}");*

                    *cs.setString(1, custname);*

                    ResultSet results = cs.executeQuery();                             

                    // ... result set handling

 } catch (SQLException se) {                                                 

                    // ... logging and error handling

 }

h4. 1.3 [Defense Option 3: Escaping All User Supplied Input|https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet#Defense_Option_3:_Escaping_All_User_Supplied_Input]

This second technique is to escape user input before putting it in a query. However, this methodology is frail compared to using parameterized queries and we cannot guarantee it will prevent all SQL Injection in all situations. This technique should only be used, with caution, to retrofit legacy code in a cost effective way. Applications built from scratch, or applications requiring low risk tolerance should be built or re-written using parameterized queries.

This technique works like this. Each DBMS supports one or more character escaping schemes specific to certain kinds of queries. If you then escape all user supplied input using the proper escaping scheme for the database you are using, the DBMS will not confuse that input with SQL code written by the developer, thus avoiding any possible SQL injection vulnerabilities.

h5. 1.3.1 Use ESAPI database encoders for:

)}");*

                    cs.setString(1, custname);

                    ResultSet results = cs.executeQuery();                             

                    // ... result set handling

 } catch (SQLException se)

1.3 Defense Option 3: Escaping All User Supplied Input

This second technique is to escape user input before putting it in a query. However, this methodology is frail compared to using parameterized queries and we cannot guarantee it will prevent all SQL Injection in all situations. This technique should only be used, with caution, to retrofit legacy code in a cost effective way. Applications built from scratch, or applications requiring low risk tolerance should be built or re-written using parameterized queries.

This technique works like this. Each DBMS supports one or more character escaping schemes specific to certain kinds of queries. If you then escape all user supplied input using the proper escaping scheme for the database you are using, the DBMS will not confuse that input with SQL code written by the developer, thus avoiding any possible SQL injection vulnerabilities.

1.3.1 Use ESAPI database encoders for:

https://owasp-esapi-java.googlecode.com/svn/trunk_doc/latest/org/owasp/esapi/codecs/Codec.html

...

1.3.2

...

Escaping

...

Dynamic

...

Queries:

...

Turn

...

off

...

character

...

replacement
1.3.2.1

...

Escaping

...

Wildcard

...

characters

...

in

...

Like

...

Clauses

...

The

...

LIKE

...

keyword

...

allows

...

for

...

text

...

scanning

...

searches.

...

In

...

Oracle,

...

the

...

underscore

...

'_'

...

character

...

matches

...

only

...

one

...

character,

...

while

...

the

...

ampersand

...

'%'

...

is

...

used

...

to

...

match

...

zero

...

or

...

more

...

occurrences

...

of

...

any

...

characters.

...

These

...

characters

...

must

...

be

...

escaped

...

in

...

LIKE

...

clause

...

criteria.

...

For

...

example:

Code Block

SELECT name FROM emp
WHERE id LIKE '%/_%' ESCAPE '/';
SELECT name FROM emp
WHERE id LIKE '%\%%%%%' ESCAPE '\';\\

h2. * *


h3. SQL Injection Best Practices


h4. Least Privilege

To minimize the potential damage of a successful SQL injection attack, you should minimize the privileges assigned to every database account in your environment. Do not assign DBA or admin type access rights to your application accounts. We understand that this is easy, and everything just 'works' when you do it this way, but it is very dangerous. Start from the ground up to determine what access rights your application accounts require, rather than trying to figure out what access rights you need to take away. Make sure that accounts that only need read access are only granted read access to the tables they need access to. If an account only needs access to portions of a table, consider creating a view that limits access to that portion of the data and assigning the account access to the view instead, rather than the underlying table. Rarely, if ever, grant create or delete access to database accounts.

If you adopt a policy where you use stored procedures everywhere, and don't allow application accounts to directly execute their own queries, then restrict those accounts to only be able to execute the stored procedures they need. Don't grant them any rights directly to the tables in the database.

SQL injection is not the only threat to your database data. Attackers can simply change the parameter values from one of the legal values they are presented with, to a value that is unauthorized for them, but the application itself might be authorized to access. As such, minimizing the privileges granted to your application will reduce the likelihood of such unauthorized access attempts, even when an attacker is not trying to use SQL injection as part of their exploit.

While you are at it, you should minimize the privileges of the operating system account that the DBMS runs under. Don't run your DBMS as root or system\! Most DBMSs run out of the box with a very powerful system account. For example, MySQL runs as system on Windows by default\! Change the DBMS's OS account to something more appropriate, with restricted privileges.

h4. Multiple DB Users

The designer of web applications should not only avoid using the same owner/admin account in the web applications to connect to the database. Different DB users could be used for different web applications. In general, each separate web application that requires access to the database could have a designated database user account that the web-app will use to connect to the DB. That way, the designer of the application can have good granularity in the access control, thus reducing the privileges as much as possible. Each DB user will then have select access to what it needs only, and write-access as needed.

As an example, a login page requires read access to the username and password fields of a table, but no write access of any form (no insert, update, or delete). However, the sign-up page certainly requires insert privilege to that table; this restriction can only be enforced if these web apps use different DB users to connect to the database.

h4. Views

SQL views can further increase the granularity of access by limiting the read access to specific fields of a table or joins of tables. It could potentially have additional benefits: for example, suppose that the system is required (perhaps due to some specific legal requirements) to store the passwords of the users, instead of salted-hashed passwords. The designer could use views to compensate for this limitation; revoke all access to the table (from all DB users except the owner/admin) and create a view that outputs the hash of the password field and not the field itself. Any SQL injection attack that succeeds in stealing DB information will be restricted to stealing the hash of the passwords (could even be a keyed hash), since no DB user for any of the web applications has access to the table itself.

Java applications using XML libraries are particularly vulnerable to XXE because the default settings for most Java XML parsers is to have XXE enabled. To use these parsers safely, you have to explicitly disable XXE in the parser you use. The following describes how to disable XXE in the most commonly used XML parsers for Java.

h3.  A 1.2 XML External Entity (XXE)

An _XML External Entity_ attack is a type of attack against an application that parses XML input. This attack occurs when *XML input containing a reference to an external entity is processed by a weakly configured XML parser*. This attack may lead to the disclosure of confidential data, denial of service, server side request forgery, port scanning from the perspective of the machine where the parser is located, and other system impacts. The following guide provides concise information to prevent this vulnerability. For more information on XXE, please visit [XML External Entity (XXE) Processing|https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Processing].

h4. *A 1.2. Best Practices*

The safest way to prevent XXE is always to disable DTDs (External Entities) completely. Depending on the parser, the method should be similar to the 

* *SQL Injection Best Practices

Least Privilege

To minimize the potential damage of a successful SQL injection attack, you should minimize the privileges assigned to every database account in your environment. Do not assign DBA or admin type access rights to your application accounts. We understand that this is easy, and everything just 'works' when you do it this way, but it is very dangerous. Start from the ground up to determine what access rights your application accounts require, rather than trying to figure out what access rights you need to take away. Make sure that accounts that only need read access are only granted read access to the tables they need access to. If an account only needs access to portions of a table, consider creating a view that limits access to that portion of the data and assigning the account access to the view instead, rather than the underlying table. Rarely, if ever, grant create or delete access to database accounts.

If you adopt a policy where you use stored procedures everywhere, and don't allow application accounts to directly execute their own queries, then restrict those accounts to only be able to execute the stored procedures they need. Don't grant them any rights directly to the tables in the database.

SQL injection is not the only threat to your database data. Attackers can simply change the parameter values from one of the legal values they are presented with, to a value that is unauthorized for them, but the application itself might be authorized to access. As such, minimizing the privileges granted to your application will reduce the likelihood of such unauthorized access attempts, even when an attacker is not trying to use SQL injection as part of their exploit.

While you are at it, you should minimize the privileges of the operating system account that the DBMS runs under. Don't run your DBMS as root or system! Most DBMSs run out of the box with a very powerful system account. For example, MySQL runs as system on Windows by default! Change the DBMS's OS account to something more appropriate, with restricted privileges.

Multiple DB Users

The designer of web applications should not only avoid using the same owner/admin account in the web applications to connect to the database. Different DB users could be used for different web applications. In general, each separate web application that requires access to the database could have a designated database user account that the web-app will use to connect to the DB. That way, the designer of the application can have good granularity in the access control, thus reducing the privileges as much as possible. Each DB user will then have select access to what it needs only, and write-access as needed.

As an example, a login page requires read access to the username and password fields of a table, but no write access of any form (no insert, update, or delete). However, the sign-up page certainly requires insert privilege to that table; this restriction can only be enforced if these web apps use different DB users to connect to the database.

Views

SQL views can further increase the granularity of access by limiting the read access to specific fields of a table or joins of tables. It could potentially have additional benefits: for example, suppose that the system is required (perhaps due to some specific legal requirements) to store the passwords of the users, instead of salted-hashed passwords. The designer could use views to compensate for this limitation; revoke all access to the table (from all DB users except the owner/admin) and create a view that outputs the hash of the password field and not the field itself. Any SQL injection attack that succeeds in stealing DB information will be restricted to stealing the hash of the passwords (could even be a keyed hash), since no DB user for any of the web applications has access to the table itself.

Java applications using XML libraries are particularly vulnerable to XXE because the default settings for most Java XML parsers is to have XXE enabled. To use these parsers safely, you have to explicitly disable XXE in the parser you use. The following describes how to disable XXE in the most commonly used XML parsers for Java.

 A 1.2 XML External Entity (XXE)

An XML External Entity attack is a type of attack against an application that parses XML input. This attack occurs when XML input containing a reference to an external entity is processed by a weakly configured XML parser. This attack may lead to the disclosure of confidential data, denial of service, server side request forgery, port scanning from the perspective of the machine where the parser is located, and other system impacts. The following guide provides concise information to prevent this vulnerability. For more information on XXE, please visit XML External Entity (XXE) Processing.

A 1.2. Best Practices

The safest way to prevent XXE is always to disable DTDs (External Entities) completely. Depending on the parser, the method should be similar to the following:factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl",

...

true);Disabling

...

DTDs

...

also

...

makes

...

the

...

parser

...

secure

...

against

...

denial

...

of

...

services

...

(DOS)

...

attacks

...

such

...

as

...

Billion

...

Laughs.

...

If

...

it

...

is

...

not

...

possible

...

to

...

disable

...

DTDs

...

completely,

...

then

...

external

...

entities

...

and

...

external

...

doctypes

...

must

...

be

...

disabled

...

in

...

the

...

way

...

that's

...

specific

...

to

...

each

...

parser.

...

Detailed

...

XXE

...

Prevention

...

guidance

...

for

...

a

...

number

...

of

...

languages

...

and

...

commonly

...

used

...

XML

...

parsers

...

in

...

those

...

languages

...

is

...

provided

...

below.

...


JAXP DocumentBuilderFactory and SAXParserFactory

Both DocumentBuilderFactory and SAXParserFactory XML Parsers can be configured using the same techniques to protect them against XXE. Only the DocumentBuilderFactory example is presented here. The JAXP DocumentBuilderFactory setFeature method allows a developer to control which implementation-specific XML processor features are enabled or disabled. The features can either be set on the factory or the underlying XMLReader setFeature method. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.

For a syntax highlighted code snippet for DocumentBuilderFactory, click here.

For a syntax highlighted code snippet for SAXParserFactory, click here.import javax.xml.parsers.DocumentBuilderFactory;

Code Block
import javax.xml.parsers.ParserConfigurationException; // catching unsupported features
...

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
      // This is the PRIMARY defense. If DTDs (doctypes) are disallowed, almost all XML entity attacks are prevented
      // Xerces 2 only - http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl
      String FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
      dbf.setFeature(FEATURE, true);
 
      // If you can't completely disable DTDs, then at least do the following:
      // Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities
      // Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities
      // JDK7+ - http://xml.org/sax/features/external-general-entities   
      FEATURE = "http://xml.org/sax/features/external-general-entities";
      dbf.setFeature(FEATURE, false);
 
      // Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-parameter-entities
      // Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-parameter-entities
      // JDK7+ - http://xml.org/sax/features/external-parameter-entities   
      FEATURE = "http://xml.org/sax/features/external-parameter-entities";
      dbf.setFeature(FEATURE, false);
 
      // Disable external DTDs as well
      FEATURE = "http://apache.org/xml/features/nonvalidating/load-external-dtd"
      dbf.setFeature(FEATURE, false);
 
      // and these as well, per Timothy Morgan's 2014 paper: "XML Schema, DTD, and Entity Attacks" (see reference below)
      dbf.setXIncludeAware(false);
      dbf.setExpandEntityReferences(false);

      // And, per Timothy Morgan: "If for some reason support for inline DOCTYPEs are a requirement, then
      // ensure the entity settings are disabled (as shown above) and beware that SSRF attacks
      // (http://cwe.mitre.org/data/definitions/918.html) and denial
      // of service attacks (such as billion laughs or decompression bombs via "jar:") are a risk."
 
      // remaining parser logic
      ...


        catch      catch (ParserConfigurationException e) {
            // This should catch a failed setFeature feature
            logger.info("ParserConfigurationException was thrown. The feature '" +
                        FEATURE +
                        "' is probably not supported by your XML processor.");
            ...
        }
             catch (SAXException e) {
            // On Apache, this should be thrown when disallowing DOCTYPE
            logger.warning("A DOCTYPE was passed into the XML document");
            ...
        }
            catch (IOException e) {
            // XXE that points to a file that doesn't exist
            logger.error("IOException occurred, XXE may still possible: " + e.getMessage());
            ...
        }References:

h3. [https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet|());
            ...
        }

References:

https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet

...


A 1.3.

...

ORM

...

Mappers

...

In

...

most

...

cases,

...

using

...

ORM

...

mapper

...

such

...

as

...

Hibernate

...

will

...

protect

...

you

...

from

...

SQL

...

Injection

...

since

...

all

...

database

...

calls

...

are

...

implemented

...

with

...

prepared

...

statements.

...

In

...

Hibernate,

...

avoid

...

code

...

such

...

as:

Code Block

Query query = session.createQuery("SELECT * FROM TABLE WHERE SOMEVAL = ' + user_supplied_variable + "'");

Use

...

binding

...

syntax

...

instead:

Code Block

Query query = session.createQuery("SELECT * FROM TABLE WHERE SOMEVAL= :someval";
query.setString(":someval", user_supplied_variable);\\

h3. A 

A 1.4.

...

LDAP

...

Injection

...

Use

...

positive

...

validation

...

to

...

eliminate

...

all

...

but

...

valid

...

username

...

and

...

other

...

dynamic

...

inputs.

...

The

...

following

...

code

...

is

...

vulnerable

...

to

...

LDAP

...

injection:

Code Block

String principal = "cn=" + getParameter("username") + ", ou=Users, o=example";
String password = getParameter("password");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, principal);
env.put(Context.SECURITY_CREDENTIALS, password);
// Create the initial context
DirContext ctx = new InitialDirContext(env);Instead, implement code for LDAP as follows:// if the username contains LDAP specials, stop now
if ( containsLDAPspecials(getParameter("username"))) ) {
         ) {
        throw new javax.naming.AuthenticationException();
}
String principal = "cn=" + getParameter("username") + ", ou=Users, o=example";
String password = getParameter("password");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, principal);
env.put(Context.SECURITY_CREDENTIALS, password);
// Create the initial context
DirContext ctx = new InitialDirContext(env);\\

References:

...

SANS

...

Secure

...

Coding

...

in

...

Java/JEE

...

Developing

...

Defensive

...

Applications

...

https://www.owasp.org/index.php/Top_10_2013-A1-Injection

...

https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet

...

https://www.owasp.org/index.php/

...

Interpreter_Injection

...