sql注入——php源码的审计(以sql-lab 1~15为例)(超详细)

sql注入

sql注入:web应用对用户输入数据的合法性没有判断,并且传入的参数是攻击者可控的,因此攻击者通过构造不同的sql语句来实现对数据库的操作(对数据库的操作:与数据库产生交互都有可能存在,sql注入

  • 两个关键条件:1. 参数用户可用。2. 参数带入数据库查询

  • SQL注入根据个人学习所得,大概分为如下几种:

    联合查询注入(整型注入、字符型注入、宽字节注入、二次注入、堆叠注入等等)
    报错注入(通过报错函数大致分为updatexml报错注入、floor报错注入、extractvalue报错注入、exp报错注入等等)
    盲注(时间型盲注、布尔型盲注等等)

    通过我们的上传点位置还大致分为GET注入、POST注入、http头部注入等等。

现在我们就开始sql的代码审计吧:

Less-1

源码如下:

<!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-1 **Error Based- String**</title>
</head><body bgcolor="#000000">
<div style=" margin-top:70px;color:#FFF; font-size:23px; text-align:center">Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br>
<font size="3" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
// take the variables
if(isset($_GET['id']))
{$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);// connectivity $sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);if($row){echo "<font size='5' color= '#99FF00'>";echo 'Your Login name:'. $row['username'];echo "<br>";echo 'Your Password:' .$row['password'];echo "</font>";}else {echo '<font color= "#FFFF00">';print_r(mysql_error());echo "</font>";  }
}else { echo "Please input the ID as parameter with numeric value";}?>
</font> </div></br></br></br><center>
<img src="../images/Less-1.jpg" /></center>
</body>
</html>

在源码审计之前我们要先了解一下php函数的作用:

error_reporting(0)

error_reporting(0)函数规定不同的错误级别的报告

<?php// 关闭错误报告error_reporting(0);
?>
include("../sql-connections/sql-connect.php");

​ include 表达式包含并运行指定文件。

if(isset($_GET['id']))

if 语句用于在指定条件为 true 时执行代码(与c语言,python相同)

isset($_GET['id'])

isset() 函数用于检测变量是否已设置并且非 NULL。

如果已经使用 unset() 释放了一个变量之后,再通过 isset() 判断将返回 FALSE。

若使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE。

同时要注意的是 null 字符("\0")并不等同于 PHP 的 NULL 常量。

$id=$_GET['id'];   // 将$_GET['id']赋值给$id

$id= 是php的变量函数

fopen('result.txt','a');

​ fopen() 函数打开文件或者 URL。

如果打开失败,本函数返回 FALS

该函数语法:

fopen(filename,mode,include_path,context)
fwrite($fp,'ID:'.$id."\n");

​ fwrite() 函数写入文件

语法:

fwrite(file,string,length)
fclose($fp);

fclose() 函数关闭一个打开文件。

mysql_query($sql)

mysql_query() 函数执行一条 MySQL 查询

mysql_fetch_array($result);

mysql_fetch_array() 函数从结果集中取得一行作为关联数组,或数字数组,或二者兼有

返回根据从结果集取得的行生成的数组,如果没有更多行则返回 false。

 echo "<font size='5' color= '#99FF00'>";

echo 输出文本

eg:

<?php
echo "Hello world!";
?>    // 输出   hello world
print_r(mysql_error());

print_r: 函数用于打印变量(相当于c语言中的printf python中的print)

mysql_error:mysql_error() 函数返回上一个 MySQL 操作产生的文本错误信息。本函数返回上一个 MySQL 函数的错误文本,如果没有出错则返回 ‘’(空字符串)。

源码讲解:

现在我们已经了解完源码的全部原函数,让我们开始源码审计吧:

由源码看出我们在网页上直接进行GET上传

$id=$_GET['id'];

并且可以发现:

它的sql变量可以直接到sql语句中查询

$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";

从数据库中获取的内容可以直接在页面上显示:

 if($row){echo "<font size='5' color= '#99FF00'>";echo 'Your Login name:'. $row['username'];echo "<br>";echo 'Your Password:' .$row['password'];echo "</font>";}

less-2

源码如下:

<!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-2 **Error Based- Intiger**</title>
</head><body bgcolor="#000000"><div style=" margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br>
<font size="3" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
// take the variables
if(isset($_GET['id']))
{$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);// connectivity
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);if($row){echo "<font size='5' color= '#99FF00'>";echo 'Your Login name:'. $row['username'];echo "<br>";echo 'Your Password:' .$row['password'];echo "</font>";}else {echo '<font color= "#FFFF00">';print_r(mysql_error());echo "</font>";  }
}else{  echo "Please input the ID as parameter with numeric value";}?></font> </div></br></br></br><center>
<img src="../images/Less-2.jpg" /></center>
</body>
</html>

