Preface
In MySQL, the escape functions include addslashes, mysql_real_escape_string, mysql_escape_string, and so on. Another case is magic_quote_gpc. However, PHP in higher versions will remove this feature.
First, wide byte injection is irrelevant to HTML page encoding.
<meta charset=utf8>
I gave up trying. This is a misunderstanding. SQL injection is not XSS. Although their encoding causes are similar, the locations are different.
Many materials on the Internet say that the program uses wide bytes to process the program, but it does not point out the specific program. This article describes the principle and simple use of specific vulnerabilities. The language we use here is php5.4 and database mysql5.6.
Concepts involved
Character, Character Set, and collation
Character (character) is the basic unit of character set. Assign a value (encoding) to the character to determine its position in the character set.
Collation refers to the comparison rules between characters in the same character set.
Utf8
Since there are only 128 characters in ASCII format, Unicode encoding is used in the online world, But Unicode is not efficient for characters in ASCII format. Therefore, the intermediate format character set is called universal conversion format and UTF (Universal Transformation Format ).
Byte
Gb2312, GBK, gb18030, big5, shift_jis, and so on are common width bytes. In fact, they only have two bytes. The security problem caused by wide bytes is mainly caused by ASCII characters (one byte.
Character Set conversion process of MySQL
1. MySQL Server converts the request data from character_set_client to character_set_connection when receiving the request;
2. Before performing internal operations, convert the request data from character_set_connection to the internal operation character set. The method is as follows:
• Set the character set value for each data field;
• If the preceding value does not exist, use the default Character Set Value of the corresponding data table (MySQL extension, non-SQL standard );
• If the preceding value does not exist, use the default Character Set Value of the corresponding database;
• If the preceding value does not exist, use character_set_server to set the value.
- Convert the operation result from the internal character set to character_set_results.
Note: The location where wide byte Injection occurs is that when PHP sends a request to MySQL, the character set uses character_set_client to set the value for encoding.
PHP test code:
<! Doctype HTML> <meta charset = "GBK"> <! -- Only used for basic display, and changed to utf8, which means it is hard to see --> <? PHP error_reporting (0); $ conn = mysql_connect ('2017. 0.0.1 ', 'root', ''); mysql_select_db ('mysql', $ conn); mysql_query (" set names GBK "); // insecure encoding method $ res = mysql_query ("show variables like 'character % ';"); // display the character sets set for the current database while ($ ROW = mysql_fetch_array ($ res) {var_dump ($ row );} $ user = addslashes ($ _ Get ['SQL']); // mysql_real_escape_string () magic_quote_gpc = on addslashes () mysql_escape_string () functions similar to $ SQL = "Sel ECT host, user, password from user where user = '{$ user}' "; echo $ SQL. '</BR>'; if ($ res = mysql_query ($ SQL) {While ($ ROW = mysql_fetch_array ($ res) {var_dump ($ row );}} else {echo "error ". mysql_error (). "<br/>" ;}?>
http://localhost/xl.php?sql=root%df%27%20or%201=1%23
Yes, it can be executed successfully!
URL Decoding SQL = root comment' or 1 = 1 #
Resolution Process:
$ _ Get ['SQL'] is encoded with '\' 1, root % DF % 5c % 27% 20or % 201 = 1% 232, and GBK is used for MySQL processing. character Set % DF % 5c-> the operation succeeded. % 5c % 27-> 'single quotation marks are successfully closed.
How to eat:
GBK encoding. The encoding range is 0 × 8140 ~ 0 xfefe (excluding xx7f), automatically concatenate % 5c when % DF (ASCII (223)> ASCII (128) is encountered, so eat '\', characters smaller than ASCII (128) of % 27 and % 20 are retained.
Supplement:
Gb2312 is compatible with GBK, and its high position range is 0xa1 ~ 0xf7, the low position range is 0xa1 ~ 0xfe (0x5c is not in this range), so % 5c cannot be eaten by encoding.
Other wide character sets are the same analysis process. To eat % 5c, you only need to include normal 0x5c in the low position.
Security Filtering
The above Code uses mysql_query ("set names GBK") to set encoding. In fact, mysql_set_charset ("GBK") is recommended in MySQL. The function is used to set encoding, these two functions have similar functions. The only difference is that the latter modifies the mysql-> charset attribute in the MySQL object to the set character set.
The matched filter function is mysql_real_escape_string (). The code above lists several filter functions. The difference between them is that mysql_real_escape_string () treats the passed strings according to the mysql-> charset attribute in the MySQL object, therefore, it can be filtered based on the current character set.
Specific differences can be referred to: http://www.laruence.com/2010/04/12/1396.html
Likewise
The preceding wide-byte injection is produced by transcoding, and functions with the transcoding function become the cause of the vulnerability.
Transcoding Functions
Mb_convert_encoding ()
Iconv ()
Here we use iconv () to demonstrate how to modify the above Code:
<! Doctype HTML> <meta charset = "GBK"> <? PHP error_reporting (0); $ conn = mysql_connect ('2017. 0.0.1 ', 'root', ''); mysql_select_db ('mysql', $ conn); mysql_set_charset (" utf8 "); // recommended security code $ user = mysql_real_escape_string ($ _ Get ['SQL']); // Recommended filter function $ user = iconv ('gbk ', 'utf-8', $ user); $ SQL = "select host, user, password from user where user = '{$ user}'"; echo $ SQL. '</BR>'; $ res = mysql_query ($ SQL); While ($ ROW = mysql_fetch_array ($ res) {var_dump ($ row) ;}?>
http://localhost/xl.php?sql=root%e5%27or%201=1%23
The code parsing process is still as follows.
Summarize the causes of the vulnerability:
Code 1
1. Insecure Character Set setting and filtering functions are used.
2. The vulnerability occurs when PHP requests MySQL using the character_set_client value for one transcoding.
Code 2
1. Recommended setting and filtering functions are used.
2. The parsing error occurs when the iconv () function is transcoded, GBK turns to utf8 and eats "\"
3. transcoding security when PHP requests MySQL.
In addition:
$ User = iconv ('utf-8', 'gbk', $ user) when changing the encoding direction );
Access http: // localhost/XL. php? SQL = root % E9 % 8C % A6 can be entered with a \, and then comment out single quotes.
In this case, two parameters are required for injection.
For example:
http://localhost/xl.php?sql=root%e9%8c%a6¶=%20or%201=1%23
Summary:
Width-byte injection is irrelevant to HTML page encoding.
Mysql_real_escape_string () and mysql_set_charset () are recommended for MySQL encoding and filtering functions ().
Conversion functions can also cause wide-byte injection, even if a secure setting function is used.
Reposted from: 91ri.org, not original, and added to favorites!