Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Sql Injection: Complete Guide

Understand Sql Injection, how it works, key uses, common issues, and practical considerations. Use this clear guide to make informed decisions.

Table of Contents

1. Truncate the Content of the Query

This guide provides a clear overview of sql injection, including 1. Truncate the Content of the Query, 2. Bypass the Filtering of Keywords. Use it to understand the topic, compare the available options, and make a more informed decision.

1. Truncate the Content of the Query - Sql Injection

After commenting, our query becomes:

1. Truncate the Content of the Query - Sql Injection

2. Bypass the Filtering of Keywords

A. Inline Comments

Inline comments are used very effectively in bypassing whitespace filtering. The following characters can be used to bypass whitespace filtering: /**/, %20, %09, %0a, %0b, %0c, %0d, %a0). For example:

A. Inline Comments - Sql Injection

Or bypass keyword filtering (available with MySql). In the example below, the keyword union and password are in the blacklist and have been blocked, we can bypass it by:

A. Inline Comments - Sql Injection

B. Replace Keywords

When exploiting SQL injection we often use keywords like: union, select, information_schema. In many cases programmers simply replace those keywords:

B. Replace Keywords - Sql Injection

We can easily see that the above processing code is lacking. If it's simply pattern matching, the bypass is extremely simple. Let's apply case sensitive, where uppercase and lowercase letters are interpreted differently.

Now instead of using the keyword:

select, union… 

We will use:

SeLEcT, UniOn…

The basis of this bypass is that database management systems are not case sensitive for keywords.

In some cases, the web application will filter out all or part of certain keywords (union, select.). We will bypass as follows:

id=1+uniunionon+SeLselectecT+1,2,3-- -

After the union and select are filtered out by the web application, we will be left with the following correct query:

id=1+union+SeLecT+1,2,3-- -

C. Character Encoding

We can bypass when WAF (Web Application Firewall) blocks keywords by encoding them. Many WAF applications will only decode the query once and filter out the keywords in the blacklist, then let's encode the request twice so it can be bypassed in this case.

C. Character Encoding - Sql Injection

3. Bypass Blocks Single and Double Quotes

- Let's look at an example before learning specifically how to bypass this.

3. Bypass Blocks Single and Double Quotes - Sql Injection

In this scenario, we already know a table in the database named users. The next job is to know the column name in the table to get its information. As in the above query, we use the condition: table_name='users'. But if both single quotes (') and double quotes (") are blocked by WAF, we can no longer use 'users' or "users". The built-in database system gives us a very good function to solve this problem, which is the CHAR() function (for Oracle, CHR()). For example, in the above query we will bypass by:

3. Bypass Blocks Single and Double Quotes - Sql Injection 3. Bypass Blocks Single and Double Quotes - Sql Injection

PHP programmers are all familiar with the addslashes() function. The addslashes() function has the effect of prepending special characters such as single quotes ('), double quotes ("), backslash(), NUL (null bytes) character "" to help the database management system not So, when we want to inject the query according to the script: name='someName' or '1'='1'-- the result is difficult and confusing. The results are no longer what we expected.

However, there is a technique to bypass the addslashes() function to inject the apostrophe ('). This technique has been public for quite some time, and to implement this technique is quite difficult because it is tied to the coding style applied to the website.

4. Bypass ""illegal Mix of Collation for Operation Union"" Error

In some management systems (usually found in MySql), databases and tables when collation is set, when using the UNION keyword, the error "illegal mix of collation for operation UNION" will be reported. The setting of collation (collation font collation) can be intentional by the database designer or by default setting of MySql. In the case of a union, we must ensure that the select value in each field must have the corresponding code type defined. In my opinion, this error is quite common, especially for CMSs running Apache MySql. People can learn more at: http://bugs.mysql.com/bug.php?id=57926. In this case we can use conversions to the appropriate encoding.

For example in the following case:

4. Bypass

In the above query, if column1 has been set to collation as Unicode-UTF8 or _latin1 for example, then what is selected from column2 will have to be converted to the corresponding code. We can force it like this:

4. Bypass

Do we see the downside in this bypass that we have to know that the collation code is _latin1. A better way to bypass is to use hex and unhex encoding and decoding functions.

4. Bypass

There are many other functions that can be used in place of hex and unhex.

Conclusion

Understanding Sql Injection makes it easier to compare options, avoid common mistakes, and apply the information in this guide more effectively. Review the relevant requirements before making changes or choosing a solution.

FAQ

What is Sql Injection?

Inline Comments Inline comments are used very effectively in bypassing whitespace filtering.

Why is Sql Injection important?

Understanding Sql Injection helps you evaluate features, compatibility, performance, and potential limitations before you choose a product or follow a procedure.

What should you consider when using or choosing Sql Injection?

Consider your specific goal, compatibility requirements, available features, cost, security, and the practical recommendations described in this guide.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.