使用Mysql charset 实现SQL注入

本文揭示了MySQL数据库中一种有趣的特性,当使用utf8等字符集时可能导致应用不安全。通过实例展示如何利用该特性绕过用户名长度限制,攻击者能够注册管理员级别的账号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mysql charset Truncation vulnerability

By http://www.80sec.com/

We found that there is a interesting feature in mysql database,when you are using utf8,gbk or other charsets.This feature may make your application unsecure.

Stefen Esser shows some attack manners of mysql in his paper[1], in which he issues the SQL Column Truncation vulnerability.

The application is a forum where new users can register
The administrator’s name is known e.g. ‘admin’
MySQL is used in the default mode
There is no application restriction on the length of new user names
The database column username is limited to 16 characters

Although the application restrict the length of the username, we can bypass it in the following example:


<?php
$user=$_REQUEST['user'];
mysql_connect(”localhost”, “root”, “”) or
die(”Could not connect: ” . mysql_error());
mysql_select_db(”test”);
mysql_query(”SET names utf8″);
$result = mysql_query(”SELECT * from test_user where user=’$user’”);
if(trim($user)==” or strlen($user)>20 ){
die(”Input user Invalid”);
}
if(@mysql_fetch_array($result, MYSQL_NUM)) {
die(”already exist”);
}
else {
$sql=”insert test_user values (’$user’)”;
mysql_query($sql);
echo “$user register OK!”;
}
mysql_free_result($result);
?>

Read the code here:


$result = mysql_query("SELECT * from test_user where user='$user'");

If the attacker input a username ‘admin z’, and the sql will be like this:


SELECT * FROM user WHERE username='admin z'

And the application will check the length of username with the following code:


if(trim($user)=='' or strlen($user)>20 ){
die("Input user Invalid");
}

The attack will failed because the length of the username ‘admin z’ is greater then 20.

But it will not end here, attacker can input username ‘admin0xc1zzz’, and the sql will be like this:


SELECT * FROM user WHERE username='admin0xc1zzz'

This pass the application’s logic,when the insert commond executes:


insert test_user values ('admin0xc1zzz')

because the table is created in charset utf8,the 0xc1 is not a valid utf8 character,it will be striped,also all of the next characters will be striped too.Then the attacker got a user “admin”;

As you see,when mysql works at utf8,the invalid data will be striped ,but the webapplication doesn’t know this,it works at binaray.The difference between webapplication and database make a vulnerability.

Reference:

[1] http://www.suspekt.org/2008/08/18/mysql-and-sql-column-truncation-vulnerabilities/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值