经过观察发现:less-1与less-2的源码一摸一样

源码分析请参考上一关。

less-3

<!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-3 Error Based- String (with Twist) </title></head><body bgcolor="#000000"><div style=" margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br>
<font size="3" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
// take the variables
if(isset($_GET['id']))
{$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);// connectivity $sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);if($row){echo "<font size='5' color= '#99FF00'>";echo 'Your Login name:'. $row['username'];echo "<br>";echo 'Your Password:' .$row['password'];echo "</font>";}else

代码讲解

第三关的原码与第一关的源码相差不大

仅仅只是此处发生变化:

$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";

我们不难发先,第三关只是id的包裹方式发生变化由第一关的 ‘id’ 变为 (’$id’)

所以,其余与less-1 和 less-2一样

由源码看出我们在网页上直接进行GET上传

$id=$_GET['id'];

并且可以发现:

它的sql变量可以直接到sql语句中查询

$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";

从数据库中获取的内容可以直接在页面上显示:

 if($row){echo "<font size='5' color= '#99FF00'>";echo 'Your Login name:'. $row['username'];echo "<br>";echo 'Your Password:' .$row['password'];echo "</font>";}

less-4

源码

<!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-4 Error Based- DoubleQuotes String</title>
</head><body bgcolor="#000000">
<div style=" margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br>
<font size="3" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
// take the variables
if(isset($_GET['id']))
{$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);// connectivity $id = '"' . $id . '"';
$sql="SELECT * FROM users WHERE id=($id) LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);if($row){echo "<font size='5' color= '#99FF00'>";echo 'Your Login name:'. $row['username'];echo "<br>";echo 'Your Password:' .$row['password'];echo "</font>";}else {echo '<font color= "#FFFF00">';print_r(mysql_error());echo "</font>";  }
}else { echo "Please input the ID as parameter with numeric value";}?></font> </div></br></br></br><center>
<img src="../images/Less-4.jpg" /></center>
</body>
</html>

与第三关相同 仅仅只是包裹方式的不同

$sql="SELECT * FROM users WHERE id=($id) LIMIT 0,1";

从上面的源代码看出 包裹方式变为($id)

所以,其余与less-1 和 less-2一样

由源码看出我们在网页上直接进行GET上传

$id=$_GET['id'];

并且可以发现:

它的sql变量可以直接到sql语句中查询

$sql="SELECT * FROM users WHERE id=($id) LIMIT 0,1";

从数据库中获取的内容可以直接在页面上显示:

 if($row){echo "<font size='5' color= '#99FF00'>";echo 'Your Login name:'. $row['username'];echo "<br>";echo 'Your Password:' .$row['password'];echo "</font>";}

less-5

源码

<!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-5 Double Query- Single Quotes- String</title>
</head><body bgcolor="#000000">
<div style=" margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br>
<font size="3" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
// take the variables
if(isset($_GET['id']))
{$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);// connectivity $sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);if($row){echo '<font size="5" color="#FFFF00">';   echo 'You are in...........';echo "<br>";echo "</font>";}else {echo '<font size="3" color="#FFFF00">';print_r(mysql_error());echo "</br></font>"; echo '<font color= "#0000ff" font size= 3>';    }
}else { echo "Please input the ID as parameter with numeric value";}?></font> </div></br></br></br><center>
<img src="../images/Less-5.jpg" /></center>
</body>
</html>

源码审计:

通过对比前面关卡的源码,我们发现第五关的源码少了

$result=mysql_query($sql);$row = mysql_fetch_array($result);echo 'Your Login name:'. $row['username'];echo 'Your Password:' .$row['password'];

这代表着在数据库中获取的内容无法直接展示,不过代码中有这么一行代码,回显了数据库的报错数据;

echo '<font size="3" color="#FFFF00">';print_r(mysql_error());echo "</br></font>";   echo '<font color= "#0000ff" font size= 3>';

因此,我们可以很自然的想到 上文中提到的报错注入:updatexml(具体如何报错,请关注我的相关博客)

$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";

有这条源码我们可以发现他的包裹方式为:’$id’

less-6

源码如下:

