首页是一个登录框,登录成功后,可以看到如下界面: 这里记录了很多请求头信息。接着看一下网站源码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Less-20 Cookie Injection- Error Based- string</title> </head> <body bgcolor="#000000"> <?php //including the Mysql connect parameters. include("../sql-connections/sql-connect.php"); error_reporting(0); if(!isset($_COOKIE['uname'])) { //including the Mysql connect parameters. include("../sql-connections/sql-connect.php"); echo "<div style=' margin-top:20px;color:#FFF; font-size:24px; text-align:center'> Welcome <font color='#FF0000'> Dhakkan </font><br></div>"; echo "<div align='center' style='margin:20px 0px 0px 510px;border:20px; background-color:#0CF; text-align:center;width:400px; height:150px;'>"; echo "<div style='padding-top:10px; font-size:15px;'>"; echo "<!--Form to post the contents -->"; echo '<form action=" " name="form1" method="post">'; echo ' <div style="margin-top:15px; height:30px;">Username : '; echo ' <input type="text" name="uname" value=""/> </div>'; echo ' <div> Password : '; echo ' <input type="text" name="passwd" value=""/></div></br>'; echo ' <div style=" margin-top:9px;margin-left:90px;"><input type="submit" name="submit" value="Submit" /></div>'; echo '</form>'; echo '</div>'; echo '</div>'; echo '<div style=" margin-top:10px;color:#FFF; font-size:23px; text-align:center">'; echo '<font size="3" color="#FFFF00">'; echo '<center><br><br><br>'; echo '<img src="../images/Less-20.jpg" />'; echo '</center>'; function check_input($value) { if(!empty($value)) { $value = substr($value,0,20); // truncation (see comments) } if (get_magic_quotes_gpc()) // Stripslashes if magic quotes enabled { $value = stripslashes($value); } if (!ctype_digit($value)) // Quote if not a number { $value = "'" . mysql_real_escape_string($value) . "'"; } else { $value = intval($value); } return $value; } echo "<br>"; echo "<br>"; if(isset($_POST['uname']) && isset($_POST['passwd'])) { $uname = check_input($_POST['uname']); $passwd = check_input($_POST['passwd']); $sql="SELECT users.username, users.password FROM users WHERE users.username=$uname and users.password=$passwd ORDER BY users.id DESC LIMIT 0,1"; $result1 = mysql_query($sql); $row1 = mysql_fetch_array($result1); $cookee = $row1['username']; if($row1) { echo '<font color= "#FFFF00" font size = 3 >'; setcookie('uname', $cookee, time()+3600); header ('Location: index.php'); echo "I LOVE YOU COOKIES"; echo "</font>"; echo '<font color= "#0000ff" font size = 3 >'; //echo 'Your Cookie is: ' .$cookee; echo "</font>"; echo "<br>"; print_r(mysql_error()); echo "<br><br>"; echo '<img src="../images/flag.jpg" />'; echo "<br>"; } else { echo '<font color= "#0000ff" font size="3">'; //echo "Try again looser"; print_r(mysql_error()); echo "</br>"; echo "</br>"; echo '<img src="../images/slap.jpg" />'; echo "</font>"; } } echo "</font>"; echo '</font>'; echo '</div>'; } else { if(!isset($_POST['submit'])) { $cookee = $_COOKIE['uname']; $format = 'D d M Y - H:i:s'; $timestamp = time() + 3600; echo "<center>"; echo '<br><br><br>'; echo '<img src="../images/Less-20.jpg" />'; echo "<br><br><b>"; echo '<br><font color= "red" font size="4">'; echo "YOUR USER AGENT IS : ".$_SERVER['HTTP_USER_AGENT']; echo "</font><br>"; echo '<font color= "cyan" font size="4">'; echo "YOUR IP ADDRESS IS : ".$_SERVER['REMOTE_ADDR']; echo "</font><br>"; echo '<font color= "#FFFF00" font size = 4 >'; echo "DELETE YOUR COOKIE OR WAIT FOR IT TO EXPIRE <br>"; echo '<font color= "orange" font size = 5 >'; echo "YOUR COOKIE : uname = $cookee and expires: " . date($format, $timestamp); echo "<br></font>"; $sql="SELECT * FROM users WHERE username='$cookee' LIMIT 0,1"; $result=mysql_query($sql); if (!$result) { die('Issue with your mysql: ' . mysql_error()); } $row = mysql_fetch_array($result); if($row) { echo '<font color= "pink" font size="5">'; echo 'Your Login name:'. $row['username']; echo "<br>"; echo '<font color= "grey" font size="5">'; echo 'Your Password:' .$row['password']; echo "</font></b>"; echo "<br>"; echo 'Your ID:' .$row['id']; } else { echo "<center>"; echo '<br><br><br>'; echo '<img src="../images/slap1.jpg" />'; echo "<br><br><b>"; //echo '<img src="../images/Less-20.jpg" />'; } echo '<center>'; echo '<form action="" method="post">'; echo '<input type="submit" name="submit" value="Delete Your Cookie!" />'; echo '</form>'; echo '</center>'; } else { echo '<center>'; echo "<br>"; echo "<br>"; echo "<br>"; echo "<br>"; echo "<br>"; echo "<br>"; echo '<font color= "#FFFF00" font size = 6 >'; echo " Your Cookie is deleted"; setcookie('uname', $row1['username'], time()-3600); header ('Location: index.php'); echo '</font></center></br>'; } echo "<br>"; echo "<br>"; //header ('Location: main.php'); echo "<br>"; echo "<br>"; //echo '<img src="../images/slap.jpg" /></center>'; //logging the connection parameters to a file for analysis. $fp=fopen('result.txt','a'); fwrite($fp,'Cookie:'.$cookee."\n"); fclose($fp); } ?> </body> </html>通过源码可以得知,网站的大致流程就是当我们输入账户和密码登录的时候,会将uname字段存入cookee中。$cookee = $_COOKIE['uname'];,然后当我们再次访问页面的时候,会判断cookee是否存在,如果存在则$sql="SELECT * FROM users WHERE username='$cookee' LIMIT 0,1";通过这个SQL语句查询我们的账户信息。
而且这个$cookee是没有经过过滤的,因此我们的注入点就在这里。
通过观察这条SQL语句 $sql="SELECT * FROM users WHERE username='$cookee' LIMIT 0,1"; 我们可以使用联合查询进行注入。
那么接口在哪里呢? 这里我们要利用cookie,浏览器每次向服务器发送请求的时候,如果本地存有相关的cookie信息,则会将cookie一并发送给服务器。
就本题而论,浏览器存储的cookie如下图: cookie中uname字段的值就是我们的登录的账号(明文),而后台查询的条件也是它。因此我们需要对这个字段进行修改,让浏览器发送的uanme字段为我们构造的注入语句。
那么,则有两种更改cookie的思路:
直接更改本地cookie信息;让浏览器请求的时候,直接将本地更改好的cookie信息发送给服务器。在浏览器发送请求的时候,我们进行抓包,直接更改发送包中的cookie字段。下面分别就两种方法进行展示。
由于要使用联合查询,因此我们需要知道网站SQL语句检索字段的个数。 这里是直接使用edit this cookie对本地的cookie信息进行了更改,更改为admin' order by 4 # 经典的判断检索字段个数的语句。我们可以判断出原本的SQL语句的检索字段为3。
然后使用联合查询,查一下当前使用的数据库以及该数据库下有哪些数据表。 这里构造的语句为:' union select 1, (database()), (select group_concat(table_name) from information_schema.tables where table_schema="security") # 这条语句在之前关卡中几乎是不可或缺的,就不再罗嗦了。
知道了这个手法以后,后面的操作就和之前无异了。因此不在多说。
抓包的方式有很多中,可以使用burp suite抓包更改数据,我这里就用tamper data进行演示了。 在发送到服务器之前,我们将cookie中的信息进行更改,这里使用的还是' union select 1, (database()), (select group_concat(table_name) from information_schema.tables where table_schema="security") #。
然后可以看一下结果 和修改本地cookie的结果一致。 和第一个手法不同的是,我们本地的cookie却并未改变。