An issue of bypassing SQL Injection by using UNHEX

Source: Internet
Author: User
During normal development, you may know about SQL injection in the Code and general solutions. However, we recently saw another way to prevent SQL injection: {code ...} that is, the bin2hex function that comes with PHP will be used to input... during normal development, you may know about SQL injection in the Code and general solutions.

However, we recently see another method to prevent SQL injection as follows:

$sql = sprintf("SELECT * FROM `test1` WHERE `name` = unhex('%s')", bin2hex($name));

That is, use the built-in functions of PHP first.bin2hexThe function processes the input string to be queried into a hexadecimal string, and then uses the database'sunhexThe function reverts The hexadecimal string to the original normal string.

In this way, even if the value of $ name is'maratrix' or 1 = 1It will also be processed as a query string without the problem of SQL injection.

I checked the relevant information and did not understand the principle of bypassing SQL injection. I hope you can help explain the reasons.

Thank you ~

Reply content:

During normal development, you may know about SQL injection in the Code and general solutions.

However, we recently see another method to prevent SQL injection as follows:

$sql = sprintf("SELECT * FROM `test1` WHERE `name` = unhex('%s')", bin2hex($name));

That is, use the built-in functions of PHP first.bin2hexThe function processes the input string to be queried into a hexadecimal string, and then uses the database'sunhexThe function reverts The hexadecimal string to the original normal string.

In this way, even if the value of $ name is'maratrix' or 1 = 1It will also be processed as a query string without the problem of SQL injection.

I checked the relevant information and did not understand the principle of bypassing SQL injection. I hope you can help explain the reasons.

Thank you ~

After bin2hex, $ name is all converted into hex data. After being spliced into SQL, no vulnerable statements will be generated. We recommend that you set the value of $ namemaratrix' or 1 = 1And then echo the $ SQL value to understand.

Because unhex is a mysql function, it is okay to execute it in mysql.
SELECT * FROMtest1WHEREname= Unhex ('6d6172617472697827206f722031203d2031 ');
No matter what the value of name is, the SQL statement will not be split.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.