<!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-6 Double Query- Double Quotes- String</title>
</head><body bgcolor="#000000">
<div style=" margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br>
<font size="3" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
// take the variables
if(isset($_GET['id']))
{$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);// connectivity $id = '"'.$id.'"';
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);if($row){echo '<font size="5" color="#FFFF00">';   echo 'You are in...........';echo "<br>";echo "</font>";}else {echo '<font size="3"  color= "#FFFF00">';print_r(mysql_error());echo "</br></font>";   echo '<font color= "#0000ff" font size= 3>';    }
}else { echo "Please input the ID as parameter with numeric value";}?>
</font> </div></br></br></br><center>
<img src="../images/Less-6.jpg" /></center>
</body>
</html>

通过观察发现:less-6与less-5的源码及其的相似

都只有

echo '<font size="3" color="#FFFF00">';print_r(mysql_error());echo "</br></font>";   echo '<font color= "#0000ff" font size= 3>';

都无法在在数据库中获取的内容无法直接展示,只能进行报错注入

仅仅只是包裹方式不同:

$id = '"'.$id.'"';
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";

less-7

源码如下:

<!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-7 Dump into Outfile</title></head><body bgcolor="#000000"><div style=" margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br>
<font size="3" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
// take the variables
if(isset($_GET['id']))
{$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);// connectivity $sql="SELECT * FROM users WHERE id=(('$id')) LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);if($row){echo '<font color= "#FFFF00">';  echo 'You are in.... Use outfile......';echo "<br>";echo "</font>";}else {echo '<font color= "#FFFF00">';echo 'You have an error in your SQL syntax';//print_r(mysql_error());echo "</font>";  }
}else { echo "Please input the ID as parameter with numeric value";}?>
</font> </div></br></br></br><center>
<img src="../images/Less-7.jpg" /></center>
</body>

源码分析:

相比于less-5的源码,我们发现less-7的源码少了:

 print_r(mysql_error());

表示less-7无法进行数据库报错数据的回显,代表着上面机关用的updatexml的报错回显的方法已经无法使用,

并且在less-7中 也给了我们相关的提示:

     echo 'You are in.... Use outfile......';

在上一关中的回显中是一个“You are in…”,不过这一关中的源码很明显将输出数据库错误的代码进行了一个注释(页面正确和错误的显示不同),而且在上面都有一个isset函数对文件夹进行一个输入,从而实现了了对错的比较。

这时候我们就因该想到使用罗尔盲注的手法了。(罗尔盲注的用法,可以看我相关的博客)

less-8

源码如下:

<!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-8 Blind- Boolian- Single Quotes- String</title>
</head><body bgcolor="#000000">
<div style=" margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br>
<font size="3" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
// take the variables
if(isset($_GET['id']))
{$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);// connectivity $sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);if($row){echo '<font size="5" color="#FFFF00">';   echo 'You are in...........';echo "<br>";echo "</font>";}else {echo '<font size="5" color="#FFFF00">';//echo 'You are in...........';//print_r(mysql_error());//echo "You have an error in your SQL syntax";echo "</br></font>";  echo '<font color= "#0000ff" font size= 3>';    }
}else { echo "Please input the ID as parameter with numeric value";}?></font> </div></br></br></br><center>
<img src="../images/Less-8.jpg" /></center>
</body>
</html>

源码分析:

通过对比发现 less-7和leaa-8的源码相差不大,都没有

print_r(mysql_error());

都无法进行一个报错注入,只能进行盲注。

仅仅只是一个包裹的不同

$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";

less-9

<!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-9 Blind- Time based- Single Quotes- String</title>
</head><body bgcolor="#000000">
<div style=" margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br>
<font size="3" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);// take the variables
if(isset($_GET['id']))
{$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);// connectivity $sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);if($row){echo '<font size="5" color="#FFFF00">';   echo 'You are in...........';echo "<br>";echo "</font>";}else {echo '<font size="5" color="#FFFF00">';echo 'You are in...........';//print_r(mysql_error());//echo "You have an error in your SQL syntax";echo "</br></font>";    echo '<font color= "#0000ff" font size= 3>';    }
}else { echo "Please input the ID as parameter with numeric value";}?>

源码审计:

观察源码我们发现:

