Part 1 — Security in Web Application(Injection)

In this series, I will try to explain the most common attacks in a web application and how to mitigate those. Most web applications manipulate personal or business data which is very sensitive like passwords, emails, phone numbers, card information. Being exposed to the public, web applications naturally have a large attack surface and maybe features with a large number of various potentially vulnerable elements.

What are the most common vulnerabilities in a web application?

  1. Injection

Let's deep dive into all these vulnerabilities. We'll start with an injection at first.

1. Injection

fig: injection

An attacker may be able to manipulate your web application into altering the commands submitted to its systems by simply sending malformed requests with tainted payloads. The best known of these attacks is SQL injection, where a user can cause your app to change this:

into this

This allows the attacker to log in to your application as an administrator, without even knowing the password. Other uses of this attack would be to steal secrets, change data or even erase all traces of activity.

Other forms include LDAP Injection, XPath injection, Command Injection, SMTP injection — any time the application concatenates untrusted user input into a command that is passed to an interpreter. The abnormal data can trick the interpreter into executing unintended commands or accessing data without proper authorization.

The injection is a major problem in web security. It is listed as the number one web application security risk in the OWASP top 10, and for good reason. Injection attacks, particularly SQL injection, are very dangerous, but they are also widespread, especially in legacy applications.

How to determine if you are vulnerable?

The best way to determine if your applications are vulnerable to injection attacks is to search the source code for all calls to external resources (e.g., system, exec, fork, Runtime.exec, SQL queries, or whatever the syntax is for making requests to interpreters in your environment). Note that many languages have multiple ways to run external commands. Developers should review their code and search for all places where input from an HTTP request could make its way into any of these calls. You should carefully examine each of these calls to be sure that the protection steps outlined below are followed.

What are the different types of injection attacks?

i. Code Injection

Injects application code that can execute operating system commands as the user running the web application. Advanced attacks can make use of privilege escalation vulnerabilities to gain even higher privileges if necessary, which may lead to full system compromise.

ii. CRLF Injection

Injects an unexpected CRLF (Carriage Return and Line Feed) character sequence used to split an HTTP response header and write arbitrary contents to the response body, including Cross-site Scripting (XSS).

iii. Cross-site Scription(XSS)

Injects arbitrary JavaScript into a legitimate website or web application which is then executed inside a victim’s browser.

iv. Email(Mail command/ SMTP injection)

Injects IMAP/SMTP statements to a mail server that is not directly available via a web application.

v. Host Header Injection

Abuses the implicit trust of the HTTP Host Header to poison password-reset functionality and poison web caches.

vi. LDAP Injection

Injects LDAP (Lightweight Directory Access Protocol) statements to execute arbitrary LDAP commands including granting permissions and modifying the contents of an LDAP tree.

vii. OS Command Injection

Injects operating system commands as the user running the web application. Advanced variations of this attack can leverage privilege escalation vulnerabilities which may lead to full system compromise.

viii. SQL Injection

Injects SQL commands that can read or modify data from a database. Advanced variations of this attack can be used to write arbitrary files to the server and even execute OS commands which may lead to full system compromise.

How to handle SQL Injection?

SQL injections are introduced within a system when software developers create dynamic database queries that include user-supplied input. To avoid SQL injection flaws is simple. Developers need to either stop writing dynamic queries or prevent user-supplied input which contains malicious SQL from affecting the logic of the executed query. Listed below are the defense actions that can be taken to prevent SQL injection.

Option 1: Use of prepared statements (with parameterized queries)

The use of prepared statements with variable binding (aka parameterized queries) is how all developers should first be taught how to write database queries. They are simple to write and easier to understand than dynamic queries. Parameterized queries force the developer to first define all the SQL code, and then pass in each parameter to the query later. This coding style allows the database to distinguish between code and data, regardless of what user input is supplied.

Prepared statements ensure that an attacker is not able to change the intent of a query, even if SQL commands are inserted by an attacker. In the safe example below, if an attacker were to enter the userID of tom’ or ‘1’=’1, the parameterized query would not be vulnerable and would instead look for a username that literally matched the entire string tom’ or ‘1’=’1.

Safe C# .NET Prepared Statement Example

With .NET, it’s even more straightforward. The creation and execution of the query don’t change. All you have to do is simply pass the parameters to the query using the Parameters.Add() call as shown here.

