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.