else {echo '<font size="5" color="#FFFF00">';echo 'You are in...........';//print_r(mysql_error());//echo "You have an error in your SQL syntax";echo "</br></font>";    echo '<font color= "#0000ff" font size= 3>';    }

这一关的对于错的回显是一样的,所以上几关使用的布尔盲注我们已经无法在使用了。这时候,我们只能使用最后的法宝:时间盲注(时间盲注的用法,可以看我相关的博客)。

less-10

<!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-10 Blind- Time based- Double Quotes- String</title>
</head><body bgcolor="#000000">
<div style=" margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br>
<font size="3" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);// take the variables
if(isset($_GET['id']))
{$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);// connectivity $id = '"'.$id.'"';
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);if($row){echo '<font size="5" color="#FFFF00">';   echo 'You are in...........';echo "<br>";echo "</font>";}else {echo '<font size="5" color="#FFFF00">';echo 'You are in...........';//print_r(mysql_error());//echo "You have an error in your SQL syntax";echo "</br></font>";    echo '<font color= "#0000ff" font size= 3>';    }
}else { echo "Please input the ID as parameter with numeric value";}?>
</font> </div></br></br></br><center>
<img src="../images/Less-10.jpg" /></center>
</body>
</html>

源码分析:

通过对比less-9和less-10 我们发现他们的源码相差不大,仅仅只是包裹方式不同

$id = '"'.$id.'"';
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";

less-11

源码

<!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-11- Error Based- String</title>
</head><body bgcolor="#000000">
<div style=" margin-top:20px;color:#FFF; font-size:24px; text-align:center"> Welcome&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br></div><div  align="center" style="margin:40px 0px 0px 520px;border:20px; background-color:#0CF; text-align:center; width:400px; height:150px;"><div style="padding-top:10px; font-size:15px;"><!--Form to post the data for sql injections Error based SQL Injection-->
<form action="" name="form1" method="post"><div style="margin-top:15px; height:30px;">Username : &nbsp;&nbsp;&nbsp;<input type="text"  name="uname" value=""/></div>  <div> Password  : &nbsp;&nbsp;&nbsp;<input type="text" name="passwd" value=""/></div></br><div style=" margin-top:9px;margin-left:90px;"><input type="submit" name="submit" value="Submit" /></div>
</form></div></div><div style=" margin-top:10px;color:#FFF; font-size:23px; text-align:center">
<font size="6" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);// take the variables
if(isset($_POST['uname']) && isset($_POST['passwd']))
{$uname=$_POST['uname'];$passwd=$_POST['passwd'];//logging the connection parameters to a file for analysis.$fp=fopen('result.txt','a');fwrite($fp,'User Name:'.$uname);fwrite($fp,'Password:'.$passwd."\n");fclose($fp);// connectivity @$sql="SELECT username, password FROM users WHERE username='$uname' and password='$passwd' LIMIT 0,1";$result=mysql_query($sql);$row = mysql_fetch_array($result);if($row){//echo '<font color= "#0000ff">';   echo "<br>";echo '<font color= "#FFFF00" font size = 4>';//echo " You Have successfully logged in\n\n " ;echo '<font size="3" color="#0000ff">';    echo "<br>";echo 'Your Login name:'. $row['username'];echo "<br>";echo 'Your Password:' .$row['password'];echo "<br>";echo "</font>";echo "<br>";echo "<br>";echo '<img src="../images/flag.jpg"  />';   echo "</font>";}else  {echo '<font color= "#0000ff" font size="3">';//echo "Try again looser";print_r(mysql_error());echo "</br>";echo "</br>";echo "</br>";echo '<img src="../images/slap.jpg" />'; echo "</font>";  }
}?></font>
</div>
</body>
</html>

源码审计:

观察less-11源码我们发现这道题和之前10关的传输方式不同,为post传参

在PHP部分和less-1的源代码相似:

它的sql变量可以直接到sql语句中查询

@$sql="SELECT username, password FROM users WHERE username='$uname' and password='$passwd' LIMIT 0,1";

从数据库中获取的内容可以直接在页面上显示:

echo '<font size="3" color="#0000ff">';    echo "<br>";echo 'Your Login name:'. $row['username'];echo "<br>";echo 'Your Password:' .$row['password'];echo "<br>";echo "</font>";echo "<br>";echo "<br>";echo '<img src="../images/flag.jpg"  />';   echo "</font>";}

所以我们可以直接使用联合传参的方式。(和less-1的方法相同)

less-12

源码如下:

<!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-12- Error Based- Double quotes- String</title>
</head><body bgcolor="#000000">
<div style=" margin-top:20px;color:#FFF; font-size:24px; text-align:center"> Welcome&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br></div><div  align="center" style="margin:40px 0px 0px 520px;border:20px; background-color:#0CF; text-align:center; width:400px; height:150px;"><div style="padding-top:10px; font-size:15px;"><!--Form to post the data for sql injections Error based SQL Injection-->
<form action="" name="form1" method="post"><div style="margin-top:15px; height:30px;">Username : &nbsp;&nbsp;&nbsp;<input type="text"  name="uname" value=""/></div>  <div> Password  : &nbsp;&nbsp;&nbsp;<input type="text" name="passwd" value=""/></div></br><div style=" margin-top:9px;margin-left:90px;"><input type="submit" name="submit" value="Submit" /></div>
</form></div></div><div style=" margin-top:10px;color:#FFF; font-size:23px; text-align:center">
<font size="6" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);// take the variables
if(isset($_POST['uname']) && isset($_POST['passwd']))
{$uname=$_POST['uname'];$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,'Password:'.$passwd."\n");fclose($fp);// connectivity$uname='"'.$uname.'"';$passwd='"'.$passwd.'"'; @$sql="SELECT username, password FROM users WHERE username=($uname) and password=($passwd) LIMIT 0,1";$result=mysql_query($sql);$row = mysql_fetch_array($result);if($row){//echo '<font color= "#0000ff">';    echo "<br>";echo '<font color= "#FFFF00" font size = 4>';//echo " You Have successfully logged in " ;echo '<font size="3" color="#0000ff">';    echo "<br>";echo 'Your Login name:'. $row['username'];echo "<br>";echo 'Your Password:' .$row['password'];echo "<br>";echo "</font>";echo "<br>";echo "<br>";echo '<img src="../images/flag.jpg"   />';  echo "</font>";}else  {echo '<font color= "#0000ff" font size="3">';//echo "Try again looser";print_r(mysql_error());echo "</br>";echo "</br>";echo "</br>";echo '<img src="../images/slap.jpg"   />';   echo "</font>";  }
}?></font>
</div>
</body>
</html>

源码审计:

通过对比我们发现less-11和less-12相似,都使用post传参,仅仅只是包裹方式不同

$uname='"'.$uname.'"';$passwd='"'.$passwd.'"'; @$sql="SELECT username, password FROM users WHERE username=($uname) and password=($passwd) LIMIT 0,1";

less-13

源码如下:

<!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-13- Double Injection- String- with twist</title>
</head><body bgcolor="#000000">
<div style=" margin-top:20px;color:#FFF; font-size:24px; text-align:center"> Welcome&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br></div><div  align="center" style="margin:40px 0px 0px 520px;border:20px; background-color:#0CF; text-align:center; width:400px; height:150px;"><div style="padding-top:10px; font-size:15px;"><!--Form to post the data for sql injections Error based SQL Injection-->
<form action="" name="form1" method="post"><div style="margin-top:15px; height:30px;">Username : &nbsp;&nbsp;&nbsp;<input type="text"  name="uname" value=""/></div>  <div> Password  : &nbsp;&nbsp;&nbsp;<input type="text" name="passwd" value=""/></div></br><div style=" margin-top:9px;margin-left:90px;"><input type="submit" name="submit" value="Submit" /></div>
</form></div></div><div style=" margin-top:10px;color:#FFF; font-size:23px; text-align:center">
<font size="6" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);// take the variables
if(isset($_POST['uname']) && isset($_POST['passwd']))
{$uname=$_POST['uname'];$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,'Password:'.$passwd."\n");fclose($fp);// connectivity @$sql="SELECT username, password FROM users WHERE username=('$uname') and password=('$passwd') LIMIT 0,1";$result=mysql_query($sql);$row = mysql_fetch_array($result);if($row){//echo '<font color= "#0000ff">';    echo "<br>";echo '<font color= "#FFFF00" font size = 4>';//echo " You Have successfully logged in " ;echo '<font size="3" color="#0000ff">';    echo "<br>";//echo 'Your Login name:'. $row['username'];//echo "<br>";//echo 'Your Password:' .$row['password'];//echo "<br>";echo "</font>";echo "<br>";echo "<br>";echo '<img src="../images/flag.jpg"   />';  echo "</font>";}else  {echo '<font color= "#0000ff" font size="3">';//echo "Try again looser";print_r(mysql_error());echo "</br>";echo "</br>";echo "</br>";echo '<img src="../images/slap.jpg"   />';   echo "</font>";  }
}?></font>
</div>
</body>
</html>

源码分析:

通过对比我们不难发现,less-13少了:

echo "<br>";echo '<font color= "#FFFF00" font size = 4>';//echo " You Have successfully logged in " ;echo '<font size="3" color="#0000ff">'; echo "<br>";echo 'Your Login name:'. $row['username'];echo "<br>";echo 'Your Password:' .$row['password'];echo "<br>";echo "</font>";echo "<br>";echo "<br>";echo '<img src="../images/flag.jpg"   />';  echo "</font>";

这代表着在数据库中获取的内容无法直接展示,不过代码中有这么一行代码,回显了数据库的报错数据;

 print_r(mysql_error());

回想less-5的做法我们自然而然的使用updatexml的报错手法;

最后一定要记得观察 id 的包裹手法:

@$sql="SELECT username, password FROM users WHERE username=('$uname') and password=('$passwd') LIMIT 0,1";

less-14

源码如下:

<!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-14- Double Injection- Double quotes- String</title>
</head><body bgcolor="#000000">
<div style=" margin-top:20px;color:#FFF; font-size:24px; text-align:center"> Welcome&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br></div><div  align="center" style="margin:40px 0px 0px 520px;border:20px; background-color:#0CF; text-align:center; width:400px; height:150px;"><div style="padding-top:10px; font-size:15px;"><!--Form to post the data for sql injections Error based SQL Injection-->
<form action="" name="form1" method="post"><div style="margin-top:15px; height:30px;">Username : &nbsp;&nbsp;&nbsp;<input type="text"  name="uname" value=""/></div>  <div> Password  : &nbsp;&nbsp;&nbsp;<input type="text" name="passwd" value=""/></div></br><div style=" margin-top:9px;margin-left:90px;"><input type="submit" name="submit" value="Submit" /></div>
</form></div></div><div style=" margin-top:10px;color:#FFF; font-size:23px; text-align:center">
<font size="6" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);// take the variables
if(isset($_POST['uname']) && isset($_POST['passwd']))
{$uname=$_POST['uname'];$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,'Password:'.$passwd."\n");fclose($fp);// connectivity$uname='"'.$uname.'"';$passwd='"'.$passwd.'"'; @$sql="SELECT username, password FROM users WHERE username=$uname and password=$passwd LIMIT 0,1";$result=mysql_query($sql);$row = mysql_fetch_array($result);if($row){//echo '<font color= "#0000ff">';    echo "<br>";echo '<font color= "#FFFF00" font size = 4>';//echo " You Have successfully logged in " ;echo '<font size="3" color="#0000ff">';    echo "<br>";//echo 'Your Login name:'. $row['username'];//echo "<br>";//echo 'Your Password:' .$row['password'];//echo "<br>";echo "</font>";echo "<br>";echo "<br>";echo '<img src="../images/flag.jpg" />';    echo "</font>";}else  {echo '<font color= "#0000ff" font size="3">';//echo "Try again looser";print_r(mysql_error());echo "</br>";echo "</br>";echo "</br>";echo '<img src="../images/slap.jpg"  />';    echo "</font>";  }
}?></font>
</div>
</body>
</html>

源码审计:

观察发现:

less-14 和 less-15 的区别不大,仅仅只是包裹手法的不同:

 $uname='"'.$uname.'"';$passwd='"'.$passwd.'"'; @$sql="SELECT username, password FROM users WHERE username=$uname and password=$passwd LIMIT 0,1";

less-15

源码如下:

<!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-15- Blind- Boolian Based- String</title>
</head><body bgcolor="#000000">
<div style=" margin-top:20px;color:#FFF; font-size:24px; text-align:center"> Welcome&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br></div><div  align="center" style="margin:40px 0px 0px 520px;border:20px; background-color:#0CF; text-align:center; width:400px; height:150px;"><div style="padding-top:10px; font-size:15px;"><!--Form to post the data for sql injections Error based SQL Injection-->
<form action="" name="form1" method="post"><div style="margin-top:15px; height:30px;">Username : &nbsp;&nbsp;&nbsp;<input type="text"  name="uname" value=""/></div>  <div> Password  : &nbsp;&nbsp;&nbsp;<input type="text" name="passwd" value=""/></div></br><div style=" margin-top:9px;margin-left:90px;"><input type="submit" name="submit" value="Submit" /></div>
</form></div></div><div style=" margin-top:10px;color:#FFF; font-size:23px; text-align:center">
<font size="6" color="#FFFF00"><?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);// take the variables
if(isset($_POST['uname']) && isset($_POST['passwd']))
{$uname=$_POST['uname'];$passwd=$_POST['passwd'];//logging the connection parameters to a file for analysis.$fp=fopen('result.txt','a');fwrite($fp,'User Name:'.$uname);fwrite($fp,'Password:'.$passwd."\n");fclose($fp);// connectivity @$sql="SELECT username, password FROM users WHERE username='$uname' and password='$passwd' LIMIT 0,1";$result=mysql_query($sql);$row = mysql_fetch_array($result);if($row){//echo '<font color= "#0000ff">';   echo "<br>";echo '<font color= "#FFFF00" font size = 4>';//echo " You Have successfully logged in\n\n " ;echo '<font size="3" color="#0000ff">';    echo "<br>";//echo 'Your Login name:'. $row['username'];echo "<br>";//echo 'Your Password:' .$row['password'];echo "<br>";echo "</font>";echo "<br>";echo "<br>";echo '<img src="../images/flag.jpg"  />';   echo "</font>";}else  {echo '<font color= "#0000ff" font size="3">';//echo "Try again looser";//print_r(mysql_error());echo "</br>";echo "</br>";echo "</br>";echo '<img src="../images/slap.jpg"   />'; echo "</font>";  }
}?></font>
</div>
</body>
</html>