String query = “SELECT account_balance FROM user_data WHERE user_name = ?”;
try {
OleDbCommand command = new OleDbCommand(query, connection);
command.Parameters.Add(new OleDbParameter(“customerName”, CustomerName Name.Text));
OleDbDataReader reader = command.ExecuteReader();
// …
} catch (OleDbException se) {
// error handling
}

Option 2: Stored Procedures

Stored procedures are not always safe from SQL injection. However, certain standard stored procedure programming constructs have the same effect as the use of parameterized queries when implemented safely which is the norm for most stored procedure languages.

They require the developer to just build SQL statements with parameters that are automatically parameterized unless the developer does something largely out of the norm. The difference between prepared statements and stored procedures is that the SQL code for a stored procedure is defined and stored in the database itself, and then called from the application. Both of these techniques have the same effectiveness in preventing SQL injection so your organization should choose which approach makes the most sense for you.

Note: ‘Implemented safely’ means the stored procedure does not include any unsafe dynamic SQL generation. Developers do not usually generate dynamic SQL inside stored procedures. However, it can be done but should be avoided. If it can’t be avoided, the stored procedure must use input validation or proper escaping as described in this article to make sure that all user-supplied input to the stored procedure can’t be used to inject SQL code into the dynamically generated query. Auditors should always look for uses of sp_execute, execute, or exec within SQL Server stored procedures. Similar audit guidelines are necessary for similar functions for other vendors.

There are also several cases where stored procedures can increase risk. For example, on MS SQL server, you have 3 main default roles: db_datareader, db_datawriter, and db_owner. Before stored procedures came into use, DBA’s would give db_datareader or db_datawriter rights to the web service's user, depending on the requirements. However, stored procedures require executing rights, a role that is not available by default. Some setups where the user management has been centralized but is limited to those 3 roles, cause all web apps to run under db_owner rights so stored procedures can work. Naturally, that means that if a server is breached the attacker has full rights to the database, where previously they might only have had read-access.

Option 3: White List Input Validation

Various parts of SQL queries aren’t legal locations for the use of bind variables, such as the names of tables or columns, and the sort order indicator (ASC or DESC). In such situations, input validation or query redesign is the most appropriate defense. For the names of tables or columns, ideally, those values come from the code, and not from user parameters.

But if user parameter values are used to make difference for table names and column names, then the parameter values should be mapped to the legal/expected table or column names to make sure unvalidated user input doesn’t end up in the query. Please note, this is a symptom of poor design and a full re-write should be considered if time allows.

Here is an example of table name validation.

String tableName;
switch(PARAM):
case “Value1”: tableName = “fooTable”;
break;
case “Value2”: tableName = “barTable”;
break;

default : throw new InputValidationException(“unexpected value provided”
+ “ for table name”);

The tableName can then be directly appended to the SQL query since it is now known to be one of the legal and expected values for a table name in this query. Keep in mind that generic table validation functions can lead to data loss as table names are used in queries where they are not expected.

For something simple like sort order, it would be best if the user-supplied input is converted to a boolean, and then that boolean is used to select the safe value to append to the query. This is a very standard need in dynamic query creation.

For example:

public String someMethod(boolean sortOrder) {
String SQLquery = “some SQL … order by Salary “ + (sortOrder ? “ASC” : “DESC”);`

Any time user input can be converted to a non-String, like a date, numeric, boolean, enumerated type, etc. before it is appended to a query, or used to select a value to append to the query, this ensures it is safe to do so.

Option 4: Escaping all User-Supplied Input

This technique should only be used as a last resort when none of the above are feasible. Input validation is probably a better choice as this methodology is frail compared to other defenses and we cannot guarantee it will prevent all SQL Injection in all situations.

This technique is to escape user input before putting it in a query. It is very database-specific in its implementation. It’s usually only recommended to retrofit legacy code when implementing input validation isn’t cost-effective. Applications built from scratch, or applications requiring low-risk tolerance should be built or re-written using parameterized queries, stored procedures, or some kind of Object Relational Mapper (ORM) that builds your queries for you.

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.

Option 5: Lease 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 a system on Windows by default! Change the DBMS’s OS account to something more appropriate, with restricted privileges.

Option 6: Multiple Database 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.

Option 7: Views

You can use SQL views to 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.

Full stack developer, Principle Engineer, Software Architect