An issue that bypasses SQL injection by using Unhex

Source: Internet
Author: User
In the normal development process, there is the problem of SQL injection in the code, presumably we all know and understand, but also understand the common solution.

But recently there's another way to prevent SQL injection:

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

That is, using PHP's own function bin2hex function to process the input to query string into a 16 string, and then the SQL execution process using the database itself to unhex reverse the 16 binary string to the original normal string.

This way, even if the value of $name is 'maratrix' or 1 = 1 normally treated as a query string, there is no problem with SQL injection.

Looked up the relevant information, also did not understand that this can bypass the principle of SQL injection, I hope you help explain the reasons.

Thank you ~

Reply content:

In the normal development process, there is the problem of SQL injection in the code, presumably we all know and understand, but also understand the common solution.

But recently there's another way to prevent SQL injection:

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

That is, using PHP's own function bin2hex function to process the input to query string into a 16 string, and then the SQL execution process using the database itself to unhex reverse the 16 binary string to the original normal string.

This way, even if the value of $name is 'maratrix' or 1 = 1 normally treated as a query string, there is no problem with SQL injection.

Looked up the relevant information, also did not understand that this can bypass the principle of SQL injection, I hope you help explain the reasons.

Thank you ~

Bin2Hex after the $name all converted to hex format data, after stitching into SQL inside will not produce the existence of a loophole statement. It is best to suggest that you let the $name value maratrix' or 1 = 1 , and then echo the value of $sql to understand.

Because Unhex is a MySQL function, it is no problem to perform naturally in MySQL.
SELECT * from test1 WHERE name = Unhex (' 6d6172617472697827206f722031203d2031 ');
Name no matter what value, will not split the SQL.

  • Related Article

    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.