源码审计:

相比于less-13的不同less-15它少了:

 print_r(mysql_error());

并且观察源码我们发现,当我们输入时,页面显示正确和错误的样子不同:

if($row){//echo '<font color= "#0000ff">';    echo "<br>";echo '<font color= "#FFFF00" font size = 4>';//echo " You Have successfully logged in\n\n " ;echo '<font size="3" color="#0000ff">';    echo "<br>";//echo 'Your Login name:'. $row['username'];echo "<br>";//echo 'Your Password:' .$row['password'];echo "<br>";echo "</font>";echo "<br>";echo "<br>";echo '<img src="../images/flag.jpg"  />';   echo "</font>";}else  {echo '<font color= "#0000ff" font size="3">';//echo "Try again looser";//print_r(mysql_error());echo "</br>";echo "</br>";echo "</br>";echo '<img src="../images/slap.jpg"   />'; echo "</font>";  }

联想到第七关我们决定使用布尔盲注。

最后观察包裹方式:

 @$sql="SELECT username, password FROM users WHERE username='$uname' and password='$passwd' LIMIT 0,1";

注意:

如果有小伙伴已经做了less-15就会发现:

less-9的输入为:

解析库名长度:  ?id=1' and if(length(database())=8,sleep(5),1)-- q解析数据库名称:?id=1' and if((ascii(substr(database(),1,1))=115),sleep(5),1)-- q解析表名: ?id=1' and if((ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1,1))=101),sleep(5),1)-- q  解析字段名:?id=1' and if((ascii(substr((select column_name from information_schema.columns where table_schema='security' and table_name='emails' limit 0,1),1,1))=105),sleep(5),1)-- q

less-15的输入为

判断数据库长度:'or (length(database()))=8-- q判断数据库名字:'or (ascii(substr(database(),1,1)))=115-- q判断表的名字:'or (ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1,1)))=101-- q判断列的名字:'or (ascii(substr((select column_name from information_schema.columns where table_schema='security' and table_name='emails' limit 0,1),1,1)))=105-- q

就会有这样一个疑惑: 为啥,九关为 and 是十五关为 or

让我们来观察下源码:

less-9:

$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";

less-15:

@$sql="SELECT username, password FROM users WHERE username='$uname' and password='$passwd' LIMIT 0,1";

观察这两句源代码,不难看出 如果我们使用 and 就会变成:

@$sql="SELECT username, password FROM users WHERE username='$uname' 'or (length(database()))=8-- q and password='$passwd' LIMIT 0,1";

显然不成立,因为我们不清楚username的值。如果使用or,那么后面的值成立 就 整体成立。而在9题中 id=1 是成立的所以可以使用and来作为语句的连接。

sql注入——php源码的审计(以sql-lab 1~15为例)(超详细)相关推荐

  1. Yolov5-5.0源码分享以及环境配置——Yolov5训练及测试教程(超详细含数据集制作,格式转换,数据集划分)

    yolov5-5.0百度网盘连接 链接: https://pan.baidu.com/s/1Hd2KKBixuEWRv3jcH6Bcsw 提取码: g6xf 复制这段内容后打开百度网盘手机App,操作 ...

  2. SQL注入看这一篇可能还不够——SQL注入各类型总结+靶场实战

    SQL注入简介 SQL注入原理 SQL注入是通过将恶意的sql语句插入到应用代码中,由于过滤不严导致的在后台执行恶意sql语句而产生的漏洞. SQL注入类型 注入参数:数字型注入.字符型注入.搜索型注 ...

  3. Mybatis 源码探究 (4) 将sql 语句中的#{id} 替换成 ‘?

    Mybatis 源码探究 (4) 将sql 语句中的#{id} 替换成 '? 出于好奇,然后就有了这篇文章啦. 源码给我的感觉,是一座大山的感觉.曲曲折折的路很多,点进去就有可能出不来. 不过慢慢看下 ...

  4. 淘宝数据库OceanBase SQL编译器部分 源码阅读--生成物理查询计划

    淘宝数据库OceanBase SQL编译器部分 源码阅读--生成物理查询计划 SQL编译解析三部曲分为:构建语法树,制定逻辑计划,生成物理执行计划.前两个步骤请参见我的博客<<淘宝数据库O ...

  5. 防sql注入 php代码,完美的php防sql注入代码

    一款比较完美的php防sql注入代码,很多初学者都有被sql注入的经验吧,今天我们来分享你一款比较完整的sql防注入代码,有需要的同学可以参考一下: /************************ ...

  6. Java程序员从笨鸟到菜鸟之(一百)sql注入攻击详解(一)sql注入原理详解

    前段时间,在很多博客和微博中暴漏出了12306铁道部网站的一些漏洞,作为这么大的一个项目,要说有漏洞也不是没可能,但其漏洞确是一些菜鸟级程序员才会犯的错误.其实sql注入漏洞就是一个.作为一个菜鸟小程 ...

  7. MyBatis原理分析之四:一次SQL查询的源码分析

    上回我们讲到Mybatis加载相关的配置文件进行初始化,这回我们讲一下一次SQL查询怎么进行的. 准备工作 Mybatis完成一次SQL查询需要使用的代码如下: Java代码   String res ...

  8. Spark之SQL解析(源码阅读十)

    如何能更好的运用与监控sparkSQL?或许我们改更深层次的了解它深层次的原理是什么.之前总结的已经写了传统数据库与Spark的sql解析之间的差别.那么我们下来直切主题~ 如今的Spark已经支持多 ...

  9. 原理分析之四:一次SQL查询的源码分析

    上回我们讲到Mybatis加载相关的配置文件进行初始化,这回我们讲一下一次SQL查询怎么进行的. 准备工作 Mybatis完成一次SQL查询需要使用的代码如下: Java代码   String res ...

最新文章

  1. CSS超出部分隐藏,显示滚动条
  2. [企业化NET]Window Server 2008 R2[3]-SVN 服务端 和 客户端 基本使用
  3. [P1363] 幻想迷宫
  4. [转]double free or corruption (!prev): 0x080644c8 ***
  5. 从进程说起:容器到底是怎么一回事儿?
  6. C++中的定位放置new(placement new)
  7. Oracle修改表存储参数,Oracle存储结构之参数文件
  8. 个位百位AS3实现经典算法(二) 水仙花数
  9. 计算机文件不能复制到u盘,大文件无法复制到u盘里解决方法
  10. 微信公众号开发测试平台地址
  11. python查找单词的位置_Python实现单词查询文件查找
  12. 湖南大学操作系统导论课程作业
  13. 数组循环左移问题,将一个N个元素的数组向左移i个位置
  14. 多WAN路由器配置 实现简述
  15. 【Postman】 tests常用方法
  16. LaTeX中使用bicaption、tabula包绘制三线表
  17. 商城购买会员打折满减优惠券商品
  18. STP 4 - MST 和 PVST 对比 (侧重MST)
  19. ecstore网站换服务器,Nginx下ecstore伪静态开启后的后台跳转问题
  20. c++入门笔记自己整理+代码分析附目录(下)

热门文章

  1. 计算2017年11月11日 11时 11分 11秒 距离当前日期的时间差, 并以指定的格式(天/时/分/秒)的形式显示.
  2. 教您读懂数字货币开发_交易系统开发
  3. 什么是软件测试?软件测试和研发的区别
  4. 域控服务器新建共享文件夹,在ActiveDirectory中如何创建共享文件夹?
  5. 华为认证HCNA-IoT物联网工程师培训 V1.0
  6. 星露谷物语多玩家显示联机服务器没法邀请,星露谷物语怎么联机
  7. Mac(Alfred) 办公提效工具
  8. MaxCompute2.0助力众安保险快速成长
  9. 【蓝桥杯单片机】DS1302模块
  10. 定制CRM系统如何整理各部门需求